Skip to content

Commit

Permalink
Tweak test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Apr 25, 2024
1 parent b0e93c5 commit 26251da
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions tests/test_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,29 +567,24 @@ async def test_with_timeout(
monkeypatch: pytest.MonkeyPatch,
timeout: Optional[float],
) -> None:
"""Test write timeout.
Relates to this issue:
https://github.com/sysid/sse-starlette/issues/89
"""
"""Test that a timeout occurs when client is not reading responses."""
timeout_raised = False

async def frozen_write(_data: bytes) -> None:
await asyncio.sleep(42)
should_raise_timeout = timeout is not None

async def handler(request: web.Request) -> EventSourceResponse:
sse = EventSourceResponse(timeout=timeout)
sse.ping_interval = 42
await sse.prepare(request)
monkeypatch.setattr(sse, "write", frozen_write)

async with sse:
try:
await sse.send("foo")
except TimeoutError:
nonlocal timeout_raised
timeout_raised = True
raise
while True:
# .send() only yields if socket is full, so yield here to run client.
await asyncio.sleep(0)
try:
await sse.send("x" * 10000000) # Enough data to fill socket
except TimeoutError:
nonlocal timeout_raised
timeout_raised = True
break

return sse # pragma: no cover

Expand All @@ -600,6 +595,4 @@ async def handler(request: web.Request) -> EventSourceResponse:
async with client.get("/") as resp:
assert resp.status == 200
await asyncio.sleep(0.5)
assert resp.connection and resp.connection.closed is bool(timeout)

assert timeout_raised is bool(timeout)
assert timeout_raised is should_raise_timeout

0 comments on commit 26251da

Please sign in to comment.