Files
live-hub-py/tests/test_background_runtime.py
T

127 lines
3.8 KiB
Python

"""独立后台进程生命周期测试。"""
from __future__ import annotations
from web.backend import background_runtime
def test_background_services_start_and_stop_once(monkeypatch):
calls: list[str] = []
def mark(name: str):
return lambda *_args, **_kwargs: calls.append(name) or True
monkeypatch.setattr(
"web.backend.services.automation.register_default_jobs",
mark("register"),
)
monkeypatch.setattr(
"web.backend.services.automation.start_scheduler",
mark("scheduler"),
)
monkeypatch.setattr(
"web.backend.services.automation.stop_scheduler",
mark("stop-scheduler"),
)
monkeypatch.setattr(
"web.backend.services.execution_worker.start_execution_job_worker",
mark("execution"),
)
monkeypatch.setattr(
"web.backend.services.execution_worker.stop_execution_job_worker",
mark("stop-execution"),
)
monkeypatch.setattr(
"web.backend.services.platform_fulfillment.reconciler."
"start_platform_fulfillment_reconciler",
mark("reconciler"),
)
monkeypatch.setattr(
"web.backend.services.platform_fulfillment.reconciler."
"stop_platform_fulfillment_reconciler",
mark("stop-reconciler"),
)
monkeypatch.setattr(
"web.backend.services.platform_fulfillment.auto_allocator."
"start_platform_fulfillment_auto_allocator",
mark("allocator"),
)
monkeypatch.setattr(
"web.backend.services.platform_fulfillment.auto_allocator."
"stop_platform_fulfillment_auto_allocator",
mark("stop-allocator"),
)
monkeypatch.setattr(
"web.backend.services.binding_poll_scheduler."
"start_binding_poll_scheduler",
mark("binding"),
)
monkeypatch.setattr(
"web.backend.services.binding_poll_scheduler."
"stop_binding_poll_scheduler",
mark("stop-binding"),
)
monkeypatch.setattr(
"web.backend.services.payment_poll_scheduler."
"start_payment_poll_scheduler",
mark("payment"),
)
monkeypatch.setattr(
"web.backend.services.payment_poll_scheduler."
"stop_payment_poll_scheduler",
mark("stop-payment"),
)
monkeypatch.setattr(
"web.backend.services.integration.start_integration_outbox_worker",
mark("outbox"),
)
monkeypatch.setattr(
"web.backend.services.integration.stop_integration_outbox_worker",
mark("stop-outbox"),
)
assert background_runtime.start_background_services() is True
assert background_runtime.start_background_services() is False
background_runtime.stop_background_services()
background_runtime.stop_background_services()
assert calls == [
"execution",
"register",
"scheduler",
"outbox",
"reconciler",
"allocator",
"binding",
"payment",
"stop-payment",
"stop-binding",
"stop-allocator",
"stop-reconciler",
"stop-outbox",
"stop-scheduler",
"stop-execution",
]
def test_execution_only_worker_can_disable_control_plane(monkeypatch):
calls: list[str] = []
monkeypatch.setenv("BACKGROUND_CONTROL_PLANE_ENABLED", "false")
monkeypatch.setattr(
"web.backend.services.execution_worker.start_execution_job_worker",
lambda: calls.append("start") or True,
)
monkeypatch.setattr(
"web.backend.services.execution_worker.stop_execution_job_worker",
lambda: calls.append("stop"),
)
monkeypatch.setattr(
"web.backend.services.automation.start_scheduler",
lambda: (_ for _ in ()).throw(AssertionError("control plane must stay off")),
)
assert background_runtime.start_background_services() is True
background_runtime.stop_background_services()
assert calls == ["start", "stop"]