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

Disabling retry_persistent_connection in Tests #9396

Merged
merged 21 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions CHANGES/9141.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Modified the `ClientSession` class by adding a private attribute `_retry_connection` of type `bool`, with a default value of `True` to the class.
`retry_persistent_connection` in `ClientSession` is set to `False` if `_retry_connection` was `False`.
In the session of `TestClient`, `ClientSession` attribute `_retry_connection` is changed to `False` -- by :user:`ShubhAgarwal-dev`.
ShubhAgarwal-dev marked this conversation as resolved.
Show resolved Hide resolved
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
13 changes: 13 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,18 @@
result.stdout.fnmatch_lines(["*TypeError*"])


async def test_disable_retry_persistent_connection(aiohttp_client: AiohttpClient):
async def handler(request: web.Request) -> web.Response:
request.close()
return web.Response(200)
Fixed Show fixed Hide fixed

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)
async with client.get("/") as _:
assert client._session.closed is False


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