Skip to content

Commit

Permalink
Fix client timeouts in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 20, 2021
1 parent 597917e commit e898151
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def __init__(
raise_for_status: Union[
bool, Callable[[ClientResponse], Awaitable[None]]
] = False,
timeout: Union[_SENTINEL, ClientTimeout] = sentinel,
timeout: Union[_SENTINEL, ClientTimeout, None] = sentinel,
auto_decompress: bool = True,
trust_env: bool = False,
requote_redirect_url: bool = True,
Expand Down Expand Up @@ -256,7 +256,7 @@ def __init__(
self._default_auth = auth
self._version = version
self._json_serialize = json_serialize
if timeout is sentinel:
if timeout is sentinel or timeout is None:
self._timeout = DEFAULT_TIMEOUT
else:
self._timeout = timeout # type: ignore[assignment]
Expand Down Expand Up @@ -345,7 +345,7 @@ async def _request(
read_until_eof: bool = True,
proxy: Optional[StrOrURL] = None,
proxy_auth: Optional[BasicAuth] = None,
timeout: Union[ClientTimeout, _SENTINEL] = sentinel,
timeout: Union[ClientTimeout, _SENTINEL, None] = sentinel,
ssl: Optional[Union[SSLContext, bool, Fingerprint]] = None,
proxy_headers: Optional[LooseHeaders] = None,
trace_request_ctx: Optional[SimpleNamespace] = None,
Expand Down Expand Up @@ -396,8 +396,8 @@ async def _request(
except ValueError as e:
raise InvalidURL(proxy) from e

if timeout is sentinel:
real_timeout = self._timeout # type: ClientTimeout
if timeout is sentinel or timeout is None:
real_timeout: ClientTimeout = self._timeout
else:
real_timeout = timeout # type: ignore[assignment]
# timeout is cumulative for all request operations
Expand Down Expand Up @@ -646,7 +646,7 @@ def ws_connect(
*,
method: str = hdrs.METH_GET,
protocols: Iterable[str] = (),
timeout: Union[ClientWSTimeout, float, _SENTINEL] = sentinel,
timeout: Union[ClientWSTimeout, float, _SENTINEL, None] = sentinel,
receive_timeout: Optional[float] = None,
autoclose: bool = True,
autoping: bool = True,
Expand Down Expand Up @@ -692,7 +692,7 @@ async def _ws_connect(
*,
method: str = hdrs.METH_GET,
protocols: Iterable[str] = (),
timeout: Union[ClientWSTimeout, float, _SENTINEL] = sentinel,
timeout: Union[ClientWSTimeout, float, _SENTINEL, None] = sentinel,
receive_timeout: Optional[float] = None,
autoclose: bool = True,
autoping: bool = True,
Expand All @@ -708,7 +708,9 @@ async def _ws_connect(
compress: int = 0,
max_msg_size: int = 4 * 1024 * 1024,
) -> ClientWebSocketResponse:
if timeout is not sentinel:
if timeout is sentinel or timeout is None:
ws_timeout = DEFAULT_WS_CLIENT_TIMEOUT
else:
if isinstance(timeout, ClientWSTimeout):
ws_timeout = timeout
else:
Expand All @@ -720,8 +722,6 @@ async def _ws_connect(
stacklevel=2,
)
ws_timeout = ClientWSTimeout(ws_close=timeout) # type: ignore[arg-type]
else:
ws_timeout = DEFAULT_WS_CLIENT_TIMEOUT
if receive_timeout is not None:
warnings.warn(
"float parameter 'receive_timeout' "
Expand Down
6 changes: 3 additions & 3 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ async def handler(request):
client = await aiohttp_client(app)

with pytest.raises(asyncio.TimeoutError):
await client.get("/", timeout=0.01)
await client.get("/", timeout=aiohttp.ClientTimeout(total=0.01))


async def test_timeout_on_conn_reading_headers(
Expand All @@ -601,7 +601,7 @@ async def handler(request):
client = await aiohttp_client(app, connector=conn)

with pytest.raises(asyncio.TimeoutError):
await client.get("/", timeout=0.01)
await client.get("/", timeout=aiohttp.ClientTimeout(total=0.01))


async def test_timeout_on_session_read_timeout(
Expand Down Expand Up @@ -687,7 +687,7 @@ async def handler(request):
app.router.add_route("GET", "/", handler)
client = await aiohttp_client(app)

resp = await client.get("/", timeout=1)
resp = await client.get("/", timeout=aiohttp.ClientTimeout(1))
await fut

with pytest.raises(asyncio.TimeoutError):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,6 @@ async def ws_handler(request):
with pytest.raises(WSServerHandshakeError):
await client.ws_connect("/ws")

resp = await client.get("/api/null", timeout=1)
resp = await client.get("/api/null", timeout=aiohttp.ClientTimeout(total=1))
assert (await resp.json()) == {"err": None}
resp.close()

0 comments on commit e898151

Please sign in to comment.