Skip to content

Commit 597161d

Browse files
authored
Fix flakey client functional keep alive tests (#10933)
1 parent 6b3672f commit 597161d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

tests/test_client_functional.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ async def handler(request: web.Request) -> web.Response:
260260
assert 0 == len(client._session.connector._conns)
261261

262262

263-
async def test_keepalive_timeout_async_sleep() -> None:
263+
async def test_keepalive_timeout_async_sleep(unused_port_socket: socket.socket) -> None:
264264
async def handler(request: web.Request) -> web.Response:
265265
body = await request.read()
266266
assert b"" == body
@@ -272,17 +272,18 @@ async def handler(request: web.Request) -> web.Response:
272272
runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
273273
await runner.setup()
274274

275-
port = unused_port()
276-
site = web.TCPSite(runner, host="localhost", port=port)
275+
site = web.SockSite(runner, unused_port_socket)
277276
await site.start()
278277

278+
host, port = unused_port_socket.getsockname()[:2]
279+
279280
try:
280281
async with aiohttp.ClientSession() as sess:
281-
resp1 = await sess.get(f"http://localhost:{port}/")
282+
resp1 = await sess.get(f"http://{host}:{port}/")
282283
await resp1.read()
283284
# wait for server keepalive_timeout
284285
await asyncio.sleep(0.01)
285-
resp2 = await sess.get(f"http://localhost:{port}/")
286+
resp2 = await sess.get(f"http://{host}:{port}/")
286287
await resp2.read()
287288
finally:
288289
await asyncio.gather(runner.shutdown(), site.stop())
@@ -292,7 +293,7 @@ async def handler(request: web.Request) -> web.Response:
292293
sys.version_info[:2] == (3, 11),
293294
reason="https://github.com/pytest-dev/pytest/issues/10763",
294295
)
295-
async def test_keepalive_timeout_sync_sleep() -> None:
296+
async def test_keepalive_timeout_sync_sleep(unused_port_socket: socket.socket) -> None:
296297
async def handler(request: web.Request) -> web.Response:
297298
body = await request.read()
298299
assert b"" == body
@@ -304,18 +305,19 @@ async def handler(request: web.Request) -> web.Response:
304305
runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
305306
await runner.setup()
306307

307-
port = unused_port()
308-
site = web.TCPSite(runner, host="localhost", port=port)
308+
site = web.SockSite(runner, unused_port_socket)
309309
await site.start()
310310

311+
host, port = unused_port_socket.getsockname()[:2]
312+
311313
try:
312314
async with aiohttp.ClientSession() as sess:
313-
resp1 = await sess.get(f"http://localhost:{port}/")
315+
resp1 = await sess.get(f"http://{host}:{port}/")
314316
await resp1.read()
315317
# wait for server keepalive_timeout
316318
# time.sleep is a more challenging scenario than asyncio.sleep
317319
time.sleep(0.01)
318-
resp2 = await sess.get(f"http://localhost:{port}/")
320+
resp2 = await sess.get(f"http://{host}:{port}/")
319321
await resp2.read()
320322
finally:
321323
await asyncio.gather(runner.shutdown(), site.stop())

0 commit comments

Comments
 (0)