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()

        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()
    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 = 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:
            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_debug.assert_called_with("Struct error: ")
                assert task.result() is None

        await tcp_client.disconnect()
        await tcp_server.disconnect()
    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()
    def setup_class(cls):
        """Set up the test class."""
        cls.host = "127.0.0.1"
        cls.port = get_unused_tcp_port()

        cls.server_pbk = "server_pbk"
        cls.client_pbk_1 = "client_pbk_1"
        cls.client_pbk_2 = "client_pbk_2"

        cls.server_conn = TCPServerConnection(cls.server_pbk, cls.host, cls.port)
        cls.client_conn_1 = TCPClientConnection(cls.client_pbk_1, cls.host, cls.port)
        cls.client_conn_2 = TCPClientConnection(cls.client_pbk_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.connection_status.is_connected
        assert not cls.client_conn_1.connection_status.is_connected
        assert not cls.client_conn_2.connection_status.is_connected

        cls.server_multiplexer.connect()
        cls.client_1_multiplexer.connect()
        cls.client_2_multiplexer.connect()
    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()
Beispiel #6
0
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_from_config(self):
     """Test the creation of the connection from a configuration."""
     port = get_unused_tcp_port()
     TCPClientConnection.from_config("public_key", ConnectionConfig(host="127.0.0.1", port=port))