Skip to content

Commit

Permalink
Downgrade warnings to backwater debug messages (#2382)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Jan 19, 2022
1 parent bb517dd commit ac388d6
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 29 deletions.
56 changes: 31 additions & 25 deletions sanic/asgi.py
@@ -1,45 +1,51 @@
from __future__ import annotations

import warnings

from typing import Optional
from typing import TYPE_CHECKING, Optional
from urllib.parse import quote

import sanic.app # noqa

from sanic.compat import Header
from sanic.exceptions import ServerError
from sanic.helpers import _default
from sanic.http import Stage
from sanic.log import logger
from sanic.models.asgi import ASGIReceive, ASGIScope, ASGISend, MockTransport
from sanic.request import Request
from sanic.response import BaseHTTPResponse
from sanic.server import ConnInfo
from sanic.server.websockets.connection import WebSocketConnection


if TYPE_CHECKING: # no cov
from sanic import Sanic


class Lifespan:
def __init__(self, asgi_app: "ASGIApp") -> None:
def __init__(self, asgi_app: ASGIApp) -> None:
self.asgi_app = asgi_app

if (
"server.init.before"
in self.asgi_app.sanic_app.signal_router.name_index
):
warnings.warn(
'You have set a listener for "before_server_start" '
"in ASGI mode. "
"It will be executed as early as possible, but not before "
"the ASGI server is started."
)
if (
"server.shutdown.after"
in self.asgi_app.sanic_app.signal_router.name_index
):
warnings.warn(
'You have set a listener for "after_server_stop" '
"in ASGI mode. "
"It will be executed as late as possible, but not after "
"the ASGI server is stopped."
)
if self.asgi_app.sanic_app.state.verbosity > 0:
if (
"server.init.before"
in self.asgi_app.sanic_app.signal_router.name_index
):
logger.debug(
'You have set a listener for "before_server_start" '
"in ASGI mode. "
"It will be executed as early as possible, but not before "
"the ASGI server is started."
)
if (
"server.shutdown.after"
in self.asgi_app.sanic_app.signal_router.name_index
):
logger.debug(
'You have set a listener for "after_server_stop" '
"in ASGI mode. "
"It will be executed as late as possible, but not after "
"the ASGI server is stopped."
)

async def startup(self) -> None:
"""
Expand Down Expand Up @@ -88,7 +94,7 @@ async def __call__(


class ASGIApp:
sanic_app: "sanic.app.Sanic"
sanic_app: Sanic
request: Request
transport: MockTransport
lifespan: Lifespan
Expand Down
114 changes: 110 additions & 4 deletions tests/test_asgi.py
@@ -1,11 +1,13 @@
import asyncio
import logging

from collections import deque, namedtuple

import pytest
import uvicorn

from sanic import Sanic
from sanic.application.state import Mode
from sanic.asgi import MockTransport
from sanic.exceptions import Forbidden, InvalidUsage, ServiceUnavailable
from sanic.request import Request
Expand Down Expand Up @@ -44,7 +46,7 @@ def protocol(transport):
return transport.get_protocol()


def test_listeners_triggered():
def test_listeners_triggered(caplog):
app = Sanic("app")
before_server_start = False
after_server_start = False
Expand Down Expand Up @@ -82,9 +84,31 @@ def install_signal_handlers(self):
config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0)
server = CustomServer(config=config)

with pytest.warns(UserWarning):
start_message = (
'You have set a listener for "before_server_start" in ASGI mode. '
"It will be executed as early as possible, but not before the ASGI "
"server is started."
)
stop_message = (
'You have set a listener for "after_server_stop" in ASGI mode. '
"It will be executed as late as possible, but not after the ASGI "
"server is stopped."
)

with caplog.at_level(logging.DEBUG):
server.run()

assert (
"sanic.root",
logging.DEBUG,
start_message,
) not in caplog.record_tuples
assert (
"sanic.root",
logging.DEBUG,
stop_message,
) not in caplog.record_tuples

all_tasks = asyncio.all_tasks(asyncio.get_event_loop())
for task in all_tasks:
task.cancel()
Expand All @@ -94,8 +118,38 @@ def install_signal_handlers(self):
assert before_server_stop
assert after_server_stop

app.state.mode = Mode.DEBUG
with caplog.at_level(logging.DEBUG):
server.run()

assert (
"sanic.root",
logging.DEBUG,
start_message,
) not in caplog.record_tuples
assert (
"sanic.root",
logging.DEBUG,
stop_message,
) not in caplog.record_tuples

app.state.verbosity = 2
with caplog.at_level(logging.DEBUG):
server.run()

assert (
"sanic.root",
logging.DEBUG,
start_message,
) in caplog.record_tuples
assert (
"sanic.root",
logging.DEBUG,
stop_message,
) in caplog.record_tuples


def test_listeners_triggered_async(app):
def test_listeners_triggered_async(app, caplog):
before_server_start = False
after_server_start = False
before_server_stop = False
Expand Down Expand Up @@ -132,9 +186,31 @@ def install_signal_handlers(self):
config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0)
server = CustomServer(config=config)

with pytest.warns(UserWarning):
start_message = (
'You have set a listener for "before_server_start" in ASGI mode. '
"It will be executed as early as possible, but not before the ASGI "
"server is started."
)
stop_message = (
'You have set a listener for "after_server_stop" in ASGI mode. '
"It will be executed as late as possible, but not after the ASGI "
"server is stopped."
)

with caplog.at_level(logging.DEBUG):
server.run()

assert (
"sanic.root",
logging.DEBUG,
start_message,
) not in caplog.record_tuples
assert (
"sanic.root",
logging.DEBUG,
stop_message,
) not in caplog.record_tuples

all_tasks = asyncio.all_tasks(asyncio.get_event_loop())
for task in all_tasks:
task.cancel()
Expand All @@ -144,6 +220,36 @@ def install_signal_handlers(self):
assert before_server_stop
assert after_server_stop

app.state.mode = Mode.DEBUG
with caplog.at_level(logging.DEBUG):
server.run()

assert (
"sanic.root",
logging.DEBUG,
start_message,
) not in caplog.record_tuples
assert (
"sanic.root",
logging.DEBUG,
stop_message,
) not in caplog.record_tuples

app.state.verbosity = 2
with caplog.at_level(logging.DEBUG):
server.run()

assert (
"sanic.root",
logging.DEBUG,
start_message,
) in caplog.record_tuples
assert (
"sanic.root",
logging.DEBUG,
stop_message,
) in caplog.record_tuples


def test_non_default_uvloop_config_raises_warning(app):
app.config.USE_UVLOOP = True
Expand Down

0 comments on commit ac388d6

Please sign in to comment.