|
15 | 15 | create_memory_object_stream,
|
16 | 16 | create_task_group,
|
17 | 17 | fail_after,
|
| 18 | + get_cancelled_exc_class, |
18 | 19 | wait_all_tasks_blocked,
|
19 | 20 | )
|
20 | 21 | from anyio.abc import ObjectReceiveStream, ObjectSendStream, TaskStatus
|
@@ -326,34 +327,27 @@ async def scoped_receiver(task_status: TaskStatus[CancelScope]) -> None:
|
326 | 327 |
|
327 | 328 | async def test_cancel_during_receive_before_send_nowait() -> None:
|
328 | 329 | """
|
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. |
337 | 332 |
|
338 | 333 | """
|
339 | 334 |
|
340 | 335 | async def scoped_receiver(task_status: TaskStatus[CancelScope]) -> None:
|
341 | 336 | with CancelScope() as receiver_scope:
|
342 | 337 | task_status.started(receiver_scope)
|
343 |
| - received.append(await receive.receive()) |
| 338 | + with pytest.raises(get_cancelled_exc_class()): |
| 339 | + await receive.receive() |
344 | 340 |
|
345 | 341 | assert receiver_scope.cancel_called
|
346 | 342 |
|
347 |
| - received: list[str] = [] |
348 | 343 | send, receive = create_memory_object_stream[str]()
|
349 | 344 | with send, receive:
|
350 | 345 | async with create_task_group() as tg:
|
351 | 346 | receiver_scope = await tg.start(scoped_receiver)
|
352 | 347 | await wait_all_tasks_blocked()
|
353 | 348 | receiver_scope.cancel()
|
354 |
| - send.send_nowait("hello") |
355 |
| - |
356 |
| - assert received == ["hello"] |
| 349 | + with pytest.raises(WouldBlock): |
| 350 | + send.send_nowait("hello") |
357 | 351 |
|
358 | 352 |
|
359 | 353 | async def test_close_receive_after_send() -> None:
|
|
0 commit comments