Ejemplo n.º 1
0
    async def test_no_callback(self, mocker):

        s = StatusMixIn(CommandStatus, CommandStatus.READY)

        s.status = CommandStatus.READY
        assert s.status == CommandStatus.READY

        s.status = CommandStatus.RUNNING
        assert s.status == CommandStatus.RUNNING
Ejemplo n.º 2
0
    def __init__(
        self,
        commander_id: Union[int, str, None] = None,
        command_id: Union[int, str] = 0,
        consumer_id: Union[int, str] = 0,
        actor: Optional[Actor_co] = None,
        parent: Optional[BaseCommand[Actor_co, Future_co]] = None,
        reply_callback: Optional[Callable[[Any], None]] = None,
        status_callback: Optional[Callable[[CommandStatus], Any]] = None,
        call_now: bool = False,
        default_keyword: str = "text",
        silent: bool = False,
        time_limit: float | None = None,
        loop: Optional[asyncio.AbstractEventLoop] = None,
    ):

        self.commander_id = commander_id
        self.consumer_id = consumer_id
        self.command_id = command_id

        # Casting here so that we can type Command[SomeActor] and not have to
        # assert command.actor every time.
        self.actor = cast(Actor_co, actor)
        self.parent = parent

        self.silent = silent

        self._reply_callback = reply_callback

        self.default_keyword = default_keyword
        self.loop = loop or asyncio.get_event_loop()

        #: A list of replies this command has received. The type of
        #: reply object depends on the actor or client issuing the command.
        self.replies = ReplyList([])

        asyncio.Future.__init__(self)

        self._status: CommandStatus
        StatusMixIn.__init__(
            self,
            CommandStatus,
            initial_status=CommandStatus.READY,
            callback_func=status_callback,
            call_now=call_now,
        )

        self._timer_handler = None
        if time_limit:
            self._timer_handler = asyncio.get_running_loop().call_later(
                time_limit,
                self.set_status,
                CommandStatus.TIMEDOUT,
            )
Ejemplo n.º 3
0
    async def test_callback(self, mocker):

        callback = mocker.MagicMock()

        s = StatusMixIn(CommandStatus,
                        CommandStatus.READY,
                        callback_func=callback)

        s.status = CommandStatus.RUNNING

        await asyncio.sleep(0.01)

        callback.assert_called_once()
Ejemplo n.º 4
0
    async def test_callback_tuple(self, mocker):

        callback = (mocker.MagicMock(), mocker.MagicMock())

        s = StatusMixIn(CommandStatus,
                        CommandStatus.READY,
                        callback_func=callback)

        s.status = CommandStatus.READY  # This should not trigger callbacks
        s.status = CommandStatus.RUNNING

        await asyncio.sleep(0.01)

        callback[0].assert_called_once()
        callback[1].assert_called_once()
Ejemplo n.º 5
0
    async def test_wait_for_status_same(self):

        s = StatusMixIn(CommandStatus, CommandStatus.READY)

        await s.wait_for_status(CommandStatus.READY)

        assert s.status == CommandStatus.READY
        assert s.watcher is None
Ejemplo n.º 6
0
    async def test_wait_for_status(self, event_loop):
        def set_status(mixin, status):
            mixin.status = status

        s = StatusMixIn(CommandStatus, CommandStatus.READY)

        event_loop.call_later(0.01, set_status, s, CommandStatus.DONE)

        await s.wait_for_status(CommandStatus.DONE)

        assert s.status == CommandStatus.DONE
        assert s.watcher is None
Ejemplo n.º 7
0
    async def test_callback_call_now(self, mocker):

        callback = mocker.MagicMock()

        StatusMixIn(
            CommandStatus,
            CommandStatus.READY,
            callback_func=callback,
            call_now=True,
        )

        await asyncio.sleep(0.01)

        callback.assert_called_once()