def test_send_more_than_64_kilobytes(self): """Test that we can send more than 64KB messages.""" with NetworkOEFNode(): proxy = OEFNetworkProxy("test_send_more_than_64_kilobytes", "127.0.0.1", 3333) agent = AgentTest(proxy) expected_msg_id = 0 expected_dialogue_id = 0 expected_content = b"a" * 2**16 expected_origin = agent.public_key agent.connect() agent.send_message(expected_msg_id, expected_dialogue_id, agent.public_key, expected_content) asyncio.ensure_future(agent.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) agent.stop() actual_msg_id, actual_dialogue_id, actual_origin, actual_content = agent.received_msg[ 0] # assert that we received only one message assert 1 == len(agent.received_msg) # assert that the message contains what we've sent. assert expected_msg_id == actual_msg_id assert expected_dialogue_id == actual_dialogue_id assert expected_origin == actual_origin assert expected_content == actual_content
def test_connection_error_public_key_already_in_use(self): """Test that a OEFConnectionError is raised when we try to connect two agents with the same public key.""" with pytest.raises(OEFConnectionError, match="Public key already in use."): with NetworkOEFNode(): agent_1 = OEFAgent("the_same_public_key", "127.0.0.1", 3333) agent_2 = OEFAgent(agent_1.public_key, "127.0.0.1", 3333) agent_1.connect() agent_2.connect()
def test_disconnect(self): """Test that the disconnect method works correctly.""" with NetworkOEFNode(): agent_1 = OEFAgent("disconnect", "127.0.0.1", 3333) assert not agent_1._oef_proxy.is_connected() agent_1.connect() assert agent_1._oef_proxy.is_connected() agent_1.disconnect() assert not agent_1._oef_proxy.is_connected()
def test_that_two_connect_attempts_work_correctly(self): """Test that two call to the :func:'~agents.Agent.connect()' method work correctly. Use the local implementation of the OEF.""" with NetworkOEFNode(): agent_1 = OEFAgent("two_connect_attempt", "127.0.0.1", 3333) first_status = agent_1.connect() second_status = agent_1.connect() assert first_status assert second_status
def test_connection_error_on_send(self): """Test that a OEFConnectionError is raised when we try to send a message before the connection has been established.""" with NetworkOEFNode(): with pytest.raises(OEFConnectionError, match="Connection not established yet."): proxy = OEFNetworkProxy("test_oef_connection_error_when_send", "127.0.0.1", 3333) agent = Agent(proxy) agent.send_message(0, 0, proxy.public_key, b"message")
def setup_network_proxies(n: int, prefix: str) -> List[OEFNetworkProxy]: """ Set up a list of :class:`oef.proxy.OEFNetworkProxy`. :param n: the number of proxies to set up. :param prefix: the prefix to add to the proxies' public keys. """ public_key_prefix = prefix + "-" if prefix else "" proxies = [OEFNetworkProxy("{}agent-{}".format(public_key_prefix, i), "127.0.0.1", 3333) for i in range(n)] try: with NetworkOEFNode(): yield proxies except BaseException: raise
def test_group_dialogue_one_client_n_servers(self): with NetworkOEFNode(): client_proxy = OEFNetworkProxy("client", oef_addr="127.0.0.1", port=3333) client = ClientAgentGroupDialogueTest(client_proxy) client.connect() N = 10 server_proxies = [ OEFNetworkProxy("server_{:02d}".format(i), oef_addr="127.0.0.1", port=3333) for i in range(N) ] server_data_model = DataModel("server", [AttributeSchema("foo", bool, True)]) servers = [ ServerAgentTest(server_proxy, price=random.randint(10, 100)) for server_proxy in server_proxies ] for server in servers: server.connect() server.register_service( 0, Description({"foo": True}, server_data_model)) best_server = ServerAgentTest(OEFNetworkProxy("best_server", oef_addr="127.0.0.1", port=3333), price=5) best_server.connect() best_server.register_service( 0, Description({"foo": True}, server_data_model)) servers.append(best_server) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) query = Query([Constraint("foo", Eq(True))], server_data_model) client.search_services(0, query) asyncio.get_event_loop().run_until_complete( asyncio.gather(client.async_run(), *[server.async_run() for server in servers])) assert "best_server" == client.group.best_agent assert 5 == client.group.best_price
def test_more_than_once_async_run_call(self): """Test that when we call async_run more than once we get a warning message.""" with NetworkOEFNode(): with patch('logging.Logger.warning') as mock: proxy = OEFNetworkProxy("test_more_than_one_async_run_call", "127.0.0.1", 3333) agent = Agent(proxy) agent.connect() asyncio.ensure_future(agent.async_run()) asyncio.ensure_future(agent.async_run()) asyncio.get_event_loop().run_until_complete( asyncio.sleep(_ASYNCIO_DELAY)) mock.assert_called_with( "Agent {} already scheduled for running.".format( agent.public_key))