@@ -248,8 +248,8 @@ async def handler(request):
248
248
assert 0 == len (client ._session .connector ._conns )
249
249
250
250
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 :
253
253
body = await request .read ()
254
254
assert b"" == body
255
255
return web .Response (body = b"OK" )
@@ -260,17 +260,18 @@ async def handler(request):
260
260
runner = web .AppRunner (app , tcp_keepalive = True , keepalive_timeout = 0.001 )
261
261
await runner .setup ()
262
262
263
- port = unused_port ()
264
- site = web .TCPSite (runner , host = "localhost" , port = port )
263
+ site = web .SockSite (runner , unused_port_socket )
265
264
await site .start ()
266
265
266
+ host , port = unused_port_socket .getsockname ()[:2 ]
267
+
267
268
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 } /" )
270
271
await resp1 .read ()
271
272
# wait for server keepalive_timeout
272
273
await asyncio .sleep (0.01 )
273
- resp2 = await sess .get (f"http://localhost :{ port } /" )
274
+ resp2 = await sess .get (f"http://{ host } :{ port } /" )
274
275
await resp2 .read ()
275
276
finally :
276
277
await asyncio .gather (runner .shutdown (), site .stop ())
@@ -280,8 +281,8 @@ async def handler(request):
280
281
sys .version_info [:2 ] == (3 , 11 ),
281
282
reason = "https://github.com/pytest-dev/pytest/issues/10763" ,
282
283
)
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 :
285
286
body = await request .read ()
286
287
assert b"" == body
287
288
return web .Response (body = b"OK" )
@@ -292,18 +293,19 @@ async def handler(request):
292
293
runner = web .AppRunner (app , tcp_keepalive = True , keepalive_timeout = 0.001 )
293
294
await runner .setup ()
294
295
295
- port = unused_port ()
296
- site = web .TCPSite (runner , host = "localhost" , port = port )
296
+ site = web .SockSite (runner , unused_port_socket )
297
297
await site .start ()
298
298
299
+ host , port = unused_port_socket .getsockname ()[:2 ]
300
+
299
301
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 } /" )
302
304
await resp1 .read ()
303
305
# wait for server keepalive_timeout
304
306
# time.sleep is a more challenging scenario than asyncio.sleep
305
307
time .sleep (0.01 )
306
- resp2 = await sess .get (f"http://localhost :{ port } /" )
308
+ resp2 = await sess .get (f"http://{ host } :{ port } /" )
307
309
await resp2 .read ()
308
310
finally :
309
311
await asyncio .gather (runner .shutdown (), site .stop ())
0 commit comments