This commit is contained in:
Adam Hopkins
2022-12-12 11:26:29 +02:00
parent 4e4e2b036b
commit 0e7ee94574
2 changed files with 85 additions and 5 deletions

View File

@@ -1,13 +1,18 @@
import re
import signal
import threading
from asyncio import Event
from logging import DEBUG
from pathlib import Path
from unittest.mock import Mock
from unittest.mock import Mock, patch
import pytest
from sanic.app import Sanic
from sanic.constants import RestartOrder
from sanic.worker.loader import AppLoader
from sanic.worker.process import ProcessState, WorkerProcess
from sanic.worker.reloader import Reloader
@@ -154,3 +159,58 @@ def test_check_file(tmp_path):
assert Reloader.check_file(current, mtimes) is False
mtimes[current] = mtimes[current] - 1
assert Reloader.check_file(current, mtimes) is True
@pytest.mark.parametrize(
"order,expected",
(
(
"shutdown_first",
[
"Restarting a process",
"Begin restart termination",
"Starting a process",
],
),
(
"startup_first",
[
"Restarting a process",
"Starting a process",
"Begin restart termination",
"Process acked. Terminating",
],
),
),
)
def test_default_reload_shutdown_order(monkeypatch, caplog, order, expected):
current_process = Mock()
worker_process = WorkerProcess(
lambda **_: current_process,
"Test",
lambda **_: ...,
{},
{},
RestartOrder[order.upper()],
)
def start(self):
worker_process.set_state(ProcessState.ACKED)
self._target()
monkeypatch.setattr(threading.Thread, "start", start)
with caplog.at_level(DEBUG):
worker_process.restart()
ansi = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
def clean(msg: str):
msg, _ = ansi.sub("", msg).split(":", 1)
return msg
debug = [clean(record[2]) for record in caplog.record_tuples]
assert debug == expected
current_process.start.assert_called_once()
current_process.terminate.assert_called_once()