Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

fix(tools): web_check drains the browser's last output before declaring it exited; loop is testable

eyalgolanfcf2860d1d2b
diff --git a/tools/README.md b/tools/README.md
index 42564536b..55e7e1feb 100644
--- a/tools/README.md
+++ b/tools/README.md
@@ -23,7 +23,7 @@ Run from the repo root; this installs every pinned dependency (rasterio included
uv run --with pytest==9.1.1 --with rasterio==1.5.2 --with numpy==2.5.3 --with pyyaml==6.0.3 --with pillow==12.3.0 --with requests==2.32.5 pytest tools/tests -q
```
-Expected: `103 passed`. Omitting a dependency (e.g. rasterio) silently drops tests or fails collection.
+Expected: `106 passed`. Omitting a dependency (e.g. rasterio) silently drops tests or fails collection.
## Required attribution (Copernicus DEM licence, modified data)
diff --git a/tools/tests/test_web_check.py b/tools/tests/test_web_check.py
index d59c0a782..9c40db997 100644
--- a/tools/tests/test_web_check.py
+++ b/tools/tests/test_web_check.py
@@ -1,6 +1,7 @@
"""tools/web_check.py: the console-line parser and the static server's headers (no browser needed)."""
import gzip
import http.client
+import queue
import re
import urllib.request
@@ -74,3 +75,36 @@ def test_compressed_scripts_and_data_keep_their_types_and_traversal_is_404(tmp_p
finally:
server.shutdown()
server.server_close()
+
+
+def run_watch(lines, exit_code=None, expect=("GOLDEN OK",), timeout=5.0, late=()):
+ """Drives wc.watch with a fake browser: exit_code is what poll() returns (None = still running); late lines
+ arrive only after the reader thread is joined (output written just before the exit)."""
+ inbox = queue.Queue()
+ for l in lines:
+ inbox.put(l)
+ clock = [0.0]
+
+ def join_reader():
+ for l in late:
+ inbox.put(l)
+
+ return wc.watch(inbox, lambda: exit_code, join_reader, list(expect), FAIL, timeout,
+ now=lambda: clock[0], sleep=lambda d: clock.__setitem__(0, clock[0] + d))
+
+
+def test_a_pass_line_written_just_before_the_exit_is_a_pass():
+ assert run_watch([], exit_code=0, late=[line("GOLDEN OK 1950 a")]) == 0
+ assert run_watch([line("GOLDEN OK 1950 a")], exit_code=0) == 0
+
+
+def test_an_exit_without_the_expected_lines_is_browser_exited(capsys):
+ assert run_watch([line("something else")], exit_code=3) == 1
+ assert "BROWSER EXITED code 3" in capsys.readouterr().out
+
+
+def test_a_failure_line_fails_and_a_silent_running_browser_times_out(capsys):
+ assert run_watch([line("GOLDEN FAIL 1950 a expected b")]) == 1
+ assert "FAIL: GOLDEN FAIL" in capsys.readouterr().out
+ assert run_watch([], exit_code=None) == 1
+ assert "TIMEOUT" in capsys.readouterr().out
diff --git a/tools/web_check.py b/tools/web_check.py
index ac4c3bfa6..710492d86 100644
--- a/tools/web_check.py
+++ b/tools/web_check.py
@@ -93,6 +93,51 @@ def scan(lines, expect, fail):
return ("pass" if not pending else "pending"), pending, texts
+def drain(inbox: queue.Queue) -> list:
+ batch = []
+ while True: # lines are dropped once scanned: only the batch is held
+ try:
+ batch.append(inbox.get_nowait())
+ except queue.Empty:
+ return batch
+
+
+def watch(inbox, poll, join_reader, expect, fail, timeout, now=time.time, sleep=time.sleep) -> int:
+ """The check loop: 0 on PASS, 1 on a failure line, on BROWSER EXITED or on TIMEOUT. poll() is the browser's
+ returncode (None while running); join_reader() waits briefly for the stderr reader thread to hit EOF, so output
+ written just before the exit is still scanned."""
+ pending, deadline = list(expect), now() + timeout
+
+ def look():
+ nonlocal pending
+ status, pending, texts = scan(drain(inbox), pending, fail)
+ for t in texts:
+ print("console:", t)
+ if status == "fail":
+ print(f"FAIL: {texts[-1]}")
+ return 1
+ if status == "pass":
+ print("PASS: saw " + ", ".join(expect))
+ return 0
+ return None
+
+ while now() < deadline:
+ result = look()
+ if result is not None:
+ return result
+ code = poll()
+ if code is not None:
+ join_reader()
+ result = look()
+ if result is not None:
+ return result
+ print(f"BROWSER EXITED code {code}; never saw " + ", ".join(pending))
+ return 1
+ sleep(0.2)
+ print("TIMEOUT: never saw " + ", ".join(pending))
+ return 1
+
+
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("build", type=Path)
@@ -115,36 +160,10 @@ def main() -> int:
if a.swiftshader:
flags += ["--use-angle=swiftshader", "--enable-unsafe-swiftshader"]
proc = subprocess.Popen([str(chrome), *flags, url], stderr=subprocess.PIPE, stdout=subprocess.DEVNULL, text=True, errors="replace")
- fail, pending, deadline = re.compile(a.fail), list(a.expect), time.time() + a.timeout
inbox: queue.Queue = queue.Queue()
- threading.Thread(target=lambda: [inbox.put(l) for l in iter(proc.stderr.readline, "")], daemon=True).start()
- idle = 0
- while time.time() < deadline:
- batch = []
- while True: # lines are dropped once scanned: only the batch is held
- try:
- batch.append(inbox.get_nowait())
- except queue.Empty:
- break
- status, pending, texts = scan(batch, pending, fail)
- for t in texts:
- print("console:", t)
- if status == "fail":
- print(f"FAIL: {texts[-1]}")
- return 1
- if status == "pass":
- print("PASS: saw " + ", ".join(a.expect))
- return 0
- if proc.poll() is not None and not batch:
- idle += 1 # two idle polls after exit: the reader thread has drained stderr
- else:
- idle = 0
- if idle >= 2:
- print(f"BROWSER EXITED code {proc.returncode}; never saw " + ", ".join(pending))
- return 1
- time.sleep(0.2)
- print("TIMEOUT: never saw " + ", ".join(pending))
- return 1
+ reader = threading.Thread(target=lambda: [inbox.put(l) for l in iter(proc.stderr.readline, "")], daemon=True)
+ reader.start()
+ return watch(inbox, proc.poll, lambda: reader.join(2), list(a.expect), re.compile(a.fail), a.timeout)
finally:
if proc is not None:
proc.terminate()