|
8 | 8 | from anyio.abc import TaskGroup |
9 | 9 | from inline_snapshot import snapshot |
10 | 10 | from pydantic import AnyUrl |
| 11 | +from sse_starlette.sse import AppStatus |
11 | 12 | from starlette.applications import Starlette |
12 | 13 | from starlette.requests import Request |
13 | 14 | from starlette.responses import Response |
|
32 | 33 | Tool, |
33 | 34 | ) |
34 | 35 |
|
| 36 | + |
| 37 | +@pytest.fixture(autouse=True) |
| 38 | +def reset_sse_app_status(): |
| 39 | + """Reset sse-starlette's global AppStatus singleton before each test. |
| 40 | +
|
| 41 | + This is necessary because AppStatus.should_exit_event (a global anyio.Event) gets bound |
| 42 | + to one event loop but accessed from others during parallel test execution (xdist workers), |
| 43 | + causing RuntimeError("bound to a different event loop"), which prevents the SSE server |
| 44 | + from responding (leaving status at 499) and causes ClosedResourceError during teardown. |
| 45 | + """ |
| 46 | + AppStatus.should_exit_event = anyio.Event() |
| 47 | + |
| 48 | + |
35 | 49 | SERVER_NAME = "test_server_for_SSE" |
36 | 50 | TEST_SERVER_HOST = "testserver" |
37 | 51 | TEST_SERVER_BASE_URL = f"http://{TEST_SERVER_HOST}" |
@@ -106,8 +120,6 @@ async def handle_sse(request: Request) -> Response: |
106 | 120 |
|
107 | 121 |
|
108 | 122 | # Test fixtures |
109 | | - |
110 | | - |
111 | 123 | @pytest.fixture() |
112 | 124 | def server_app() -> Starlette: |
113 | 125 | """Create test Starlette app with SSE transport""" |
@@ -243,7 +255,6 @@ async def sse_client_mounted_server_app_session( |
243 | 255 |
|
244 | 256 | async with sse_client( |
245 | 257 | f"{TEST_SERVER_BASE_URL}/mounted_app/sse", |
246 | | - sse_read_timeout=0.5, |
247 | 258 | httpx_client_factory=asgi_client_factory, |
248 | 259 | ) as streams: |
249 | 260 | async with ClientSession(*streams) as session: |
@@ -329,7 +340,6 @@ async def test_request_context_propagation(tg: TaskGroup, context_server_app: St |
329 | 340 | f"{TEST_SERVER_BASE_URL}/sse", |
330 | 341 | headers=custom_headers, |
331 | 342 | httpx_client_factory=asgi_client_factory, |
332 | | - sse_read_timeout=0.5, |
333 | 343 | ) as streams: |
334 | 344 | async with ClientSession(*streams) as session: |
335 | 345 | # Initialize the session |
|
0 commit comments