def test_on_cfp(self, local): """ Test that an agent can send a CFP to another agent, with different types of queries. """ with setup_test_agents(2, local, prefix="on_cfp") as agents: for a in agents: a.connect() agent_0, agent_1 = agents # type: AgentTest, AgentTest agent_0.send_cfp(0, 0, agent_1.public_key, 0, None) agent_0.send_cfp(0, 1, agent_1.public_key, 0, b"hello") agent_0.send_cfp(0, 2, agent_1.public_key, 0, Query([Constraint("foo", Eq(0))])) asyncio.ensure_future(agent_1.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) expected_message_01 = (0, 0, agent_0.public_key, 0, None) expected_message_02 = (0, 1, agent_0.public_key, 0, b"hello") expected_message_03 = (0, 2, agent_0.public_key, 0, Query([Constraint("foo", Eq(0))])) assert 3 == len(agent_1.received_msg) assert expected_message_01 == agent_1.received_msg[0] assert expected_message_02 == agent_1.received_msg[1] assert expected_message_03 == agent_1.received_msg[2]
def test_unregister_service(self, local): """ Test that the unregistration of services works correctly. """ with setup_test_agents(2, local, prefix="unregister_service") as agents: for a in agents: a.connect() agent_0, agent_1 = agents dummy_datamodel = DataModel("dummy_datamodel", [AttributeSchema("foo", int, False)]) dummy_service_description = Description({}, dummy_datamodel) agent_1.register_service(0, dummy_service_description) agent_1.unregister_service(0, dummy_service_description) agent_0.search_services( 0, Query([Constraint("foo", Eq(0))], dummy_datamodel)) asyncio.ensure_future(agent_0.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) assert 1 == len(agent_0.received_msg) assert (0, []) == agent_0.received_msg[0]
def test_on_search_result_agents(self, local): """ Test that an agent can do a search for agents. """ with setup_test_agents(3, local, prefix="search_agents") as agents: for a in agents: a.connect() agent_0, agent_1, agent_2 = agents foo_attr = AttributeSchema("foo", int, False, "A foo attribute.") bar_attr = AttributeSchema("bar", str, False, "A bar attribute.") dummy_datamodel = DataModel("dummy_datamodel", [foo_attr, bar_attr]) agent_1.register_agent( 0, Description({ "foo": 15, "bar": "BAR" }, dummy_datamodel)) agent_2.register_agent( 0, Description({ "foo": 5, "bar": "ABC" }, dummy_datamodel)) agent_0.search_agents( 0, Query([Constraint("foo", Eq(0))], dummy_datamodel)) agent_0.search_agents( 0, Query([ Constraint("foo", Gt(10)), Constraint("bar", Gt("B")), ], dummy_datamodel)) agent_0.search_agents( 0, Query([ Constraint("bar", Gt("A")), ], dummy_datamodel)) asyncio.ensure_future(agent_0.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) agent_1.unregister_agent(0) agent_2.unregister_agent(0) expected_message_01 = (0, []) expected_message_02 = (0, [agent_1.public_key]) expected_message_03 = (0, [agent_1.public_key, agent_2.public_key]) assert 3 == len(agent_0.received_msg) assert expected_message_01 == agent_0.received_msg[0] assert expected_message_02 == agent_0.received_msg[1] assert expected_message_03 == agent_0.received_msg[2]
def test_on_message(self, local): """ Test that 3 agents can send a simple message to themselves and each other and that the messages are properly processed and dispatched. """ with setup_test_agents(3, local, prefix="on_message") as agents: for a in agents: a.connect() agent_0, agent_1, agent_2 = agents msg = b"hello" agent_0.send_message(0, 0, agent_0.public_key, msg) agent_0.send_message(0, 0, agent_1.public_key, msg) agent_0.send_message(0, 0, agent_2.public_key, msg) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) agent_1.send_message(0, 1, agent_0.public_key, msg) agent_1.send_message(0, 1, agent_1.public_key, msg) agent_1.send_message(0, 1, agent_2.public_key, msg) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) agent_2.send_message(0, 2, agent_0.public_key, msg) agent_2.send_message(0, 2, agent_1.public_key, msg) agent_2.send_message(0, 2, agent_2.public_key, msg) asyncio.ensure_future( asyncio.gather(agent_0.async_run(), agent_1.async_run(), agent_2.async_run())) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) assert 3 == len(agent_0.received_msg) assert 3 == len(agent_1.received_msg) assert 3 == len(agent_2.received_msg) assert (0, 0, agent_0.public_key, msg) == agent_0.received_msg[0] assert (0, 1, agent_1.public_key, msg) == agent_0.received_msg[1] assert (0, 2, agent_2.public_key, msg) == agent_0.received_msg[2] assert (0, 0, agent_0.public_key, msg) == agent_1.received_msg[0] assert (0, 1, agent_1.public_key, msg) == agent_1.received_msg[1] assert (0, 2, agent_2.public_key, msg) == agent_1.received_msg[2] assert (0, 0, agent_0.public_key, msg) == agent_2.received_msg[0] assert (0, 1, agent_1.public_key, msg) == agent_2.received_msg[1] assert (0, 2, agent_2.public_key, msg) == agent_2.received_msg[2]
def test_on_decline(self, local): """ Test that an agent can send a Decline to another agent. """ with setup_test_agents(2, local, prefix="on_decline") as agents: for a in agents: a.connect() agent_0, agent_1 = agents agent_0.send_decline(0, 0, agent_1.public_key, 0) asyncio.ensure_future(agent_1.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) assert 1 == len(agent_1.received_msg) assert (0, 0, agent_0.public_key, 0) == agent_1.received_msg[0]
def test_oef_error_when_failing_in_unregistering_service(self): """Test that we receive an OEF Error message when we try to unregister a non existing service.""" with setup_test_agents( 1, False, prefix="oef_error_unregister_description") as agents: for a in agents: a.connect() agent_0 = agents[0] # type: AgentTest agent_0.on_oef_error = MagicMock() agent_0.unregister_service(0, Description({"foo": 1})) asyncio.ensure_future(agent_0.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) agent_0.on_oef_error.assert_called_with( 0, OEFErrorOperation.UNREGISTER_SERVICE)
def test_dialogue_error_when_destination_is_not_connected(self): """Test that we receive an ``DialogueError`` message when we try to send a message to an unconnected agent.""" with setup_test_agents( 1, False, prefix="dialogue_error_unconnected_destination") as agents: for a in agents: a.connect() agent_0 = agents[0] # type: AgentTest agent_0.on_dialogue_error = MagicMock() # send a message to an unconnected agent agent_0.send_message(0, 0, "unconnected_agent", b"dummy_message") asyncio.ensure_future(agent_0.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) agent_0.on_dialogue_error.assert_called_with(0, 0, "unconnected_agent")
def test_on_propose(self, local): """ Test that an agent can send a Propose to another agent, with different types of proposals. """ with setup_test_agents(2, local, prefix="on_propose") as agents: for a in agents: a.connect() agent_0, agent_1 = agents agent_0.send_propose(0, 0, agent_1.public_key, 0, b"hello") agent_0.send_propose(0, 0, agent_1.public_key, 0, []) agent_0.send_propose(0, 0, agent_1.public_key, 0, [Description({})]) agent_0.send_propose( 0, 0, agent_1.public_key, 0, [Description({}), Description({})]) asyncio.ensure_future(agent_1.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) expected_message_01 = (0, 0, agent_0.public_key, 0, b"hello") expected_message_02 = (0, 0, agent_0.public_key, 0, []) expected_message_03 = (0, 0, agent_0.public_key, 0, [Description({})]) expected_message_04 = (0, 0, agent_0.public_key, 0, [Description({}), Description({})]) assert 4 == len(agent_1.received_msg) assert expected_message_01 == agent_1.received_msg[0] assert expected_message_02 == agent_1.received_msg[1] assert expected_message_03 == agent_1.received_msg[2] assert expected_message_04 == agent_1.received_msg[3]