From 095820959ce7dd18350fb1fb818cf2810181ddee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBy=C5=BAniewski?= Date: Wed, 22 Jan 2025 17:22:36 +0100 Subject: [PATCH 1/6] test: use asynccontextmanager for FastAPI lifespan --- tests/unit_tests/test_axon.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit_tests/test_axon.py b/tests/unit_tests/test_axon.py index 5dac992d21..20fdee4369 100644 --- a/tests/unit_tests/test_axon.py +++ b/tests/unit_tests/test_axon.py @@ -16,6 +16,7 @@ # DEALINGS IN THE SOFTWARE. +import contextlib import re import threading import time @@ -795,6 +796,7 @@ async def test_threaded_fastapi(): server_started = threading.Event() server_stopped = threading.Event() + @contextlib.asynccontextmanager async def lifespan(app): server_started.set() yield From 321d359a14c1fc7b3fff5debeb206eee6da23cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBy=C5=BAniewski?= Date: Wed, 22 Jan 2025 18:11:30 +0100 Subject: [PATCH 2/6] test: wait for uvicorn server to start completly --- tests/unit_tests/test_axon.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit_tests/test_axon.py b/tests/unit_tests/test_axon.py index 20fdee4369..47533a2487 100644 --- a/tests/unit_tests/test_axon.py +++ b/tests/unit_tests/test_axon.py @@ -16,6 +16,7 @@ # DEALINGS IN THE SOFTWARE. +import asyncio import contextlib import re import threading @@ -816,6 +817,9 @@ async def lifespan(app): server_started.wait() + while not (server.started or server_stopped.is_set()): + await asyncio.sleep(1) + assert server.is_running is True async with aiohttp.ClientSession( From e6d00ad17f94d0e06ab147a19caf91ae9db873ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBy=C5=BAniewski?= Date: Mon, 3 Feb 2025 12:40:28 +0100 Subject: [PATCH 3/6] tests: add timeouts --- tests/unit_tests/test_axon.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/unit_tests/test_axon.py b/tests/unit_tests/test_axon.py index 47533a2487..88c6bb2d7d 100644 --- a/tests/unit_tests/test_axon.py +++ b/tests/unit_tests/test_axon.py @@ -815,10 +815,11 @@ async def lifespan(app): ) server.start() - server_started.wait() + server_started.wait(3.0) - while not (server.started or server_stopped.is_set()): - await asyncio.sleep(1) + async with asyncio.timeout(3.0): + while not (server.started or server_stopped.is_set()): + await asyncio.sleep(1.0) assert server.is_running is True From 52558f59dd48fa109ebb69cef8ac192d82c7f477 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 3 Feb 2025 14:21:05 +0200 Subject: [PATCH 4/6] Python 3.9 compatibility. --- tests/unit_tests/test_axon.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_axon.py b/tests/unit_tests/test_axon.py index 88c6bb2d7d..0363dc0135 100644 --- a/tests/unit_tests/test_axon.py +++ b/tests/unit_tests/test_axon.py @@ -817,10 +817,12 @@ async def lifespan(app): server_started.wait(3.0) - async with asyncio.timeout(3.0): + async def wait_for_server(): while not (server.started or server_stopped.is_set()): await asyncio.sleep(1.0) + await asyncio.wait_for(wait_for_server(), 7.0) + assert server.is_running is True async with aiohttp.ClientSession( From 9a5b538e1be950aac7202ea32095def854d28268 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 3 Feb 2025 14:24:24 +0200 Subject: [PATCH 5/6] Compatibility if uvloop is installed. --- tests/unit_tests/test_axon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit_tests/test_axon.py b/tests/unit_tests/test_axon.py index 0363dc0135..bca8f2a954 100644 --- a/tests/unit_tests/test_axon.py +++ b/tests/unit_tests/test_axon.py @@ -811,6 +811,7 @@ async def lifespan(app): server = FastAPIThreadedServer( uvicorn.Config( app, + loop="none" ), ) server.start() From 7fe707659dfc767233b9a21f7e4792f2887e5694 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 3 Feb 2025 14:26:36 +0200 Subject: [PATCH 6/6] Ruff --- tests/unit_tests/test_axon.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/unit_tests/test_axon.py b/tests/unit_tests/test_axon.py index bca8f2a954..5415999d2a 100644 --- a/tests/unit_tests/test_axon.py +++ b/tests/unit_tests/test_axon.py @@ -809,10 +809,7 @@ async def lifespan(app): app.get("/")(lambda: "Hello World") server = FastAPIThreadedServer( - uvicorn.Config( - app, - loop="none" - ), + uvicorn.Config(app, loop="none"), ) server.start()