Skip to content

Commit 0f513d2

Browse files
Remove signal testing order dependency (#2382)
* test received signal * improve error feedback, do not shadow earlier failures * fix import sorting --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent ff54b02 commit 0f513d2

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

tests/supervisors/test_signal.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import httpx
66
import pytest
77

8-
from tests.utils import run_server
8+
from tests.utils import assert_signal, run_server
99
from uvicorn import Server
1010
from uvicorn.config import Config
1111

@@ -31,8 +31,8 @@ async def wait_app(scope, receive, send):
3131

3232
config = Config(app=wait_app, reload=False, port=unused_tcp_port, timeout_graceful_shutdown=1)
3333
server: Server
34-
async with run_server(config) as server:
35-
async with httpx.AsyncClient() as client:
34+
with assert_signal(signal.SIGINT):
35+
async with run_server(config) as server, httpx.AsyncClient() as client:
3636
req = asyncio.create_task(client.get(f"http://127.0.0.1:{unused_tcp_port}"))
3737
await asyncio.sleep(0.1) # ensure next tick
3838
server.handle_exit(sig=signal.SIGINT, frame=None) # exit
@@ -64,8 +64,8 @@ async def forever_app(scope, receive, send):
6464

6565
config = Config(app=forever_app, reload=False, port=unused_tcp_port, timeout_graceful_shutdown=1)
6666
server: Server
67-
async with run_server(config) as server:
68-
async with httpx.AsyncClient() as client:
67+
with assert_signal(signal.SIGINT):
68+
async with run_server(config) as server, httpx.AsyncClient() as client:
6969
req = asyncio.create_task(client.get(f"http://127.0.0.1:{unused_tcp_port}"))
7070
await asyncio.sleep(0.1) # next tick
7171
# trigger exit, this request should time out in ~1 sec
@@ -94,10 +94,10 @@ async def app(scope, receive, send):
9494

9595
config = Config(app=app, reload=False, port=unused_tcp_port, timeout_graceful_shutdown=1)
9696
server: Server
97-
async with run_server(config) as server:
98-
# exit and ensure we do not accept more requests
99-
server.handle_exit(sig=signal.SIGINT, frame=None)
100-
await asyncio.sleep(0.1) # next tick
101-
async with httpx.AsyncClient() as client:
97+
with assert_signal(signal.SIGINT):
98+
async with run_server(config) as server, httpx.AsyncClient() as client:
99+
# exit and ensure we do not accept more requests
100+
server.handle_exit(sig=signal.SIGINT, frame=None)
101+
await asyncio.sleep(0.1) # next tick
102102
with pytest.raises(httpx.ConnectError):
103103
await client.get(f"http://127.0.0.1:{unused_tcp_port}")

tests/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import os
5+
import signal
56
from collections.abc import AsyncIterator
67
from contextlib import asynccontextmanager, contextmanager
78
from pathlib import Path
@@ -22,6 +23,18 @@ async def run_server(config: Config, sockets: list[socket] | None = None) -> Asy
2223
task.cancel()
2324

2425

26+
@contextmanager
27+
def assert_signal(sig: signal.Signals):
28+
"""Check that a signal was received and handled in a block"""
29+
seen: set[int] = set()
30+
prev_handler = signal.signal(sig, lambda num, frame: seen.add(num))
31+
try:
32+
yield
33+
assert sig in seen, f"process signal {signal.Signals(sig)!r} was not received or handled"
34+
finally:
35+
signal.signal(sig, prev_handler)
36+
37+
2538
@contextmanager
2639
def as_cwd(path: Path):
2740
"""Changes working directory and returns to previous on exit."""

0 commit comments

Comments
 (0)