"""独立后台进程生命周期测试。""" from __future__ import annotations import json from pathlib import Path from web.backend import background_runtime def test_background_services_start_and_stop_once(monkeypatch, tmp_path): calls: list[str] = [] heartbeat_path = tmp_path / "background_runtime.json" monkeypatch.setenv("BACKGROUND_RUNTIME_HEARTBEAT_PATH", str(heartbeat_path)) 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 not heartbeat_path.exists() 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, tmp_path): calls: list[str] = [] monkeypatch.setenv("BACKGROUND_CONTROL_PLANE_ENABLED", "false") monkeypatch.setenv( "BACKGROUND_RUNTIME_HEARTBEAT_PATH", str(tmp_path / "background_runtime.json"), ) 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"] def test_background_runtime_heartbeat_reports_scheduler_process( monkeypatch, tmp_path, ): heartbeat_path = tmp_path / "background_runtime.json" monkeypatch.setenv("BACKGROUND_RUNTIME_HEARTBEAT_PATH", str(heartbeat_path)) monkeypatch.setattr( "web.backend.services.automation.scheduler.scheduler_status", lambda: { "enabled": True, "running": True, "tick_seconds": 5, "job_interval_seconds": 300, "registered": ["ck_relogin"], }, ) background_runtime._write_runtime_heartbeat() status = background_runtime.background_runtime_status() payload = json.loads(Path(heartbeat_path).read_text(encoding="utf-8")) assert status["running"] is True assert status["pid"] == payload["pid"] assert status["scheduler"]["running"] is True assert status["ageSeconds"] is not None