Skip to content
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

#7297 Add tests for closed keepalive connections #7308

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 59 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pathlib
import socket
import ssl
import time
from typing import Any, AsyncIterator
from unittest import mock

Expand Down Expand Up @@ -126,6 +127,64 @@ async def handler(request):
assert 0 == len(client._session.connector._conns)


async def test_keepalive_timeout_async_sleep() -> None:
async def handler(request):
body = await request.read()
assert b"" == body
return web.Response(body=b"OK")

app = web.Application()
app.router.add_route("GET", "/", handler)

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)
await site.start()

try:
async with aiohttp.client.ClientSession() as sess:
resp1 = await sess.get(f"http://localhost:{port}/")
await resp1.read()
# wait for server keepalive_timeout
await asyncio.sleep(0.01)
resp2 = await sess.get(f"http://localhost:{port}/")
await resp2.read()
finally:
await asyncio.gather(runner.shutdown(), site.stop())


@pytest.mark.xfail(reason="Reproducer for #7297")
async def test_keepalive_timeout_sync_sleep() -> None:
async def handler(request):
body = await request.read()
assert b"" == body
return web.Response(body=b"OK")

app = web.Application()
app.router.add_route("GET", "/", handler)

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)
await site.start()

try:
async with aiohttp.client.ClientSession() as sess:
resp1 = await sess.get(f"http://localhost:{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}/")
await resp2.read()
finally:
await asyncio.gather(runner.shutdown(), site.stop())


async def test_release_early(aiohttp_client: Any) -> None:
async def handler(request):
await request.read()
Expand Down