Skip to content

Commit

Permalink
Fix test_client_session_timeout_zero to not use internet connection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Sep 3, 2024
1 parent 5c0b8e4 commit 3bbe1a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES/9004.packaging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``test_client_session_timeout_zero`` to not require internet access -- by :user:`Dreamsorcerer`.
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import pytest

from aiohttp.client_proto import ResponseHandler
from aiohttp.http import WS_KEY
from aiohttp.test_utils import loop_context

Expand Down Expand Up @@ -95,8 +96,8 @@ def pipe_name() -> str:

@pytest.fixture
def create_mocked_conn(loop: Any):
def _proto_factory(conn_closing_result=None, **kwargs):
proto = mock.Mock(**kwargs)
def _proto_factory(conn_closing_result=None, **kwargs) -> ResponseHandler:
proto = mock.create_autospec(ResponseHandler, **kwargs)
proto.closed = loop.create_future()
proto.closed.set_result(conn_closing_result)
return proto
Expand Down
31 changes: 25 additions & 6 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from aiohttp.client_proto import ResponseHandler
from aiohttp.client_reqrep import ClientRequest, ConnectionKey
from aiohttp.connector import BaseConnector, Connection, TCPConnector, UnixConnector
from aiohttp.http import RawResponseMessage
from aiohttp.pytest_plugin import AiohttpClient
from aiohttp.test_utils import make_mocked_coro
from aiohttp.tracing import Trace
Expand Down Expand Up @@ -975,13 +976,31 @@ async def test_client_session_timeout_default_args(
await session1.close()


async def test_client_session_timeout_zero() -> None:
async def test_client_session_timeout_zero(
create_mocked_conn: Callable[[], ResponseHandler]
) -> None:
async def create_connection(
req: object, traces: object, timeout: object
) -> ResponseHandler:
await asyncio.sleep(0.01)
conn = create_mocked_conn()
conn.connected = True # type: ignore[misc]
assert conn.transport is not None
conn.transport.is_closing.return_value = False # type: ignore[attr-defined]
msg = mock.create_autospec(RawResponseMessage, spec_set=True, code=200)
conn.read.return_value = (msg, mock.Mock()) # type: ignore[attr-defined]
return conn

timeout = client.ClientTimeout(total=10, connect=0, sock_connect=0, sock_read=0)
try:
async with ClientSession(timeout=timeout) as session:
await session.get("http://example.com")
except asyncio.TimeoutError: # pragma: no cover
pytest.fail("0 should disable timeout.")
async with ClientSession(timeout=timeout) as session:
with mock.patch.object(
session._connector, "_create_connection", create_connection
):
try:
resp = await session.get("http://example.com")
except asyncio.TimeoutError: # pragma: no cover
pytest.fail("0 should disable timeout.")
resp.close()


async def test_client_session_timeout_bad_argument() -> None:
Expand Down

0 comments on commit 3bbe1a5

Please sign in to comment.