Skip to content

Commit 9a7cfe7

Browse files
Fix some flaky tests (#7896)
1 parent fde031f commit 9a7cfe7

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
@@ -254,9 +254,11 @@ async def test_no_handler_cancellation(aiohttp_unused_port) -> None:
254254
timeout_event = asyncio.Event()
255255
done_event = asyncio.Event()
256256
port = aiohttp_unused_port()
257+
started = False
257258

258259
async def on_request(_: web.Request) -> web.Response:
259-
nonlocal done_event, timeout_event
260+
nonlocal done_event, started, timeout_event
261+
started = True
260262
await asyncio.wait_for(timeout_event.wait(), timeout=5)
261263
done_event.set()
262264
return web.Response()
@@ -273,7 +275,7 @@ async def on_request(_: web.Request) -> web.Response:
273275

274276
try:
275277
async with client.ClientSession(
276-
timeout=client.ClientTimeout(total=0.1)
278+
timeout=client.ClientTimeout(total=0.2)
277279
) as sess:
278280
with pytest.raises(asyncio.TimeoutError):
279281
await sess.get(f"http://localhost:{port}/")
@@ -282,6 +284,7 @@ async def on_request(_: web.Request) -> web.Response:
282284

283285
with suppress(asyncio.TimeoutError):
284286
await asyncio.wait_for(done_event.wait(), timeout=1)
287+
assert started
285288
assert done_event.is_set()
286289
finally:
287290
await asyncio.gather(runner.shutdown(), site.stop())

tests/test_web_urldispatcher.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ async def test_access_root_of_static_handler(
7171
client = await aiohttp_client(app)
7272

7373
# Request the root of the static directory.
74-
r = await client.get(prefix)
75-
assert r.status == status
74+
async with await client.get(prefix) as r:
75+
assert r.status == status
7676

77-
if data:
78-
assert r.headers["Content-Type"] == "text/html; charset=utf-8"
79-
read_ = await r.read()
80-
assert read_ == data
77+
if data:
78+
assert r.headers["Content-Type"] == "text/html; charset=utf-8"
79+
read_ = await r.read()
80+
assert read_ == data
8181

8282

8383
async def test_follow_symlink(

0 commit comments

Comments
 (0)