From 7f5d2066456e9f40b539e290f41d5c4b736389f7 Mon Sep 17 00:00:00 2001 From: Fu Date: Tue, 24 Jul 2018 19:28:59 +0800 Subject: [PATCH] fix never run code (#3145) * fix never run code * add client session timeout arguments unit test * add change log --- CHANGES/3145.bugfix | 1 + aiohttp/client.py | 25 +++++++++++-------------- tests/test_client_session.py | 21 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 CHANGES/3145.bugfix diff --git a/CHANGES/3145.bugfix b/CHANGES/3145.bugfix new file mode 100644 index 00000000000..08034877dad --- /dev/null +++ b/CHANGES/3145.bugfix @@ -0,0 +1 @@ +Fix timeout precondition checkers diff --git a/aiohttp/client.py b/aiohttp/client.py index 7e5236dd4dc..ec6e5a5bae8 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -146,24 +146,21 @@ def __init__(self, *, connector=None, loop=None, cookies=None, self._json_serialize = json_serialize if timeout is not sentinel: self._timeout = timeout + if read_timeout is not sentinel: + raise ValueError("read_timeout and timeout parameters " + "conflict, please setup " + "timeout.read") + if conn_timeout is not None: + raise ValueError("conn_timeout and timeout parameters " + "conflict, please setup " + "timeout.connect") else: self._timeout = DEFAULT_TIMEOUT if read_timeout is not sentinel: - if timeout is not sentinel: - raise ValueError("read_timeout and timeout parameters " - "conflict, please setup " - "timeout.read") - else: - self._timeout = attr.evolve(self._timeout, - total=read_timeout) + self._timeout = attr.evolve(self._timeout, total=read_timeout) if conn_timeout is not None: - if timeout is not sentinel: - raise ValueError("conn_timeout and timeout parameters " - "conflict, please setup " - "timeout.connect") - else: - self._timeout = attr.evolve(self._timeout, - connect=conn_timeout) + self._timeout = attr.evolve(self._timeout, + connect=conn_timeout) self._raise_for_status = raise_for_status self._auto_decompress = auto_decompress self._trust_env = trust_env diff --git a/tests/test_client_session.py b/tests/test_client_session.py index 047ed796f66..bc304bc79c7 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -12,7 +12,7 @@ from yarl import URL import aiohttp -from aiohttp import hdrs, web +from aiohttp import client, hdrs, web from aiohttp.client import ClientSession from aiohttp.client_reqrep import ClientRequest from aiohttp.connector import BaseConnector, TCPConnector @@ -646,3 +646,22 @@ def test_client_session_custom_attr(loop): session = ClientSession(loop=loop) with pytest.warns(DeprecationWarning): session.custom = None + + +def test_client_session_timeout_args(loop): + session1 = ClientSession(loop=loop) + assert session1._timeout == client.DEFAULT_TIMEOUT + + session2 = ClientSession(loop=loop, read_timeout=20*60, conn_timeout=30*60) + assert session2._timeout == client.ClientTimeout(total=20*60, + connect=30*60) + + with pytest.raises(ValueError): + ClientSession(loop=loop, + timeout=client.ClientTimeout(total=10*60), + read_timeout=20*60) + + with pytest.raises(ValueError): + ClientSession(loop=loop, + timeout=client.ClientTimeout(total=10 * 60), + conn_timeout=30 * 60)