Skip to content

Commit

Permalink
NOMERGE: Test that MemoryObjectSendStream.send_nowait() raises WouldB…
Browse files Browse the repository at this point in the history
…lock when called immediately after cancelling receive()
  • Loading branch information
gschaffner committed May 10, 2024
1 parent 10bcc86 commit e70b0e4
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions tests/streams/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
create_memory_object_stream,
create_task_group,
fail_after,
get_cancelled_exc_class,
wait_all_tasks_blocked,
)
from anyio.abc import ObjectReceiveStream, ObjectSendStream, TaskStatus
Expand Down Expand Up @@ -326,34 +327,27 @@ async def scoped_receiver(task_status: TaskStatus[CancelScope]) -> None:

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

async def scoped_receiver(task_status: TaskStatus[CancelScope]) -> None:
with CancelScope() as receiver_scope:
task_status.started(receiver_scope)
received.append(await receive.receive())
with pytest.raises(get_cancelled_exc_class()):
await receive.receive()

assert receiver_scope.cancel_called

received: list[str] = []
send, receive = create_memory_object_stream[str]()
with send, receive:
async with create_task_group() as tg:
receiver_scope = await tg.start(scoped_receiver)
await wait_all_tasks_blocked()
receiver_scope.cancel()
send.send_nowait("hello")

assert received == ["hello"]
with pytest.raises(WouldBlock):
send.send_nowait("hello")


async def test_close_receive_after_send() -> None:
Expand Down

0 comments on commit e70b0e4

Please sign in to comment.