Allow fork in limited cases (#2624)

This commit is contained in:
Adam Hopkins
2022-12-15 11:49:26 +02:00
committed by GitHub
parent 064168f3c8
commit b276b91c21
16 changed files with 156 additions and 55 deletions

View File

@@ -1,13 +1,20 @@
from logging import ERROR, INFO
from signal import SIGINT, SIGKILL
from signal import SIGINT
from unittest.mock import Mock, call, patch
import pytest
from sanic.compat import OS_IS_WINDOWS
from sanic.exceptions import ServerKilled
from sanic.worker.manager import WorkerManager
if not OS_IS_WINDOWS:
from signal import SIGKILL
else:
SIGKILL = SIGINT
def fake_serve():
...

View File

@@ -1,3 +1,5 @@
import sys
from multiprocessing import Event
from os import environ, getpid
from typing import Any, Dict, Type, Union
@@ -6,6 +8,7 @@ from unittest.mock import Mock
import pytest
from sanic import Sanic
from sanic.compat import use_context
from sanic.worker.multiplexer import WorkerMultiplexer
from sanic.worker.state import WorkerState
@@ -28,6 +31,10 @@ def m(monitor_publisher, worker_state):
del environ["SANIC_WORKER_NAME"]
@pytest.mark.skipif(
sys.platform not in ("linux", "darwin"),
reason="This test requires fork context",
)
def test_has_multiplexer_default(app: Sanic):
event = Event()
@@ -41,7 +48,8 @@ def test_has_multiplexer_default(app: Sanic):
app.shared_ctx.event.set()
app.stop()
app.run()
with use_context("fork"):
app.run()
assert event.is_set()

View File

@@ -0,0 +1,25 @@
from unittest.mock import patch
import pytest
from sanic import Sanic
@pytest.mark.parametrize(
"start_method,platform,expected",
(
(None, "linux", "spawn"),
(None, "other", "spawn"),
("fork", "linux", "fork"),
("fork", "other", "fork"),
("forkserver", "linux", "forkserver"),
("forkserver", "other", "forkserver"),
("spawn", "linux", "spawn"),
("spawn", "other", "spawn"),
),
)
def test_get_context(start_method, platform, expected):
if start_method:
Sanic.start_method = start_method
with patch("sys.platform", platform):
assert Sanic._get_startup_method() == expected