Skip to content

Commit a000e30

Browse files
test: poll child started marker in msvcrt cross-process lock test
1 parent cac3994 commit a000e30

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

tests/test_export_state_store.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212

1313
REPO_ROOT = Path(__file__).resolve().parent.parent
1414

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+
1524
from utils.export_state_store import (
1625
atomic_write_export_state,
1726
export_state_lock,
@@ -119,22 +128,33 @@ def test_export_state_lock_blocks_second_process(tmp_path: Path) -> None:
119128
"""While the parent holds ``msvcrt`` lock, a child process cannot acquire it."""
120129
state_file = tmp_path / "export_state.json"
121130
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"
123133
child = (
124134
"import sys\n"
125135
"from pathlib import Path\n"
126136
f"sys.path.insert(0, {str(REPO_ROOT)!r})\n"
127137
"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"
129140
"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"
131142
)
132143
with export_state_lock(str(state_file)):
133144
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+
],
135153
cwd=str(REPO_ROOT),
136154
)
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+
)
139159
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

Comments
 (0)