From 9a9a1ea5d6a8dc0b7619e628eb99ba8aa826fa12 Mon Sep 17 00:00:00 2001 From: prryplatypus <25409753+prryplatypus@users.noreply.github.com> Date: Mon, 18 Oct 2021 21:33:05 +0200 Subject: [PATCH 1/4] Reattempt testing number 1 --- tests/test_app.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index f222fba1..59463cfb 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -8,7 +8,10 @@ from unittest.mock import Mock, patch import pytest +import sanic.app + from sanic import Sanic +from sanic.compat import OS_IS_WINDOWS from sanic.config import Config from sanic.exceptions import SanicException from sanic.response import text @@ -444,3 +447,35 @@ def test_custom_context(): app = Sanic("custom", ctx=ctx) assert app.ctx == ctx + + +def test_uvloop_config_enabled(monkeypatch): + app = Sanic("uvloop") + + err_logger = Mock() + monkeypatch.setattr(sanic.app, "error_logger", err_logger) + + use_uvloop = Mock(return_value=uvloop_installed()) + monkeypatch.setattr(sanic.app, "use_uvloop", use_uvloop) + + @app.get("/1") + def _(request): + if OS_IS_WINDOWS: + use_uvloop.assert_not_called() + return text("test") + + use_uvloop.assert_called_once() + + if not uvloop_installed(): + err_logger.assert_called_with( + "You are trying to use uvloop, but uvloop is not " + "installed in your system. In order to use uvloop " + "you must first install it. Otherwise, you can disable " + "uvloop completely by setting the 'USE_UVLOOP' " + "configuration value to false. Sanic will now continue " + "to run without using uvloop." + ) + + return text("test") + + app.test_client.get("/1") From 6563ee28bfa61f66040de2d550406ee176f26d94 Mon Sep 17 00:00:00 2001 From: prryplatypus <25409753+prryplatypus@users.noreply.github.com> Date: Sat, 23 Oct 2021 21:16:49 +0200 Subject: [PATCH 2/4] Fix tests (hopefully?) --- tests/test_exceptions.py | 18 ++++++------------ tests/test_exceptions_handler.py | 6 ++++-- tests/test_graceful_shutdown.py | 6 +++--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 503e47cb..041766a5 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -4,7 +4,6 @@ import warnings import pytest from bs4 import BeautifulSoup -from websockets.version import version as websockets_version from sanic import Sanic from sanic.exceptions import ( @@ -260,15 +259,10 @@ def test_exception_in_ws_logged(caplog): raise Exception("...") with caplog.at_level(logging.INFO): - app.test_client.websocket("/feed") - # Websockets v10.0 and above output an additional - # INFO message when a ws connection is accepted - ws_version_parts = websockets_version.split(".") - ws_major = int(ws_version_parts[0]) - record_index = 2 if ws_major >= 10 else 1 - assert caplog.record_tuples[record_index][0] == "sanic.error" - assert caplog.record_tuples[record_index][1] == logging.ERROR + req, _ = app.test_client.websocket("/feed") + assert ( - "Exception occurred while handling uri:" - in caplog.record_tuples[record_index][2] - ) + "sanic.error", + logging.ERROR, + "Exception occurred while handling uri: %s" % repr(req.url) + ) in caplog.record_tuples diff --git a/tests/test_exceptions_handler.py b/tests/test_exceptions_handler.py index 9bedf7e6..e5b8fb8c 100644 --- a/tests/test_exceptions_handler.py +++ b/tests/test_exceptions_handler.py @@ -219,11 +219,13 @@ def test_single_arg_exception_handler_notice(exception_handler_app, caplog): with caplog.at_level(logging.WARNING): _, response = exception_handler_app.test_client.get("/1") - assert caplog.records[0].message == ( + assert ( + "sanic.error", + logging.WARNING, "You are using a deprecated error handler. The lookup method should " "accept two positional parameters: (exception, route_name: " "Optional[str]). Until you upgrade your ErrorHandler.lookup, " "Blueprint specific exceptions will not work properly. Beginning in " "v22.3, the legacy style lookup method will not work at all." - ) + ) in caplog.record_tuples assert response.status == 400 diff --git a/tests/test_graceful_shutdown.py b/tests/test_graceful_shutdown.py index 8380ed50..a2e3bb79 100644 --- a/tests/test_graceful_shutdown.py +++ b/tests/test_graceful_shutdown.py @@ -40,7 +40,7 @@ def test_no_exceptions_when_cancel_pending_request(app, caplog): assert counter[logging.INFO] == 5 assert logging.ERROR not in counter - assert ( - caplog.record_tuples[3][2] - == "Request: GET http://127.0.0.1:8000/ stopped. Transport is closed." + assert any( + r[2] == "Request: GET http://127.0.0.1:8000/ stopped. Transport is closed." + for r in caplog.record_tuples ) From a1f36609b4d7ea23acd33c08ac5781a215483cee Mon Sep 17 00:00:00 2001 From: prryplatypus <25409753+prryplatypus@users.noreply.github.com> Date: Sat, 23 Oct 2021 21:40:57 +0200 Subject: [PATCH 3/4] Fix logging --- tests/test_logging.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index 639bb2ee..33b0dcdf 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -187,17 +187,17 @@ def test_logger_static_and_secure(caplog): port = test_client.port - assert caplog.record_tuples[0] == ( + assert ( "sanic.root", logging.INFO, f"Goin' Fast @ https://127.0.0.1:{port}", - ) - assert caplog.record_tuples[1] == ( + ) in caplog.record_tuples + assert ( "sanic.root", logging.INFO, f"https://127.0.0.1:{port}/", - ) - assert caplog.record_tuples[2] == ("sanic.root", logging.INFO, rand_string) + ) in caplog.record_tuples + assert ("sanic.root", logging.INFO, rand_string) in caplog.record_tuples assert caplog.record_tuples[-1] == ( "sanic.root", logging.INFO, From 0d1e591a9be7a592338c1e8a0d3958cac7bda2c7 Mon Sep 17 00:00:00 2001 From: prryplatypus <25409753+prryplatypus@users.noreply.github.com> Date: Sat, 23 Oct 2021 21:42:46 +0200 Subject: [PATCH 4/4] Fix logo tests? --- sanic/app.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index e122a3f9..ca7e6a7d 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1010,8 +1010,6 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): "#asynchronous-support" ) - self._configure_event_loop() - if auto_reload or auto_reload is None and debug: self.auto_reload = True if os.environ.get("SANIC_SERVER_RUNNING") != "true": @@ -1134,7 +1132,6 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): WebSocketProtocol if self.websocket_enabled else HttpProtocol ) - self._configure_event_loop() # if access_log is passed explicitly change config.ACCESS_LOG if access_log is not None: self.config.ACCESS_LOG = access_log @@ -1146,7 +1143,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): ssl=ssl, sock=sock, unix=unix, - loop=get_event_loop(), + create_loop=True, protocol=protocol, backlog=backlog, run_async=return_asyncio_server, @@ -1256,7 +1253,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): sock=None, unix=None, workers=1, - loop=None, + create_loop=None, protocol=HttpProtocol, backlog=100, register_sys_signals=True, @@ -1293,7 +1290,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): "ssl": ssl, "app": self, "signal": ServerSignal(), - "loop": loop, + "loop": None, "register_sys_signals": register_sys_signals, "backlog": backlog, } @@ -1324,6 +1321,10 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): else BASE_LOGO ) + self._configure_event_loop() + if create_loop: + server_settings["loop"] = get_event_loop() + if run_async: server_settings["run_async"] = True