def test_multiple_builds_with_component_instance(): """Test multiple calls to the 'build()' method when adding component instances.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") a_protocol = Protocol( ProtocolConfig("a_protocol", "author", "0.1.0"), DefaultMessage ) builder.add_component_instance(a_protocol) # the first call works aea_1 = builder.build() assert isinstance(aea_1, AEA) # the second call fails with pytest.raises(ValueError, match="Cannot build.*"): builder.build() # after reset, it works builder.reset() builder.set_name("aea_1") builder.add_private_key("fetchai") builder.add_component_instance(a_protocol) aea_2 = builder.build() assert isinstance(aea_2, AEA)
def test_storage_access_from_behaviour(): """Test storage access from behaviour component.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key(DEFAULT_LEDGER) skill_context = SkillContext() behaviour = TBehaviour(name="behaviour", skill_context=skill_context) test_skill = Skill( SkillConfig(name="test_skill", author="fetchai"), skill_context=skill_context, handlers={}, behaviours={"behaviour": behaviour}, ) builder.add_component_instance(test_skill) builder.set_storage_uri("sqlite://:memory:") aea = builder.build() skill_context.set_agent_context(aea.context) aea.runtime._threaded = True aea.runtime.start() try: wait_for_condition(lambda: aea.is_running, timeout=10) wait_for_condition(lambda: behaviour.counter > 0, timeout=10) col = skill_context.storage.get_sync_collection(behaviour.COL_NAME) assert col.get(behaviour.OBJ_ID) == behaviour.OBJ_BODY finally: aea.runtime.stop() aea.runtime.wait_completed(sync=True, timeout=10)
def setup(self) -> None: """Set test cae instance.""" agent_name = "MyAgent" builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, COSMOS_PRIVATE_KEY_PATH) self.handler_called = 0 def handler_func(*args, **kwargs): self.handler_called += 1 skill_context = SkillContext() handler_cls = make_handler_cls_from_funcion(handler_func) behaviour_cls = make_behaviour_cls_from_funcion(handler_func) self.handler = handler_cls(name="handler1", skill_context=skill_context) self.behaviour = behaviour_cls(name="behaviour1", skill_context=skill_context) test_skill = Skill( SkillConfig(name="test_skill", author="fetchai"), skill_context=skill_context, handlers={"handler": self.handler}, behaviours={"behaviour": self.behaviour}, ) skill_context._skill = test_skill # weird hack builder.add_component_instance(test_skill) self.aea = builder.build() self.aea_tool = AeaTool(self.aea)
def make_aea(self, name: str = "my_aea", components: List[Component] = None) -> AEA: """ Create AEA from name and already loaded components. :param name: name of the agent :param components: list of components to add to agent :return: AEA """ components = components or [] builder = AEABuilder() builder.set_name(self.name) # See https://github.com/fetchai/agents-aea/issues/1237 builder._private_key_paths[ FetchAICrypto.identifier] = None # type: ignore for component in components: builder.add_component_instance(component) aea = builder.build() return aea
def test_load_abstract_component(): """Test abstract component loading.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") skill = Skill.from_dir(dummy_skill_path, Mock(agent_name="name")) skill.configuration.is_abstract = True builder.add_component_instance(skill) builder._load_and_add_components( ComponentType.SKILL, Resources(), "aea_1", agent_context=Mock(agent_name="name") )
def test_remove_contract(): """Test add/remove contract.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") contract = Contract.from_dir(contract_path) num_deps = len(builder._package_dependency_manager.all_dependencies) builder.add_component_instance(contract) assert len(builder._package_dependency_manager.all_dependencies) == num_deps + 1 builder.remove_contract(contract.public_id) assert len(builder._package_dependency_manager.all_dependencies) == num_deps
def test_remove_skill(): """Test add/remove skill.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") skill = Skill.from_dir(dummy_skill_path, Mock(agent_name="name")) num_deps = len(builder._package_dependency_manager.all_dependencies) builder.add_component_instance(skill) assert len(builder._package_dependency_manager.all_dependencies) == num_deps + 1 builder.remove_skill(skill.public_id) assert len(builder._package_dependency_manager.all_dependencies) == num_deps
def test_remove_connection(): """Test add/remove connection.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") num_deps = len(builder._package_dependency_manager.all_dependencies) conn = _make_dummy_connection() builder.add_component_instance(conn) assert len(builder._package_dependency_manager.all_dependencies) == num_deps + 1 builder.remove_connection(conn.public_id) assert len(builder._package_dependency_manager.all_dependencies) == num_deps
def test_remove_protocol(): """Test add/remove protocol.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") a_protocol = Protocol( ProtocolConfig("a_protocol", "author", "0.1.0"), DefaultMessage ) num_deps = len(builder._package_dependency_manager.all_dependencies) builder.add_component_instance(a_protocol) assert len(builder._package_dependency_manager.all_dependencies) == num_deps + 1 builder.remove_protocol(a_protocol.public_id) assert len(builder._package_dependency_manager.all_dependencies) == num_deps
def test_process_connection_ids_bad_default_connection(): """Test fail on incorrect default connections.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") connection = _make_dummy_connection() builder.add_component_instance(connection) with pytest.raises( ValueError, match= r"Default connection not a dependency. Please add it and retry.", ): builder.set_default_connection( ConnectionConfig("conn", "author", "0.1.0").public_id) builder._process_connection_ids([connection.public_id])
def test_storage_access_from_handler(): """Test storage access from handler component.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key(DEFAULT_LEDGER) skill_context = SkillContext() handler = THandler(name="behaviour", skill_context=skill_context) test_skill = Skill( SkillConfig(name="test_skill", author="fetchai"), skill_context=skill_context, handlers={"handler": handler}, behaviours={}, ) builder.add_component_instance(test_skill) builder.set_storage_uri("sqlite://:memory:") aea = builder.build() skill_context.set_agent_context(aea.context) aea.runtime._threaded = True aea.runtime.start() msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.to = aea.identity.address msg.sender = aea.identity.address envelope = Envelope(to=msg.to, sender=msg.sender, message=msg,) try: wait_for_condition(lambda: aea.is_running, timeout=10) aea.runtime.multiplexer.in_queue.put(envelope) wait_for_condition(lambda: handler.counter > 0, timeout=10) col = skill_context.storage.get_sync_collection(handler.COL_NAME) assert col.get(handler.OBJ_ID) == handler.OBJ_BODY finally: aea.runtime.stop() aea.runtime.wait_completed(sync=True, timeout=10)
def prepare(self, function: Callable) -> None: """Prepare aea_tool for testing. :param function: function be called on react handle or/and Behaviour.act :return: None """ agent_name = "MyAgent" builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(FetchAICrypto.identifier, FETCHAI_PRIVATE_KEY_PATH) self.function_finished = False def handler_func(*args, **kwargs): function() self.function_finished = True skill_context = SkillContext() handler_cls = make_handler_cls_from_funcion(handler_func) behaviour_cls = make_behaviour_cls_from_funcion(handler_func) test_skill = Skill( SkillConfig(name="test_skill", author="fetchai"), skill_context=skill_context, handlers={ "handler1": handler_cls(name="handler1", skill_context=skill_context) }, behaviours={ "behaviour1": behaviour_cls( name="behaviour1", skill_context=skill_context ) }, ) skill_context._skill = test_skill # weird hack builder.add_component_instance(test_skill) aea = builder.build() self.aea_tool = AeaTool(aea) self.aea_tool.put_inbox(AeaTool.dummy_envelope())
def _builder(self, agent_name="agent1", act_func=None) -> AEABuilder: """Build an aea instance.""" builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(FetchAICrypto.identifier, FETCHAI_PRIVATE_KEY_PATH) skill_context = SkillContext() act_func = act_func or (lambda self: self) behaviour_cls = make_behaviour_cls_from_funcion(act_func) behaviour = behaviour_cls(name="behaviour", skill_context=skill_context) test_skill = Skill( SkillConfig(name="test_skill", author="fetchai"), skill_context=skill_context, behaviours={"behaviour": behaviour}, ) skill_context._skill = test_skill # weird hack builder.add_component_instance(test_skill) builder.set_runtime_mode("async") return builder
def make_aea(self, name: str = "my_aea", components: List[Component] = None) -> AEA: """ Create AEA from name and already loaded components. :param name: name of the agent :param components: list of components to add to agent :return: AEA """ components = components or [] builder = AEABuilder() builder.set_name(self.name) builder.add_private_key(FetchAICrypto.identifier, private_key_path=None) for component in components: builder.add_component_instance(component) aea = builder.build() return aea
def test_component_add_bad_dep(): """Test component load failed cause dependency.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") connection = _make_dummy_connection() connection.configuration.pypi_dependencies = { "something": Dependency("something", "==0.1.0") } builder.add_component_instance(connection) a_protocol = Protocol( ProtocolConfig("a_protocol", "author", "0.1.0"), DefaultMessage ) a_protocol.configuration.pypi_dependencies = { "something": Dependency("something", "==0.2.0") } with pytest.raises( AEAException, match=r"Conflict on package something: specifier set .*" ): builder.add_component_instance(a_protocol)
def run(): # Create a private key create_private_key(CosmosCrypto.identifier) # 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) # Instantiate the builder and build the AEA # By default, the default protocol, error skill and stub connection are added builder = AEABuilder() builder.set_name("my_aea") builder.add_private_key(CosmosCrypto.identifier, COSMOS_PRIVATE_KEY_FILE) # Add the echo skill (assuming it is present in the local directory 'packages') builder.add_skill("./packages/fetchai/skills/echo") # create skill and handler manually from aea.protocols.base import Message from aea.protocols.default.message import DefaultMessage from aea.skills.base import Handler class DummyHandler(Handler): """Dummy handler to handle messages.""" SUPPORTED_PROTOCOL = DefaultMessage.protocol_id def setup(self) -> None: """Noop setup.""" def teardown(self) -> None: """Noop teardown.""" def handle(self, message: Message) -> None: """Handle incoming message.""" self.context.logger.info("You got a message: {}".format( str(message))) config = SkillConfig(name="test_skill", author="fetchai") skill = Skill(configuration=config) dummy_handler = DummyHandler(name="dummy_handler", skill_context=skill.skill_context) skill.handlers.update({dummy_handler.name: dummy_handler}) builder.add_component_instance(skill) # Create our AEA my_aea = builder.build() # Set the AEA running in a different thread try: t = Thread(target=my_aea.start) t.start() # Wait for everything to start up time.sleep(4) # Create a message inside an envelope and get the stub connection to pass it on to the echo skill message_text = ( "my_aea,other_agent,fetchai/default:0.3.0,\x08\x01*\x07\n\x05hello," ) with open(INPUT_FILE, "w") as f: f.write(message_text) print("input message: " + message_text) # Wait for the envelope to get processed time.sleep(4) # Read the output envelope generated by the echo skill with open(OUTPUT_FILE, "r") as f: print("output message: " + f.readline()) finally: # Shut down the AEA my_aea.stop() t.join() t = None