|
12 | 12 |
|
13 | 13 | REPO_ROOT = Path(__file__).resolve().parent.parent |
14 | 14 |
|
| 15 | + |
| 16 | +def _wait_for_file(path: Path, timeout: float = 5.0) -> None: |
| 17 | + deadline = time.monotonic() + timeout |
| 18 | + while time.monotonic() < deadline: |
| 19 | + if path.is_file(): |
| 20 | + return |
| 21 | + time.sleep(0.01) |
| 22 | + raise AssertionError(f"timed out waiting for {path}") |
| 23 | + |
15 | 24 | from utils.export_state_store import ( |
16 | 25 | atomic_write_export_state, |
17 | 26 | export_state_lock, |
@@ -119,22 +128,33 @@ def test_export_state_lock_blocks_second_process(tmp_path: Path) -> None: |
119 | 128 | """While the parent holds ``msvcrt`` lock, a child process cannot acquire it.""" |
120 | 129 | state_file = tmp_path / "export_state.json" |
121 | 130 | state_file.write_text("{}", encoding="utf-8") |
122 | | - marker = tmp_path / "child_acquired.lock" |
| 131 | + started_marker = tmp_path / "child_started.lock" |
| 132 | + acquired_marker = tmp_path / "child_acquired.lock" |
123 | 133 | child = ( |
124 | 134 | "import sys\n" |
125 | 135 | "from pathlib import Path\n" |
126 | 136 | f"sys.path.insert(0, {str(REPO_ROOT)!r})\n" |
127 | 137 | "from utils.export_state_store import export_state_lock\n" |
128 | | - "path, marker = sys.argv[1], sys.argv[2]\n" |
| 138 | + "path, started, acquired = sys.argv[1], sys.argv[2], sys.argv[3]\n" |
| 139 | + "Path(started).write_text('ok', encoding='utf-8')\n" |
129 | 140 | "with export_state_lock(path):\n" |
130 | | - " Path(marker).write_text('ok', encoding='utf-8')\n" |
| 141 | + " Path(acquired).write_text('ok', encoding='utf-8')\n" |
131 | 142 | ) |
132 | 143 | with export_state_lock(str(state_file)): |
133 | 144 | proc = subprocess.Popen( |
134 | | - [sys.executable, "-c", child, str(state_file), str(marker)], |
| 145 | + [ |
| 146 | + sys.executable, |
| 147 | + "-c", |
| 148 | + child, |
| 149 | + str(state_file), |
| 150 | + str(started_marker), |
| 151 | + str(acquired_marker), |
| 152 | + ], |
135 | 153 | cwd=str(REPO_ROOT), |
136 | 154 | ) |
137 | | - time.sleep(0.5) |
138 | | - assert not marker.is_file(), "child acquired lock while parent still holds it" |
| 155 | + _wait_for_file(started_marker) |
| 156 | + assert not acquired_marker.is_file(), ( |
| 157 | + "child acquired lock while parent still holds it" |
| 158 | + ) |
139 | 159 | assert proc.wait(timeout=10) == 0 |
140 | | - assert marker.read_text(encoding="utf-8") == "ok" |
| 160 | + assert acquired_marker.read_text(encoding="utf-8") == "ok" |
0 commit comments