Skip to content

Commit

Permalink
Disabling retry_persistent_connection in Tests (#9396)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhAgarwal-dev authored Oct 6, 2024
1 parent 21f5f92 commit 6d8562d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES/9141.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Disabled automatic retries of failed requests in :class:`aiohttp.test_utils.TestClient`'s client session
(which could potentially hide errors in tests) -- by :user:`ShubhAgarwal-dev`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ Sergey Skripnick
Serhii Charykov
Serhii Kostel
Serhiy Storchaka
Shubh Agarwal
Simon Kennedy
Sin-Woo Bang
Stanislas Plum
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class ClientSession:
"_resolve_charset",
"_default_proxy",
"_default_proxy_auth",
"_retry_connection",
)

def __init__(
Expand Down Expand Up @@ -367,6 +368,7 @@ def __init__(

self._default_proxy = proxy
self._default_proxy_auth = proxy_auth
self._retry_connection: bool = True

def __init_subclass__(cls: Type["ClientSession"]) -> None:
raise TypeError(
Expand Down Expand Up @@ -539,7 +541,9 @@ async def _request(
try:
with timer:
# https://www.rfc-editor.org/rfc/rfc9112.html#name-retrying-requests
retry_persistent_connection = method in IDEMPOTENT_METHODS
retry_persistent_connection = (
self._retry_connection and method in IDEMPOTENT_METHODS
)
while True:
url, auth_from_url = strip_auth_from_url(url)
if not url.raw_host:
Expand Down
1 change: 1 addition & 0 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def __init__( # type: ignore[misc]
if cookie_jar is None:
cookie_jar = aiohttp.CookieJar(unsafe=True)
self._session = ClientSession(cookie_jar=cookie_jar, **kwargs)
self._session._retry_connection = False
self._closed = False
self._responses: List[ClientResponse] = []
self._websockets: List[ClientWebSocketResponse] = []
Expand Down
22 changes: 22 additions & 0 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import aiohttp
from aiohttp import web
from aiohttp.pytest_plugin import AiohttpClient
from aiohttp.test_utils import (
AioHTTPTestCase,
RawTestServer,
Expand Down Expand Up @@ -316,6 +317,27 @@ def test_noop(self) -> None:
result.stdout.fnmatch_lines(["*TypeError*"])


async def test_disable_retry_persistent_connection(
aiohttp_client: AiohttpClient,
) -> None:
num_requests = 0

async def handler(request: web.Request) -> web.Response:
nonlocal num_requests

num_requests += 1
request.protocol.force_close()
return web.Response()

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)
with pytest.raises(aiohttp.ServerDisconnectedError):
await client.get("/")

assert num_requests == 1


async def test_server_context_manager(
app: web.Application, loop: asyncio.AbstractEventLoop
) -> None:
Expand Down

0 comments on commit 6d8562d

Please sign in to comment.