2022-01-16 07:03:04 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.application.state import ApplicationServerInfo
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def no_skip():
|
|
|
|
should_auto_reload = Sanic.should_auto_reload
|
|
|
|
Sanic.should_auto_reload = Mock(return_value=False)
|
|
|
|
yield
|
|
|
|
Sanic._app_registry = {}
|
|
|
|
Sanic.should_auto_reload = should_auto_reload
|
2022-09-18 15:17:23 +01:00
|
|
|
try:
|
|
|
|
del os.environ["SANIC_MOTD_OUTPUT"]
|
|
|
|
except KeyError:
|
|
|
|
...
|
2022-01-16 07:03:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_primary(app: Sanic) -> ApplicationServerInfo:
|
|
|
|
return app.state.server_info[0]
|
|
|
|
|
|
|
|
|
|
|
|
def test_dev(app: Sanic):
|
|
|
|
app.prepare(dev=True)
|
|
|
|
|
|
|
|
assert app.state.is_debug
|
|
|
|
assert app.state.auto_reload
|
|
|
|
|
|
|
|
|
|
|
|
def test_motd_display(app: Sanic):
|
|
|
|
app.prepare(motd_display={"foo": "bar"})
|
|
|
|
|
|
|
|
assert app.config.MOTD_DISPLAY["foo"] == "bar"
|
|
|
|
del app.config.MOTD_DISPLAY["foo"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("dirs", ("./foo", ("./foo", "./bar")))
|
|
|
|
def test_reload_dir(app: Sanic, dirs, caplog):
|
|
|
|
messages = []
|
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
|
app.prepare(reload_dir=dirs)
|
|
|
|
|
|
|
|
if isinstance(dirs, str):
|
|
|
|
dirs = (dirs,)
|
|
|
|
for d in dirs:
|
|
|
|
assert Path(d) in app.state.reload_dirs
|
|
|
|
messages.append(
|
|
|
|
f"Directory {d} could not be located",
|
|
|
|
)
|
|
|
|
|
|
|
|
for message in messages:
|
|
|
|
assert ("sanic.root", logging.WARNING, message) in caplog.record_tuples
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
def test_fast(app: Sanic, caplog):
|
|
|
|
@app.after_server_start
|
|
|
|
async def stop(app, _):
|
|
|
|
app.stop()
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
try:
|
|
|
|
workers = len(os.sched_getaffinity(0))
|
|
|
|
except AttributeError:
|
|
|
|
workers = os.cpu_count() or 1
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
with caplog.at_level(logging.INFO):
|
|
|
|
app.prepare(fast=True)
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
assert app.state.fast
|
|
|
|
assert app.state.workers == workers
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
messages = [m[2] for m in caplog.record_tuples]
|
2022-01-16 07:03:04 +00:00
|
|
|
assert f"mode: production, goin' fast w/ {workers} workers" in messages
|