Skip to content

Commit ddc2a26

Browse files
[PR #7896/9a7cfe77 backport][3.9] Fix some flaky tests (#7900)
**This is a backport of PR #7896 as merged into master (9a7cfe7).** Co-authored-by: Sam Bull <[email protected]>
1 parent 2ae4d6f commit ddc2a26

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

tests/test_web_server.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,11 @@ async def test_no_handler_cancellation(aiohttp_unused_port) -> None:
219219
timeout_event = asyncio.Event()
220220
done_event = asyncio.Event()
221221
port = aiohttp_unused_port()
222+
started = False
222223

223224
async def on_request(_: web.Request) -> web.Response:
224-
nonlocal done_event, timeout_event
225+
nonlocal done_event, started, timeout_event
226+
started = True
225227
await asyncio.wait_for(timeout_event.wait(), timeout=5)
226228
done_event.set()
227229
return web.Response()
@@ -238,7 +240,7 @@ async def on_request(_: web.Request) -> web.Response:
238240

239241
try:
240242
async with client.ClientSession(
241-
timeout=client.ClientTimeout(total=0.1)
243+
timeout=client.ClientTimeout(total=0.2)
242244
) as sess:
243245
with pytest.raises(asyncio.TimeoutError):
244246
await sess.get(f"http://localhost:{port}/")
@@ -247,6 +249,7 @@ async def on_request(_: web.Request) -> web.Response:
247249

248250
with suppress(asyncio.TimeoutError):
249251
await asyncio.wait_for(done_event.wait(), timeout=1)
252+
assert started
250253
assert done_event.is_set()
251254
finally:
252255
await asyncio.gather(runner.shutdown(), site.stop())

tests/test_web_urldispatcher.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ async def test_access_root_of_static_handler(
9393
client = await aiohttp_client(app)
9494

9595
# Request the root of the static directory.
96-
r = await client.get(prefix)
97-
assert r.status == status
96+
async with await client.get(prefix) as r:
97+
assert r.status == status
9898

99-
if data:
100-
assert r.headers["Content-Type"] == "text/html; charset=utf-8"
101-
read_ = await r.read()
102-
assert read_ == data
99+
if data:
100+
assert r.headers["Content-Type"] == "text/html; charset=utf-8"
101+
read_ = await r.read()
102+
assert read_ == data
103103

104104

105105
async def test_follow_symlink(

0 commit comments

Comments
 (0)