Exemplo n.º 1
0
def test_outbox_empty():
    """Test thet the outbox queue is empty."""
    multiplexer = Multiplexer([DummyConnection()])
    multiplexer.connect()
    outbox = OutBox(multiplexer)
    assert outbox.empty(), "The outbox is not empty"
    multiplexer.disconnect()
Exemplo n.º 2
0
def test_multiplexer_connect_one_raises_error_many_connections():
    """Test the case when the multiplexer raises an exception while attempting the connection of one connection."""
    node = LocalNode()
    tmpdir = Path(tempfile.mkdtemp())
    d = tmpdir / "test_stub"
    d.mkdir(parents=True)
    input_file_path = d / "input_file.csv"
    output_file_path = d / "input_file.csv"

    connection_1 = OEFLocalConnection(
        "my_addr", node, connection_id=PublicId("fetchai", "local", "0.1.0")
    )
    connection_2 = StubConnection(
        input_file_path,
        output_file_path,
        connection_id=PublicId("fetchai", "stub", "0.1.0"),
    )
    connection_3 = DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)
    multiplexer = Multiplexer([connection_1, connection_2, connection_3])

    assert not connection_1.connection_status.is_connected
    assert not connection_2.connection_status.is_connected
    assert not connection_3.connection_status.is_connected

    with unittest.mock.patch.object(connection_3, "connect", side_effect=Exception):
        with pytest.raises(
            AEAConnectionError, match="Failed to connect the multiplexer."
        ):
            multiplexer.connect()

    assert not connection_1.connection_status.is_connected
    assert not connection_2.connection_status.is_connected
    assert not connection_3.connection_status.is_connected

    shutil.rmtree(tmpdir)
Exemplo n.º 3
0
def test_connect_twice_with_loop():
    """Test that connecting twice the multiplexer behaves correctly."""
    running_loop = asyncio.new_event_loop()
    thread_loop = Thread(target=running_loop.run_forever)
    thread_loop.start()

    try:
        multiplexer = Multiplexer(
            [_make_dummy_connection()],
            loop=running_loop,
        )

        with unittest.mock.patch.object(aea.mail.base.logger,
                                        "debug") as mock_logger_debug:
            assert not multiplexer.connection_status.is_connected
            multiplexer.connect()
            assert multiplexer.connection_status.is_connected
            multiplexer.connect()
            assert multiplexer.connection_status.is_connected

            mock_logger_debug.assert_called_with(
                "Multiplexer already connected.")

            multiplexer.disconnect()
            running_loop.call_soon_threadsafe(running_loop.stop)
    finally:
        thread_loop.join()
Exemplo n.º 4
0
def test_send_message_no_supported_protocol():
    """Test the case when we send an envelope with a specific connection that does not support the protocol."""
    with LocalNode() as node:
        address_1 = "address_1"
        public_id = PublicId.from_str("fetchai/my_private_protocol:0.1.0")
        connection_1 = _make_local_connection(
            address_1,
            node,
            restricted_to_protocols={public_id},
            excluded_protocols={public_id},
        )
        multiplexer = Multiplexer([connection_1])

        multiplexer.connect()

        with mock.patch.object(aea.mail.base.logger,
                               "warning") as mock_logger_warning:
            protocol_id = UNKNOWN_PROTOCOL_PUBLIC_ID
            envelope = Envelope(
                to=address_1,
                sender=address_1,
                protocol_id=protocol_id,
                message=b"some bytes",
            )
            multiplexer.put(envelope)
            time.sleep(0.5)
            mock_logger_warning.assert_called_with(
                "Connection {} cannot handle protocol {}. Cannot send the envelope."
                .format(connection_1.connection_id, protocol_id))

        multiplexer.disconnect()
Exemplo n.º 5
0
def test_outbox_put():
    """Tests that an envelope is putted into the queue."""
    msg = DefaultMessage(
        dialogue_reference=("", ""),
        message_id=1,
        target=0,
        performative=DefaultMessage.Performative.BYTES,
        content=b"hello",
    )
    message_bytes = DefaultSerializer().encode(msg)
    multiplexer = Multiplexer(
        [DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)])
    outbox = OutBox(multiplexer)
    inbox = InBox(multiplexer)
    multiplexer.connect()
    envelope = Envelope(
        to="Agent1",
        sender="Agent0",
        protocol_id=DefaultMessage.protocol_id,
        message=message_bytes,
    )
    outbox.put(envelope)
    time.sleep(0.5)
    assert not inbox.empty(
    ), "Inbox must not be empty after putting an envelope"
    multiplexer.disconnect()
Exemplo n.º 6
0
def test_multiplexer_connect_all_raises_error():
    """Test the case when the multiplexer raises an exception while connecting."""
    multiplexer = Multiplexer([DummyConnection()])

    with unittest.mock.patch.object(multiplexer, "_connect_all", side_effect=Exception):
        with pytest.raises(AEAConnectionError, match="Failed to connect the multiplexer."):
            multiplexer.connect()
Exemplo n.º 7
0
def test_multiplexer_disconnect_one_raises_error_many_connections():
    """Test the case when the multiplexer raises an exception while attempting the disconnection of one connection."""
    with LocalNode() as node:
        tmpdir = Path(tempfile.mktemp())
        d = tmpdir / "test_stub"
        d.mkdir(parents=True)
        input_file_path = d / "input_file.csv"
        output_file_path = d / "input_file.csv"

        connection_1 = OEFLocalConnection("my_pbk", node)
        connection_2 = StubConnection(input_file_path, output_file_path)
        connection_3 = DummyConnection()
        multiplexer = Multiplexer([connection_1, connection_2, connection_3])

        assert not connection_1.connection_status.is_connected
        assert not connection_2.connection_status.is_connected
        assert not connection_3.connection_status.is_connected

        multiplexer.connect()

        assert connection_1.connection_status.is_connected
        assert connection_2.connection_status.is_connected
        assert connection_3.connection_status.is_connected

        with unittest.mock.patch.object(connection_3, "disconnect", side_effect=Exception):
            # with pytest.raises(AEAConnectionError, match="Failed to disconnect the multiplexer."):
            multiplexer.disconnect()

        # TODO is this what we want?
        assert not connection_1.connection_status.is_connected
        assert not connection_2.connection_status.is_connected
        assert connection_3.connection_status.is_connected

        shutil.rmtree(tmpdir)
Exemplo n.º 8
0
def test_multiplexer_connect_one_raises_error_many_connections():
    """Test the case when the multiplexer raises an exception while attempting the connection of one connection."""
    node = LocalNode()
    tmpdir = Path(tempfile.mkdtemp())
    d = tmpdir / "test_stub"
    d.mkdir(parents=True)
    input_file_path = d / "input_file.csv"
    output_file_path = d / "input_file.csv"

    connection_1 = _make_local_connection("my_addr", node)
    connection_2 = _make_stub_connection(input_file_path, output_file_path)
    connection_3 = _make_dummy_connection()
    multiplexer = Multiplexer([connection_1, connection_2, connection_3])

    assert not connection_1.connection_status.is_connected
    assert not connection_2.connection_status.is_connected
    assert not connection_3.connection_status.is_connected

    with unittest.mock.patch.object(connection_3,
                                    "connect",
                                    side_effect=Exception):
        with pytest.raises(AEAConnectionError,
                           match="Failed to connect the multiplexer."):
            multiplexer.connect()

    assert not connection_1.connection_status.is_connected
    assert not connection_2.connection_status.is_connected
    assert not connection_3.connection_status.is_connected

    try:
        shutil.rmtree(tmpdir)
    except OSError as e:
        logger.warning("Couldn't delete {}".format(tmpdir))
        logger.exception(e)
Exemplo n.º 9
0
def test_multiplexer():
    """Tests if the multiplexer is connected."""
    with LocalNode() as node:
        public_key_1 = "public_key_1"
        multiplexer = Multiplexer([OEFLocalConnection(public_key_1, node)])
        multiplexer.connect()
        assert multiplexer.is_connected, "Mailbox cannot connect to the specific Connection(OEFLocalConnection)"
        multiplexer.disconnect()
Exemplo n.º 10
0
def test_outbox_empty():
    """Test thet the outbox queue is empty."""
    multiplexer = Multiplexer(
        [DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)])
    multiplexer.connect()
    outbox = OutBox(multiplexer)
    assert outbox.empty(), "The outbox is not empty"
    multiplexer.disconnect()
Exemplo n.º 11
0
def test_multiple_connection():
    """Test that we can send a message with two different connections."""
    with LocalNode() as node:
        address_1 = "address_1"
        address_2 = "address_2"
        connection_1_id = PublicId.from_str("author/local_1:0.1.0")
        connection_2_id = PublicId.from_str("author/local_2:0.1.0")

        connection_1 = OEFLocalConnection(node,
                                          address=address_1,
                                          connection_id=connection_1_id)

        connection_2 = OEFLocalConnection(node,
                                          address=address_2,
                                          connection_id=connection_2_id)

        multiplexer = Multiplexer([connection_1, connection_2])

        assert not connection_1.connection_status.is_connected
        assert not connection_2.connection_status.is_connected

        multiplexer.connect()

        assert connection_1.connection_status.is_connected
        assert connection_2.connection_status.is_connected

        message = DefaultMessage(
            dialogue_reference=("", ""),
            message_id=1,
            target=0,
            performative=DefaultMessage.Performative.BYTES,
            content=b"hello",
        )
        envelope_from_1_to_2 = Envelope(
            to=address_2,
            sender=address_1,
            protocol_id=DefaultMessage.protocol_id,
            message=DefaultSerializer().encode(message),
            context=EnvelopeContext(connection_id=connection_1_id),
        )
        multiplexer.put(envelope_from_1_to_2)

        actual_envelope = multiplexer.get(block=True, timeout=2.0)
        assert envelope_from_1_to_2 == actual_envelope

        envelope_from_2_to_1 = Envelope(
            to=address_1,
            sender=address_2,
            protocol_id=DefaultMessage.protocol_id,
            message=DefaultSerializer().encode(message),
            context=EnvelopeContext(connection_id=connection_2_id),
        )
        multiplexer.put(envelope_from_2_to_1)

        actual_envelope = multiplexer.get(block=True, timeout=2.0)
        assert envelope_from_2_to_1 == actual_envelope

        multiplexer.disconnect()
Exemplo n.º 12
0
 def test_connection(self):
     """Test that an OEF connection can be established to the OEF."""
     crypto = DefaultCrypto()
     connection = OEFConnection(crypto.public_key,
                                oef_addr="127.0.0.1",
                                oef_port=10000)
     multiplexer = Multiplexer([connection])
     multiplexer.connect()
     multiplexer.disconnect()
Exemplo n.º 13
0
 def test_connection(self):
     """Test that an OEF connection can be established to the OEF."""
     crypto = FetchAICrypto()
     connection = _make_oef_connection(
         crypto.address, oef_addr="127.0.0.1", oef_port=10000,
     )
     multiplexer = Multiplexer([connection])
     multiplexer.connect()
     multiplexer.disconnect()
Exemplo n.º 14
0
 def test_connection(self):
     """Test that an OEF connection can be established to the OEF."""
     connection = _make_oef_connection(
         FETCHAI_ADDRESS_ONE,
         oef_addr="127.0.0.1",
         oef_port=10000,
     )
     multiplexer = Multiplexer([connection])
     multiplexer.connect()
     multiplexer.disconnect()
Exemplo n.º 15
0
def test_multiplexer():
    """Tests if the multiplexer is connected."""
    with LocalNode() as node:
        address_1 = "address_1"
        oef_local_connection = _make_local_connection(address_1, node)
        multiplexer = Multiplexer([oef_local_connection])
        multiplexer.connect()
        assert (
            multiplexer.is_connected
        ), "Mailbox cannot connect to the specific Connection(OEFLocalConnection)"
        multiplexer.disconnect()
Exemplo n.º 16
0
async def test_receiving_loop_terminated():
    """Test that connecting twice the multiplexer behaves correctly."""
    multiplexer = Multiplexer([DummyConnection()])
    multiplexer.connect()

    with unittest.mock.patch.object(aea.mail.base.logger, "debug") as mock_logger_debug:
        multiplexer.connection_status.is_connected = False
        await multiplexer._receiving_loop()
        mock_logger_debug.assert_called_with("Receiving loop terminated.")
        multiplexer.connection_status.is_connected = True
        multiplexer.disconnect()
Exemplo n.º 17
0
def test_connect_twice():
    """Test that connecting twice the multiplexer behaves correctly."""
    multiplexer = Multiplexer([DummyConnection()])

    assert not multiplexer.connection_status.is_connected
    multiplexer.connect()
    assert multiplexer.connection_status.is_connected
    multiplexer.connect()
    assert multiplexer.connection_status.is_connected

    multiplexer.disconnect()
Exemplo n.º 18
0
def test_connection():
    """Test that two OEF local connection can connect to a local node."""
    with LocalNode() as node:

        multiplexer1 = Multiplexer([OEFLocalConnection("multiplexer1", node)])
        multiplexer2 = Multiplexer([OEFLocalConnection("multiplexer2", node)])

        multiplexer1.connect()
        multiplexer2.connect()

        multiplexer1.disconnect()
        multiplexer2.disconnect()
Exemplo n.º 19
0
async def test_sending_loop_cancelled():
    """Test the case when the sending loop is cancelled."""
    multiplexer = Multiplexer([DummyConnection(connection_id="dummy")])

    multiplexer.connect()

    with unittest.mock.patch.object(aea.mail.base.logger, "debug") as mock_logger_debug:
        multiplexer._send_loop_task.cancel()
        await asyncio.sleep(0.1)
        mock_logger_debug.assert_called_with("Sending loop cancelled.")

    multiplexer.disconnect()
Exemplo n.º 20
0
async def test_receiving_loop_raises_exception():
    """Test the case when an error occurs when a receive is started."""
    connection = DummyConnection(connection_id="dummy")
    multiplexer = Multiplexer([connection])

    with unittest.mock.patch("asyncio.wait", side_effect=Exception("a weird error.")):
        with unittest.mock.patch.object(aea.mail.base.logger, "error") as mock_logger_error:
            multiplexer.connect()
            time.sleep(0.1)
            mock_logger_error.assert_called_with("Error in the receiving loop: a weird error.")

    multiplexer.disconnect()
Exemplo n.º 21
0
 def test_connection(self):
     """Test that an OEF connection can be established to the OEF."""
     crypto = FetchAICrypto()
     connection = OEFConnection(
         crypto.address,
         oef_addr="127.0.0.1",
         oef_port=10000,
         connection_id=PublicId("fetchai", "oef", "0.1.0"),
     )
     multiplexer = Multiplexer([connection])
     multiplexer.connect()
     multiplexer.disconnect()
Exemplo n.º 22
0
def test_connect_twice():
    """Test that connecting twice the multiplexer behaves correctly."""
    multiplexer = Multiplexer(
        [DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)]
    )

    assert not multiplexer.connection_status.is_connected
    multiplexer.connect()
    assert multiplexer.connection_status.is_connected
    multiplexer.connect()
    assert multiplexer.connection_status.is_connected

    multiplexer.disconnect()
Exemplo n.º 23
0
async def test_send_envelope_with_non_registered_connection():
    """Test that sending an envelope with an unregistered connection raises an exception."""
    connection = DummyConnection(connection_id="dummy")
    multiplexer = Multiplexer([connection])
    multiplexer.connect()

    envelope = Envelope(to="", sender="", protocol_id="default", message=b"",
                        context=EnvelopeContext(connection_id="this_is_an_unexisting_connection_id"))

    with pytest.raises(AEAConnectionError, match="No connection registered with id:.*"):
        await multiplexer._send(envelope)

    multiplexer.disconnect()
Exemplo n.º 24
0
def test_multiplexer_disconnect_all_raises_error():
    """Test the case when the multiplexer raises an exception while disconnecting."""
    multiplexer = Multiplexer([DummyConnection()])
    multiplexer.connect()

    assert multiplexer.connection_status.is_connected

    with unittest.mock.patch.object(multiplexer, "_disconnect_all", side_effect=Exception):
        with pytest.raises(AEAConnectionError, match="Failed to disconnect the multiplexer."):
            multiplexer.disconnect()

    # TODO is this what we want?
    assert multiplexer.connection_status.is_connected
Exemplo n.º 25
0
def test_outbox_put_message():
    """Tests that an envelope is created from the message is in the queue."""
    msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
    message_bytes = DefaultSerializer().encode(msg)
    multiplexer = Multiplexer([DummyConnection()])
    outbox = OutBox(multiplexer)
    inbox = InBox(multiplexer)
    multiplexer.connect()
    outbox.put_message("Agent1", "Agent0", DefaultMessage.protocol_id,
                       message_bytes)
    time.sleep(0.5)
    assert not inbox.empty(
    ), "Inbox will not be empty after putting a message."
    multiplexer.disconnect()
Exemplo n.º 26
0
def test_send_envelope_error_is_logged_by_send_loop():
    """Test that the AEAConnectionError in the '_send' method is logged by the '_send_loop'."""
    connection = DummyConnection(connection_id="dummy")
    multiplexer = Multiplexer([connection])
    multiplexer.connect()

    envelope = Envelope(to="", sender="", protocol_id="default", message=b"",
                        context=EnvelopeContext(connection_id="this_is_an_unexisting_connection_id"))

    with unittest.mock.patch.object(aea.mail.base.logger, "error") as mock_logger_error:
        multiplexer.put(envelope)
        time.sleep(0.1)
        mock_logger_error.assert_called_with("No connection registered with id: this_is_an_unexisting_connection_id.")

    multiplexer.disconnect()
Exemplo n.º 27
0
def test_multiplexer():
    """Tests if the multiplexer is connected."""
    with LocalNode() as node:
        address_1 = "address_1"
        multiplexer = Multiplexer([
            OEFLocalConnection(address_1,
                               node,
                               connection_id=PublicId("fetchai", "local",
                                                      "0.1.0"))
        ])
        multiplexer.connect()
        assert (
            multiplexer.is_connected
        ), "Mailbox cannot connect to the specific Connection(OEFLocalConnection)"
        multiplexer.disconnect()
Exemplo n.º 28
0
    def setup_class(cls):
        """Set the test up"""
        cls.cwd = os.getcwd()
        cls.t = tempfile.mkdtemp()
        # os.chdir(cls.t)

        port_relay_1 = DEFAULT_PORT + 10
        cls.connection_relay_1 = _make_libp2p_connection(port_relay_1)
        cls.multiplexer_relay_1 = Multiplexer([cls.connection_relay_1])
        cls.multiplexer_relay_1.connect()

        port_relay_2 = DEFAULT_PORT + 100
        cls.connection_relay_2 = _make_libp2p_connection(port_relay_2)
        cls.multiplexer_relay_2 = Multiplexer([cls.connection_relay_2])
        cls.multiplexer_relay_2.connect()

        time.sleep(2)
        relay_peer_1 = cls.connection_relay_1.node.multiaddrs
        relay_peer_2 = cls.connection_relay_2.node.multiaddrs

        cls.connections = []
        cls.multiplexers = []

        port = port_relay_1
        for _ in range(int(DEFAULT_NET_SIZE / 2)):
            port += 1
            conn = _make_libp2p_connection(
                port=port, relay=False, entry_peers=relay_peer_1
            )
            mux = Multiplexer([conn])
            cls.connections.append(conn)
            cls.multiplexers.append(mux)
            mux.connect()

        port = port_relay_2
        for _ in range(int(DEFAULT_NET_SIZE / 2)):
            port += 1
            conn = _make_libp2p_connection(
                port=port, relay=False, entry_peers=relay_peer_2
            )
            mux = Multiplexer([conn])
            cls.connections.append(conn)
            cls.multiplexers.append(mux)
            mux.connect()

        time.sleep(2)
Exemplo n.º 29
0
def test_outbox_put():
    """Tests that an envelope is putted into the queue."""
    msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
    message_bytes = DefaultSerializer().encode(msg)
    multiplexer = Multiplexer([DummyConnection()])
    outbox = OutBox(multiplexer)
    inbox = InBox(multiplexer)
    multiplexer.connect()
    envelope = Envelope(to="Agent1",
                        sender="Agent0",
                        protocol_id=DefaultMessage.protocol_id,
                        message=message_bytes)
    outbox.put(envelope)
    time.sleep(0.5)
    assert not inbox.empty(
    ), "Inbox must not be empty after putting an envelope"
    multiplexer.disconnect()
Exemplo n.º 30
0
def test_connection():
    """Test that two OEF local connection can connect to a local node."""
    with LocalNode() as node:

        local_id_1 = PublicId("fetchai", "local1", "0.1.0")
        local_id_2 = PublicId("fetchai", "local2", "0.1.0")
        multiplexer1 = Multiplexer([
            OEFLocalConnection("multiplexer1", node, connection_id=local_id_1)
        ])
        multiplexer2 = Multiplexer([
            OEFLocalConnection("multiplexer2", node, connection_id=local_id_2)
        ])

        multiplexer1.connect()
        multiplexer2.connect()

        multiplexer1.disconnect()
        multiplexer2.disconnect()