Skip to content

Commit 38c23ed

Browse files
authored
[PR #10933/597161d backport][3.12] Fix flakey client functional keep alive tests (#10937)
1 parent 11aaa23 commit 38c23ed

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

tests/test_client_functional.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ async def handler(request):
248248
assert 0 == len(client._session.connector._conns)
249249

250250

251-
async def test_keepalive_timeout_async_sleep() -> None:
252-
async def handler(request):
251+
async def test_keepalive_timeout_async_sleep(unused_port_socket: socket.socket) -> None:
252+
async def handler(request: web.Request) -> web.Response:
253253
body = await request.read()
254254
assert b"" == body
255255
return web.Response(body=b"OK")
@@ -260,17 +260,18 @@ async def handler(request):
260260
runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
261261
await runner.setup()
262262

263-
port = unused_port()
264-
site = web.TCPSite(runner, host="localhost", port=port)
263+
site = web.SockSite(runner, unused_port_socket)
265264
await site.start()
266265

266+
host, port = unused_port_socket.getsockname()[:2]
267+
267268
try:
268-
async with aiohttp.client.ClientSession() as sess:
269-
resp1 = await sess.get(f"http://localhost:{port}/")
269+
async with aiohttp.ClientSession() as sess:
270+
resp1 = await sess.get(f"http://{host}:{port}/")
270271
await resp1.read()
271272
# wait for server keepalive_timeout
272273
await asyncio.sleep(0.01)
273-
resp2 = await sess.get(f"http://localhost:{port}/")
274+
resp2 = await sess.get(f"http://{host}:{port}/")
274275
await resp2.read()
275276
finally:
276277
await asyncio.gather(runner.shutdown(), site.stop())
@@ -280,8 +281,8 @@ async def handler(request):
280281
sys.version_info[:2] == (3, 11),
281282
reason="https://github.com/pytest-dev/pytest/issues/10763",
282283
)
283-
async def test_keepalive_timeout_sync_sleep() -> None:
284-
async def handler(request):
284+
async def test_keepalive_timeout_sync_sleep(unused_port_socket: socket.socket) -> None:
285+
async def handler(request: web.Request) -> web.Response:
285286
body = await request.read()
286287
assert b"" == body
287288
return web.Response(body=b"OK")
@@ -292,18 +293,19 @@ async def handler(request):
292293
runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
293294
await runner.setup()
294295

295-
port = unused_port()
296-
site = web.TCPSite(runner, host="localhost", port=port)
296+
site = web.SockSite(runner, unused_port_socket)
297297
await site.start()
298298

299+
host, port = unused_port_socket.getsockname()[:2]
300+
299301
try:
300-
async with aiohttp.client.ClientSession() as sess:
301-
resp1 = await sess.get(f"http://localhost:{port}/")
302+
async with aiohttp.ClientSession() as sess:
303+
resp1 = await sess.get(f"http://{host}:{port}/")
302304
await resp1.read()
303305
# wait for server keepalive_timeout
304306
# time.sleep is a more challenging scenario than asyncio.sleep
305307
time.sleep(0.01)
306-
resp2 = await sess.get(f"http://localhost:{port}/")
308+
resp2 = await sess.get(f"http://{host}:{port}/")
307309
await resp2.read()
308310
finally:
309311
await asyncio.gather(runner.shutdown(), site.stop())

0 commit comments

Comments
 (0)