async def test_wrong_dialogue(self): """Test that at the beginning, the search request returns an empty search result.""" query = Query(constraints=[Constraint("foo", ConstraintType("==", 1))], model=None) # build and send the request search_services_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, message_id=2, target=1, dialogue_reference=self.dialogues. new_self_initiated_dialogue_reference(), query=query, ) search_services_request.to = str(OEFLocalConnection.connection_id) # the incorrect message cannot be sent into a dialogue, so this is omitted. search_services_request.sender = self.address_1 envelope = Envelope( to=search_services_request.to, sender=search_services_request.sender, message=search_services_request, ) with unittest.mock.patch.object( self.node, "_handle_oef_message", side_effect=self.node._handle_oef_message) as mock_handle: with unittest.mock.patch.object(self.node.logger, "warning") as mock_logger: self.multiplexer.put(envelope) wait_for_condition(lambda: mock_handle.called, timeout=1.0) mock_logger.assert_any_call( AnyStringWith("Could not create dialogue for message="))
def test_on_oef_error(self): """Test the oef error.""" oef_connection = self.multiplexer1.connections[0] oef_channel = oef_connection.channel oef_channel.oef_msg_id += 1 dialogue_reference = ("1", "") query = Query( constraints=[Constraint("foo", ConstraintType("==", "bar"))], model=None, ) dialogues = oef_channel.oef_search_dialogues oef_search_msg = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=dialogue_reference, query=query, ) oef_search_msg.to = str(oef_connection.connection_id) oef_search_msg.sender = "agent" dialogue = dialogues.update(oef_search_msg) assert dialogue is not None oef_channel.oef_msg_id_to_dialogue[oef_channel.oef_msg_id] = dialogue oef_channel.on_oef_error( answer_id=oef_channel.oef_msg_id, operation=OEFErrorOperation.SEARCH_SERVICES, ) envelope = self.multiplexer1.get(block=True, timeout=5.0) dec_msg = envelope.message assert dec_msg.dialogue_reference[0] == dialogue_reference[0] assert (dec_msg.performative is OefSearchMessage.Performative.OEF_ERROR ), "It should be an error message"
async def test_send_oef_message(self, pytestconfig, caplog): """Test the send oef message.""" with warnings.catch_warnings(): warnings.simplefilter("ignore") oef_connection = _make_oef_connection( address=FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000, ) await oef_connection.connect() oef_search_dialogues = OefSearchDialogues(FETCHAI_ADDRESS_ONE) msg = OefSearchMessage( performative=OefSearchMessage.Performative.OEF_ERROR, dialogue_reference=oef_search_dialogues. new_self_initiated_dialogue_reference(), oef_error_operation=OefSearchMessage.OefErrorOperation. SEARCH_SERVICES, ) msg.to = str(oef_connection.connection_id) msg.sender = FETCHAI_ADDRESS_ONE envelope = Envelope( to=msg.to, sender=msg.sender, message=msg, ) with caplog.at_level(logging.DEBUG, "aea.packages.fetchai.connections.oef"): await oef_connection.send(envelope) assert "Could not create dialogue for message=" in caplog.text data_model = DataModel("foobar", attributes=[Attribute("foo", str, True)]) query = Query( constraints=[Constraint("foo", ConstraintType("==", "bar"))], model=data_model, ) msg, sending_dialogue = oef_search_dialogues.create( counterparty=str(oef_connection.connection_id), performative=OefSearchMessage.Performative.SEARCH_SERVICES, query=query, ) envelope = Envelope( to=msg.to, sender=msg.sender, message=msg, ) await oef_connection.send(envelope) envelope = await oef_connection.receive() search_result = envelope.message response_dialogue = oef_search_dialogues.update(search_result) assert (search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT) assert sending_dialogue == response_dialogue await asyncio.sleep(2.0) await oef_connection.disconnect()
async def test_bad_performative(self): """Test fail on bad perfromative.""" agent_location = Location(52.2057092, 2.1183431) service_instance = {"location": agent_location} service_description = Description( service_instance, data_model=models.AGENT_LOCATION_MODEL) message = OefSearchMessage( performative="oef_error", dialogue_reference=self.oef_search_dialogues. new_self_initiated_dialogue_reference(), service_description=service_description, ) message.to = str(SOEFConnection.connection_id.to_any()) message.sender = self.crypto.address envelope = Envelope( to=message.to, sender=message.sender, message=message, ) with pytest.raises(ValueError): await self.connection.send(envelope)
async def test_bad_performative(self, caplog): """Test fail on bad perfromative.""" agent_location = Location(52.2057092, 2.1183431) service_instance = {"location": agent_location} service_description = Description( service_instance, data_model=models.AGENT_LOCATION_MODEL) message = OefSearchMessage( performative="oef_error", dialogue_reference=self.oef_search_dialogues. new_self_initiated_dialogue_reference(), service_description=service_description, ) message.counterparty = SOEFConnection.connection_id.latest sending_dialogue = self.oef_search_dialogues.update(message) assert sending_dialogue is None message.sender = self.crypto.address envelope = Envelope( to=message.counterparty, sender=message.sender, protocol_id=message.protocol_id, message=message, ) with pytest.raises(ValueError): await self.connection.send(envelope)