4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
"""Tests for the Telegram gateway daemon lifecycle helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from gateway import daemon
|
|
|
|
_SLEEPER = (sys.executable, "-c", "import time; time.sleep(30)")
|
|
_CRASHER = (sys.executable, "-c", "print('boom'); raise SystemExit(1)")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _isolated_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(daemon, "GATEWAY_PID_FILE", tmp_path / "gateway.pid")
|
|
monkeypatch.setattr(daemon, "GATEWAY_LOG_FILE", tmp_path / "gateway.log")
|
|
monkeypatch.setattr(daemon, "GATEWAY_COMPONENTS_FILE", tmp_path / "components.json")
|
|
yield
|
|
daemon.stop_gateway_daemon(timeout=5.0)
|
|
|
|
|
|
def test_pid_is_none_without_pidfile() -> None:
|
|
assert daemon.gateway_daemon_pid() is None
|
|
|
|
|
|
def test_stale_pidfile_is_cleared(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
daemon.GATEWAY_PID_FILE.write_text("12345\n")
|
|
monkeypatch.setattr(daemon, "_alive", lambda _pid: False)
|
|
|
|
assert daemon.gateway_daemon_pid() is None
|
|
assert not daemon.GATEWAY_PID_FILE.exists()
|
|
|
|
|
|
def test_garbage_pidfile_is_ignored() -> None:
|
|
daemon.GATEWAY_PID_FILE.write_text("not-a-pid\n")
|
|
assert daemon.gateway_daemon_pid() is None
|
|
|
|
|
|
def test_start_stop_round_trip() -> None:
|
|
ok, message = daemon.start_gateway_daemon(startup_wait=0.3, argv=_SLEEPER)
|
|
assert ok, message
|
|
pid = daemon.gateway_daemon_pid()
|
|
assert pid is not None
|
|
assert f"pid {pid}" in message
|
|
|
|
ok, message = daemon.start_gateway_daemon(startup_wait=0.3, argv=_SLEEPER)
|
|
assert ok
|
|
assert "already running" in message
|
|
|
|
ok, message = daemon.stop_gateway_daemon(timeout=5.0)
|
|
assert ok, message
|
|
assert daemon.gateway_daemon_pid() is None
|
|
assert not daemon.GATEWAY_PID_FILE.exists()
|
|
|
|
|
|
def test_start_reports_startup_crash_with_log_tail() -> None:
|
|
ok, message = daemon.start_gateway_daemon(startup_wait=5.0, argv=_CRASHER)
|
|
assert not ok
|
|
assert "exited during startup" in message
|
|
assert "boom" in message
|
|
assert daemon.gateway_daemon_pid() is None
|
|
|
|
|
|
def test_stop_escalates_to_sigkill_when_sigterm_is_ignored() -> None:
|
|
stubborn = (
|
|
sys.executable,
|
|
"-c",
|
|
"import signal, time; signal.signal(signal.SIGTERM, signal.SIG_IGN); time.sleep(60)",
|
|
)
|
|
ok, message = daemon.start_gateway_daemon(startup_wait=0.5, argv=stubborn)
|
|
assert ok, message
|
|
|
|
ok, message = daemon.stop_gateway_daemon(timeout=1.0)
|
|
assert ok, message
|
|
assert "force-killed" in message
|
|
assert daemon.gateway_daemon_pid() is None
|
|
|
|
|
|
def test_stop_when_not_running_is_ok() -> None:
|
|
ok, message = daemon.stop_gateway_daemon()
|
|
assert ok
|
|
assert "not running" in message
|
|
|
|
|
|
def test_component_status_round_trip() -> None:
|
|
components = {"web": "serving :8000", "telegram": "polling", "scheduler": "idle"}
|
|
daemon.write_component_status(components)
|
|
|
|
assert daemon.read_component_status() == components # writer pid (ours) is alive
|
|
|
|
daemon.clear_component_status()
|
|
assert daemon.read_component_status() == {}
|
|
|
|
|
|
def test_component_status_ignored_when_process_is_dead(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
daemon.write_component_status({"web": "serving :8000"})
|
|
monkeypatch.setattr(daemon, "_alive", lambda _pid: False)
|
|
|
|
assert daemon.read_component_status() == {}
|
|
|
|
|
|
def test_log_tail_returns_last_lines() -> None:
|
|
daemon.GATEWAY_LOG_FILE.write_text("".join(f"line-{i}\n" for i in range(100)))
|
|
tail = daemon.read_gateway_log_tail(3)
|
|
assert tail == "line-97\nline-98\nline-99"
|
|
assert daemon.read_gateway_log_tail(0) == ""
|