def test_start_stop_and_start_stop_again(): """Tests AEA can be started/stopped twice.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build() with run_in_thread(agent.start, timeout=20): wait_for_condition(lambda: agent.is_running, timeout=10) behaviour = agent.resources.get_behaviour(DUMMY_SKILL_PUBLIC_ID, "dummy") time.sleep(1) wait_for_condition(lambda: behaviour.nb_act_called > 0, timeout=5) agent.stop() wait_for_condition(lambda: agent.is_stopped, timeout=10) behaviour.nb_act_called = 0 time.sleep(2) assert behaviour.nb_act_called == 0 with run_in_thread(agent.start, timeout=20): wait_for_condition(lambda: agent.is_running, timeout=10) time.sleep(1) wait_for_condition(lambda: behaviour.nb_act_called > 0, timeout=5) agent.stop() wait_for_condition(lambda: agent.is_stopped, timeout=10)
def test_react(): """Tests income messages.""" with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(FETCHAI, private_key_path) builder.add_protocol( Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")) builder.add_connection( Path(ROOT_DIR, "packages", "fetchai", "connections", "local")) builder.set_default_connection( PublicId.from_str("fetchai/local:0.1.0")) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build( connection_ids=[PublicId.from_str("fetchai/local:0.1.0")]) # This is a temporary workaround to feed the local node to the OEF Local connection # TODO remove it. list(agent._connections)[0]._local_node = node msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.counterparty = agent.identity.address message_bytes = DefaultSerializer().encode(msg) envelope = Envelope( to=agent.identity.address, sender=agent.identity.address, protocol_id=DefaultMessage.protocol_id, message=message_bytes, ) with run_in_thread(agent.start, timeout=20, on_exit=agent.stop): wait_for_condition( lambda: agent._main_loop and agent._main_loop.is_running, timeout=10) agent.outbox.put(envelope) default_protocol_public_id = DefaultMessage.protocol_id dummy_skill_public_id = DUMMY_SKILL_PUBLIC_ID handler = agent.resources.get_handler(default_protocol_public_id, dummy_skill_public_id) assert handler is not None, "Handler is not set." wait_for_condition( lambda: msg in handler.handled_messages, timeout=10, error_msg="The message is not inside the handled_messages.", ) agent.stop()
def test_react(): """Tests income messages.""" with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_protocol( Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")) builder.add_connection( Path(ROOT_DIR, "packages", "fetchai", "connections", "local")) local_connection_id = OEFLocalConnection.connection_id builder.set_default_connection(local_connection_id) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build(connection_ids=[local_connection_id]) # This is a temporary workaround to feed the local node to the OEF Local connection # TODO remove it. local_connection = agent.resources.get_connection(local_connection_id) local_connection._local_node = node msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.to = agent.identity.address msg.sender = agent.identity.address envelope = Envelope( to=msg.to, sender=msg.sender, protocol_id=msg.protocol_id, message=msg, ) with run_in_thread(agent.start, timeout=20, on_exit=agent.stop): wait_for_condition(lambda: agent.is_running, timeout=20) agent.outbox.put(envelope) default_protocol_public_id = DefaultMessage.protocol_id dummy_skill_public_id = DUMMY_SKILL_PUBLIC_ID handler = agent.resources.get_handler(default_protocol_public_id, dummy_skill_public_id) assert handler is not None, "Handler is not set." wait_for_condition( lambda: len(handler.handled_messages) > 0, timeout=20, error_msg="The message is not inside the handled_messages.", )
def test_handle_envelope(self): """Test one envelope handling.""" handler = CountHandler.make() agent = self.FAKE_AGENT_CLASS(handlers=[handler]) agent_loop = self.AGENT_LOOP_CLASS(agent) handler.setup() with run_in_thread(agent_loop.start, timeout=10): wait_for_condition(lambda: agent_loop.is_running, timeout=10) agent.put_inbox("msg") wait_for_condition(lambda: handler.counter == 1, timeout=2) agent_loop.stop()
def test_internal_messages(self): """Test internal meesages are processed.""" agent = self.FAKE_AGENT_CLASS() agent_loop = self.AGENT_LOOP_CLASS(agent) with run_in_thread(agent_loop.start, timeout=5): wait_for_condition(lambda: agent_loop.is_running, timeout=10) agent.put_internal_message("msg") wait_for_condition( lambda: agent.filter._process_internal_message.called is True, timeout=5, ) agent_loop.stop()
def test_new_behaviours(self): """Test new behaviours are added.""" agent = self.FAKE_AGENT_CLASS() agent_loop = self.AGENT_LOOP_CLASS(agent) agent_loop.NEW_BEHAVIOURS_PROCESS_SLEEP = 0.5 with run_in_thread(agent_loop.start, timeout=5): wait_for_condition(lambda: agent_loop.is_running, timeout=10) wait_for_condition( lambda: agent.filter._handle_new_behaviours.call_count >= 2, timeout=agent_loop.NEW_BEHAVIOURS_PROCESS_SLEEP * 3, ) agent_loop.stop()
def test_start_stop(): """Tests the act function of the AEA.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build() with run_in_thread(agent.start, timeout=20): wait_for_condition(lambda: agent.is_running, timeout=20) agent.stop()
def test_start_stop(): """Tests the act function of the AEA.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(FETCHAI, private_key_path) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build() with run_in_thread(agent.start, timeout=20): wait_for_condition( lambda: agent._main_loop and agent._main_loop.is_running, timeout=10) agent.stop()
def test_behaviour_act(self): """Test behaviour act called by schedule.""" tick_interval = 0.1 behaviour = CountBehaviour.make(tick_interval=tick_interval) behaviour.setup() agent = self.FAKE_AGENT_CLASS(behaviours=[behaviour]) agent_loop = self.AGENT_LOOP_CLASS(agent) with run_in_thread(agent_loop.start, timeout=5): wait_for_condition(lambda: agent_loop.is_running, timeout=10) # test behaviour called wait_for_condition(lambda: behaviour.counter >= 1, timeout=tick_interval * 2) agent_loop.stop()
def test_act(): """Tests the act function of the AEA.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(FETCHAI, private_key_path) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build() with run_in_thread(agent.start, timeout=20): wait_for_condition( lambda: agent._main_loop and agent._main_loop.is_running, timeout=10) behaviour = agent.resources.get_behaviour(DUMMY_SKILL_PUBLIC_ID, "dummy") wait_for_condition(lambda: behaviour.nb_act_called > 0, timeout=10) agent.stop()
def test_act(): """Tests the act function of the AEA.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build() with run_in_thread(agent.start, timeout=20): wait_for_condition(lambda: agent.is_running, timeout=20) behaviour = agent.resources.get_behaviour(DUMMY_SKILL_PUBLIC_ID, "dummy") time.sleep(1) wait_for_condition(lambda: behaviour.nb_act_called > 0, timeout=20) agent.stop()
def test_double_start(): """Tests the act function of the AEA.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) agent = builder.build() with run_in_thread(agent.start, timeout=20): try: wait_for_condition(lambda: agent.is_running, timeout=20) t = Thread(target=agent.start) t.start() time.sleep(1) assert not t.is_alive() finally: agent.stop()
def test_add_behaviour_dynamically(): """Test that we can add a behaviour dynamically.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") wallet = Wallet({FETCHAI: private_key_path}) resources = Resources() identity = Identity(agent_name, address=wallet.addresses[FETCHAI]) connection = _make_local_connection(identity.address, LocalNode()) agent = AEA( identity, wallet, resources, default_connection=connection.public_id, ) resources.add_connection(connection) resources.add_component( Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"), agent_context=agent.context)) for skill in resources.get_all_skills(): skill.skill_context.set_agent_context(agent.context) with run_in_thread(agent.start, timeout=5, on_exit=agent.stop): wait_for_condition( lambda: agent._main_loop and agent._main_loop.is_running, timeout=10) dummy_skill_id = PublicId("dummy_author", "dummy", "0.1.0") dummy_skill = agent.resources.get_skill(dummy_skill_id) wait_for_condition(lambda: dummy_skill is not None, timeout=10) new_behaviour = DummyBehaviour(name="dummy2", skill_context=dummy_skill.skill_context) dummy_skill.skill_context.new_behaviours.put(new_behaviour) wait_for_condition(lambda: new_behaviour.nb_act_called > 0, timeout=10) wait_for_condition( lambda: len(agent.resources.get_behaviours(dummy_skill_id)) == 2, timeout=10)
def test_add_behaviour_dynamically(): """Test that we can add a behaviour dynamically.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) resources = Resources() identity = Identity(agent_name, address=wallet.addresses[DEFAULT_LEDGER]) connection = _make_local_connection(identity.address, LocalNode()) agent = AEA( identity, wallet, resources, default_connection=connection.public_id, ) resources.add_connection(connection) resources.add_component( Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"), agent_context=agent.context)) for skill in resources.get_all_skills(): skill.skill_context.set_agent_context(agent.context) with run_in_thread(agent.start, timeout=5, on_exit=agent.stop): wait_for_condition(lambda: agent.is_running, timeout=10) dummy_skill_id = DUMMY_SKILL_PUBLIC_ID dummy_skill = agent.resources.get_skill(dummy_skill_id) wait_for_condition(lambda: dummy_skill is not None, timeout=10) new_behaviour = DummyBehaviour(name="dummy2", skill_context=dummy_skill.skill_context) dummy_skill.skill_context.new_behaviours.put(new_behaviour) wait_for_condition(lambda: new_behaviour.nb_act_called > 0, timeout=10) wait_for_condition( lambda: len(agent.resources.get_behaviours(dummy_skill_id)) == 2, timeout=10)
def test_initialize_aea_programmatically_build_resources(): """Test that we can initialize the agent by building the resource object.""" try: temp = tempfile.mkdtemp(prefix="test_aea_resources") with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) identity = Identity(agent_name, address=wallet.addresses[DEFAULT_LEDGER]) connection = _make_local_connection(agent_name, node) resources = Resources() aea = AEA( identity, wallet, resources=resources, default_connection=connection.public_id, ) default_protocol = Protocol.from_dir( str(Path(AEA_DIR, "protocols", "default"))) resources.add_protocol(default_protocol) resources.add_connection(connection) error_skill = Skill.from_dir(str(Path(AEA_DIR, "skills", "error")), agent_context=aea.context) dummy_skill = Skill.from_dir(str( Path(CUR_PATH, "data", "dummy_skill")), agent_context=aea.context) resources.add_skill(dummy_skill) resources.add_skill(error_skill) default_protocol_id = DefaultMessage.protocol_id expected_message = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) expected_message.counterparty = agent_name expected_message.sender = agent_name with run_in_thread(aea.start, timeout=5, on_exit=aea.stop): wait_for_condition(lambda: aea.is_running, timeout=10) aea.outbox.put( Envelope( to=agent_name, sender=agent_name, protocol_id=default_protocol_id, message=expected_message, )) dummy_skill_id = DUMMY_SKILL_PUBLIC_ID dummy_behaviour_name = "dummy" dummy_behaviour = aea.resources.get_behaviour( dummy_skill_id, dummy_behaviour_name) wait_for_condition(lambda: dummy_behaviour is not None, timeout=10) wait_for_condition(lambda: dummy_behaviour.nb_act_called > 0, timeout=10) dummy_task = DummyTask() task_id = aea.task_manager.enqueue_task(dummy_task) async_result = aea.task_manager.get_task_result(task_id) expected_dummy_task = async_result.get(10.0) wait_for_condition( lambda: expected_dummy_task.nb_execute_called > 0, timeout=10) dummy_handler_name = "dummy" dummy_handler = aea.resources._handler_registry.fetch( (dummy_skill_id, dummy_handler_name)) dummy_handler_alt = aea.resources.get_handler( DefaultMessage.protocol_id, dummy_skill_id) wait_for_condition(lambda: dummy_handler == dummy_handler_alt, timeout=10) wait_for_condition(lambda: dummy_handler is not None, timeout=10) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 1, timeout=10) wait_for_condition( lambda: dummy_handler.handled_messages[0] == expected_message, timeout=10, ) finally: Path(temp).rmdir()
def test_initialize_aea_programmatically(): """Test that we can initialize an AEA programmatically.""" with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_protocol( Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")) builder.add_connection( Path(ROOT_DIR, "packages", "fetchai", "connections", "local")) local_connection_id = PublicId.from_str("fetchai/local:0.6.0") builder.set_default_connection(local_connection_id) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) aea = builder.build( connection_ids=[PublicId.from_str("fetchai/local:0.6.0")]) local_connection = aea.resources.get_connection(local_connection_id) local_connection._local_node = node expected_message = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) expected_message.counterparty = aea.identity.address expected_message.sender = aea.identity.address envelope = Envelope( to=expected_message.counterparty, sender=expected_message.sender, protocol_id=expected_message.protocol_id, message=expected_message, ) with run_in_thread(aea.start, timeout=5, on_exit=aea.stop): wait_for_condition(lambda: aea.is_running, timeout=10) aea.outbox.put(envelope) dummy_skill_id = DUMMY_SKILL_PUBLIC_ID dummy_behaviour_name = "dummy" dummy_behaviour = aea.resources.get_behaviour( dummy_skill_id, dummy_behaviour_name) wait_for_condition(lambda: dummy_behaviour is not None, timeout=10) wait_for_condition(lambda: dummy_behaviour.nb_act_called > 0, timeout=10) # TODO the previous code caused an error: # _pickle.PicklingError: Can't pickle <class 'tasks.DummyTask'>: import of module 'tasks' failed dummy_task = DummyTask() task_id = aea.task_manager.enqueue_task(dummy_task) async_result = aea.task_manager.get_task_result(task_id) expected_dummy_task = async_result.get(10.0) wait_for_condition( lambda: expected_dummy_task.nb_execute_called > 0, timeout=10) dummy_handler = aea.resources.get_handler( DefaultMessage.protocol_id, dummy_skill_id) dummy_handler_alt = aea.resources._handler_registry.fetch( (dummy_skill_id, "dummy")) wait_for_condition(lambda: dummy_handler == dummy_handler_alt, timeout=10) wait_for_condition(lambda: dummy_handler is not None, timeout=10) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 1, timeout=10) wait_for_condition( lambda: dummy_handler.handled_messages[0] == expected_message, timeout=10, )
def test_handle(): """Tests handle method of an agent.""" with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(DEFAULT_LEDGER, private_key_path) builder.add_protocol( Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")) builder.add_connection( Path(ROOT_DIR, "packages", "fetchai", "connections", "local")) local_connection_id = PublicId.from_str("fetchai/local:0.6.0") builder.set_default_connection(local_connection_id) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) aea = builder.build( connection_ids=[PublicId.from_str("fetchai/local:0.6.0")]) # This is a temporary workaround to feed the local node to the OEF Local connection # TODO remove it. local_connection = aea.resources.get_connection(local_connection_id) local_connection._local_node = node msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.counterparty = aea.identity.address msg.sender = aea.identity.address envelope = Envelope( to=msg.counterparty, sender=msg.sender, protocol_id=UNKNOWN_PROTOCOL_PUBLIC_ID, message=msg, ) with run_in_thread(aea.start, timeout=5): wait_for_condition(lambda: aea.is_running, timeout=10) dummy_skill = aea.resources.get_skill(DUMMY_SKILL_PUBLIC_ID) dummy_handler = dummy_skill.handlers["dummy"] aea.outbox.put(envelope) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 1, timeout=1, ) # DECODING ERROR envelope = Envelope( to=aea.identity.address, sender=aea.identity.address, protocol_id=DefaultMessage.protocol_id, message=b"", ) # send envelope via localnode back to agent/bypass `outbox` put consistency checks aea.outbox._multiplexer.put(envelope) """ inbox twice cause first message is invalid. generates error message and it accepted """ wait_for_condition( lambda: len(dummy_handler.handled_messages) == 2, timeout=1, ) # UNSUPPORTED SKILL msg = FipaMessage( performative=FipaMessage.Performative.ACCEPT, message_id=1, dialogue_reference=(str(0), ""), target=0, ) msg.counterparty = aea.identity.address msg.sender = aea.identity.address envelope = Envelope( to=msg.counterparty, sender=msg.sender, protocol_id=msg.protocol_id, message=msg, ) # send envelope via localnode back to agent aea.outbox.put(envelope) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 3, timeout=2, ) aea.stop()
def test_start_stop(self): """Test runtime tart/stop.""" with run_in_thread(self.runtime.start, timeout=20): wait_for_condition(lambda: self.runtime.is_running, timeout=20) self.runtime.stop() wait_for_condition(lambda: not self.runtime.is_running, timeout=20)
async def test_handle(): """Tests handle method of an agent.""" with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") builder = AEABuilder() builder.set_name(agent_name) builder.add_private_key(FETCHAI, private_key_path) builder.add_protocol( Path(ROOT_DIR, "packages", "fetchai", "protocols", "oef_search")) builder.add_connection( Path(ROOT_DIR, "packages", "fetchai", "connections", "local")) builder.set_default_connection( PublicId.from_str("fetchai/local:0.1.0")) builder.add_skill(Path(CUR_PATH, "data", "dummy_skill")) aea = builder.build( connection_ids=[PublicId.from_str("fetchai/local:0.1.0")]) # This is a temporary workaround to feed the local node to the OEF Local connection # TODO remove it. list(aea._connections)[0]._local_node = node msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) message_bytes = DefaultSerializer().encode(msg) envelope = Envelope( to=aea.identity.address, sender=aea.identity.address, protocol_id=UNKNOWN_PROTOCOL_PUBLIC_ID, message=message_bytes, ) with run_in_thread(aea.start, timeout=5, on_exit=aea.stop): wait_for_condition( lambda: aea._main_loop and aea._main_loop.is_running, timeout=10) dummy_skill = aea.resources.get_skill(DUMMY_SKILL_PUBLIC_ID) dummy_handler = dummy_skill.handlers["dummy"] aea.outbox.put(envelope) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 1, timeout=1, ) # DECODING ERROR envelope = Envelope( to=aea.identity.address, sender=aea.identity.address, protocol_id=DefaultMessage.protocol_id, message=b"", ) # send envelope via localnode back to agent aea.outbox.put(envelope) """ inbox twice cause first message is invalid. generates error message and it accepted """ wait_for_condition( lambda: len(dummy_handler.handled_messages) == 2, timeout=1, ) # UNSUPPORTED SKILL msg = FipaSerializer().encode( FipaMessage( performative=FipaMessage.Performative.ACCEPT, message_id=1, dialogue_reference=(str(0), ""), target=0, )) envelope = Envelope( to=aea.identity.address, sender=aea.identity.address, protocol_id=FipaMessage.protocol_id, message=msg, ) # send envelope via localnode back to agent aea.outbox.put(envelope) wait_for_condition( lambda: len(dummy_handler.handled_messages) == 3, timeout=1, ) aea.stop()
def test_loop_start_stop(self): """Test loop start and stopped properly.""" agent_loop = self.AGENT_LOOP_CLASS(self.FAKE_AGENT_CLASS()) with run_in_thread(agent_loop.start): wait_for_condition(lambda: agent_loop.is_running, timeout=10) agent_loop.stop()