def test_enqueue_task_stopped(self): """Test enqueue_task method manager stopped.""" obj = TaskManager() func = mock.Mock() with self.assertRaises(ValueError): obj.enqueue_task(func) func.assert_not_called()
def test_start_stop_reflected_by_is_started(self) -> None: """Test is_started property of task manaher.""" self.task_manager = TaskManager() assert not self.task_manager.is_started self.task_manager.start() assert self.task_manager.is_started self.task_manager.stop() assert not self.task_manager.is_started
def test_start_lazy_pool_start(self): """Test start method with lazy pool start.""" obj = TaskManager(is_lazy_pool_start=False) with mock.patch.object(obj.logger, "debug") as debug_mock: obj.start() obj._stopped = True obj.start() debug_mock.assert_called_with("Pool was already started!") obj.start()
def setup(cls) -> None: """Set up the skill test case.""" identity = Identity("test_agent_name", "test_agent_address") cls._multiplexer = AsyncMultiplexer() cls._multiplexer._out_queue = ( # pylint: disable=protected-access asyncio.Queue() ) cls._outbox = OutBox(cast(Multiplexer, cls._multiplexer)) agent_context = AgentContext( identity=identity, connection_status=cls._multiplexer.connection_status, outbox=cls._outbox, decision_maker_message_queue=Queue(), decision_maker_handler_context=SimpleNamespace(), task_manager=TaskManager(), default_ledger_id=identity.default_address_key, currency_denominations=DEFAULT_CURRENCY_DENOMINATIONS, default_connection=None, default_routing={}, search_service_address="dummy_search_service_address", decision_maker_address="dummy_decision_maker_address", ) cls._skill = Skill.from_dir(str(cls.path_to_skill), agent_context)
def __init__( self, agent: AbstractAgent, loop_mode: Optional[str] = None, loop: Optional[AbstractEventLoop] = None, threaded: bool = False, ) -> None: """ Init runtime. :param agent: Agent to run. :param loop_mode: agent main loop mode. :param loop: optional event loop. if not provided a new one will be created. :return: None """ Runnable.__init__(self, threaded=threaded, loop=loop if not threaded else None) logger = get_logger(__name__, agent.name) WithLogger.__init__(self, logger=logger) self._agent: AbstractAgent = agent self._state: AsyncState = AsyncState(RuntimeStates.stopped, RuntimeStates) self._state.add_callback(self._log_runtime_state) self._multiplexer: AsyncMultiplexer = self._get_multiplexer_instance() self._task_manager = TaskManager() self._decision_maker: Optional[DecisionMaker] = None self._loop_mode = loop_mode or self.DEFAULT_RUN_LOOP self.main_loop: BaseAgentLoop = self._get_main_loop_instance(self._loop_mode)
def test_start_already_started(self): """Test start method already started.""" obj = TaskManager() with mock.patch.object(obj.logger, "debug") as debug_mock: obj._stopped = False obj.start() debug_mock.assert_called_once() obj.stop()
def __init__( self, identity: Identity, connections: List[Connection], wallet: Wallet, ledger_apis: LedgerApis, resources: Resources, loop: Optional[AbstractEventLoop] = None, timeout: float = 0.0, is_debug: bool = False, is_programmatic: bool = True, max_reactions: int = 20, ) -> 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 wallet: the wallet of the agent. :param ledger_apis: the ledger apis of the agent. :param resources: the resources of the agent. :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. :param is_programmatic: if True, run the agent in programmatic mode (skips loading of resources from directory). :param max_reactions: the processing rate of messages per iteration. :return: None """ super().__init__( identity=identity, connections=connections, loop=loop, timeout=timeout, is_debug=is_debug, is_programmatic=is_programmatic, ) self.max_reactions = max_reactions self._task_manager = TaskManager() self._decision_maker = DecisionMaker( self.name, self.max_reactions, self.outbox, wallet, ledger_apis ) self._context = AgentContext( self.identity, ledger_apis, self.multiplexer.connection_status, self.outbox, self.decision_maker.message_in_queue, self.decision_maker.ownership_state, self.decision_maker.preferences, self.decision_maker.goal_pursuit_readiness, self.task_manager, ) self._resources = resources self._filter = Filter(self.resources, self.decision_maker.message_out_queue)
def test_lazy_pool_start_on_enqueue(self, apply_async_mock: Mock) -> None: """ Test lazy pool created on enqueue once. :param apply_async_mock: is mock for aea.skills.tasks.Pool.apply_async """ self.task_manager = TaskManager(is_lazy_pool_start=True) self.task_manager.start() assert not self.task_manager._pool self.task_manager.enqueue_task(print) apply_async_mock.assert_called_once() assert self.task_manager._pool """Check pool created once on several enqueues""" pool = self.task_manager._pool self.task_manager.enqueue_task(print) assert self.task_manager._pool is pool
def test_init_worker_positive(self, init_worker_mock): """Test init_worker method positive result.""" task_manager = TaskManager(is_lazy_pool_start=False) task_manager.start() try: init_worker_mock.assert_called() finally: task_manager.stop()
def test_enqueue_task_positive(self): """Test enqueue_task method positive result.""" obj = TaskManager() func = mock.Mock() obj._stopped = False obj._pool = mock.Mock() obj._pool.apply_async = mock.Mock(return_value="async_result") obj.enqueue_task(func) obj._pool.apply_async.assert_called_once()
def setup(cls, **kwargs: Any) -> None: """Set up the skill test case.""" identity = Identity("test_agent_name", "test_agent_address") cls._multiplexer = AsyncMultiplexer() cls._multiplexer._out_queue = ( # pylint: disable=protected-access asyncio.Queue() ) cls._outbox = OutBox(cast(Multiplexer, cls._multiplexer)) _shared_state = cast(Dict[str, Any], kwargs.pop("shared_state", dict())) _skill_config_overrides = cast( Dict[str, Any], kwargs.pop("config_overrides", dict()) ) agent_context = AgentContext( identity=identity, connection_status=cls._multiplexer.connection_status, outbox=cls._outbox, decision_maker_message_queue=Queue(), decision_maker_handler_context=SimpleNamespace(), task_manager=TaskManager(), default_ledger_id=identity.default_address_key, currency_denominations=DEFAULT_CURRENCY_DENOMINATIONS, default_connection=None, default_routing={}, search_service_address="dummy_search_service_address", decision_maker_address="dummy_decision_maker_address", data_dir=os.getcwd(), ) # This enables pre-populating the 'shared_state' prior to loading the skill if _shared_state != dict(): for key, value in _shared_state.items(): agent_context.shared_state[key] = value skill_configuration_file_path: Path = Path(cls.path_to_skill, "skill.yaml") loader = ConfigLoaders.from_package_type(PackageType.SKILL) with open_file(skill_configuration_file_path) as fp: skill_config: SkillConfig = loader.load(fp) # This enables overriding the skill's config prior to loading if _skill_config_overrides != {}: skill_config.update(_skill_config_overrides) skill_config.directory = cls.path_to_skill cls._skill = Skill.from_config(skill_config, agent_context)
def test_start_already_started(self, debug_mock): """Test start method already started.""" obj = TaskManager() obj._stopped = False obj.start() debug_mock.assert_called_once()
def test_nb_workers_positive(self): """Test nb_workers property positive result.""" obj = TaskManager() obj.nb_workers
def test_stop_already_stopped(self, debug_mock): """Test stop method already stopped.""" obj = TaskManager() obj.stop() debug_mock.assert_called_once()
def test_not_lazy_pool_is_started(self) -> None: """Lazy pool creation assumes pool create on first task enqueue.""" self.task_manager = TaskManager(is_lazy_pool_start=False) self.task_manager.start() assert self.task_manager._pool
def setup_class(cls): """Set the tests up.""" cls.task_manager = TaskManager(nb_workers=5) cls.task_manager.start()
def __init__( self, identity: Identity, wallet: Wallet, resources: Resources, loop: Optional[AbstractEventLoop] = None, timeout: float = 0.05, execution_timeout: float = 0, max_reactions: int = 20, decision_maker_handler_class: Type[ DecisionMakerHandler] = DefaultDecisionMakerHandler, skill_exception_policy: ExceptionPolicyEnum = ExceptionPolicyEnum. propagate, loop_mode: Optional[str] = None, runtime_mode: Optional[str] = None, default_connection: Optional[PublicId] = None, default_routing: Optional[Dict[PublicId, PublicId]] = None, connection_ids: Optional[Collection[PublicId]] = None, search_service_address: str = "fetchai/soef:*", **kwargs, ) -> None: """ Instantiate the agent. :param identity: the identity of the agent :param wallet: the wallet of the agent. :param resources: the resources (protocols and skills) 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 exeution_timeout: amount of time to limit single act/handle to execute. :param max_reactions: the processing rate of envelopes per tick (i.e. single loop). :param decision_maker_handler_class: the class implementing the decision maker handler to be used. :param skill_exception_policy: the skill exception policy enum :param loop_mode: loop_mode to choose agent run loop. :param runtime_mode: runtime mode (async, threaded) to run AEA in. :param default_connection: public id to the default connection :param default_routing: dictionary for default routing. :param connection_ids: active connection ids. Default: consider all the ones in the resources. :param search_service_address: the address of the search service used. :param kwargs: keyword arguments to be attached in the agent context namespace. :return: None """ super().__init__( identity=identity, connections=[], loop=loop, timeout=timeout, loop_mode=loop_mode, runtime_mode=runtime_mode, ) self.max_reactions = max_reactions self._task_manager = TaskManager() decision_maker_handler = decision_maker_handler_class( identity=identity, wallet=wallet) self._decision_maker = DecisionMaker( decision_maker_handler=decision_maker_handler) self._context = AgentContext( self.identity, self.multiplexer.connection_status, self.outbox, self.decision_maker.message_in_queue, decision_maker_handler.context, self.task_manager, default_connection, default_routing if default_routing is not None else {}, search_service_address, **kwargs, ) self._execution_timeout = execution_timeout self._connection_ids = connection_ids self._resources = resources self._filter = Filter(self.resources, self.decision_maker.message_out_queue) self._skills_exception_policy = skill_exception_policy self._setup_loggers()
class TestTaskPoolManagementManager(TestCase): """Tests for pool management by task manager. Lazy and non lazy.""" def tearDown(self): """Stop task manager. assumed it's created on each test.""" self.task_manager.stop() def test_start_stop_reflected_by_is_started(self) -> None: """Test is_started property of task manaher.""" self.task_manager = TaskManager() assert not self.task_manager.is_started self.task_manager.start() assert self.task_manager.is_started self.task_manager.stop() assert not self.task_manager.is_started def test_lazy_pool_not_started(self) -> None: """Lazy pool creation assumes pool create on first task enqueue.""" self.task_manager = TaskManager(is_lazy_pool_start=True) self.task_manager.start() assert not self.task_manager._pool def test_not_lazy_pool_is_started(self) -> None: """Lazy pool creation assumes pool create on first task enqueue.""" self.task_manager = TaskManager(is_lazy_pool_start=False) self.task_manager.start() assert self.task_manager._pool @patch("aea.skills.tasks.Pool.apply_async") def test_lazy_pool_start_on_enqueue(self, apply_async_mock: Mock) -> None: """ Test lazy pool created on enqueue once. :param apply_async_mock: is mock for aea.skills.tasks.Pool.apply_async """ self.task_manager = TaskManager(is_lazy_pool_start=True) self.task_manager.start() assert not self.task_manager._pool self.task_manager.enqueue_task(print) apply_async_mock.assert_called_once() assert self.task_manager._pool """Check pool created once on several enqueues""" pool = self.task_manager._pool self.task_manager.enqueue_task(print) assert self.task_manager._pool is pool
def test_get_task_result_positive(self): """Test get_task_result method positive result.""" obj = TaskManager() obj._results_by_task_id = {"task_id": "result"} obj.get_task_result("task_id")
def test_get_task_result_id_not_present(self): """Test get_task_result method id not present.""" obj = TaskManager() with self.assertRaises(ValueError): obj.get_task_result("task_id")
def __init__( self, identity: Identity, connections: List[Connection], wallet: Wallet, ledger_apis: LedgerApis, resources: Resources, loop: Optional[AbstractEventLoop] = None, timeout: float = 0.05, execution_timeout: float = 0, is_debug: bool = False, max_reactions: int = 20, decision_maker_handler_class: Type[ DecisionMakerHandler ] = DefaultDecisionMakerHandler, skill_exception_policy: ExceptionPolicyEnum = ExceptionPolicyEnum.propagate, loop_mode: Optional[str] = None, **kwargs, ) -> None: """ Instantiate the agent. :param identity: the identity of the agent :param connections: the list of connections of the agent. :param wallet: the wallet of the agent. :param ledger_apis: the APIs the agent will use to connect to ledgers. :param resources: the resources (protocols and skills) 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 exeution_timeout: amount of time to limit single act/handle to execute. :param is_debug: if True, run the agent in debug mode (does not connect the multiplexer). :param max_reactions: the processing rate of envelopes per tick (i.e. single loop). :param decision_maker_handler_class: the class implementing the decision maker handler to be used. :param skill_exception_policy: the skill exception policy enum :param loop_mode: loop_mode to choose agent run loop. :param kwargs: keyword arguments to be attached in the agent context namespace. :return: None """ super().__init__( identity=identity, connections=connections, loop=loop, timeout=timeout, is_debug=is_debug, loop_mode=loop_mode, ) self.max_reactions = max_reactions self._task_manager = TaskManager() decision_maker_handler = decision_maker_handler_class( identity=identity, wallet=wallet, ledger_apis=ledger_apis ) self._decision_maker = DecisionMaker( decision_maker_handler=decision_maker_handler ) self._context = AgentContext( self.identity, ledger_apis, self.multiplexer.connection_status, self.outbox, self.decision_maker.message_in_queue, decision_maker_handler.context, self.task_manager, **kwargs, ) self._execution_timeout = execution_timeout self._resources = resources self._filter = Filter(self.resources, self.decision_maker.message_out_queue) self._skills_exception_policy = skill_exception_policy