Skip to content

[PR #10933/597161d backport][3.12] Fix flakey client functional keep alive tests #10937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ async def handler(request):
assert 0 == len(client._session.connector._conns)


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

port = unused_port()
site = web.TCPSite(runner, host="localhost", port=port)
site = web.SockSite(runner, unused_port_socket)
await site.start()

host, port = unused_port_socket.getsockname()[:2]

try:
async with aiohttp.client.ClientSession() as sess:
resp1 = await sess.get(f"http://localhost:{port}/")
async with aiohttp.ClientSession() as sess:
resp1 = await sess.get(f"http://{host}:{port}/")
await resp1.read()
# wait for server keepalive_timeout
await asyncio.sleep(0.01)
resp2 = await sess.get(f"http://localhost:{port}/")
resp2 = await sess.get(f"http://{host}:{port}/")
await resp2.read()
finally:
await asyncio.gather(runner.shutdown(), site.stop())
Expand All @@ -280,8 +281,8 @@ async def handler(request):
sys.version_info[:2] == (3, 11),
reason="https://github.com/pytest-dev/pytest/issues/10763",
)
async def test_keepalive_timeout_sync_sleep() -> None:
async def handler(request):
async def test_keepalive_timeout_sync_sleep(unused_port_socket: socket.socket) -> None:
async def handler(request: web.Request) -> web.Response:
body = await request.read()
assert b"" == body
return web.Response(body=b"OK")
Expand All @@ -292,18 +293,19 @@ async def handler(request):
runner = web.AppRunner(app, tcp_keepalive=True, keepalive_timeout=0.001)
await runner.setup()

port = unused_port()
site = web.TCPSite(runner, host="localhost", port=port)
site = web.SockSite(runner, unused_port_socket)
await site.start()

host, port = unused_port_socket.getsockname()[:2]

try:
async with aiohttp.client.ClientSession() as sess:
resp1 = await sess.get(f"http://localhost:{port}/")
async with aiohttp.ClientSession() as sess:
resp1 = await sess.get(f"http://{host}:{port}/")
await resp1.read()
# wait for server keepalive_timeout
# time.sleep is a more challenging scenario than asyncio.sleep
time.sleep(0.01)
resp2 = await sess.get(f"http://localhost:{port}/")
resp2 = await sess.get(f"http://{host}:{port}/")
await resp2.read()
finally:
await asyncio.gather(runner.shutdown(), site.stop())
Expand Down
Loading