def get_connector(port, num_threads, directory): # Listen on the specified port. connector = connector.Connector(converter=converters.StringConverter, port=port) add_file_writers(connector, num_threads, directory) connector.run() return connector
def test_disconnect_finish_queue(self): converter = _PausingConverter() connector = self._get_connector(listening=True, converter=converter) connector.run() # Listen for the connector. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listener.bind(('', TestConnector._LISTEN_PORT)) listener.listen(1) # The connector should create a half-open connection to the listening socket. connection, success = connector.connect_waiting( '', TestConnector._LISTEN_PORT) self.assertTrue(success) # Accept the connection so now a full connection. s, addr = listener.accept() # Wait for the first message to be partially serialized. connector.send(connection, 'ONE') converter._wait_for_callback() # Enqueue the second message and disconnect. connector.send(connection, 'TWO') connector.disconnect(connection, now=True) # Continue serializing the first message. converter._use_callback() # Serialize the second message. converter._wait_for_callback() converter._use_callback() # Both messages are sent completely. msg = self.recv() self.assertEqual(msg.type(), connector.Event.FINISH_DISCONNECT) self.assertIsNone(msg.incomplete()) self.assertFalse(msg.queued()) # Both messages are received completely. all_recv_bytes = self._read_until_closed(s) self.assertEqual(all_recv_bytes, 'abcONE123abcTWO123') connector.shutdown()
def get_connector(): # Do not listen on any port. connector = connector.Connector(converter=converters.FileConverter) connector.run() return connector