コード例 #1
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()
コード例 #2
0
ファイル: test_mail.py プロジェクト: 8ball030/agents-aea
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()
コード例 #3
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)
コード例 #4
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()
コード例 #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()
コード例 #6
0
ファイル: test_mail.py プロジェクト: 8ball030/agents-aea
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()
コード例 #7
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()
コード例 #8
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()
コード例 #9
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()
コード例 #10
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()
コード例 #11
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()
コード例 #12
0
def run():
    # Ensure the input and output files do not exist initially
    if os.path.isfile(INPUT_FILE):
        os.remove(INPUT_FILE)
    if os.path.isfile(OUTPUT_FILE):
        os.remove(OUTPUT_FILE)

    # create the connection and multiplexer objects
    stub_connection = StubConnection(input_file_path=INPUT_FILE,
                                     output_file_path=OUTPUT_FILE)
    multiplexer = Multiplexer([stub_connection])
    try:
        # Set the multiplexer running in a different thread
        t = Thread(target=multiplexer.connect)
        t.start()

        # Wait for everything to start up
        time.sleep(3)

        # Create a message inside an envelope and get the stub connection to pass it into the multiplexer
        message_text = (
            "multiplexer,some_agent,fetchai/default:0.1.0,\x08\x01*\x07\n\x05hello,"
        )
        with open(INPUT_FILE, "w") as f:
            f.write(message_text)

        # Wait for the envelope to get processed
        time.sleep(2)

        # get the envelope
        envelope = multiplexer.get()  # type: Optional[Envelope]
        assert envelope is not None

        # Inspect its contents
        print(
            "Envelope received by Multiplexer: sender={}, to={}, protocol_id={}, message={}"
            .format(envelope.sender, envelope.to, envelope.protocol_id,
                    envelope.message))

        # Create a mirrored response envelope
        response_envelope = copy(envelope)
        response_envelope.to = envelope.sender
        response_envelope.sender = envelope.to

        # Send the envelope back
        multiplexer.put(response_envelope)

        # Read the output envelope generated by the multiplexer
        with open(OUTPUT_FILE, "r") as f:
            print("Envelope received from Multiplexer: " + f.readline())
    finally:
        # Shut down the multiplexer
        multiplexer.disconnect()
        t.join()
コード例 #13
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()
コード例 #14
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()
コード例 #15
0
ファイル: test_mail.py プロジェクト: yangjue-han/agents-aea
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()
コード例 #16
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()
コード例 #17
0
ファイル: test_misc.py プロジェクト: 8ball030/agents-aea
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()
コード例 #18
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()
コード例 #19
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()
コード例 #20
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()
コード例 #21
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()
コード例 #22
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
コード例 #23
0
ファイル: test_mail.py プロジェクト: 8ball030/agents-aea
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()
コード例 #24
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()
コード例 #25
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()
コード例 #26
0
ファイル: test_mail.py プロジェクト: 8ball030/agents-aea
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()
コード例 #27
0
def test_multiplexer_disconnect_all_raises_error():
    """Test the case when the multiplexer raises an exception while disconnecting."""
    multiplexer = Multiplexer([_make_dummy_connection()])
    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()

    # # do the true disconnection - for clean the test up
    assert multiplexer.connection_status.is_connected
    multiplexer.disconnect()
    assert not multiplexer.connection_status.is_connected
コード例 #28
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_CONNECTION_PUBLIC_ID)
    multiplexer = Multiplexer([connection])
    multiplexer.connect()

    envelope = Envelope(
        to="",
        sender="",
        protocol_id=DefaultMessage.protocol_id,
        message=b"",
        context=EnvelopeContext(connection_id=UNKNOWN_CONNECTION_PUBLIC_ID),
    )

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

    multiplexer.disconnect()
コード例 #29
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()
コード例 #30
0
async 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.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

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

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

        # clean the test up.
        await connection_3.disconnect()
        multiplexer.disconnect()
        shutil.rmtree(tmpdir)