Skip to content

Commit

Permalink
Disabling retry_persistent_connection in Tests (aio-libs#9396)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6d8562d)
  • Loading branch information
ShubhAgarwal-dev committed Oct 6, 2024
1 parent cd54f11 commit f2fede0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
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 @@ -300,6 +300,7 @@ Sergey Skripnick
Serhii Charykov
Serhii Kostel
Serhiy Storchaka
Shubh Agarwal
Simon Kennedy
Sin-Woo Bang
Stanislas Plum
Expand Down
63 changes: 32 additions & 31 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,36 +225,34 @@ class ClientTimeout:
class ClientSession:
"""First-class interface for making HTTP requests."""

ATTRS = frozenset(
[
"_base_url",
"_source_traceback",
"_connector",
"requote_redirect_url",
"_loop",
"_cookie_jar",
"_connector_owner",
"_default_auth",
"_version",
"_json_serialize",
"_requote_redirect_url",
"_timeout",
"_raise_for_status",
"_auto_decompress",
"_trust_env",
"_default_headers",
"_skip_auto_headers",
"_request_class",
"_response_class",
"_ws_response_class",
"_trace_configs",
"_read_bufsize",
"_max_line_size",
"_max_field_size",
"_resolve_charset",
"_default_proxy",
"_default_proxy_auth",
]
__slots__ = (
"_base_url",
"_source_traceback",
"_connector",
"_loop",
"_cookie_jar",
"_connector_owner",
"_default_auth",
"_version",
"_json_serialize",
"_requote_redirect_url",
"_timeout",
"_raise_for_status",
"_auto_decompress",
"_trust_env",
"_default_headers",
"_skip_auto_headers",
"_request_class",
"_response_class",
"_ws_response_class",
"_trace_configs",
"_read_bufsize",
"_max_line_size",
"_max_field_size",
"_resolve_charset",
"_default_proxy",
"_default_proxy_auth",
"_retry_connection",
)

_source_traceback: Optional[traceback.StackSummary] = None
Expand Down Expand Up @@ -402,6 +400,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:
warnings.warn(
Expand Down Expand Up @@ -593,7 +592,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 @@ -313,6 +313,7 @@ def __init__(
if cookie_jar is None:
cookie_jar = aiohttp.CookieJar(unsafe=True, loop=loop)
self._session = ClientSession(loop=loop, cookie_jar=cookie_jar, **kwargs)
self._session._retry_connection = False
self._closed = False
self._responses: List[ClientResponse] = []
self._websockets: List[ClientWebSocketResponse] = []
Expand Down
30 changes: 27 additions & 3 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 as _RawTestServer,
Expand Down Expand Up @@ -334,9 +335,32 @@ def test_noop(self) -> None:
result.stdout.fnmatch_lines(["*RuntimeError*"])


async def test_server_context_manager(app, loop) -> None:
async with TestServer(app, loop=loop) as server:
async with aiohttp.ClientSession(loop=loop) as client:
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:
async with TestServer(app) as server:
async with aiohttp.ClientSession() as client:
async with client.head(server.make_url("/")) as resp:
assert resp.status == 200

Expand Down

0 comments on commit f2fede0

Please sign in to comment.