async def test_receive_raises_struct_error(self): """Test the case when a receive raises a struct error.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() with unittest.mock.patch.object(tcp_client.logger, "debug") as mock_logger: with unittest.mock.patch.object(tcp_client, "_recv", side_effect=struct.error): task = asyncio.ensure_future(tcp_client.receive()) await asyncio.sleep(0.1) mock_logger.assert_called_with("Struct error: ") assert task.result() is None await tcp_client.disconnect() await tcp_server.disconnect()
def setup(self): """Initialise the test case.""" self.identity = Identity("name", address="my_key") self.agent_address = self.identity.address self.host = get_host() self.port = get_unused_tcp_port() self.api_spec_path = os.path.join( ROOT_DIR, "tests", "data", "petstore_sim.yaml" ) self.connection_id = HTTPServerConnection.connection_id self.protocol_id = PublicId.from_str("fetchai/http:0.4.0") self.configuration = ConnectionConfig( host=self.host, port=self.port, api_spec_path=self.api_spec_path, connection_id=HTTPServerConnection.connection_id, restricted_to_protocols=set([self.protocol_id]), ) self.http_connection = HTTPServerConnection( configuration=self.configuration, identity=self.identity, ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.http_connection.connect()) self.connection_address = str(HTTPServerConnection.connection_id) self._dialogues = HttpDialogues(self.connection_address) self.original_timeout = self.http_connection.channel.RESPONSE_TIMEOUT
async def test_receive_cancelled(self): """Test that cancelling a receive task works correctly.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() with unittest.mock.patch.object(tcp_client.logger, "debug") as mock_logger: task = asyncio.ensure_future(tcp_client.receive()) await asyncio.sleep(0.1) task.cancel() await asyncio.sleep(0.1) mock_logger.assert_called_with( "[{}] Read cancelled.".format("address_client")) assert task.result() is None await tcp_client.disconnect() await tcp_server.disconnect()
async def test_receive_raises_exception(self): """Test the case when a receive raises a generic exception.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() await asyncio.sleep(0.1) with unittest.mock.patch.object(tcp_server.logger, "error") as mock_logger: with unittest.mock.patch( "asyncio.wait", side_effect=Exception("generic exception")): result = await tcp_server.receive() assert result is None mock_logger.assert_any_call( "Error in the receiving loop: generic exception") await tcp_client.disconnect() await tcp_server.disconnect()
async def test_receive_raises_exception(self): """Test the case when a receive raises a generic exception.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() with pytest.raises(Exception, match="generic exception"): with unittest.mock.patch.object( tcp_client, "_recv", side_effect=Exception("generic exception")): task = asyncio.ensure_future(tcp_client.receive()) await asyncio.sleep(0.1) assert task.result() is None await tcp_client.disconnect() await tcp_server.disconnect()
def setup_server(self): """Set up server connection.""" self.server_agent_address = "server_agent_address" self.server_agent_identity = Identity( "agent running server", address=self.server_agent_address ) self.host = get_host() self.port = get_unused_tcp_port() self.connection_id = HTTPServerConnection.connection_id self.protocol_id = PublicId.from_str("fetchai/http:0.4.0") self.configuration = ConnectionConfig( host=self.host, port=self.port, api_spec_path=None, # do not filter on API spec connection_id=HTTPServerConnection.connection_id, restricted_to_protocols=set([self.protocol_id]), ) self.server = HTTPServerConnection( configuration=self.configuration, identity=self.server_agent_identity, ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.server.connect()) # skill side dialogues self._server_dialogues = HttpDialogues(self.server_agent_address)
async def test_receive_raises_exception(self, caplog): """Test the case when a receive raises a generic exception.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() await asyncio.sleep(0.1) with caplog.at_level( logging.DEBUG, "aea.packages.fetchai.connections.tcp.tcp_server"): with unittest.mock.patch( "asyncio.wait", side_effect=Exception("generic exception")): result = await tcp_server.receive() assert result is None assert "Error in the receiving loop: generic exception" in caplog.text await tcp_client.disconnect() await tcp_server.disconnect()
async def test_receive_raises_struct_error(self, caplog): """Test the case when a receive raises a struct error.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() with caplog.at_level( logging.DEBUG, "aea.packages.fetchai.connections.tcp.tcp_client"): with unittest.mock.patch.object(tcp_client, "_recv", side_effect=struct.error): task = asyncio.ensure_future(tcp_client.receive()) await asyncio.sleep(0.1) assert "Struct error: " in caplog.text assert task.result() is None await tcp_client.disconnect() await tcp_server.disconnect()
async def test_receive_cancelled(self, caplog): """Test that cancelling a receive task works correctly.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection( "address_server", "127.0.0.1", port, ) tcp_client = _make_tcp_client_connection( "address_client", "127.0.0.1", port, ) await tcp_server.connect() await tcp_client.connect() with caplog.at_level( logging.DEBUG, "aea.packages.fetchai.connections.tcp.tcp_client"): task = asyncio.ensure_future(tcp_client.receive()) await asyncio.sleep(0.1) task.cancel() await asyncio.sleep(0.1) assert "[{}] Read cancelled.".format( "address_client") in caplog.text assert task.result() is None await tcp_client.disconnect() await tcp_server.disconnect()
async def test_disconnect_when_already_disconnected(): """Test that disconnecting a connection already disconnected works correctly.""" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) with unittest.mock.patch.object(tcp_connection.logger, "warning") as mock_logger: await tcp_connection.disconnect() mock_logger.assert_called_with("Connection already disconnected.")
async def test_disconnect_when_already_disconnected(caplog): """Test that disconnecting a connection already disconnected works correctly.""" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) with caplog.at_level(logging.WARNING, "aea.packages.fetchai.connections.tcp"): await tcp_connection.disconnect() assert "Connection already disconnected." in caplog.text
async def test_disconnect_when_already_disconnected(): """Test that disconnecting a connection already disconnected works correctly.""" port = get_unused_tcp_port() tcp_connection = TCPServerConnection("public_key", "127.0.0.1", port) with unittest.mock.patch.object(aea.connections.tcp.base.logger, "warning") as mock_logger_warning: await tcp_connection.disconnect() mock_logger_warning.assert_called_with( "Connection already disconnected.")
async def test_connect_raises_exception(): """Test the case that a connection attempt raises an exception.""" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) with unittest.mock.patch.object(tcp_connection.logger, "error") as mock_logger: with unittest.mock.patch.object( tcp_connection, "setup", side_effect=Exception("error during setup") ): await tcp_connection.connect() mock_logger.assert_called_with("error during setup")
async def test_connect_twice(): """Test that connecting twice the tcp connection works correctly.""" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) await tcp_connection.connect() await asyncio.sleep(0.1) with unittest.mock.patch.object(tcp_connection.logger, "warning") as mock_logger: await tcp_connection.connect() mock_logger.assert_called_with("Connection already set up.") await tcp_connection.disconnect()
def setup(self): """Initialise the class.""" self.address = get_host() self.port = get_unused_tcp_port() self.agent_identity = Identity("name", address="some string") configuration = ConnectionConfig( host=self.address, port=self.port, connection_id=HTTPClientConnection.connection_id, ) self.http_client_connection = HTTPClientConnection( configuration=configuration, identity=self.agent_identity) self.http_client_connection.loop = asyncio.get_event_loop()
async def test_send_to_unknown_destination(): """Test that a message to an unknown destination logs an error.""" public_key = "public_key" port = get_unused_tcp_port() tcp_connection = TCPServerConnection(public_key, "127.0.0.1", port) envelope = Envelope(to="non_existing_destination", sender="public_key", protocol_id="default", message=b"") with unittest.mock.patch.object(aea.connections.tcp.base.logger, "error") as mock_logger_error: await tcp_connection.send(envelope) mock_logger_error.assert_called_with( "[{}]: Cannot send envelope {}".format(public_key, envelope))
def setup(self): """Initialise the class.""" self.address = get_host() self.port = get_unused_tcp_port() self.agent_identity = Identity("name", address="some string") self.agent_address = self.agent_identity.address configuration = ConnectionConfig( host=self.address, port=self.port, connection_id=HTTPClientConnection.connection_id, ) self.http_client_connection = HTTPClientConnection( configuration=configuration, identity=self.agent_identity) self.connection_address = str(HTTPClientConnection.connection_id) self.http_dialogs = HttpDialogues(self.agent_address)
async def test_connect_twice(caplog): """Test that connecting twice the tcp connection works correctly.""" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) loop = asyncio.get_event_loop() tcp_connection.loop = loop await tcp_connection.connect() await asyncio.sleep(0.1) with caplog.at_level(logging.WARNING, "aea.packages.fetchai.connections.tcp"): await tcp_connection.connect() assert "Connection already set up." in caplog.text await tcp_connection.disconnect()
async def test_connect_raises_exception(caplog): """Test the case that a connection attempt raises an exception.""" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) loop = asyncio.get_event_loop() tcp_connection.loop = loop with caplog.at_level(logging.ERROR, "aea.packages.fetchai.connections.tcp"): with unittest.mock.patch.object( tcp_connection, "setup", side_effect=Exception("error during setup")): await tcp_connection.connect() assert "error during setup" in caplog.text
async def test_send_to_unknown_destination(caplog): """Test that a message to an unknown destination logs an error.""" address = "address" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) envelope = Envelope( to="non_existing_destination", sender="address", protocol_id=DefaultMessage.protocol_id, message=b"", ) with caplog.at_level(logging.ERROR, "aea.packages.fetchai.connections.tcp"): await tcp_connection.send(envelope) assert "[{}]: Cannot send envelope {}".format(address, envelope) in caplog.text
async def test_send_to_unknown_destination(): """Test that a message to an unknown destination logs an error.""" address = "address" port = get_unused_tcp_port() tcp_connection = _make_tcp_server_connection("address", "127.0.0.1", port) envelope = Envelope( to="non_existing_destination", sender="address", protocol_id=DefaultMessage.protocol_id, message=b"", ) with unittest.mock.patch.object(tcp_connection.logger, "error") as mock_logger: await tcp_connection.send(envelope) mock_logger.assert_called_with("[{}]: Cannot send envelope {}".format( address, envelope))
async def test_connect_raises_exception(): """Test the case that a connection attempt raises an exception.""" port = get_unused_tcp_port() tcp_connection = TCPServerConnection("public_key", "127.0.0.1", port) loop = asyncio.get_event_loop() tcp_connection.loop = loop with unittest.mock.patch.object(aea.connections.tcp.base.logger, "error") as mock_logger_error: with unittest.mock.patch.object( tcp_connection, "setup", side_effect=Exception("error during setup")): await tcp_connection.connect() mock_logger_error.assert_called_with("error during setup")
async def test_connect_twice(): """Test that connecting twice the tcp connection works correctly.""" port = get_unused_tcp_port() tcp_connection = TCPServerConnection("public_key", "127.0.0.1", port) loop = asyncio.get_event_loop() tcp_connection.loop = loop await tcp_connection.connect() await asyncio.sleep(0.1) with unittest.mock.patch.object(aea.connections.tcp.base.logger, "warning") as mock_logger_warning: await tcp_connection.connect() mock_logger_warning.assert_called_with("Connection already set up.") await tcp_connection.disconnect()
async def test_receive_raises_exception(self): """Test the case when a receive raises a generic exception.""" port = get_unused_tcp_port() tcp_server = TCPServerConnection("public_key_server", "127.0.0.1", port) tcp_client = TCPClientConnection("public_key_client", "127.0.0.1", port) await tcp_server.connect() await tcp_client.connect() await asyncio.sleep(0.1) with unittest.mock.patch.object(aea.connections.tcp.tcp_server.logger, "error") as mock_logger_error: with unittest.mock.patch("asyncio.wait", side_effect=Exception("generic exception")): result = await tcp_server.receive() assert result is None mock_logger_error.assert_called_with("Error in the receiving loop: generic exception") await tcp_client.disconnect() await tcp_server.disconnect()
def setup(self): """Initialise the class.""" self.host = get_host() self.port = get_unused_tcp_port() self.identity = Identity("", address="some string") self.path = "/webhooks/topic/{topic}/" self.loop = asyncio.get_event_loop() configuration = ConnectionConfig( webhook_address=self.host, webhook_port=self.port, webhook_url_path=self.path, connection_id=WebhookConnection.connection_id, ) self.webhook_connection = WebhookConnection( configuration=configuration, data_dir=MagicMock(), identity=self.identity, ) self.dialogues = HttpDialogues(self.identity.address)
async def test_receive_cancelled(self): """Test that cancelling a receive task works correctly.""" port = get_unused_tcp_port() tcp_server = TCPServerConnection("public_key_server", "127.0.0.1", port) tcp_client = TCPClientConnection("public_key_client", "127.0.0.1", port) await tcp_server.connect() await tcp_client.connect() with unittest.mock.patch.object(aea.connections.tcp.tcp_client.logger, "debug") as mock_logger_debug: task = asyncio.ensure_future(tcp_client.receive()) await asyncio.sleep(0.1) task.cancel() await asyncio.sleep(0.1) mock_logger_debug.assert_called_with("[{}] Read cancelled.".format("public_key_client")) assert task.result() is None await tcp_client.disconnect() await tcp_server.disconnect()
async def test_send_cancelled(): """Test that cancelling a send works correctly.""" port = get_unused_tcp_port() tcp_server = TCPServerConnection("public_key_server", "127.0.0.1", port) tcp_client = TCPClientConnection("public_key_client", "127.0.0.1", port) await tcp_server.connect() await tcp_client.connect() with unittest.mock.patch.object(tcp_client._writer, "drain", side_effect=CancelledError): envelope = Envelope(to="public_key_client", sender="public_key_server", protocol_id="default", message=b"") await tcp_client.send(envelope) await tcp_client.disconnect() await tcp_server.disconnect()
async def test_send_cancelled(): """Test that cancelling a send works correctly.""" port = get_unused_tcp_port() tcp_server = _make_tcp_server_connection("address_server", "127.0.0.1", port) tcp_client = _make_tcp_client_connection("address_client", "127.0.0.1", port) await tcp_server.connect() await tcp_client.connect() with unittest.mock.patch.object( tcp_client._writer, "drain", side_effect=CancelledError ): envelope = Envelope( to="address_client", sender="address_server", protocol_specification_id=DefaultMessage.protocol_specification_id, message=b"", ) await tcp_client.send(envelope) await tcp_client.disconnect() await tcp_server.disconnect()
def setup_server(self): """Set up server connection.""" self.server_agent_address = "server_agent_address" self.server_agent_identity = Identity( "agent running server", address=self.server_agent_address) self.host = get_host() self.port = get_unused_tcp_port() self.connection_id = HTTPServerConnection.connection_id self.protocol_id = HttpMessage.protocol_id self.configuration = ConnectionConfig( host=self.host, port=self.port, api_spec_path=None, # do not filter on API spec connection_id=HTTPServerConnection.connection_id, restricted_to_protocols=set([self.protocol_id]), ) self.server = HTTPServerConnection( configuration=self.configuration, identity=self.server_agent_identity, ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.server.connect()) # skill side dialogues def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address) -> BaseDialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return HttpDialogue.Role.SERVER self._server_dialogues = HttpDialogues( self.server_agent_address, role_from_first_message=role_from_first_message)
def setup_class(cls): """Set up the test class.""" cls.host = "127.0.0.1" cls.port = get_unused_tcp_port() cls.server_addr = "server_addr" cls.client_addr_1 = "client_addr_1" cls.client_addr_2 = "client_addr_2" cls.server_conn = _make_tcp_server_connection( cls.server_addr, cls.host, cls.port, ) cls.client_conn_1 = _make_tcp_client_connection( cls.client_addr_1, cls.host, cls.port, ) cls.client_conn_2 = _make_tcp_client_connection( cls.client_addr_2, cls.host, cls.port, ) cls.server_multiplexer = Multiplexer([cls.server_conn]) cls.client_1_multiplexer = Multiplexer([cls.client_conn_1]) cls.client_2_multiplexer = Multiplexer([cls.client_conn_2]) assert not cls.server_conn.is_connected assert not cls.client_conn_1.is_connected assert not cls.client_conn_2.is_connected cls.server_multiplexer.connect() cls.client_1_multiplexer.connect() cls.client_2_multiplexer.connect()