Skip to content

Commit eb4b2d2

Browse files
authored
test: synchronize stream cancellation test on an event instead of a sleep (#188)
test_cancellation_propagates_cleanly slept 10 ms and assumed body consumption had begun and that the body's 1 s sleep was still running when it cancelled. A stall over 1 s let the body finish, and a cancel before consumption began still passed. The body now signals once the first chunk is consumed and then waits on an event that is never set.
1 parent c924aad commit eb4b2d2

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎tests/test_client_stream.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,13 @@ def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
162162

163163

164164
async def test_cancellation_propagates_cleanly() -> None:
165+
first_chunk_consumed = asyncio.Event()
166+
165167
async def slow_body() -> typing.AsyncIterator[bytes]:
166168
yield b"first"
167-
await asyncio.sleep(1.0)
168-
yield b"second" # pragma: no cover — the task is cancelled during the sleep before this yield
169+
first_chunk_consumed.set()
170+
await asyncio.Event().wait()
171+
yield b"second" # pragma: no cover — the task is cancelled while the body waits before this yield
169172

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

180183
task = asyncio.create_task(consume())
181-
await asyncio.sleep(0.01) # let body consumption begin
184+
async with asyncio.timeout(5.0):
185+
await first_chunk_consumed.wait()
182186
task.cancel()
183187
with pytest.raises(asyncio.CancelledError):
184188
await task

0 commit comments

Comments
 (0)