async def test_async_state(): """Test AsyncState class.""" initial = None target_state = "target_state" async_state = AsyncState(initial_state=initial) assert async_state.state == initial assert await async_state.wait(initial) == (None, initial) task = create_task(async_state.wait(target_state)) await asyncio.sleep(0) async_state.state = target_state assert await asyncio.wait_for(task, timeout=1) == (initial, target_state)
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 __init__(self, agent: "Agent", loop: Optional[AbstractEventLoop] = None) -> None: """ Init runtime. :param agent: Agent to run. :param loop: optional event loop. if not provided a new one will be created. :return: None """ self._agent: "Agent" = agent self._loop = ensure_loop( loop ) # TODO: decide who constructs loop: agent, runtime, multiplexer. self._state: AsyncState = AsyncState(RuntimeStates.initial)