From ec226e33cb3a75af3407fca56dbf0aa4b7078db3 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Thu, 16 Aug 2018 15:00:23 +1000 Subject: [PATCH] Pin aiohttp <= 3.2.1 in requirements-dev.txt (fixes errors for new contributors checking out the code and setting up a dev environment) Future-proof the some test cases so they work with aiohttp >= 3.3.0, in case we bump the aiohttp version in the future. --- requirements-dev.txt | 2 +- tests/test_keep_alive_timeout.py | 30 +++++++++++-- tests/test_request_timeout.py | 73 ++++++++++++++++++++++++++------ 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 3d94c51d..004f6f9e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,5 @@ aiofiles -aiohttp>=2.3.0 +aiohttp>=2.3.0,<=3.2.1 chardet<=2.3.0 beautifulsoup4 coverage diff --git a/tests/test_keep_alive_timeout.py b/tests/test_keep_alive_timeout.py index 2a9e93a2..53a2872e 100644 --- a/tests/test_keep_alive_timeout.py +++ b/tests/test_keep_alive_timeout.py @@ -9,14 +9,39 @@ import aiohttp from aiohttp import TCPConnector from sanic.testing import SanicTestClient, HOST, PORT +try: + try: + import packaging # direct use + except ImportError: + # setuptools v39.0 and above. + try: + from setuptools.extern import packaging + except ImportError: + # Before setuptools v39.0 + from pkg_resources.extern import packaging + version = packaging.version +except ImportError: + raise RuntimeError("The 'packaging' library is missing.") + +aiohttp_version = version.parse(aiohttp.__version__) class ReuseableTCPConnector(TCPConnector): def __init__(self, *args, **kwargs): super(ReuseableTCPConnector, self).__init__(*args, **kwargs) self.old_proto = None - if aiohttp.__version__ >= '3.0': - + if aiohttp_version >= version.parse('3.3.0'): + async def connect(self, req, traces, timeout): + new_conn = await super(ReuseableTCPConnector, self)\ + .connect(req, traces, timeout) + if self.old_proto is not None: + if self.old_proto != new_conn._protocol: + raise RuntimeError( + "We got a new connection, wanted the same one!") + print(new_conn.__dict__) + self.old_proto = new_conn._protocol + return new_conn + elif aiohttp_version >= version.parse('3.0.0'): async def connect(self, req, traces=None): new_conn = await super(ReuseableTCPConnector, self)\ .connect(req, traces=traces) @@ -28,7 +53,6 @@ class ReuseableTCPConnector(TCPConnector): self.old_proto = new_conn._protocol return new_conn else: - async def connect(self, req): new_conn = await super(ReuseableTCPConnector, self)\ .connect(req) diff --git a/tests/test_request_timeout.py b/tests/test_request_timeout.py index b3eb78aa..672d0588 100644 --- a/tests/test_request_timeout.py +++ b/tests/test_request_timeout.py @@ -5,9 +5,24 @@ import asyncio from sanic.response import text from sanic.config import Config import aiohttp -from aiohttp import TCPConnector +from aiohttp import TCPConnector, ClientResponse from sanic.testing import SanicTestClient, HOST, PORT +try: + try: + import packaging # direct use + except ImportError: + # setuptools v39.0 and above. + try: + from setuptools.extern import packaging + except ImportError: + # Before setuptools v39.0 + from pkg_resources.extern import packaging + version = packaging.version +except ImportError: + raise RuntimeError("The 'packaging' library is missing.") + +aiohttp_version = version.parse(aiohttp.__version__) class DelayableTCPConnector(TCPConnector): @@ -38,8 +53,11 @@ class DelayableTCPConnector(TCPConnector): self.orig_start = getattr(resp, 'start') try: - ret = await self.orig_start(connection, - read_until_eof) + if aiohttp_version >= version.parse("3.3.0"): + ret = await self.orig_start(connection) + else: + ret = await self.orig_start(connection, + read_until_eof) except Exception as e: raise e return ret @@ -57,15 +75,31 @@ class DelayableTCPConnector(TCPConnector): await asyncio.sleep(self.delay) t = req.loop.time() print("sending at {}".format(t), flush=True) - conn = next(iter(args)) # first arg is connection - if aiohttp.__version__ >= "3.1.0": + conn = next(iter(args)) # first arg is connection + + if aiohttp_version >= version.parse("3.1.0"): try: delayed_resp = await self.orig_send(*args, **kwargs) except Exception as e: - return aiohttp.ClientResponse(req.method, req.url, - writer=None, continue100=None, timer=None, - request_info=None, auto_decompress=None, traces=[], - loop=req.loop, session=None) + if aiohttp_version >= version.parse("3.3.0"): + return aiohttp.ClientResponse(req.method, req.url, + writer=None, + continue100=None, + timer=None, + request_info=None, + traces=[], + loop=req.loop, + session=None) + else: + return aiohttp.ClientResponse(req.method, req.url, + writer=None, + continue100=None, + timer=None, + request_info=None, + auto_decompress=None, + traces=[], + loop=req.loop, + session=None) else: try: delayed_resp = self.orig_send(*args, **kwargs) @@ -73,7 +107,7 @@ class DelayableTCPConnector(TCPConnector): return aiohttp.ClientResponse(req.method, req.url) return delayed_resp - if aiohttp.__version__ >= "3.1.0": + if aiohttp_version >= version.parse("3.1.0"): # aiohttp changed the request.send method to async async def send(self, *args, **kwargs): gen = self.delayed_send(*args, **kwargs) @@ -96,12 +130,25 @@ class DelayableTCPConnector(TCPConnector): self._post_connect_delay = _post_connect_delay self._pre_request_delay = _pre_request_delay - if aiohttp.__version__ >= '3.0': - + if aiohttp_version >= version.parse("3.3.0"): + async def connect(self, req, traces, timeout): + d_req = DelayableTCPConnector.\ + RequestContextManager(req, self._pre_request_delay) + conn = await super(DelayableTCPConnector, self).\ + connect(req, traces, timeout) + if self._post_connect_delay and self._post_connect_delay > 0: + await asyncio.sleep(self._post_connect_delay, + loop=self._loop) + req.send = d_req.send + t = req.loop.time() + print("Connected at {}".format(t), flush=True) + return conn + elif aiohttp_version >= version.parse("3.0.0"): async def connect(self, req, traces=None): d_req = DelayableTCPConnector.\ RequestContextManager(req, self._pre_request_delay) - conn = await super(DelayableTCPConnector, self).connect(req, traces=traces) + conn = await super(DelayableTCPConnector, self).\ + connect(req, traces=traces) if self._post_connect_delay and self._post_connect_delay > 0: await asyncio.sleep(self._post_connect_delay, loop=self._loop)