From 1722e94564f9bdd3da98e3e13ddf099122fb0ccd Mon Sep 17 00:00:00 2001 From: Jean Hominal Date: Sat, 2 Jul 2022 08:07:41 +0200 Subject: [PATCH] make http_disconnect_while_sending test more robust in the face of scheduling issues --- tests/middleware/test_base.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/middleware/test_base.py b/tests/middleware/test_base.py index 7519fdf1d4..8c110ca5d5 100644 --- a/tests/middleware/test_base.py +++ b/tests/middleware/test_base.py @@ -288,13 +288,18 @@ async def cancel_on_disconnect(): task_group.start_soon(cancel_on_disconnect) - await send( - { - "type": "http.response.body", - "body": b"chunk", - "more_body": True, - } - ) + # A timeout is set for 0.1 second in order to ensure that + # cancel_on_disconnect is scheduled by the event loop + with anyio.move_on_after(0.1): + while True: + await send( + { + "type": "http.response.body", + "body": b"chunk ", + "more_body": True, + } + ) + pytest.fail( "http.disconnect should have been received and canceled the scope" ) # pragma: no cover