def test_connection(): """Test that two OEF local connection can connect to a local node.""" with LocalNode() as node: multiplexer1 = Multiplexer([_make_local_connection("multiplexer1", node)]) multiplexer2 = Multiplexer([_make_local_connection("multiplexer2", node)]) multiplexer1.connect() multiplexer2.connect() multiplexer1.disconnect() multiplexer2.disconnect()
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address_1" cls.multiplexer1 = Multiplexer( [_make_local_connection(cls.address_1, cls.node,)] ) cls.address_2 = "address_2" cls.multiplexer2 = Multiplexer( [_make_local_connection(cls.address_2, cls.node,)] ) cls.multiplexer1.connect() cls.multiplexer2.connect() cls.dialogues = OefSearchDialogues(cls.address_1)
async def test_receiving_when_not_connected_raise_exception(): """Test that when we try to receive an envelope from a not connected connection we raise exception.""" with LocalNode() as node: with pytest.raises(ConnectionError): address = "address" connection = _make_local_connection(address, node) await connection.receive()
async def test_connection_twice_return_none(): """Test that connecting twice works.""" with LocalNode() as node: address = "address" connection = _make_local_connection(address, node) await connection.connect() await node.connect(address, connection._reader) message = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) expected_envelope = Envelope( to=address, sender=address, protocol_id=DefaultMessage.protocol_id, message=message, ) await connection.send(expected_envelope) actual_envelope = await connection.receive() assert expected_envelope == actual_envelope await connection.disconnect()
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address_1" cls.multiplexer1 = Multiplexer( [_make_local_connection( cls.address_1, cls.node, )])
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()
async def test_receiving_returns_none_when_error_occurs(): """Test that when we try to receive an envelope and an error occurs we return None.""" with LocalNode() as node: address = "address" connection = _make_local_connection(address, node) await connection.connect() with unittest.mock.patch.object(connection._reader, "get", side_effect=Exception): result = await connection.receive() assert result is None await connection.disconnect()
def setup_class(cls): """Set the test up.""" agent_name = "my_agent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) identity = Identity(agent_name, address=wallet.addresses[DEFAULT_LEDGER]) connection = _make_local_connection(identity.address, LocalNode()) resources = Resources() cls.context_namespace = {"key1": 1, "key2": 2} cls.agent = AEA(identity, wallet, resources, **cls.context_namespace) resources.add_connection(connection) resources.add_component( Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"), agent_context=cls.agent.context)) for skill in resources.get_all_skills(): skill.skill_context.set_agent_context(cls.agent.context)
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address" cls.multiplexer = Multiplexer( [_make_local_connection( cls.address_1, cls.node, )]) cls.multiplexer.connect() # register a service. request_id = 1 cls.data_model = DataModel( "foobar", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=cls.data_model) register_service_request = OefSearchMessage( performative=OefSearchMessage.Performative.REGISTER_SERVICE, dialogue_reference=(str(request_id), ""), service_description=service_description, ) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OefSearchMessage.protocol_id, message=register_service_request, ) cls.multiplexer.put(envelope)
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address" cls.multiplexer = Multiplexer( [_make_local_connection( cls.address_1, cls.node, )]) cls.multiplexer.connect() # register a service. cls.dialogues = OefSearchDialogues(cls.address_1) cls.data_model = DataModel( "foobar", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=cls.data_model) register_service_request, cls.sending_dialogue = cls.dialogues.create( counterparty=str(OEFLocalConnection.connection_id), performative=OefSearchMessage.Performative.REGISTER_SERVICE, service_description=service_description, ) envelope = Envelope( to=register_service_request.to, sender=register_service_request.sender, protocol_id=register_service_request.protocol_id, message=register_service_request, ) cls.multiplexer.put(envelope)
def test_add_behaviour_dynamically(): """Test that we can add a behaviour dynamically.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) resources = Resources() identity = Identity(agent_name, address=wallet.addresses[DEFAULT_LEDGER]) connection = _make_local_connection(identity.address, LocalNode()) agent = AEA( identity, wallet, resources, default_connection=connection.public_id, ) resources.add_connection(connection) resources.add_component( Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"), agent_context=agent.context)) for skill in resources.get_all_skills(): skill.skill_context.set_agent_context(agent.context) with run_in_thread(agent.start, timeout=5, on_exit=agent.stop): wait_for_condition(lambda: agent.is_running, timeout=10) dummy_skill_id = DUMMY_SKILL_PUBLIC_ID dummy_skill = agent.resources.get_skill(dummy_skill_id) wait_for_condition(lambda: dummy_skill is not None, timeout=10) new_behaviour = DummyBehaviour(name="dummy2", skill_context=dummy_skill.skill_context) dummy_skill.skill_context.new_behaviours.put(new_behaviour) wait_for_condition(lambda: new_behaviour.nb_act_called > 0, timeout=10) wait_for_condition( lambda: len(agent.resources.get_behaviours(dummy_skill_id)) == 2, timeout=10)
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address_1" cls.multiplexer1 = Multiplexer( [_make_local_connection( cls.address_1, cls.node, )]) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address) -> BaseDialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return FipaDialogue.Role.SELLER cls.dialogues = FipaDialogues(cls.address_1, role_from_first_message)
def test_run_agent(): """Test that we can set up and then run the agent.""" with LocalNode() as node: agent_name = "dummyagent" agent_address = "some_address" identity = Identity(agent_name, address=agent_address) oef_local_connection = _make_local_connection(agent_address, node) oef_local_connection._local_node = node agent = DummyAgent(identity, [oef_local_connection], loop=asyncio.new_event_loop()) agent_thread = Thread(target=agent.start) assert agent.state == RuntimeStates.stopped agent_thread.start() try: wait_for_condition( lambda: agent.state == RuntimeStates.running, timeout=10, error_msg="Agent state must be 'running'", ) finally: agent.stop() assert agent.state == RuntimeStates.stopped agent_thread.join()
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "multiplexer1" cls.address_2 = "multiplexer2" cls.multiplexer1 = Multiplexer( [_make_local_connection( cls.address_1, cls.node, )]) cls.multiplexer2 = Multiplexer( [_make_local_connection( cls.address_2, cls.node, )]) cls.multiplexer1.connect() cls.multiplexer2.connect() cls.dialogues1 = OefSearchDialogues(cls.address_1) cls.dialogues2 = OefSearchDialogues(cls.address_2) # register 'multiplexer1' as a service 'foobar'. cls.data_model_foobar = DataModel( "foobar", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=cls.data_model_foobar) register_service_request, sending_dialogue = cls.dialogues1.create( counterparty=str(OEFLocalConnection.connection_id), performative=OefSearchMessage.Performative.REGISTER_SERVICE, service_description=service_description, ) envelope = Envelope( to=register_service_request.to, sender=register_service_request.sender, message=register_service_request, ) cls.multiplexer1.put(envelope) wait_for_condition(lambda: len(cls.node.services) == 1, timeout=10) # register 'multiplexer2' as a service 'barfoo'. cls.data_model_barfoo = DataModel( "barfoo", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=cls.data_model_barfoo) register_service_request, sending_dialogue = cls.dialogues2.create( counterparty=str(OEFLocalConnection.connection_id), performative=OefSearchMessage.Performative.REGISTER_SERVICE, service_description=service_description, ) envelope = Envelope( to=register_service_request.to, sender=register_service_request.sender, message=register_service_request, ) cls.multiplexer2.put(envelope) wait_for_condition(lambda: len(cls.node.services) == 2, timeout=10) # unregister multiplexer1 data_model = DataModel( "foobar", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=data_model) msg, sending_dialogue = cls.dialogues1.create( counterparty=str(OEFLocalConnection.connection_id), performative=OefSearchMessage.Performative.UNREGISTER_SERVICE, service_description=service_description, ) envelope = Envelope( to=msg.to, sender=msg.sender, message=msg, ) cls.multiplexer1.put(envelope) # ensure one service stays registered wait_for_condition(lambda: len(cls.node.services) == 1, timeout=10)
def test_initialize_aea_programmatically_build_resources(): """Test that we can initialize the agent by building the resource object.""" try: temp = tempfile.mkdtemp(prefix="test_aea_resources") with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) identity = Identity(agent_name, address=wallet.addresses[DEFAULT_LEDGER]) connection = _make_local_connection(agent_name, node) resources = Resources() aea = AEA( identity, wallet, resources=resources, default_connection=connection.public_id, ) default_protocol = Protocol.from_dir( str(Path("packages", "fetchai", "protocols", "default"))) resources.add_protocol(default_protocol) resources.add_connection(connection) error_skill = Skill.from_dir( str(Path("packages", "fetchai", "skills", "error")), agent_context=aea.context, ) dummy_skill = Skill.from_dir(str( Path(CUR_PATH, "data", "dummy_skill")), agent_context=aea.context) resources.add_skill(dummy_skill) resources.add_skill(error_skill) default_protocol_id = DefaultMessage.protocol_id expected_message = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) expected_message.to = agent_name expected_message.sender = agent_name with run_in_thread(aea.start, timeout=5, on_exit=aea.stop): wait_for_condition(lambda: aea.is_running, timeout=10) aea.outbox.put( Envelope( to=agent_name, sender=agent_name, protocol_id=default_protocol_id, message=expected_message, )) dummy_skill_id = DUMMY_SKILL_PUBLIC_ID dummy_behaviour_name = "dummy" dummy_behaviour = aea.resources.get_behaviour( dummy_skill_id, dummy_behaviour_name) wait_for_condition(lambda: dummy_behaviour is not None, timeout=10) wait_for_condition(lambda: dummy_behaviour.nb_act_called > 0, timeout=10) dummy_task = DummyTask() task_id = aea.enqueue_task(dummy_task) async_result = aea.get_task_result(task_id) expected_dummy_task = async_result.get(10.0) wait_for_condition( lambda: expected_dummy_task.nb_execute_called > 0, timeout=10) dummy_handler_name = "dummy" dummy_handler = aea.resources._handler_registry.fetch( (dummy_skill_id, dummy_handler_name)) dummy_handler_alt = aea.resources.get_handler( DefaultMessage.protocol_id, dummy_skill_id) wait_for_condition(lambda: dummy_handler == dummy_handler_alt, timeout=10) wait_for_condition(lambda: dummy_handler is not None, timeout=10) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 1, timeout=10) wait_for_condition( lambda: dummy_handler.handled_messages[0] == expected_message, timeout=10, ) finally: Path(temp).rmdir()
def test_communication(): """Test that two multiplexer can communicate through the node.""" with LocalNode() as node: multiplexer1 = Multiplexer( [_make_local_connection("multiplexer1", node)]) multiplexer2 = Multiplexer( [_make_local_connection("multiplexer2", node)]) multiplexer1.connect() multiplexer2.connect() msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) envelope = Envelope( to="multiplexer2", sender="multiplexer1", protocol_id=DefaultMessage.protocol_id, message=msg, ) multiplexer1.put(envelope) msg = FipaMessage( performative=FipaMessage.Performative.CFP, dialogue_reference=(str(0), ""), message_id=1, target=0, query=Query([Constraint("something", ConstraintType(">", 1))]), ) envelope = Envelope( to="multiplexer2", sender="multiplexer1", protocol_id=FipaMessage.protocol_id, message=msg, ) multiplexer1.put(envelope) msg = FipaMessage( performative=FipaMessage.Performative.PROPOSE, dialogue_reference=(str(0), ""), message_id=2, target=1, proposal=Description({}), ) envelope = Envelope( to="multiplexer2", sender="multiplexer1", protocol_id=FipaMessage.protocol_id, message=msg, ) multiplexer1.put(envelope) msg = FipaMessage( performative=FipaMessage.Performative.ACCEPT, dialogue_reference=(str(0), ""), message_id=1, target=0, ) envelope = Envelope( to="multiplexer2", sender="multiplexer1", protocol_id=FipaMessage.protocol_id, message=msg, ) multiplexer1.put(envelope) msg = FipaMessage( performative=FipaMessage.Performative.DECLINE, dialogue_reference=(str(0), ""), message_id=1, target=0, ) envelope = Envelope( to="multiplexer2", sender="multiplexer1", protocol_id=FipaMessage.protocol_id, message=msg, ) multiplexer1.put(envelope) envelope = multiplexer2.get(block=True, timeout=1.0) msg = envelope.message assert envelope.protocol_id == DefaultMessage.protocol_id assert msg.content == b"hello" envelope = multiplexer2.get(block=True, timeout=1.0) msg = envelope.message assert envelope.protocol_id == FipaMessage.protocol_id assert msg.performative == FipaMessage.Performative.CFP envelope = multiplexer2.get(block=True, timeout=1.0) msg = envelope.message assert envelope.protocol_id == FipaMessage.protocol_id assert msg.performative == FipaMessage.Performative.PROPOSE envelope = multiplexer2.get(block=True, timeout=1.0) msg = envelope.message assert envelope.protocol_id == FipaMessage.protocol_id assert msg.performative == FipaMessage.Performative.ACCEPT envelope = multiplexer2.get(block=True, timeout=1.0) msg = envelope.message assert envelope.protocol_id == FipaMessage.protocol_id assert msg.performative == FipaMessage.Performative.DECLINE multiplexer1.disconnect() multiplexer2.disconnect()
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "multiplexer1" cls.address_2 = "multiplexer2" cls.multiplexer1 = Multiplexer( [_make_local_connection( cls.address_1, cls.node, )]) cls.multiplexer2 = Multiplexer( [_make_local_connection( cls.address_2, cls.node, )]) cls.multiplexer1.connect() cls.multiplexer2.connect() # register 'multiplexer1' as a service 'foobar'. request_id = 1 cls.data_model_foobar = DataModel( "foobar", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=cls.data_model_foobar) register_service_request = OefSearchMessage( performative=OefSearchMessage.Performative.REGISTER_SERVICE, dialogue_reference=(str(request_id), ""), service_description=service_description, ) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OefSearchMessage.protocol_id, message=register_service_request, ) cls.multiplexer1.put(envelope) time.sleep(1.0) # register 'multiplexer2' as a service 'barfoo'. cls.data_model_barfoo = DataModel( "barfoo", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=cls.data_model_barfoo) register_service_request = OefSearchMessage( performative=OefSearchMessage.Performative.REGISTER_SERVICE, dialogue_reference=(str(request_id), ""), service_description=service_description, ) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_2, protocol_id=OefSearchMessage.protocol_id, message=register_service_request, ) cls.multiplexer2.put(envelope) # unregister multiplexer1 data_model = DataModel( "foobar", attributes=[ Attribute("foo", int, True), Attribute("bar", str, True) ], ) service_description = Description({ "foo": 1, "bar": "baz" }, data_model=data_model) msg = OefSearchMessage( performative=OefSearchMessage.Performative.UNREGISTER_SERVICE, dialogue_reference=(str(1), ""), service_description=service_description, ) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OefSearchMessage.protocol_id, message=msg, ) cls.multiplexer1.put(envelope)