From ddc2a26c9e0c43fd1229e4424f2a30d1b10ced13 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 25 Nov 2023 21:26:54 +0000 Subject: [PATCH] [PR #7896/9a7cfe77 backport][3.9] Fix some flaky tests (#7900) **This is a backport of PR #7896 as merged into master (9a7cfe77623b9a61e4e58f425fff99529de2f795).** Co-authored-by: Sam Bull --- tests/test_web_server.py | 7 +++++-- tests/test_web_urldispatcher.py | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/test_web_server.py b/tests/test_web_server.py index 73e69831991..d0fd95acdb4 100644 --- a/tests/test_web_server.py +++ b/tests/test_web_server.py @@ -219,9 +219,11 @@ async def test_no_handler_cancellation(aiohttp_unused_port) -> None: timeout_event = asyncio.Event() done_event = asyncio.Event() port = aiohttp_unused_port() + started = False async def on_request(_: web.Request) -> web.Response: - nonlocal done_event, timeout_event + nonlocal done_event, started, timeout_event + started = True await asyncio.wait_for(timeout_event.wait(), timeout=5) done_event.set() return web.Response() @@ -238,7 +240,7 @@ async def on_request(_: web.Request) -> web.Response: try: async with client.ClientSession( - timeout=client.ClientTimeout(total=0.1) + timeout=client.ClientTimeout(total=0.2) ) as sess: with pytest.raises(asyncio.TimeoutError): await sess.get(f"http://localhost:{port}/") @@ -247,6 +249,7 @@ async def on_request(_: web.Request) -> web.Response: with suppress(asyncio.TimeoutError): await asyncio.wait_for(done_event.wait(), timeout=1) + assert started assert done_event.is_set() finally: await asyncio.gather(runner.shutdown(), site.stop()) diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py index 4fbf5b02ecc..8ca8dcd7b99 100644 --- a/tests/test_web_urldispatcher.py +++ b/tests/test_web_urldispatcher.py @@ -93,13 +93,13 @@ async def test_access_root_of_static_handler( client = await aiohttp_client(app) # Request the root of the static directory. - r = await client.get(prefix) - assert r.status == status + async with await client.get(prefix) as r: + assert r.status == status - if data: - assert r.headers["Content-Type"] == "text/html; charset=utf-8" - read_ = await r.read() - assert read_ == data + if data: + assert r.headers["Content-Type"] == "text/html; charset=utf-8" + read_ = await r.read() + assert read_ == data async def test_follow_symlink(