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 _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.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()
async def test_outbox_negative(): """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"", ) context = EnvelopeContext(connection_id=connection_1.connection_id) envelope = Envelope( to="to", sender="sender", protocol_specification_id=msg.protocol_specification_id, message=b"", context=context, ) try: await multiplexer.connect() outbox = OutBox(multiplexer) assert outbox.empty() with pytest.raises(ValueError) as execinfo: outbox.put(envelope) assert ( str(execinfo.value) == "Only Message type allowed in envelope message field when putting into outbox." ) assert outbox.empty() with pytest.raises(ValueError) as execinfo: outbox.put_message("") assert str(execinfo.value) == "Provided message not of type Message." assert outbox.empty() with pytest.raises(ValueError) as execinfo: outbox.put_message(msg) assert str( execinfo.value) == "Provided message has message.to not set." assert outbox.empty() msg.to = "to" with pytest.raises(ValueError) as execinfo: outbox.put_message(msg) assert str( execinfo.value) == "Provided message has message.sender not set." finally: await multiplexer.disconnect()
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 _process_envelopes( agent_name: str, inbox: InBox, outbox: OutBox, dialogues: Dialogues, message_class: Type[Message], ) -> None: """ Process envelopes. :param agent_name: name of an agent. :param inbox: an inbox object. :param outbox: an outbox object. :param dialogues: the dialogues object. :param message_class: the message class. :return: None. """ envelope = _try_construct_envelope(agent_name, dialogues, message_class) if envelope is None: _check_for_incoming_envelope(inbox, message_class) else: outbox.put(envelope) click.echo(_construct_message("sending", envelope, message_class))