Skip to content

Commit 9843353

Browse files
committed
test/refactor: use subprocess client
1 parent 917f3b4 commit 9843353

2 files changed

Lines changed: 58 additions & 21 deletions

File tree

tests/unit/test_response/demo.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
from collections.abc import AsyncIterator
3+
4+
import anyio
5+
6+
from litestar import Litestar, get
7+
from litestar.response import ServerSentEvent
8+
from litestar.response.streaming import ClientDisconnectError
9+
10+
11+
@get("/notify/{topic:str}")
12+
async def get_notified(topic: str) -> ServerSentEvent:
13+
file = await anyio.NamedTemporaryFile("w", suffix=topic, prefix="test").__aenter__()
14+
await file.write(topic)
15+
16+
async def generator() -> AsyncIterator[str]:
17+
try:
18+
yield topic
19+
except ClientDisconnectError:
20+
await file.__aexit__(None, None, None)
21+
while True:
22+
await asyncio.sleep(0.1)
23+
24+
return ServerSentEvent(generator(), event_type="Notifier")
25+
26+
27+
def create_test_app() -> Litestar:
28+
return Litestar(
29+
route_handlers=[get_notified],
30+
)
31+
32+
33+
app = create_test_app()

tests/unit/test_response/test_sse.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import pathlib
12
from collections.abc import AsyncIterator, Iterator
23

34
import anyio
5+
import httpx
46
import pytest
57
from httpx_sse import ServerSentEvent as HTTPXServerSentEvent
68
from httpx_sse import aconnect_sse
@@ -9,9 +11,18 @@
911
from litestar.exceptions import ImproperlyConfiguredException
1012
from litestar.response import ServerSentEvent
1113
from litestar.response.sse import ServerSentEventMessage
12-
from litestar.testing import create_async_test_client
14+
from litestar.testing import create_async_test_client, subprocess_async_client
1315
from litestar.types import SSEData
1416

17+
ROOT = pathlib.Path(__file__).parent
18+
APP = "demo:app"
19+
20+
21+
@pytest.fixture(name="async_client")
22+
async def fx_async_client() -> AsyncIterator[httpx.AsyncClient]:
23+
async with subprocess_async_client(workdir=ROOT, app=APP) as client:
24+
yield client
25+
1526

1627
async def test_sse_steaming_response() -> None:
1728
@get(
@@ -96,28 +107,21 @@ async def numbers() -> AsyncIterator[SSEData]:
96107
assert events[i].retry == expected_events[i].retry
97108

98109

99-
async def test_sse_cleanup() -> None:
100-
shared_state = []
110+
async def test_sse_cleanup(async_client: httpx.AsyncClient) -> None:
111+
topic = "cleanup"
101112

102-
@get("/testme")
103-
async def handler() -> ServerSentEvent:
104-
async def numbers() -> AsyncIterator[SSEData]:
105-
try:
106-
yield 0
107-
await anyio.sleep(5)
108-
yield 1
109-
finally:
110-
shared_state.append(42)
111-
112-
return ServerSentEvent(numbers(), event_type="special", event_id="123", retry_duration=1000)
113-
114-
async with create_async_test_client(handler) as client:
115-
async with aconnect_sse(client, "GET", f"{client.base_url}/testme") as event_source:
116-
async for sse in event_source.aiter_sse():
117-
assert sse.data == "0"
118-
break
113+
async with (
114+
anyio.NamedTemporaryFile("r", suffix=topic, prefix="test") as file,
115+
async_client as client,
116+
aconnect_sse(client, "GET", f"notify/{topic}") as event_source,
117+
):
118+
file_path = pathlib.Path(str(file.name))
119+
assert not file_path.exists()
120+
async for sse in event_source.aiter_sse():
121+
assert sse.data == topic
122+
assert file_path.read_text() == topic
119123

120-
assert shared_state.pop() == 42
124+
assert not file_path.exists()
121125

122126

123127
def test_invalid_content_type_raises() -> None:

0 commit comments

Comments
 (0)