From 8e7c5ed9dad2237834d8b4bfd3d6a890af38a0b3 Mon Sep 17 00:00:00 2001 From: Nachtalb Date: Fri, 3 Nov 2023 19:54:55 +0100 Subject: [PATCH] Fix test due to asyncio task scheduling race condition --- tests/test_webhook/test_aiohtt_server.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_webhook/test_aiohtt_server.py b/tests/test_webhook/test_aiohtt_server.py index 4e3f6658..f29895ba 100644 --- a/tests/test_webhook/test_aiohtt_server.py +++ b/tests/test_webhook/test_aiohtt_server.py @@ -181,10 +181,20 @@ class TestSimpleRequestHandler: "aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request", new_callable=AsyncMock, ) as mocked_silent_call_request: + method_called_event = asyncio.Event() + mocked_silent_call_request.side_effect = ( + lambda *args, **kwargs: method_called_event.set() + ) + handler_event.clear() resp = await self.make_reqest(client=client) assert resp.status == 200 await asyncio.wait_for(handler_event.wait(), timeout=1) + await asyncio.wait_for(method_called_event.wait(), timeout=1) + # Python 3.12 had some changes to asyncio which make it quite a bit faster. But + # probably because of that the assert_awaited call is consistently scheduled before the + # silent_call_request call - failing the test. So we wait for the method to be called + # before asserting if it has been awaited. mocked_silent_call_request.assert_awaited() result = await resp.json() assert not result