Skip to content

Commit e70b0e4

Browse files
committed
NOMERGE: Test that MemoryObjectSendStream.send_nowait() raises WouldBlock when called immediately after cancelling receive()
1 parent 10bcc86 commit e70b0e4

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

Diff for: tests/streams/test_memory.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
create_memory_object_stream,
1616
create_task_group,
1717
fail_after,
18+
get_cancelled_exc_class,
1819
wait_all_tasks_blocked,
1920
)
2021
from anyio.abc import ObjectReceiveStream, ObjectSendStream, TaskStatus
@@ -326,34 +327,27 @@ async def scoped_receiver(task_status: TaskStatus[CancelScope]) -> None:
326327

327328
async def test_cancel_during_receive_before_send_nowait() -> None:
328329
"""
329-
Test that cancelling a pending receive() operation does not cause an item about to
330-
be placed into the stream by send_nowait() to be lost.
331-
332-
Note: AnyIO's memory stream behavior here currently differs slightly from Trio's
333-
memory channel behavior. This test is intended only as a regression test for the bug
334-
where AnyIO dropped items in this situation; addressing the (possible) issue where
335-
AnyIO behaves slightly differently from Trio in this situation will involve
336-
modifying this test. See #728.
330+
Test that cancelling a pending receive() operation causes a send_nowait() call made
331+
before the receive() call has raised to raise WouldBlock.
337332
338333
"""
339334

340335
async def scoped_receiver(task_status: TaskStatus[CancelScope]) -> None:
341336
with CancelScope() as receiver_scope:
342337
task_status.started(receiver_scope)
343-
received.append(await receive.receive())
338+
with pytest.raises(get_cancelled_exc_class()):
339+
await receive.receive()
344340

345341
assert receiver_scope.cancel_called
346342

347-
received: list[str] = []
348343
send, receive = create_memory_object_stream[str]()
349344
with send, receive:
350345
async with create_task_group() as tg:
351346
receiver_scope = await tg.start(scoped_receiver)
352347
await wait_all_tasks_blocked()
353348
receiver_scope.cancel()
354-
send.send_nowait("hello")
355-
356-
assert received == ["hello"]
349+
with pytest.raises(WouldBlock):
350+
send.send_nowait("hello")
357351

358352

359353
async def test_close_receive_after_send() -> None:

0 commit comments

Comments
 (0)