Skip to content

Commit

Permalink
[PR #7896/9a7cfe77 backport][3.9] Fix some flaky tests (#7900)
Browse files Browse the repository at this point in the history
**This is a backport of PR #7896 as merged into master
(9a7cfe7).**

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Nov 25, 2023
1 parent 2ae4d6f commit ddc2a26
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 5 additions & 2 deletions tests/test_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}/")
Expand All @@ -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())
12 changes: 6 additions & 6 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ddc2a26

Please sign in to comment.