Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions tests/test_client_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,13 @@ def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001


async def test_cancellation_propagates_cleanly() -> None:
first_chunk_consumed = asyncio.Event()

async def slow_body() -> typing.AsyncIterator[bytes]:
yield b"first"
await asyncio.sleep(1.0)
yield b"second" # pragma: no cover — the task is cancelled during the sleep before this yield
first_chunk_consumed.set()
await asyncio.Event().wait()
yield b"second" # pragma: no cover — the task is cancelled while the body waits before this yield

def handler(request: httpx2.Request) -> httpx2.Response:
return httpx2.Response(HTTPStatus.OK, request=request, content=slow_body())
Expand All @@ -178,7 +181,8 @@ async def consume() -> None:
pass

task = asyncio.create_task(consume())
await asyncio.sleep(0.01) # let body consumption begin
async with asyncio.timeout(5.0):
await first_chunk_consumed.wait()
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
Expand Down
Loading