def test_from_json_and_to_json(self, connection_path): """Test the 'from_json' method and 'to_json' work correctly.""" f = open(connection_path) original_json = yaml.safe_load(f) expected_config = ConnectionConfig.from_json(original_json) assert isinstance(expected_config, ConnectionConfig) expected_json = expected_config.json actual_config = ConnectionConfig.from_json(expected_json) actual_json = actual_config.json assert expected_json == actual_json
def test_from_json_and_to_json(self, connection_path): """Test the 'from_json' method and 'to_json' work correctly.""" f = open(connection_path) original_json = yaml.safe_load(f) original_json["build_directory"] = "some" expected_config = ConnectionConfig.from_json(original_json) assert isinstance(expected_config, ConnectionConfig) assert isinstance(expected_config.package_dependencies, set) assert not expected_config.is_abstract_component expected_json = expected_config.json actual_config = ConnectionConfig.from_json(expected_json) actual_json = actual_config.json assert expected_json == actual_json
def make(cls): """Construct connection instance.""" configuration = ConnectionConfig(connection_id=cls.connection_id,) test_connection = cls( configuration=configuration, identity=Identity("name", "address") ) return test_connection
async def test_from_config(self): """Test the creation of the connection from a configuration.""" port = get_unused_tcp_port() TCPClientConnection.from_config( "address", ConnectionConfig(host="127.0.0.1", port=port), )
def setup_server(self): """Set up server connection.""" self.server_agent_address = "server_agent_address" self.server_agent_identity = Identity( "agent running server", address=self.server_agent_address ) self.host = get_host() self.port = get_unused_tcp_port() self.connection_id = HTTPServerConnection.connection_id self.protocol_id = PublicId.from_str("fetchai/http:0.4.0") self.configuration = ConnectionConfig( host=self.host, port=self.port, api_spec_path=None, # do not filter on API spec connection_id=HTTPServerConnection.connection_id, restricted_to_protocols=set([self.protocol_id]), ) self.server = HTTPServerConnection( configuration=self.configuration, identity=self.server_agent_identity, ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.server.connect()) # skill side dialogues self._server_dialogues = HttpDialogues(self.server_agent_address)
def setup(self): """Set up.""" self.crypto = make_crypto(DEFAULT_LEDGER) self.crypto2 = make_crypto(DEFAULT_LEDGER) identity = Identity("", address=self.crypto.address) self.oef_search_dialogues = OefSearchDialogues(self.crypto.address) self.data_dir = tempfile.mkdtemp() # create the connection and multiplexer objects configuration = ConnectionConfig( api_key="TwiCIriSl0mLahw17pyqoA", soef_addr="s-oef.fetch.ai", soef_port=443, restricted_to_protocols={ OefSearchMessage.protocol_specification_id }, connection_id=SOEFConnection.connection_id, ) self.connection = SOEFConnection( configuration=configuration, data_dir=self.data_dir, identity=identity, ) self.connection2 = SOEFConnection( configuration=configuration, data_dir=self.data_dir, identity=Identity("", address=self.crypto2.address), ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.connection.connect()) self.loop.run_until_complete(self.connection2.connect()) self.connection.channel.unique_page_address = "some_addr"
def setup(self): """Set up.""" self.crypto = make_crypto(DEFAULT_LEDGER) self.crypto2 = make_crypto(DEFAULT_LEDGER) self.data_dir = tempfile.mkdtemp() identity = Identity("", address=self.crypto.address) self.oef_search_dialogues = OefSearchDialogues(self.crypto.address) # create the connection and multiplexer objects self.token_storage_path = "test.storage" configuration = ConnectionConfig( api_key="TwiCIriSl0mLahw17pyqoA", soef_addr="s-oef.fetch.ai", soef_port=443, token_storage_path=self.token_storage_path, restricted_to_protocols={ OefSearchMessage.protocol_specification_id }, connection_id=SOEFConnection.connection_id, ) self.connection = SOEFConnection( configuration=configuration, data_dir=self.data_dir, identity=identity, )
def __init__(self, name: str, gym_env: gym.Env, proxy_env_queue: Queue) -> None: """ Instantiate the agent. :param name: the name of the agent :param gym_env: gym environment :param proxy_env_queue: the queue of the proxy environment :return: None """ identity = Identity(name, ADDRESS) configuration = ConnectionConfig( connection_id=GymConnection.connection_id) super().__init__( identity, [ GymConnection( gym_env, identity=identity, configuration=configuration, data_dir=os.getcwd(), ) ], period=0.01, ) self.proxy_env_queue = proxy_env_queue
def test_excluded_protocols_positive(self): """Test excluded_protocols property positive result.""" obj = self.TestConnection( ConnectionConfig("some_connection", "fetchai", "0.1.0") ) obj._excluded_protocols = "excluded_protocols" obj.excluded_protocols
def make_multiplexer_and_dialogues( ) -> Tuple[Multiplexer, OefSearchDialogues, Crypto, SOEFConnection]: """Return multplexer, dialogues and crypto instances.""" crypto = make_crypto(DEFAULT_LEDGER) identity = Identity("", address=crypto.address) oef_search_dialogues = OefSearchDialogues(crypto.address) # create the connection and multiplexer objects configuration = ConnectionConfig( api_key="TwiCIriSl0mLahw17pyqoA", soef_addr="s-oef.fetch.ai", soef_port=443, restricted_to_protocols={ OefSearchMessage.protocol_specification_id, OefSearchMessage.protocol_id, }, connection_id=SOEFConnection.connection_id, ) soef_connection = SOEFConnection( configuration=configuration, data_dir=MagicMock(), identity=identity, ) multiplexer = Multiplexer([soef_connection]) return multiplexer, oef_search_dialogues, crypto, soef_connection
def setup(self): """Set up.""" self.crypto = make_crypto(DEFAULT_LEDGER) self.crypto2 = make_crypto(DEFAULT_LEDGER) identity = Identity("", address=self.crypto.address) self.oef_search_dialogues = OefSearchDialogues(self.crypto.address) # create the connection and multiplexer objects configuration = ConnectionConfig( api_key="TwiCIriSl0mLahw17pyqoA", soef_addr="soef.fetch.ai", soef_port=9002, restricted_to_protocols={PublicId.from_str("fetchai/oef_search:0.3.0")}, connection_id=SOEFConnection.connection_id, ) self.connection = SOEFConnection( configuration=configuration, identity=identity, ) self.connection.channel.unique_page_address = "some addr" self.connection2 = SOEFConnection( configuration=configuration, identity=Identity("", address=self.crypto2.address), ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.connection.connect()) self.loop.run_until_complete(self.connection2.connect())
async def test_connecting_to_aca(self): """Test connecting to aca.""" configuration = ConnectionConfig( host=self.aca_admin_address, port=self.aca_admin_port, connection_id=HTTPClientConnection.connection_id, ) http_client_connection = HTTPClientConnection( configuration=configuration, identity=self.aea_identity) http_client_connection.loop = asyncio.get_event_loop() # Request messages request_http_message = HttpMessage( dialogue_reference=("1", ""), target=0, message_id=1, performative=HttpMessage.Performative.REQUEST, method="GET", url="http://{}:{}/status".format(self.aca_admin_address, self.aca_admin_port), headers="", version="", body=b"", ) request_http_message.to = "ACA" request_envelope = Envelope( to="ACA", sender="AEA", protocol_id=HttpMessage.protocol_id, message=request_http_message, ) try: # connect to ACA await http_client_connection.connect() assert http_client_connection.is_connected is True # send request to ACA await http_client_connection.send(envelope=request_envelope) # receive response from ACA response_envelop = await http_client_connection.receive() # check the response assert response_envelop.to == self.aea_address assert response_envelop.sender == "HTTP Server" assert response_envelop.protocol_id == HttpMessage.protocol_id decoded_response_message = response_envelop.message assert (decoded_response_message.performative == HttpMessage.Performative.RESPONSE) assert decoded_response_message.version == "" assert decoded_response_message.status_code == 200 assert decoded_response_message.status_text == "OK" assert decoded_response_message.headers is not None assert decoded_response_message.version is not None finally: # disconnect from ACA await http_client_connection.disconnect() assert http_client_connection.is_connected is False
def setup_client(self): """Set up client connection.""" self.client_agent_address = "client_agent_address" self.client_agent_identity = Identity( "agent running client", address=self.client_agent_address) configuration = ConnectionConfig( host="localost", port="8888", # TODO: remove host/port for client? connection_id=HTTPClientConnection.connection_id, ) self.client = HTTPClientConnection(configuration=configuration, identity=self.client_agent_identity) self.loop.run_until_complete(self.client.connect()) # skill side dialogues def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address) -> BaseDialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return HttpDialogue.Role.CLIENT self._client_dialogues = HttpDialogues( self.client_agent_address, role_from_first_message=role_from_first_message)
def _run_interaction_channel(): loader = ConfigLoader.from_configuration_type(PackageType.AGENT) agent_configuration = loader.load(Path(DEFAULT_AEA_CONFIG_FILE).open()) agent_name = agent_configuration.name identity_stub = Identity(agent_name + "_interact", "interact") _load_packages(identity_stub) # load agent configuration file from packages.fetchai.connections.stub.connection import ( # noqa: F811 # pylint: disable=import-outside-toplevel DEFAULT_INPUT_FILE_NAME, DEFAULT_OUTPUT_FILE_NAME, StubConnection, ) from packages.fetchai.protocols.default.dialogues import ( # noqa: F811 # pylint: disable=import-outside-toplevel DefaultDialogue, DefaultDialogues, ) from packages.fetchai.protocols.default.message import ( # noqa: F811 # pylint: disable=import-outside-toplevel DefaultMessage, ) # load stub connection configuration = ConnectionConfig( input_file=DEFAULT_OUTPUT_FILE_NAME, output_file=DEFAULT_INPUT_FILE_NAME, connection_id=StubConnection.connection_id, ) stub_connection = StubConnection( configuration=configuration, identity=identity_stub ) multiplexer = Multiplexer([stub_connection]) inbox = InBox(multiplexer) outbox = OutBox(multiplexer) def role_from_first_message( # pylint: disable=unused-argument message: Message, receiver_address: Address ) -> BaseDialogue.Role: """Infer the role of the agent from an incoming/outgoing first message :param message: an incoming/outgoing first message :param receiver_address: the address of the receiving agent :return: The role of the agent """ return DefaultDialogue.Role.AGENT dialogues = DefaultDialogues(identity_stub.name, role_from_first_message) try: multiplexer.connect() while True: # pragma: no cover _process_envelopes(agent_name, inbox, outbox, dialogues, DefaultMessage) except KeyboardInterrupt: click.echo("Interaction interrupted!") except BaseException as e: # pylint: disable=broad-except # pragma: no cover click.echo(e) finally: multiplexer.disconnect()
def setup(self): """Initialise the test case.""" self.identity = Identity("name", address="my_key") self.agent_address = self.identity.address self.host = get_host() self.port = get_unused_tcp_port() self.api_spec_path = os.path.join( ROOT_DIR, "tests", "data", "petstore_sim.yaml" ) self.connection_id = HTTPServerConnection.connection_id self.protocol_id = PublicId.from_str("fetchai/http:0.4.0") self.configuration = ConnectionConfig( host=self.host, port=self.port, api_spec_path=self.api_spec_path, connection_id=HTTPServerConnection.connection_id, restricted_to_protocols=set([self.protocol_id]), ) self.http_connection = HTTPServerConnection( configuration=self.configuration, identity=self.identity, ) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(self.http_connection.connect()) self.connection_address = str(HTTPServerConnection.connection_id) self._dialogues = HttpDialogues(self.connection_address) self.original_timeout = self.http_connection.channel.RESPONSE_TIMEOUT
def test_loop_fails_on_non_running_loop(self): """Test loop property positive result.""" obj = self.TestConnection( ConnectionConfig("some_connection", "fetchai", "0.1.0") ) with pytest.raises(AEAEnforceError): obj.loop
def run(): # Ensure the input and output files do not exist initially if os.path.isfile(INPUT_FILE): os.remove(INPUT_FILE) if os.path.isfile(OUTPUT_FILE): os.remove(OUTPUT_FILE) # create the connection and multiplexer objects configuration = ConnectionConfig( input_file=INPUT_FILE, output_file=OUTPUT_FILE, connection_id=StubConnection.connection_id, ) stub_connection = StubConnection(configuration=configuration, identity=Identity("some_agent", "some_address")) multiplexer = Multiplexer([stub_connection]) try: # Set the multiplexer running in a different thread t = Thread(target=multiplexer.connect) t.start() # Wait for everything to start up time.sleep(3) # Create a message inside an envelope and get the stub connection to pass it into the multiplexer message_text = ( "multiplexer,some_agent,fetchai/default:0.4.0,\x08\x01*\x07\n\x05hello," ) with open(INPUT_FILE, "w") as f: write_with_lock(f, message_text) # Wait for the envelope to get processed time.sleep(2) # get the envelope envelope = multiplexer.get() # type: Optional[Envelope] assert envelope is not None # Inspect its contents print( "Envelope received by Multiplexer: sender={}, to={}, protocol_id={}, message={}" .format(envelope.sender, envelope.to, envelope.protocol_id, envelope.message)) # Create a mirrored response envelope response_envelope = copy(envelope) response_envelope.to = envelope.sender response_envelope.sender = envelope.to # Send the envelope back multiplexer.put(response_envelope) # Read the output envelope generated by the multiplexer with open(OUTPUT_FILE, "r") as f: print("Envelope received from Multiplexer: " + f.readline()) finally: # Shut down the multiplexer multiplexer.disconnect() t.join()
def _run_interaction_channel(): # load agent configuration file loader = ConfigLoader.from_configuration_type(PackageType.AGENT) agent_configuration = loader.load(Path(DEFAULT_AEA_CONFIG_FILE).open()) agent_name = agent_configuration.name # load stub connection configuration = ConnectionConfig( input_file=DEFAULT_OUTPUT_FILE_NAME, output_file=DEFAULT_INPUT_FILE_NAME, connection_id=StubConnection.connection_id, ) identity_stub = Identity(agent_name + "_interact", "interact") stub_connection = StubConnection(configuration=configuration, identity=identity_stub) multiplexer = Multiplexer([stub_connection]) inbox = InBox(multiplexer) outbox = OutBox(multiplexer, default_address=identity_stub.address) dialogues = DefaultDialogues(identity_stub.name) try: multiplexer.connect() while True: # pragma: no cover _process_envelopes(agent_name, identity_stub, inbox, outbox, dialogues) except KeyboardInterrupt: click.echo("Interaction interrupted!") except BaseException as e: # pylint: disable=broad-except # pragma: no cover click.echo(e) finally: multiplexer.disconnect()
def _make_stub_connection(input_file_path: str, output_file_path: str): configuration = ConnectionConfig( input_file=input_file_path, output_file=output_file_path, connection_id=StubConnection.connection_id, ) connection = StubConnection(configuration=configuration) return connection
def _make_libp2p_connection( port: int = 10234, host: str = "127.0.0.1", relay: bool = True, delegate: bool = False, entry_peers: Optional[Sequence[MultiAddr]] = None, delegate_port: int = 11234, delegate_host: str = "127.0.0.1", node_key_file: Optional[str] = None, agent_address: Optional[Address] = None, ) -> P2PLibp2pConnection: log_file = "libp2p_node_{}.log".format(port) if os.path.exists(log_file): os.remove(log_file) address = agent_address if address is None: address = make_crypto(COSMOS).address identity = Identity("", address=address) if relay and delegate: configuration = ConnectionConfig( node_key_file=node_key_file, local_uri="{}:{}".format(host, port), public_uri="{}:{}".format(host, port), entry_peers=entry_peers, log_file=log_file, delegate_uri="{}:{}".format(delegate_host, delegate_port), connection_id=P2PLibp2pConnection.connection_id, ) elif relay and not delegate: configuration = ConnectionConfig( node_key_file=node_key_file, local_uri="{}:{}".format(host, port), public_uri="{}:{}".format(host, port), entry_peers=entry_peers, log_file=log_file, connection_id=P2PLibp2pConnection.connection_id, ) else: configuration = ConnectionConfig( node_key_file=node_key_file, local_uri="{}:{}".format(host, port), entry_peers=entry_peers, log_file=log_file, connection_id=P2PLibp2pConnection.connection_id, ) return P2PLibp2pConnection(configuration=configuration, identity=identity)
def test_set_base_state(): """Check error raised on bad state set.""" con = TConnection( configuration=ConnectionConfig("some_connection", "fetchai", "0.1.0"), data_dir=MagicMock(), ) with pytest.raises(ValueError, match="Incorrect state.*"): con.state = "some bad state"
def _make_oef_connection(address: Address, oef_addr: str, oef_port: int): configuration = ConnectionConfig( addr=oef_addr, port=oef_port, connection_id=OEFConnection.connection_id ) oef_connection = OEFConnection( configuration=configuration, identity=Identity("name", address), ) oef_connection._default_logger_name = "aea.packages.fetchai.connections.oef" return oef_connection
def test_empty_nodes(self): """Test empty nodes.""" configuration = ConnectionConfig( client_key_file=self.key_file, nodes=[{"uri": "{}:{}".format(self.node_host, self.node_port)}], connection_id=P2PLibp2pClientConnection.connection_id, ) P2PLibp2pClientConnection(configuration=configuration, identity=self.identity) configuration = ConnectionConfig( client_key_file=self.key_file, nodes=None, connection_id=P2PLibp2pClientConnection.connection_id, ) with pytest.raises(Exception): P2PLibp2pClientConnection( configuration=configuration, identity=self.identity )
def _make_libp2p_client_connection( node_port: int = 11234, node_host: str = "127.0.0.1" ) -> P2PLibp2pClientConnection: identity = Identity("", address=FetchAICrypto().address) configuration = ConnectionConfig( client_key_file=None, nodes=[{"uri": "{}:{}".format(node_host, node_port)}], connection_id=P2PLibp2pClientConnection.connection_id, ) return P2PLibp2pClientConnection(configuration=configuration, identity=identity)
def _make_tcp_client_connection(address: str, host: str, port: int): configuration = ConnectionConfig( address=host, port=port, connection_id=TCPClientConnection.connection_id ) tcp_connection = TCPClientConnection( configuration=configuration, identity=Identity("name", address), ) tcp_connection._default_logger_name = ( "aea.packages.fetchai.connections.tcp.tcp_client" ) return tcp_connection
def setup(self): """Initialise the class.""" self.env = gym.GoalEnv() configuration = ConnectionConfig( connection_id=GymConnection.connection_id) self.my_address = "my_key" identity = Identity("name", address=self.my_address) self.gym_con = GymConnection(gym_env=self.env, identity=identity, configuration=configuration) self.loop = asyncio.get_event_loop()
def test_from_json_and_to_json(self): """Test the 'from_json' method and 'to_json' work correctly.""" f = open( os.path.join(CUR_PATH, "data", "dummy_connection", "connection.yaml")) expected_json = yaml.safe_load(f) config = ConnectionConfig.from_json(expected_json) assert isinstance(config, ConnectionConfig) actual_json = config.json assert expected_json == actual_json
def setup_class(cls): """Set up the test.""" cls.connection_config = ConnectionConfig( name="connection_name", author="author", version="0.1.0", connections={cls.old_connection_id}, protocols={cls.old_protocol_id}, restricted_to_protocols={cls.old_protocol_id}, excluded_protocols={cls.old_protocol_id}, ) replace_component_ids(cls.connection_config, cls.replacements)
def test_local_uri_provided_when_public_uri_provided(self): configuration = ConnectionConfig( node_key_file=self.key_file, public_uri="{}:{}".format(self.host, self.port), entry_peers=None, log_file=None, connection_id=P2PLibp2pConnection.connection_id, ) with pytest.raises(ValueError): P2PLibp2pConnection(configuration=configuration, identity=self.identity)
def _make_p2p_client_connection(address: Address, provider_addr: str, provider_port: int): configuration = ConnectionConfig( addr=provider_addr, port=provider_port, connection_id=PeerToPeerClientConnection.connection_id, ) p2p_client_connection = PeerToPeerClientConnection( configuration=configuration, identity=Identity("", address), ) return p2p_client_connection