fix(gateway): preserve systemd handoff recovery

This commit is contained in:
fangliquan
2026-08-22 07:51:54 +08:00
committed by kshitij
parent 5b024c7ccc
commit 91fb175188
4 changed files with 146 additions and 9 deletions
+75
View File
@@ -756,6 +756,81 @@ class TestGatewaySystemServiceRouting:
assert "21627" not in out # must use the mocked budget, not live defaults
assert "27" in out
def test_systemd_restart_forces_recovery_only_when_handoff_has_no_replacement(
self, monkeypatch, capsys
):
calls = []
monkeypatch.setattr(gateway_cli, "_select_systemd_scope", lambda system=False: False)
monkeypatch.setattr(gateway_cli, "_require_service_installed", lambda action, system=False: None)
monkeypatch.setattr(gateway_cli, "_preflight_user_systemd", lambda **kwargs: None)
monkeypatch.setattr(gateway_cli, "refresh_systemd_unit_if_needed", lambda system=False: None)
monkeypatch.setattr(gateway_cli, "_get_restart_exit_wait_budget", lambda: 27.0)
monkeypatch.setattr("gateway.status.get_running_pid", lambda: 654)
monkeypatch.setattr(gateway_cli, "_graceful_restart_via_sigusr1", lambda pid, timeout: True)
waits = iter((False, True))
monkeypatch.setattr(
gateway_cli,
"_wait_for_systemd_service_restart",
lambda system=False, previous_pid=None: next(waits),
)
monkeypatch.setattr(gateway_cli, "_systemd_service_is_start_limited", lambda system=False: False)
monkeypatch.setattr(
gateway_cli,
"_read_systemd_unit_properties",
lambda system=False, properties=None: {"ActiveState": "inactive", "MainPID": "0"},
)
monkeypatch.setattr(
gateway_cli,
"_run_systemctl",
lambda args, **kwargs: calls.append((args, kwargs))
or SimpleNamespace(returncode=0, stdout="", stderr=""),
)
gateway_cli.systemd_restart()
assert [call[0][0] for call in calls] == ["reset-failed", "restart"]
assert "did not relaunch" in capsys.readouterr().out
def test_systemd_restart_does_not_force_an_unready_replacement(self, monkeypatch):
calls = []
monkeypatch.setattr(gateway_cli, "_select_systemd_scope", lambda system=False: False)
monkeypatch.setattr(gateway_cli, "_require_service_installed", lambda action, system=False: None)
monkeypatch.setattr(gateway_cli, "_preflight_user_systemd", lambda **kwargs: None)
monkeypatch.setattr(gateway_cli, "refresh_systemd_unit_if_needed", lambda system=False: None)
monkeypatch.setattr(gateway_cli, "_get_restart_exit_wait_budget", lambda: 27.0)
monkeypatch.setattr("gateway.status.get_running_pid", lambda: 654)
monkeypatch.setattr(gateway_cli, "_graceful_restart_via_sigusr1", lambda pid, timeout: True)
monkeypatch.setattr(
gateway_cli,
"_wait_for_systemd_service_restart",
lambda system=False, previous_pid=None: False,
)
monkeypatch.setattr(gateway_cli, "_systemd_service_is_start_limited", lambda system=False: False)
monkeypatch.setattr(
gateway_cli,
"_read_systemd_unit_properties",
lambda system=False, properties=None: {"ActiveState": "active", "MainPID": "777"},
)
monkeypatch.setattr(gateway_cli, "_run_systemctl", lambda args, **kwargs: calls.append(args))
gateway_cli.systemd_restart()
assert calls == []
def test_systemd_restart_wait_timeout_includes_supervisor_budgets(self, monkeypatch):
monkeypatch.setattr(
gateway_cli,
"_read_systemd_unit_properties",
lambda system=False, properties=None: {
"RestartUSec": "5s",
"TimeoutStartUSec": "1min 30s",
},
)
assert gateway_cli._systemd_restart_wait_timeout() == 155.0