From e137c2882f5e03ef57f75a01202826bd47df331c Mon Sep 17 00:00:00 2001 From: Oleg A Date: Wed, 21 Sep 2022 08:12:17 +0300 Subject: [PATCH] fix: wait for initial_sleep --- aiogram/utils/chat_action.py | 2 +- tests/test_utils/test_chat_action.py | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/aiogram/utils/chat_action.py b/aiogram/utils/chat_action.py index e3f91f3f..794f3655 100644 --- a/aiogram/utils/chat_action.py +++ b/aiogram/utils/chat_action.py @@ -74,7 +74,6 @@ class ChatActionSender: ) try: counter = 0 - await self._wait(self.initial_sleep) while not self._close_event.is_set(): start = time.monotonic() logger.debug( @@ -104,6 +103,7 @@ class ChatActionSender: self._closed_event.clear() if self.running: raise RuntimeError("Already running") + await self._wait(self.initial_sleep) self._task = asyncio.create_task(self._worker()) async def _stop(self) -> None: diff --git a/tests/test_utils/test_chat_action.py b/tests/test_utils/test_chat_action.py index 71010e29..fc76fcbe 100644 --- a/tests/test_utils/test_chat_action.py +++ b/tests/test_utils/test_chat_action.py @@ -20,12 +20,25 @@ pytestmarm = pytest.mark.asyncio class TestChatActionSender: - async def test_wait(self, bot: Bot, loop: asyncio.BaseEventLoop): + initial_sleep = 0.2 + + async def test_wait_with_event(self, bot: Bot, loop: asyncio.BaseEventLoop): sender = ChatActionSender.typing(bot=bot, chat_id=42) loop.call_soon(sender._close_event.set) start = time.monotonic() - await sender._wait(1) - assert time.monotonic() - start >= 1 + await sender._wait(self.initial_sleep) + assert time.monotonic() - start < self.initial_sleep + + async def test_wait_without_event(self, bot: Bot): + sender = ChatActionSender.typing(bot=bot, chat_id=42) + start = time.monotonic() + await sender._wait(self.initial_sleep) + assert time.monotonic() - start >= self.initial_sleep + + async def test_initial_sleep(self, bot: Bot): + start = time.monotonic() + async with ChatActionSender.typing(bot=bot, chat_id=42, initial_sleep=self.initial_sleep): + assert time.monotonic() - start >= self.initial_sleep @pytest.mark.parametrize( "action",