def test_get_from_multiplexer_when_empty(): """Test that getting an envelope from the multiplexer when the input queue is empty raises an exception.""" connection = DummyConnection(connection_id="dummy") multiplexer = Multiplexer([connection]) with pytest.raises(aea.mail.base.Empty): multiplexer.get()
def setup_class(cls): """Set up the test class.""" cls.host = "127.0.0.1" cls.port = get_unused_tcp_port() cls.server_addr = "server_addr" cls.client_addr_1 = "client_addr_1" cls.client_addr_2 = "client_addr_2" cls.server_conn = _make_tcp_server_connection( cls.server_addr, cls.host, cls.port, ) cls.client_conn_1 = _make_tcp_client_connection( cls.client_addr_1, cls.host, cls.port, ) cls.client_conn_2 = _make_tcp_client_connection( cls.client_addr_2, cls.host, cls.port, ) cls.server_multiplexer = Multiplexer([cls.server_conn]) cls.client_1_multiplexer = Multiplexer([cls.client_conn_1]) cls.client_2_multiplexer = Multiplexer([cls.client_conn_2]) assert not cls.server_conn.connection_status.is_connected assert not cls.client_conn_1.connection_status.is_connected assert not cls.client_conn_2.connection_status.is_connected cls.server_multiplexer.connect() cls.client_1_multiplexer.connect() cls.client_2_multiplexer.connect()
def test_multiplexer_connect_one_raises_error_many_connections(): """Test the case when the multiplexer raises an exception while attempting the connection of one connection.""" node = LocalNode() tmpdir = Path(tempfile.mkdtemp()) d = tmpdir / "test_stub" d.mkdir(parents=True) input_file_path = d / "input_file.csv" output_file_path = d / "input_file.csv" connection_1 = _make_local_connection("my_addr", node) connection_2 = _make_stub_connection(input_file_path, output_file_path) connection_3 = _make_dummy_connection() multiplexer = Multiplexer([connection_1, connection_2, connection_3]) assert not connection_1.connection_status.is_connected assert not connection_2.connection_status.is_connected assert not connection_3.connection_status.is_connected with unittest.mock.patch.object(connection_3, "connect", side_effect=Exception): with pytest.raises(AEAConnectionError, match="Failed to connect the multiplexer."): multiplexer.connect() assert not connection_1.connection_status.is_connected assert not connection_2.connection_status.is_connected assert not connection_3.connection_status.is_connected try: shutil.rmtree(tmpdir) except OSError as e: logger.warning("Couldn't delete {}".format(tmpdir)) logger.exception(e)
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.address_1 = "address_1" cls.multiplexer1 = Multiplexer( [ OEFLocalConnection( cls.address_1, cls.node, connection_id=PublicId("fetchai", "local", "0.1.0"), ) ] ) cls.address_2 = "address_2" cls.multiplexer2 = Multiplexer( [ OEFLocalConnection( cls.address_2, cls.node, connection_id=PublicId("fetchai", "local", "0.1.0"), ) ] ) cls.multiplexer1.connect() cls.multiplexer2.connect()
def test_multiplexer_connect_all_raises_error(): """Test the case when the multiplexer raises an exception while connecting.""" multiplexer = Multiplexer([DummyConnection()]) with unittest.mock.patch.object(multiplexer, "_connect_all", side_effect=Exception): with pytest.raises(AEAConnectionError, match="Failed to connect the multiplexer."): multiplexer.connect()
def test_multiplexer_connect_one_raises_error_many_connections(): """Test the case when the multiplexer raises an exception while attempting the connection of one connection.""" node = LocalNode() tmpdir = Path(tempfile.mktemp()) d = tmpdir / "test_stub" d.mkdir(parents=True) input_file_path = d / "input_file.csv" output_file_path = d / "input_file.csv" connection_1 = OEFLocalConnection("my_pbk", node) connection_2 = StubConnection(input_file_path, output_file_path) connection_3 = DummyConnection() multiplexer = Multiplexer([connection_1, connection_2, connection_3]) assert not connection_1.connection_status.is_connected assert not connection_2.connection_status.is_connected assert not connection_3.connection_status.is_connected with unittest.mock.patch.object(connection_3, "connect", side_effect=Exception): with pytest.raises(AEAConnectionError, match="Failed to connect the multiplexer."): multiplexer.connect() assert not connection_1.connection_status.is_connected assert not connection_2.connection_status.is_connected assert not connection_3.connection_status.is_connected shutil.rmtree(tmpdir)
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.public_key_1 = "public_key_1" cls.multiplexer1 = Multiplexer([OEFLocalConnection(cls.public_key_1, cls.node)]) cls.public_key_2 = "public_key_2" cls.multiplexer2 = Multiplexer([OEFLocalConnection(cls.public_key_2, cls.node)]) cls.multiplexer1.connect() cls.multiplexer2.connect()
def setup_class(cls): """Set up the test class.""" cls.crypto1 = DefaultCrypto() cls.crypto2 = DefaultCrypto() cls.connection1 = OEFConnection(cls.crypto1.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.connection2 = OEFConnection(cls.crypto2.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.multiplexer1 = Multiplexer([cls.connection1]) cls.multiplexer2 = Multiplexer([cls.connection2]) cls.multiplexer1.connect() cls.multiplexer2.connect()
def setup_class(cls): """Set up the test class.""" cls.crypto1 = FetchAICrypto() cls.crypto2 = FetchAICrypto() cls.connection1 = _make_oef_connection( cls.crypto1.address, oef_addr="127.0.0.1", oef_port=10000, ) cls.connection2 = _make_oef_connection( cls.crypto2.address, oef_addr="127.0.0.1", oef_port=10000, ) cls.multiplexer1 = Multiplexer([cls.connection1]) cls.multiplexer2 = Multiplexer([cls.connection2]) cls.multiplexer1.connect() cls.multiplexer2.connect()
def test_connect_twice_with_loop(): """Test that connecting twice the multiplexer behaves correctly.""" running_loop = asyncio.new_event_loop() thread_loop = Thread(target=running_loop.run_forever) thread_loop.start() try: multiplexer = Multiplexer( [_make_dummy_connection()], loop=running_loop, ) with unittest.mock.patch.object(aea.mail.base.logger, "debug") as mock_logger_debug: assert not multiplexer.connection_status.is_connected multiplexer.connect() assert multiplexer.connection_status.is_connected multiplexer.connect() assert multiplexer.connection_status.is_connected mock_logger_debug.assert_called_with( "Multiplexer already connected.") multiplexer.disconnect() running_loop.call_soon_threadsafe(running_loop.stop) finally: thread_loop.join()
def test_send_message_no_supported_protocol(): """Test the case when we send an envelope with a specific connection that does not support the protocol.""" with LocalNode() as node: address_1 = "address_1" public_id = PublicId.from_str("fetchai/my_private_protocol:0.1.0") connection_1 = _make_local_connection( address_1, node, restricted_to_protocols={public_id}, excluded_protocols={public_id}, ) multiplexer = Multiplexer([connection_1]) multiplexer.connect() with mock.patch.object(aea.mail.base.logger, "warning") as mock_logger_warning: protocol_id = UNKNOWN_PROTOCOL_PUBLIC_ID envelope = Envelope( to=address_1, sender=address_1, protocol_id=protocol_id, message=b"some bytes", ) multiplexer.put(envelope) time.sleep(0.5) mock_logger_warning.assert_called_with( "Connection {} cannot handle protocol {}. Cannot send the envelope." .format(connection_1.connection_id, protocol_id)) multiplexer.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()
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=[]) 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, ) msg_bytes = OefSearchSerializer().encode(register_service_request) envelope = Envelope( to=DEFAULT_OEF, sender=cls.address_1, protocol_id=OefSearchMessage.protocol_id, message=msg_bytes, ) cls.multiplexer.put(envelope)
def __init__( self, identity: Identity, connections: List[Connection], loop: Optional[AbstractEventLoop] = None, timeout: float = 1.0, is_debug: bool = False, is_programmatic: bool = True, # TODO to remove ) -> None: """ Instantiate the agent. :param identity: the identity of the agent. :param connections: the list of connections of the agent. :param loop: the event loop to run the connections. :param timeout: the time in (fractions of) seconds to time out an agent between act and react :param is_debug: if True, run the agent in debug mode (does not connect the multiplexer). :param is_programmatic: if True, run the agent in programmatic mode (skips loading of resources from directory). :return: None """ self._identity = identity self._connections = connections self._multiplexer = Multiplexer(self._connections, loop=loop) self._inbox = InBox(self._multiplexer) self._outbox = OutBox(self._multiplexer) self._liveness = Liveness() self._timeout = timeout self._tick = 0 self.is_debug = is_debug
def test_inbox_get_nowait_returns_none(): """Test that getting an envelope from an empty inbox returns None.""" # TODO get_nowait in this case should raise an exception, like it's done in queue.Queue multiplexer = Multiplexer( [DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)]) inbox = InBox(multiplexer) assert inbox.get_nowait() is None
def __init__(self, name: str, connections: List[Connection], wallet: Wallet, loop: Optional[AbstractEventLoop] = None, timeout: float = 1.0, debug: bool = False) -> None: """ Instantiate the agent. :param name: the name of the agent :param connections: the list of connections of the agent. :param wallet: the crypto wallet of the agent. :param loop: the event loop to run the connections. :param timeout: the time in (fractions of) seconds to time out an agent between act and react :param debug: if True, run the agent in debug mode. :return: None """ self._name = name self._connections = connections self._wallet = wallet self._multiplexer = Multiplexer(self._connections, loop=loop) self._inbox = InBox(self._multiplexer) self._outbox = OutBox(self._multiplexer) self._liveness = Liveness() self._timeout = timeout self.debug = debug
def setup_class(cls): """Initialise the decision maker.""" cls._patch_logger() cls.multiplexer = Multiplexer( [DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)]) cls.outbox = OutBox(cls.multiplexer) private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") eth_private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({ FETCHAI: private_key_pem_path, ETHEREUM: eth_private_key_pem_path }) cls.ledger_apis = LedgerApis({FETCHAI: DEFAULT_FETCHAI_CONFIG}, FETCHAI) cls.agent_name = "test" cls.ownership_state = OwnershipState() cls.preferences = Preferences() cls.decision_maker = DecisionMaker( agent_name=cls.agent_name, max_reactions=MAX_REACTIONS, outbox=cls.outbox, wallet=cls.wallet, ledger_apis=cls.ledger_apis, ) cls.multiplexer.connect() cls.tx_id = "transaction0" cls.tx_sender_addr = "agent_1" cls.tx_counterparty_addr = "pk" cls.info = {"some_info_key": "some_info_value"} cls.ledger_id = "fetchai" cls.decision_maker.start()
def setup_class(cls): """Initialise the decision maker.""" cls._patch_logger() cls.multiplexer = Multiplexer([_make_dummy_connection()]) private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") eth_private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({ FETCHAI: private_key_pem_path, ETHEREUM: eth_private_key_pem_path }) cls.ledger_apis = LedgerApis({FETCHAI: DEFAULT_FETCHAI_CONFIG}, FETCHAI) cls.agent_name = "test" cls.identity = Identity(cls.agent_name, addresses=cls.wallet.addresses, default_address_key=FETCHAI) cls.ownership_state = OwnershipState() cls.preferences = Preferences() cls.decision_maker = DecisionMaker( identity=cls.identity, wallet=cls.wallet, ledger_apis=cls.ledger_apis, ) cls.multiplexer.connect() cls.tx_id = "transaction0" cls.tx_sender_addr = "agent_1" cls.tx_counterparty_addr = "pk" cls.info = {"some_info_key": "some_info_value"} cls.ledger_id = "fetchai" cls.decision_maker.start()
def setup_class(cls): """Set up the test class.""" cls.connection1 = _make_oef_connection( FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000, ) cls.connection2 = _make_oef_connection( FETCHAI_ADDRESS_TWO, oef_addr="127.0.0.1", oef_port=10000, ) cls.multiplexer1 = Multiplexer([cls.connection1]) cls.multiplexer2 = Multiplexer([cls.connection2]) cls.multiplexer1.connect() cls.multiplexer2.connect()
def __init__( self, identity: Identity, connections: List[Connection], loop: Optional[AbstractEventLoop] = None, timeout: float = 1.0, is_debug: bool = False, loop_mode: Optional[str] = None, ) -> None: """ Instantiate the agent. :param identity: the identity of the agent. :param connections: the list of connections of the agent. :param loop: the event loop to run the connections. :param timeout: the time in (fractions of) seconds to time out an agent between act and react :param is_debug: if True, run the agent in debug mode (does not connect the multiplexer). :param loop_mode: loop_mode to choose agent run loop. :return: None """ self._identity = identity self._connections = connections self._multiplexer = Multiplexer(self._connections, loop=loop) self._inbox = InBox(self._multiplexer) self._outbox = OutBox(self._multiplexer) self._liveness = Liveness() self._timeout = timeout self._tick = 0 self._main_loop: Optional[BaseAgentLoop] = None self.is_debug = is_debug self._loop_mode = loop_mode or self.DEFAULT_RUN_LOOP
async def test_sending_loop_does_not_start_if_multiplexer_not_connected(): """Test that the sending loop is stopped does not start if the multiplexer is not connected.""" multiplexer = Multiplexer([DummyConnection(connection_id="dummy")]) with unittest.mock.patch.object(aea.mail.base.logger, "debug") as mock_logger_debug: await multiplexer._send_loop() mock_logger_debug.assert_called_with("Sending loop not started. The multiplexer is not connected.")
def setup_class(cls): """Set the test up""" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() # os.chdir(cls.t) cls.connection1 = _make_libp2p_connection(DEFAULT_PORT + 1) cls.multiplexer1 = Multiplexer([cls.connection1]) cls.multiplexer1.connect() time.sleep(2) genesis_peer = cls.connection1.node.multiaddrs cls.connection2 = _make_libp2p_connection( port=DEFAULT_PORT + 2, entry_peers=genesis_peer ) cls.multiplexer2 = Multiplexer([cls.connection2]) cls.multiplexer2.connect()
def setup_class(cls): """Set the test up.""" cls.crypto1 = FetchAICrypto() cls.connection = _make_oef_connection( cls.crypto1.address, oef_addr="127.0.0.1", oef_port=10000, ) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect()
def test_inbox_get_raises_exception_when_empty(): """Test that getting an envelope from an empty inbox raises an exception.""" multiplexer = Multiplexer([DummyConnection()]) inbox = InBox(multiplexer) with pytest.raises(aea.mail.base.Empty): with unittest.mock.patch.object(multiplexer, "get", return_value=None): inbox.get()
async def test_disconnect_twice_a_single_connection(): """Test that connecting twice a single connection behaves correctly.""" multiplexer = Multiplexer([DummyConnection()]) assert not multiplexer.connection_status.is_connected with unittest.mock.patch.object(aea.mail.base.logger, "debug") as mock_logger_debug: await multiplexer._disconnect_one("dummy") mock_logger_debug.assert_called_with("Connection dummy already disconnected.")
def setup_class(cls): """Set the test up.""" cls.crypto1 = DefaultCrypto() cls.connection = OEFConnection(cls.crypto1.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect()
def setup_class(cls): """Set the test up""" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() # os.chdir(cls.t) port_relay_1 = DEFAULT_PORT + 10 cls.connection_relay_1 = _make_libp2p_connection(port_relay_1) cls.multiplexer_relay_1 = Multiplexer([cls.connection_relay_1]) cls.multiplexer_relay_1.connect() port_relay_2 = DEFAULT_PORT + 100 cls.connection_relay_2 = _make_libp2p_connection(port_relay_2) cls.multiplexer_relay_2 = Multiplexer([cls.connection_relay_2]) cls.multiplexer_relay_2.connect() time.sleep(2) relay_peer_1 = cls.connection_relay_1.node.multiaddrs relay_peer_2 = cls.connection_relay_2.node.multiaddrs cls.connections = [] cls.multiplexers = [] port = port_relay_1 for _ in range(int(DEFAULT_NET_SIZE / 2)): port += 1 conn = _make_libp2p_connection( port=port, relay=False, entry_peers=relay_peer_1 ) mux = Multiplexer([conn]) cls.connections.append(conn) cls.multiplexers.append(mux) mux.connect() port = port_relay_2 for _ in range(int(DEFAULT_NET_SIZE / 2)): port += 1 conn = _make_libp2p_connection( port=port, relay=False, entry_peers=relay_peer_2 ) mux = Multiplexer([conn]) cls.connections.append(conn) cls.multiplexers.append(mux) mux.connect() time.sleep(2)
def setup_class(cls): """Set the test up.""" cls.connection = _make_oef_connection( FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000, ) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect()
def setup_class(cls): """Set up the test.""" cls.node = LocalNode() cls.node.start() cls.public_key_1 = "multiplexer1" cls.public_key_2 = "multiplexer2" cls.multiplexer1 = Multiplexer([OEFLocalConnection(cls.public_key_1, cls.node)]) cls.multiplexer2 = Multiplexer([OEFLocalConnection(cls.public_key_2, cls.node)]) 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(oef_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.public_key_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(oef_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.public_key_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(oef_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.public_key_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes) cls.multiplexer1.put(envelope)
def test_connect_twice(): """Test that connecting twice the multiplexer behaves correctly.""" multiplexer = Multiplexer([DummyConnection()]) assert not multiplexer.connection_status.is_connected multiplexer.connect() assert multiplexer.connection_status.is_connected multiplexer.connect() assert multiplexer.connection_status.is_connected multiplexer.disconnect()