Merge branch 'main' of github.com:sanic-org/sanic into monitor-restart

This commit is contained in:
Adam Hopkins 2022-12-12 09:41:09 +02:00
commit 4e4e2b036b
No known key found for this signature in database
GPG Key ID: 9F85EE6C807303FB
2 changed files with 31 additions and 3 deletions

View File

@ -21,9 +21,14 @@ class WorkerMultiplexer:
"state": ProcessState.ACKED.name, "state": ProcessState.ACKED.name,
} }
def restart(self, name: str = ""): def restart(self, name: str = "", all_workers: bool = False):
if name and all_workers:
raise ValueError(
"Ambiguous restart with both a named process and"
" all_workers=True"
)
if not name: if not name:
name = self.name name = "__ALL_PROCESSES__:" if all_workers else self.name
self._monitor_publisher.send(name) self._monitor_publisher.send(name)
reload = restart # no cov reload = restart # no cov

View File

@ -1,6 +1,6 @@
from multiprocessing import Event from multiprocessing import Event
from os import environ, getpid from os import environ, getpid
from typing import Any, Dict from typing import Any, Dict, Type, Union
from unittest.mock import Mock from unittest.mock import Mock
import pytest import pytest
@ -117,3 +117,26 @@ def test_properties(
assert m.workers == worker_state assert m.workers == worker_state
assert m.state == worker_state["Test"] assert m.state == worker_state["Test"]
assert isinstance(m.state, WorkerState) assert isinstance(m.state, WorkerState)
@pytest.mark.parametrize(
"params,expected",
(
({}, "Test"),
({"name": "foo"}, "foo"),
({"all_workers": True}, "__ALL_PROCESSES__:"),
({"name": "foo", "all_workers": True}, ValueError),
),
)
def test_restart_params(
monitor_publisher: Mock,
m: WorkerMultiplexer,
params: Dict[str, Any],
expected: Union[str, Type[Exception]],
):
if isinstance(expected, str):
m.restart(**params)
monitor_publisher.send.assert_called_once_with(expected)
else:
with pytest.raises(expected):
m.restart(**params)