|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import ctypes |
5 | 6 | from pathlib import Path |
| 7 | +import subprocess |
| 8 | +import sys |
6 | 9 | from unittest.mock import MagicMock, patch |
7 | 10 |
|
8 | 11 | import pytest |
@@ -98,6 +101,96 @@ def test_daemon_pid_lifecycle(tmp_path: Path, monkeypatch) -> None: |
98 | 101 | assert server.get_running_daemon_pid(port) is None |
99 | 102 |
|
100 | 103 |
|
| 104 | +@pytest.mark.parametrize( |
| 105 | + ("open_result", "last_error", "wait_result", "expected"), |
| 106 | + ( |
| 107 | + (0, 87, None, False), |
| 108 | + (0, 5, None, True), |
| 109 | + (123, 0, 0x00000102, True), |
| 110 | + (123, 0, 0x00000000, False), |
| 111 | + (123, 0, 0xFFFFFFFF, True), |
| 112 | + ), |
| 113 | +) |
| 114 | +def test_windows_pid_probe_is_non_destructive_and_conservative( |
| 115 | + open_result: int, |
| 116 | + last_error: int, |
| 117 | + wait_result: int | None, |
| 118 | + expected: bool, |
| 119 | +) -> None: |
| 120 | + kernel32 = MagicMock() |
| 121 | + kernel32.OpenProcess.return_value = open_result |
| 122 | + kernel32.WaitForSingleObject.return_value = wait_result |
| 123 | + |
| 124 | + with ( |
| 125 | + patch.object(ctypes, "WinDLL", return_value=kernel32, create=True) as win_dll, |
| 126 | + patch.object(ctypes, "get_last_error", return_value=last_error, create=True), |
| 127 | + ): |
| 128 | + assert server._is_windows_pid_running(4242) is expected |
| 129 | + |
| 130 | + win_dll.assert_called_once_with("kernel32", use_last_error=True) |
| 131 | + kernel32.OpenProcess.assert_called_once_with(0x00100000, False, 4242) |
| 132 | + if open_result: |
| 133 | + kernel32.WaitForSingleObject.assert_called_once_with(open_result, 0) |
| 134 | + kernel32.CloseHandle.assert_called_once_with(open_result) |
| 135 | + else: |
| 136 | + kernel32.WaitForSingleObject.assert_not_called() |
| 137 | + kernel32.CloseHandle.assert_not_called() |
| 138 | + |
| 139 | + |
| 140 | +def test_is_pid_running_uses_windows_probe() -> None: |
| 141 | + with ( |
| 142 | + patch.object(server.sys, "platform", "win32"), |
| 143 | + patch.object(server, "_is_windows_pid_running", return_value=True) as probe, |
| 144 | + patch.object(server.os, "kill") as kill, |
| 145 | + ): |
| 146 | + assert server.is_pid_running(4242) is True |
| 147 | + |
| 148 | + probe.assert_called_once_with(4242) |
| 149 | + kill.assert_not_called() |
| 150 | + |
| 151 | + |
| 152 | +def test_is_pid_running_rejects_out_of_range_windows_pid() -> None: |
| 153 | + with ( |
| 154 | + patch.object(server.sys, "platform", "win32"), |
| 155 | + patch.object(server, "_is_windows_pid_running") as probe, |
| 156 | + patch.object(server.os, "kill") as kill, |
| 157 | + ): |
| 158 | + assert server.is_pid_running(0x100000000) is False |
| 159 | + |
| 160 | + probe.assert_not_called() |
| 161 | + kill.assert_not_called() |
| 162 | + |
| 163 | + |
| 164 | +def test_windows_live_pid_file_is_retained_and_blocks_duplicate(tmp_path: Path, monkeypatch) -> None: |
| 165 | + monkeypatch.setattr(server, "CONFIG_DIR", tmp_path) |
| 166 | + port = 8995 |
| 167 | + pid_path = server.get_daemon_pid_path(port) |
| 168 | + pid_path.write_text("4242", encoding="utf-8") |
| 169 | + |
| 170 | + with ( |
| 171 | + patch.object(server.sys, "platform", "win32"), |
| 172 | + patch.object(server, "_is_windows_pid_running", return_value=True) as probe, |
| 173 | + ): |
| 174 | + assert server.get_running_daemon_pid(port) == 4242 |
| 175 | + assert pid_path.exists() |
| 176 | + assert server.acquire_daemon_lock(port) is False |
| 177 | + |
| 178 | + probe.assert_called_once_with(4242) |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.skipif(sys.platform != "win32", reason="requires Windows process APIs") |
| 182 | +def test_windows_pid_probe_preserves_live_child() -> None: |
| 183 | + child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"]) |
| 184 | + try: |
| 185 | + assert server.is_pid_running(child.pid) is True |
| 186 | + assert child.poll() is None |
| 187 | + finally: |
| 188 | + child.terminate() |
| 189 | + child.wait(timeout=10) |
| 190 | + |
| 191 | + assert server.is_pid_running(child.pid) is False |
| 192 | + |
| 193 | + |
101 | 194 | def test_daemon_stale_pid_cleanup(tmp_path: Path, monkeypatch) -> None: |
102 | 195 | monkeypatch.setattr(server, "CONFIG_DIR", tmp_path) |
103 | 196 | port = 8998 |
|
0 commit comments