This commit is contained in:
Adam Hopkins 2021-01-19 16:11:09 +02:00
parent a0066e5752
commit f8f215772c

View File

@ -1,282 +1,282 @@
# import asyncio import asyncio
# from asyncio import sleep as aio_sleep from asyncio import sleep as aio_sleep
# from json import JSONDecodeError from json import JSONDecodeError
# from os import environ from os import environ
# import httpcore import httpcore
# import httpx import httpx
# import pytest import pytest
# from sanic import Sanic, server from sanic import Sanic, server
# from sanic.compat import OS_IS_WINDOWS from sanic.compat import OS_IS_WINDOWS
# from sanic.response import text from sanic.response import text
# from sanic.testing import HOST, SanicTestClient from sanic.testing import HOST, SanicTestClient
# CONFIG_FOR_TESTS = {"KEEP_ALIVE_TIMEOUT": 2, "KEEP_ALIVE": True} CONFIG_FOR_TESTS = {"KEEP_ALIVE_TIMEOUT": 2, "KEEP_ALIVE": True}
# PORT = 42101 # test_keep_alive_timeout_reuse doesn't work with random port PORT = 42101 # test_keep_alive_timeout_reuse doesn't work with random port
# from httpcore._async.base import ConnectionState from httpcore._async.base import ConnectionState
# from httpcore._async.connection import AsyncHTTPConnection from httpcore._async.connection import AsyncHTTPConnection
# from httpcore._types import Origin from httpcore._types import Origin
# class ReusableSanicConnectionPool(httpcore.AsyncConnectionPool): class ReusableSanicConnectionPool(httpcore.AsyncConnectionPool):
# last_reused_connection = None last_reused_connection = None
# async def _get_connection_from_pool(self, *args, **kwargs): async def _get_connection_from_pool(self, *args, **kwargs):
# conn = await super()._get_connection_from_pool(*args, **kwargs) conn = await super()._get_connection_from_pool(*args, **kwargs)
# self.__class__.last_reused_connection = conn self.__class__.last_reused_connection = conn
# return conn return conn
# class ResusableSanicSession(httpx.AsyncClient): class ResusableSanicSession(httpx.AsyncClient):
# def __init__(self, *args, **kwargs) -> None: def __init__(self, *args, **kwargs) -> None:
# transport = ReusableSanicConnectionPool() transport = ReusableSanicConnectionPool()
# super().__init__(transport=transport, *args, **kwargs) super().__init__(transport=transport, *args, **kwargs)
# class ReuseableSanicTestClient(SanicTestClient): class ReuseableSanicTestClient(SanicTestClient):
# def __init__(self, app, loop=None): def __init__(self, app, loop=None):
# super().__init__(app) super().__init__(app)
# if loop is None: if loop is None:
# loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
# self._loop = loop self._loop = loop
# self._server = None self._server = None
# self._tcp_connector = None self._tcp_connector = None
# self._session = None self._session = None
# def get_new_session(self): def get_new_session(self):
# return ResusableSanicSession() return ResusableSanicSession()
# # Copied from SanicTestClient, but with some changes to reuse the # Copied from SanicTestClient, but with some changes to reuse the
# # same loop for the same app. # same loop for the same app.
# def _sanic_endpoint_test( def _sanic_endpoint_test(
# self, self,
# method="get", method="get",
# uri="/", uri="/",
# gather_request=True, gather_request=True,
# debug=False, debug=False,
# server_kwargs=None, server_kwargs=None,
# *request_args, *request_args,
# **request_kwargs, **request_kwargs,
# ): ):
# loop = self._loop loop = self._loop
# results = [None, None] results = [None, None]
# exceptions = [] exceptions = []
# server_kwargs = server_kwargs or {"return_asyncio_server": True} server_kwargs = server_kwargs or {"return_asyncio_server": True}
# if gather_request: if gather_request:
# def _collect_request(request): def _collect_request(request):
# if results[0] is None: if results[0] is None:
# results[0] = request results[0] = request
# self.app.request_middleware.appendleft(_collect_request) self.app.request_middleware.appendleft(_collect_request)
# if uri.startswith( if uri.startswith(
# ("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:") ("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:")
# ): ):
# url = uri url = uri
# else: else:
# uri = uri if uri.startswith("/") else f"/{uri}" uri = uri if uri.startswith("/") else f"/{uri}"
# scheme = "http" scheme = "http"
# url = f"{scheme}://{HOST}:{PORT}{uri}" url = f"{scheme}://{HOST}:{PORT}{uri}"
# @self.app.listener("after_server_start") @self.app.listener("after_server_start")
# async def _collect_response(loop): async def _collect_response(loop):
# try: try:
# response = await self._local_request( response = await self._local_request(
# method, url, *request_args, **request_kwargs method, url, *request_args, **request_kwargs
# ) )
# results[-1] = response results[-1] = response
# except Exception as e2: except Exception as e2:
# exceptions.append(e2) exceptions.append(e2)
# if self._server is not None: if self._server is not None:
# _server = self._server _server = self._server
# else: else:
# _server_co = self.app.create_server( _server_co = self.app.create_server(
# host=HOST, debug=debug, port=PORT, **server_kwargs host=HOST, debug=debug, port=PORT, **server_kwargs
# ) )
# server.trigger_events( server.trigger_events(
# self.app.listeners["before_server_start"], loop self.app.listeners["before_server_start"], loop
# ) )
# try: try:
# loop._stopping = False loop._stopping = False
# _server = loop.run_until_complete(_server_co) _server = loop.run_until_complete(_server_co)
# except Exception as e1: except Exception as e1:
# raise e1 raise e1
# self._server = _server self._server = _server
# server.trigger_events(self.app.listeners["after_server_start"], loop) server.trigger_events(self.app.listeners["after_server_start"], loop)
# self.app.listeners["after_server_start"].pop() self.app.listeners["after_server_start"].pop()
# if exceptions: if exceptions:
# raise ValueError(f"Exception during request: {exceptions}") raise ValueError(f"Exception during request: {exceptions}")
# if gather_request: if gather_request:
# self.app.request_middleware.pop() self.app.request_middleware.pop()
# try: try:
# request, response = results request, response = results
# return request, response return request, response
# except Exception: except Exception:
# raise ValueError( raise ValueError(
# f"Request and response object expected, got ({results})" f"Request and response object expected, got ({results})"
# ) )
# else: else:
# try: try:
# return results[-1] return results[-1]
# except Exception: except Exception:
# raise ValueError(f"Request object expected, got ({results})") raise ValueError(f"Request object expected, got ({results})")
# def kill_server(self): def kill_server(self):
# try: try:
# if self._server: if self._server:
# self._server.close() self._server.close()
# self._loop.run_until_complete(self._server.wait_closed()) self._loop.run_until_complete(self._server.wait_closed())
# self._server = None self._server = None
# if self._session: if self._session:
# self._loop.run_until_complete(self._session.aclose()) self._loop.run_until_complete(self._session.aclose())
# self._session = None self._session = None
# except Exception as e3: except Exception as e3:
# raise e3 raise e3
# # Copied from SanicTestClient, but with some changes to reuse the # Copied from SanicTestClient, but with some changes to reuse the
# # same TCPConnection and the sane ClientSession more than once. # same TCPConnection and the sane ClientSession more than once.
# # Note, you cannot use the same session if you are in a _different_ # Note, you cannot use the same session if you are in a _different_
# # loop, so the changes above are required too. # loop, so the changes above are required too.
# async def _local_request(self, method, url, *args, **kwargs): async def _local_request(self, method, url, *args, **kwargs):
# raw_cookies = kwargs.pop("raw_cookies", None) raw_cookies = kwargs.pop("raw_cookies", None)
# request_keepalive = kwargs.pop( request_keepalive = kwargs.pop(
# "request_keepalive", CONFIG_FOR_TESTS["KEEP_ALIVE_TIMEOUT"] "request_keepalive", CONFIG_FOR_TESTS["KEEP_ALIVE_TIMEOUT"]
# ) )
# if not self._session: if not self._session:
# self._session = self.get_new_session() self._session = self.get_new_session()
# try: try:
# response = await getattr(self._session, method.lower())( response = await getattr(self._session, method.lower())(
# url, timeout=request_keepalive, *args, **kwargs url, timeout=request_keepalive, *args, **kwargs
# ) )
# except NameError: except NameError:
# raise Exception(response.status_code) raise Exception(response.status_code)
# try: try:
# response.json = response.json() response.json = response.json()
# except (JSONDecodeError, UnicodeDecodeError): except (JSONDecodeError, UnicodeDecodeError):
# response.json = None response.json = None
# response.body = await response.aread() response.body = await response.aread()
# response.status = response.status_code response.status = response.status_code
# response.content_type = response.headers.get("content-type") response.content_type = response.headers.get("content-type")
# if raw_cookies: if raw_cookies:
# response.raw_cookies = {} response.raw_cookies = {}
# for cookie in response.cookies: for cookie in response.cookies:
# response.raw_cookies[cookie.name] = cookie response.raw_cookies[cookie.name] = cookie
# return response return response
# keep_alive_timeout_app_reuse = Sanic("test_ka_timeout_reuse") keep_alive_timeout_app_reuse = Sanic("test_ka_timeout_reuse")
# keep_alive_app_client_timeout = Sanic("test_ka_client_timeout") keep_alive_app_client_timeout = Sanic("test_ka_client_timeout")
# keep_alive_app_server_timeout = Sanic("test_ka_server_timeout") keep_alive_app_server_timeout = Sanic("test_ka_server_timeout")
# keep_alive_timeout_app_reuse.config.update(CONFIG_FOR_TESTS) keep_alive_timeout_app_reuse.config.update(CONFIG_FOR_TESTS)
# keep_alive_app_client_timeout.config.update(CONFIG_FOR_TESTS) keep_alive_app_client_timeout.config.update(CONFIG_FOR_TESTS)
# keep_alive_app_server_timeout.config.update(CONFIG_FOR_TESTS) keep_alive_app_server_timeout.config.update(CONFIG_FOR_TESTS)
# @keep_alive_timeout_app_reuse.route("/1") @keep_alive_timeout_app_reuse.route("/1")
# async def handler1(request): async def handler1(request):
# return text("OK") return text("OK")
# @keep_alive_app_client_timeout.route("/1") @keep_alive_app_client_timeout.route("/1")
# async def handler2(request): async def handler2(request):
# return text("OK") return text("OK")
# @keep_alive_app_server_timeout.route("/1") @keep_alive_app_server_timeout.route("/1")
# async def handler3(request): async def handler3(request):
# return text("OK") return text("OK")
# @pytest.mark.skipif( @pytest.mark.skipif(
# bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS, bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS,
# reason="Not testable with current client", reason="Not testable with current client",
# ) )
# def test_keep_alive_timeout_reuse(): def test_keep_alive_timeout_reuse():
# """If the server keep-alive timeout and client keep-alive timeout are """If the server keep-alive timeout and client keep-alive timeout are
# both longer than the delay, the client _and_ server will successfully both longer than the delay, the client _and_ server will successfully
# reuse the existing connection.""" reuse the existing connection."""
# try: try:
# loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
# client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop) client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
# headers = {"Connection": "keep-alive"} headers = {"Connection": "keep-alive"}
# request, response = client.get("/1", headers=headers) request, response = client.get("/1", headers=headers)
# assert response.status == 200 assert response.status == 200
# assert response.text == "OK" assert response.text == "OK"
# loop.run_until_complete(aio_sleep(1)) loop.run_until_complete(aio_sleep(1))
# request, response = client.get("/1") request, response = client.get("/1")
# assert response.status == 200 assert response.status == 200
# assert response.text == "OK" assert response.text == "OK"
# assert ReusableSanicConnectionPool.last_reused_connection assert ReusableSanicConnectionPool.last_reused_connection
# finally: finally:
# client.kill_server() client.kill_server()
# @pytest.mark.skipif( @pytest.mark.skipif(
# bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS, bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS,
# reason="Not testable with current client", reason="Not testable with current client",
# ) )
# def test_keep_alive_client_timeout(): def test_keep_alive_client_timeout():
# """If the server keep-alive timeout is longer than the client """If the server keep-alive timeout is longer than the client
# keep-alive timeout, client will try to create a new connection here.""" keep-alive timeout, client will try to create a new connection here."""
# try: try:
# loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
# client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop) client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
# headers = {"Connection": "keep-alive"} headers = {"Connection": "keep-alive"}
# request, response = client.get( request, response = client.get(
# "/1", headers=headers, request_keepalive=1 "/1", headers=headers, request_keepalive=1
# ) )
# assert response.status == 200 assert response.status == 200
# assert response.text == "OK" assert response.text == "OK"
# loop.run_until_complete(aio_sleep(2)) loop.run_until_complete(aio_sleep(2))
# exception = None exception = None
# request, response = client.get("/1", request_keepalive=1) request, response = client.get("/1", request_keepalive=1)
# assert ReusableSanicConnectionPool.last_reused_connection is None assert ReusableSanicConnectionPool.last_reused_connection is None
# finally: finally:
# client.kill_server() client.kill_server()
# @pytest.mark.skipif( @pytest.mark.skipif(
# bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS, bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS,
# reason="Not testable with current client", reason="Not testable with current client",
# ) )
# def test_keep_alive_server_timeout(): def test_keep_alive_server_timeout():
# """If the client keep-alive timeout is longer than the server """If the client keep-alive timeout is longer than the server
# keep-alive timeout, the client will either a 'Connection reset' error keep-alive timeout, the client will either a 'Connection reset' error
# _or_ a new connection. Depending on how the event-loop handles the _or_ a new connection. Depending on how the event-loop handles the
# broken server connection.""" broken server connection."""
# try: try:
# loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
# client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop) client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
# headers = {"Connection": "keep-alive"} headers = {"Connection": "keep-alive"}
# request, response = client.get( request, response = client.get(
# "/1", headers=headers, request_keepalive=60 "/1", headers=headers, request_keepalive=60
# ) )
# assert response.status == 200 assert response.status == 200
# assert response.text == "OK" assert response.text == "OK"
# loop.run_until_complete(aio_sleep(3)) loop.run_until_complete(aio_sleep(3))
# exception = None exception = None
# request, response = client.get("/1", request_keepalive=60) request, response = client.get("/1", request_keepalive=60)
# assert ReusableSanicConnectionPool.last_reused_connection is None assert ReusableSanicConnectionPool.last_reused_connection is None
# finally: finally:
# client.kill_server() client.kill_server()