|
5 | 5 | import anyio |
6 | 6 | import httpx |
7 | 7 | import pytest |
8 | | -import sse_starlette |
9 | 8 | from anyio.abc import TaskGroup |
10 | 9 | from inline_snapshot import snapshot |
11 | | -from packaging import version |
12 | 10 | from pydantic import AnyUrl |
13 | 11 | from starlette.applications import Starlette |
14 | 12 | from starlette.requests import Request |
|
34 | 32 | Tool, |
35 | 33 | ) |
36 | 34 |
|
37 | | -SSE_STARLETTE_VERSION = version.parse(sse_starlette.__version__) |
38 | | -NEEDS_RESET = SSE_STARLETTE_VERSION < version.parse("3.0.0") |
39 | | - |
40 | | - |
41 | | -@pytest.fixture(autouse=True) |
42 | | -def reset_sse_app_status(): |
43 | | - """Reset sse-starlette's global AppStatus singleton before each test. |
44 | | -
|
45 | | - AppStatus.should_exit_event is a global asyncio.Event that gets bound to |
46 | | - an event loop. This ensures each test gets a fresh Event and prevents |
47 | | - RuntimeError("bound to a different event loop") during parallel test |
48 | | - execution with pytest-xdist. |
49 | | -
|
50 | | - NOTE: This fixture is only necessary for sse-starlette < 3.0.0. |
51 | | - Version 3.0+ eliminated the global state issue entirely by using |
52 | | - context-local events instead of module-level singletons, providing |
53 | | - automatic test isolation without manual cleanup. |
54 | | -
|
55 | | - See <https://github.com/sysid/sse-starlette/pull/141> for more details. |
56 | | - """ |
57 | | - if not NEEDS_RESET: |
58 | | - yield |
59 | | - return |
60 | | - |
61 | | - # lazy import to avoid import errors |
62 | | - from sse_starlette.sse import AppStatus |
63 | | - |
64 | | - # Setup: Reset before test |
65 | | - AppStatus.should_exit_event = anyio.Event() # type: ignore[attr-defined] |
66 | | - |
67 | | - yield |
68 | | - |
69 | | - # Teardown: Reset after test to prevent contamination |
70 | | - AppStatus.should_exit_event = anyio.Event() # type: ignore[attr-defined] |
71 | | - |
72 | | - |
73 | 35 | SERVER_NAME = "test_server_for_SSE" |
74 | 36 | TEST_SERVER_HOST = "testserver" |
75 | 37 | TEST_SERVER_BASE_URL = f"http://{TEST_SERVER_HOST}" |
|
0 commit comments