|
| 1 | +import importlib.util |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | + |
| 8 | +MODULE_PATH = Path(__file__).with_name("launch_backend.py") |
| 9 | +SPEC = importlib.util.spec_from_file_location("launch_backend_under_test", MODULE_PATH) |
| 10 | +if SPEC is None or SPEC.loader is None: |
| 11 | + raise RuntimeError(f"Cannot load launch_backend module from {MODULE_PATH}") |
| 12 | +launch_backend = importlib.util.module_from_spec(SPEC) |
| 13 | +SPEC.loader.exec_module(launch_backend) |
| 14 | + |
| 15 | + |
| 16 | +class StartupHeartbeatTests(unittest.TestCase): |
| 17 | + def test_atomic_write_json_cleans_up_temp_file_when_replace_fails(self) -> None: |
| 18 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 19 | + heartbeat_path = Path(temp_dir) / "heartbeat.json" |
| 20 | + temp_path = heartbeat_path.with_name(f"{heartbeat_path.name}.tmp") |
| 21 | + |
| 22 | + with mock.patch.object( |
| 23 | + Path, |
| 24 | + "replace", |
| 25 | + autospec=True, |
| 26 | + side_effect=OSError("replace failed"), |
| 27 | + ): |
| 28 | + with self.assertRaises(OSError): |
| 29 | + launch_backend.atomic_write_json( |
| 30 | + heartbeat_path, |
| 31 | + {"pid": 42, "state": "starting", "updated_at_ms": 5000}, |
| 32 | + ) |
| 33 | + |
| 34 | + self.assertFalse(temp_path.exists()) |
| 35 | + |
| 36 | + def test_repeated_failures_warn_before_first_success(self) -> None: |
| 37 | + stop_event = mock.Mock() |
| 38 | + stop_event.wait.side_effect = [False, True] |
| 39 | + |
| 40 | + with mock.patch.object( |
| 41 | + launch_backend, |
| 42 | + "write_startup_heartbeat", |
| 43 | + side_effect=[False, False], |
| 44 | + ) as write_mock: |
| 45 | + launch_backend.heartbeat_loop(Path("/tmp/heartbeat.json"), 2.0, stop_event) |
| 46 | + |
| 47 | + self.assertEqual( |
| 48 | + [call.kwargs["warn_on_error"] for call in write_mock.call_args_list], |
| 49 | + [True, True], |
| 50 | + ) |
| 51 | + |
| 52 | + def test_repeated_failures_after_success_are_suppressed(self) -> None: |
| 53 | + stop_event = mock.Mock() |
| 54 | + stop_event.wait.side_effect = [False, False, True] |
| 55 | + |
| 56 | + with mock.patch.object( |
| 57 | + launch_backend, |
| 58 | + "write_startup_heartbeat", |
| 59 | + side_effect=[True, False, False], |
| 60 | + ) as write_mock: |
| 61 | + launch_backend.heartbeat_loop(Path("/tmp/heartbeat.json"), 2.0, stop_event) |
| 62 | + |
| 63 | + self.assertEqual( |
| 64 | + [call.kwargs["warn_on_error"] for call in write_mock.call_args_list], |
| 65 | + [True, True, False], |
| 66 | + ) |
| 67 | + |
| 68 | + def test_stop_failure_still_warns_after_earlier_failure(self) -> None: |
| 69 | + stop_event = mock.Mock() |
| 70 | + thread = mock.Mock() |
| 71 | + register = mock.Mock() |
| 72 | + |
| 73 | + with mock.patch.object( |
| 74 | + launch_backend, |
| 75 | + "write_startup_heartbeat", |
| 76 | + return_value=False, |
| 77 | + ) as write_mock: |
| 78 | + with mock.patch.object( |
| 79 | + launch_backend, |
| 80 | + "resolve_startup_heartbeat_path", |
| 81 | + return_value=Path("/tmp/heartbeat.json"), |
| 82 | + ): |
| 83 | + with mock.patch.object( |
| 84 | + launch_backend.threading, "Event", return_value=stop_event |
| 85 | + ): |
| 86 | + with mock.patch.object( |
| 87 | + launch_backend.threading, "Thread", return_value=thread |
| 88 | + ): |
| 89 | + with mock.patch.object( |
| 90 | + launch_backend.atexit, "register", register |
| 91 | + ): |
| 92 | + launch_backend.start_startup_heartbeat() |
| 93 | + thread.join.assert_not_called() |
| 94 | + on_exit = register.call_args.args[0] |
| 95 | + on_exit() |
| 96 | + |
| 97 | + thread.join.assert_called_once_with( |
| 98 | + timeout=launch_backend.STARTUP_HEARTBEAT_STOP_JOIN_TIMEOUT_SECONDS |
| 99 | + ) |
| 100 | + self.assertEqual( |
| 101 | + [call.args[1] for call in write_mock.call_args_list], |
| 102 | + ["stopping"], |
| 103 | + ) |
| 104 | + self.assertEqual( |
| 105 | + [call.kwargs["warn_on_error"] for call in write_mock.call_args_list], |
| 106 | + [True], |
| 107 | + ) |
| 108 | + |
| 109 | + def test_start_startup_heartbeat_does_not_register_exit_handler_when_thread_start_fails( |
| 110 | + self, |
| 111 | + ) -> None: |
| 112 | + stop_event = mock.Mock() |
| 113 | + thread = mock.Mock() |
| 114 | + thread.start.side_effect = RuntimeError("thread start failed") |
| 115 | + register = mock.Mock() |
| 116 | + |
| 117 | + with mock.patch.object( |
| 118 | + launch_backend, |
| 119 | + "resolve_startup_heartbeat_path", |
| 120 | + return_value=Path("/tmp/heartbeat.json"), |
| 121 | + ): |
| 122 | + with mock.patch.object( |
| 123 | + launch_backend.threading, "Event", return_value=stop_event |
| 124 | + ): |
| 125 | + with mock.patch.object( |
| 126 | + launch_backend.threading, "Thread", return_value=thread |
| 127 | + ): |
| 128 | + with mock.patch.object(launch_backend.atexit, "register", register): |
| 129 | + with self.assertRaises(RuntimeError): |
| 130 | + launch_backend.start_startup_heartbeat() |
| 131 | + |
| 132 | + register.assert_not_called() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + unittest.main() |
0 commit comments