def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address" cls.multiplexer = Multiplexer( [ OEFLocalConnection( cls.address_1, cls.node, connection_id=PublicId("fetchai", "local", "0.1.0"), ) ] ) cls.multiplexer.connect() # register a service. request_id = 1 service_id = "" cls.data_model = DataModel("foobar", attributes=[]) service_description = Description( {"foo": 1, "bar": "baz"}, data_model=cls.data_model ) register_service_request = OEFMessage( type=OEFMessage.Type.REGISTER_SERVICE, id=request_id, service_description=service_description, service_id=service_id, ) msg_bytes = OEFSerializer().encode(register_service_request) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) cls.multiplexer.put(envelope)
def on_search_result(self, search_id: int, agents: List[Address]) -> None: """ On accept event handler. :param search_id: the search id. :param agents: the list of agents. :return: None """ assert self.in_queue is not None assert self.loop is not None msg = OEFMessage( type=OEFMessage.Type.SEARCH_RESULT, id=search_id, agents=agents ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=self.address, sender=DEFAULT_OEF, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) asyncio.run_coroutine_threadsafe( self.in_queue.put(envelope), self.loop ).result()
def _unregister_service(self) -> None: """ Unregister service from OEF Service Directory. :return: None """ if self._registered_service_description is not None: self._oef_msf_id += 1 msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_SERVICE, id=self._oef_msf_id, service_description=self._registered_service_description, service_id=SERVICE_ID, ) self.context.outbox.put_message( to=DEFAULT_OEF, sender=self.context.agent_address, protocol_id=OEFMessage.protocol_id, message=OEFSerializer().encode(msg), ) self.context.logger.info( "[{}]: unregistering car park detection services from OEF.". format(self.context.agent_name)) self._registered_service_description = None
async def test_messages(self): """Test that at the beginning, the search request returns an empty search result.""" msg = FIPAMessage((str(0), ""), 0, 0, FIPAMessage.Performative.CFP, query=None) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=FIPAMessage.protocol_id, message=msg_bytes, ) with pytest.raises(AEAConnectionError): await OEFLocalConnection( self.address_1, self.node, connection_id=PublicId("fetchai", "local", "0.1.0"), ).send(envelope) self.multiplexer1.connect() msg = FIPAMessage( (str(0), str(1)), 0, 0, FIPAMessage.Performative.CFP, query=None ) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to="this_address_does_not_exist", sender=self.address_1, protocol_id=FIPAMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # check the result response_envelope = self.multiplexer1.get(block=True, timeout=5.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.sender == DEFAULT_OEF result = OEFSerializer().decode(response_envelope.message) assert result.get("type") == OEFMessage.Type.DIALOGUE_ERROR
def _unregister_service(self) -> None: """ Unregister service from OEF Service Directory. :return: None """ strategy = cast(Strategy, self.context.strategy) oef_msg_id = strategy.get_next_oef_msg_id() msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_SERVICE, id=oef_msg_id, service_description=self._registered_service_description, service_id=SERVICE_ID, ) self.context.outbox.put_message( to=DEFAULT_OEF, sender=self.context.agent_address, protocol_id=OEFMessage.protocol_id, message=OEFSerializer().encode(msg), ) self.context.logger.info( "[{}]: unregistering weather station services from OEF.".format( self.context.agent_name)) self._registered_service_description = None
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( [ OEFLocalConnection( cls.address_1, cls.node, connection_id=PublicId("fetchai", "local", "0.1.0"), ) ] ) cls.multiplexer2 = Multiplexer( [ OEFLocalConnection( cls.address_2, cls.node, connection_id=PublicId("fetchai", "local", "0.1.0"), ) ] ) cls.multiplexer1.connect() cls.multiplexer2.connect() # register 'multiplexer1' as a service 'foobar'. request_id = 1 service_id = "" cls.data_model_foobar = DataModel("foobar", attributes=[]) service_description = Description( {"foo": 1, "bar": "baz"}, data_model=cls.data_model_foobar ) register_service_request = OEFMessage( type=OEFMessage.Type.REGISTER_SERVICE, id=request_id, service_description=service_description, service_id=service_id, ) msg_bytes = OEFSerializer().encode(register_service_request) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) cls.multiplexer1.put(envelope) time.sleep(1.0) # register 'multiplexer2' as a service 'barfoo'. cls.data_model_barfoo = DataModel("barfoo", attributes=[]) service_description = Description( {"foo": 1, "bar": "baz"}, data_model=cls.data_model_barfoo ) register_service_request = OEFMessage( type=OEFMessage.Type.REGISTER_SERVICE, id=request_id, service_description=service_description, service_id=service_id, ) msg_bytes = OEFSerializer().encode(register_service_request) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_2, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) cls.multiplexer2.put(envelope) # unregister multiplexer1 data_model = DataModel("foobar", attributes=[]) service_description = Description( {"foo": 1, "bar": "baz"}, data_model=data_model ) msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_SERVICE, id=0, service_description=service_description, service_id="Test_service", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) cls.multiplexer1.put(envelope)
def test_search_agent(self): """Test the registered agents, we will not find any.""" data_model = DataModel("foobar", attributes=[]) agent_description = Description({"foo": 1, "bar": "baz"}, data_model=data_model) query = Query(constraints=[], model=data_model) # Register an agent msg = OEFMessage( type=OEFMessage.Type.REGISTER_AGENT, id=0, agent_description=agent_description, agent_id="Test_agent", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) time.sleep(0.1) # Search for the register agent msg = OEFMessage(type=OEFMessage.Type.SEARCH_AGENTS, id=0, query=query) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_2, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer2.put(envelope) # check the result response_envelope = self.multiplexer2.get(block=True, timeout=5.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.sender == DEFAULT_OEF result = OEFSerializer().decode(response_envelope.message) assert len(result.get("agents")) == 1, "There are registered agents!" # Send unregister message. msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_AGENT, id=0, agent_description=agent_description, agent_id="Test_agent", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) time.sleep(0.1) # Trigger error message. msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_AGENT, id=0, agent_description=agent_description, agent_id="Unknown_Agent", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # check the result response_envelope = self.multiplexer1.get(block=True, timeout=5.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.sender == DEFAULT_OEF result = OEFSerializer().decode(response_envelope.message) assert result.get("type") == OEFMessage.Type.OEF_ERROR
def test_unregister_service_result(self): """Test that at the beginning, the search request returns an empty search result.""" data_model = DataModel("foobar", attributes=[]) service_description = Description( {"foo": 1, "bar": "baz"}, data_model=data_model ) msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_SERVICE, id=0, service_description=service_description, service_id="Test_service", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # check the result response_envelope = self.multiplexer1.get(block=True, timeout=5.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.sender == DEFAULT_OEF result = OEFSerializer().decode(response_envelope.message) assert result.get("type") == OEFMessage.Type.OEF_ERROR msg = OEFMessage( type=OEFMessage.Type.REGISTER_SERVICE, id=0, service_description=service_description, service_id="Test_Service", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # Search for the register agent msg = OEFMessage( type=OEFMessage.Type.SEARCH_AGENTS, id=0, query=Query([Constraint("foo", ConstraintType("==", 1))]), ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # check the result response_envelope = self.multiplexer1.get(block=True, timeout=5.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.sender == DEFAULT_OEF result = OEFSerializer().decode(response_envelope.message) assert result.get("type") == OEFMessage.Type.SEARCH_RESULT assert len(result.get("agents")) == 1 # unregister the service data_model = DataModel("foobar", attributes=[]) service_description = Description( {"foo": 1, "bar": "baz"}, data_model=data_model ) msg = OEFMessage( type=OEFMessage.Type.UNREGISTER_SERVICE, id=0, service_description=service_description, service_id="Test_service", ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # the same query returns empty # Search for the register agent msg = OEFMessage( type=OEFMessage.Type.SEARCH_AGENTS, id=0, query=Query([Constraint("foo", ConstraintType("==", 1))]), ) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # check the result response_envelope = self.multiplexer1.get(block=True, timeout=5.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.sender == DEFAULT_OEF result = OEFSerializer().decode(response_envelope.message) assert result.get("type") == OEFMessage.Type.SEARCH_RESULT assert len(result.get("agents")) == 0
def setup_class(cls): """ Set the test up. Steps: - Register a service - Check that the registration worked. """ cls.crypto1 = FetchAICrypto() cls.connection = OEFConnection( cls.crypto1.address, oef_addr="127.0.0.1", oef_port=10000, connection_id=PublicId("fetchai", "oef", "0.1.0"), ) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect() cls.request_id = 1 cls.foo_datamodel = DataModel( "foo", [Attribute("bar", int, True, "A bar attribute.")] ) cls.desc = Description({"bar": 1}, data_model=cls.foo_datamodel) msg = OEFMessage( type=OEFMessage.Type.REGISTER_SERVICE, id=cls.request_id, service_description=cls.desc, service_id="", ) msg_bytes = OEFSerializer().encode(msg) cls.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=cls.crypto1.address, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) ) time.sleep(1.0) cls.request_id += 1 search_request = OEFMessage( type=OEFMessage.Type.SEARCH_SERVICES, id=cls.request_id, query=Query( [Constraint("bar", ConstraintType("==", 1))], model=cls.foo_datamodel, ), ) cls.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=cls.crypto1.address, protocol_id=OEFMessage.protocol_id, message=OEFSerializer().encode(search_request), ) ) envelope = cls.multiplexer.get(block=True, timeout=5.0) search_result = OEFSerializer().decode(envelope.message) assert search_result.get("type") == OEFMessage.Type.SEARCH_RESULT assert search_result.get("id") == cls.request_id if search_result.get("agents") != [cls.crypto1.address]: logger.warning( 'search_result.get("agents") != [self.crypto1.address] FAILED in test_oef/test_communication.py' )