async def test_default_route_applied(caplog): """Test default route is selected automatically.""" logger = logging.getLogger("aea.multiplexer") with caplog.at_level(logging.DEBUG, logger="aea.multiplexer"): connection_1 = _make_dummy_connection() connections = [connection_1] multiplexer = AsyncMultiplexer(connections, loop=asyncio.get_event_loop()) multiplexer.logger = logger envelope = Envelope( to="", sender="", protocol_id=DefaultMessage.protocol_id, message=b"", context=EnvelopeContext(), ) multiplexer.default_routing = { DefaultMessage.protocol_id: connection_1.connection_id } try: await multiplexer.connect() inbox = InBox(multiplexer) outbox = InBox(multiplexer) assert inbox.empty() assert outbox.empty() multiplexer.put(envelope) await outbox.async_get() finally: await multiplexer.disconnect() assert "Using default routing:" in caplog.text
def _process_envelopes( agent_name: str, identity_stub: Identity, inbox: InBox, outbox: OutBox, dialogues: DefaultDialogues, ) -> None: """ Process envelopes. :param agent_name: name of an agent. :param identity_stub: stub identity. :param inbox: an inbox object. :param outbox: an outbox object. :param dialogues: the dialogues object. :return: None. """ envelope = _try_construct_envelope(agent_name, identity_stub.name, dialogues) if envelope is None: if not inbox.empty(): envelope = inbox.get_nowait() assert envelope is not None, "Could not recover envelope from inbox." click.echo(_construct_message("received", envelope)) else: click.echo("Received no new envelope!") else: outbox.put(envelope) click.echo(_construct_message("sending", envelope))
def test_outbox_put(): """Tests that an envelope is putted into the queue.""" agent_address = "Agent0" receiver_address = "Agent1" msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.counterparty = receiver_address dummy_connection = _make_dummy_connection() multiplexer = Multiplexer([dummy_connection]) outbox = OutBox(multiplexer, agent_address) inbox = InBox(multiplexer) multiplexer.connect() envelope = Envelope( to=receiver_address, sender=agent_address, protocol_id=DefaultMessage.protocol_id, message=msg, ) outbox.put(envelope) time.sleep(0.5) assert not inbox.empty(), "Inbox must not be empty after putting an envelope" multiplexer.disconnect()
def test_outbox_put(): """Tests that an envelope is putted into the queue.""" agent_address = "Agent0" receiver_address = "Agent1" msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.to = receiver_address msg.sender = agent_address dummy_connection = _make_dummy_connection() multiplexer = Multiplexer([dummy_connection]) outbox = OutBox(multiplexer) inbox = InBox(multiplexer) multiplexer.connect() envelope = Envelope( to=receiver_address, sender=agent_address, message=msg, ) outbox.put(envelope) wait_for_condition(lambda: inbox.empty(), 15, "Inbox must not be empty after putting an envelope") multiplexer.disconnect()
def _check_for_incoming_envelope(inbox: InBox, message_class: Type[Message]): if not inbox.empty(): envelope = inbox.get_nowait() if envelope is None: raise ValueError("Could not recover envelope from inbox.") click.echo(_construct_message("received", envelope, message_class)) else: click.echo("Received no new envelope!")
async def test_threaded_mode(): """Test InBox OutBox objects in threaded mode.""" connection_1 = _make_dummy_connection() connections = [connection_1] multiplexer = AsyncMultiplexer(connections, threaded=True) msg = DefaultMessage( performative=DefaultMessage.Performative.BYTES, content=b"", ) msg.to = "to" msg.sender = "sender" context = EnvelopeContext(connection_id=connection_1.connection_id) envelope = Envelope( to="to", sender="sender", message=msg, context=context, ) try: await multiplexer.connect() await asyncio.sleep(0.5) inbox = InBox(multiplexer) outbox = OutBox(multiplexer) assert inbox.empty() assert outbox.empty() outbox.put(envelope) received = await inbox.async_get() assert received == envelope assert inbox.empty() assert outbox.empty() outbox.put_message(msg, context=context) await inbox.async_wait() received = inbox.get_nowait() assert received == envelope finally: await multiplexer.disconnect()
async def test_inbox_outbox(): """Test InBox OutBox objects.""" connection_1 = _make_dummy_connection() connections = [connection_1] multiplexer = AsyncMultiplexer(connections, loop=asyncio.get_event_loop()) msg = DefaultMessage(performative=DefaultMessage.Performative.BYTES, content=b"",) msg.counterparty = "to" msg.sender = "sender" context = EnvelopeContext(connection_id=connection_1.connection_id) envelope = Envelope( to="to", sender="sender", protocol_id=msg.protocol_id, message=msg, context=context, ) try: await multiplexer.connect() inbox = InBox(multiplexer) outbox = OutBox(multiplexer, "default_address") assert inbox.empty() assert outbox.empty() outbox.put(envelope) received = await inbox.async_get() assert received == envelope assert inbox.empty() assert outbox.empty() outbox.put_message(msg, context=context) await inbox.async_wait() received = inbox.get_nowait() assert received == envelope finally: await multiplexer.disconnect()
async def test_inbox_outbox(): """Test InBox OutBox objects.""" connection_1 = _make_dummy_connection() connections = [connection_1] multiplexer = AsyncMultiplexer(connections, loop=asyncio.get_event_loop()) envelope = Envelope( to="", sender="", protocol_id=DefaultMessage.protocol_id, message=b"", context=EnvelopeContext(connection_id=connection_1.connection_id), ) try: await multiplexer.connect() inbox = InBox(multiplexer) outbox = InBox(multiplexer) assert inbox.empty() assert outbox.empty() multiplexer.put(envelope) await outbox.async_get() finally: await multiplexer.disconnect()
def test_outbox_put_message(): """Tests that an envelope is created from the message is in the queue.""" agent_address = "Agent0" receiver_address = "Agent1" msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.counterparty = receiver_address dummy_connection = _make_dummy_connection() multiplexer = Multiplexer([dummy_connection]) outbox = OutBox(multiplexer, agent_address) inbox = InBox(multiplexer) multiplexer.connect() outbox.put_message(msg) time.sleep(0.5) assert not inbox.empty(), "Inbox will not be empty after putting a message." multiplexer.disconnect()
def test_inbox_empty(): """Tests if the inbox is empty.""" multiplexer = Multiplexer([_make_dummy_connection()]) _inbox = InBox(multiplexer) assert _inbox.empty(), "Inbox is not empty"