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 _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!")
def test_inbox_nowait(): """Tests the inbox without waiting.""" agent_address = "Agent0" receiver_address = "Agent1" msg = Message(content="hello") msg.counterparty = receiver_address multiplexer = Multiplexer([_make_dummy_connection()]) envelope = Envelope( to=receiver_address, sender=agent_address, protocol_id=UNKNOWN_PROTOCOL_PUBLIC_ID, message=msg, ) multiplexer.in_queue.put(envelope) inbox = InBox(multiplexer) assert (inbox.get_nowait() == envelope ), "Check for a message on the in queue and wait for no time."
def test_inbox_nowait(): """Tests the inbox without waiting.""" agent_address = "Agent0" receiver_address = "Agent1" msg = DefaultMessage(performative=DefaultMessage.Performative.BYTES, content="hello") msg.to = receiver_address multiplexer = Multiplexer([_make_dummy_connection()]) envelope = Envelope( to=receiver_address, sender=agent_address, message=msg, ) multiplexer.in_queue.put(envelope) inbox = InBox(multiplexer) assert (inbox.get_nowait() == envelope ), "Check for a message on the in queue and wait for no time."
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()
def test_inbox_get_nowait_returns_none(): """Test that getting an envelope from an empty inbox returns None.""" # TODO get_nowait in this case should raise an exception, like it's done in queue.Queue multiplexer = Multiplexer([_make_dummy_connection()]) inbox = InBox(multiplexer) assert inbox.get_nowait() is None