diff --git a/tests/services/parsing/_mineru_deadline_probe.py b/tests/services/parsing/_mineru_deadline_probe.py index b196d5374..74ab3e5a0 100644 --- a/tests/services/parsing/_mineru_deadline_probe.py +++ b/tests/services/parsing/_mineru_deadline_probe.py @@ -4,6 +4,7 @@ from collections.abc import Callable from contextlib import contextmanager import math from pathlib import Path +import time from types import SimpleNamespace import pytest @@ -11,6 +12,12 @@ import pytest from deeptutor.services.parsing import progress from deeptutor.services.parsing.engines.mineru import local +# Real wall-clock ceiling for one probe run. Healthy runs finish in ~3s, so +# this only fires when production stopped calling the harness clock; without +# it such a loop would spin until the CI job's own limit. The probe module's +# ``time`` is the real one: only ``local.time`` is monkeypatched. +_REAL_BACKSTOP_SECONDS = 120.0 + class FakeClock: # A nonzero origin exercises elapsed-time calculation. Summing the sleeps @@ -51,12 +58,27 @@ def run_deadline_probe( def fake_cli_process(cmd, log_path, env): log_path.touch() polls = 0 + _real_backstop = time.monotonic() + _REAL_BACKSTOP_SECONDS def poll() -> int | None: nonlocal polls polls += 1 - if polls > 500: - raise AssertionError("MinerU did not finish within 500 polls") + # Bound on virtual time so the cap does not couple to the + # production poll interval. A real-time ceiling stays alongside it + # because the virtual clock only advances inside a monkeypatched + # ``sleep``: if production stops calling it the virtual deadline + # never arrives and this loop would spin forever. + if clock.elapsed > 4 * configured_timeout: + raise AssertionError( + f"harness bound exceeded after {polls} polls " + f"({clock.elapsed:.1f}s virtual, deadline {configured_timeout:g}s)" + ) + if time.monotonic() > _real_backstop: + raise AssertionError( + f"probe exceeded its {_REAL_BACKSTOP_SECONDS:g}s wall-clock ceiling " + f"after {polls} polls ({clock.elapsed:.1f}s virtual); the loop is no " + "longer calling the harness clock" + ) if on_output is not None: # The loop reads this line on its next pass. with log_path.open("a", encoding="utf-8") as output: diff --git a/tests/services/parsing/test_mineru_process_lifecycle.py b/tests/services/parsing/test_mineru_process_lifecycle.py index b94b78fde..0c507449f 100644 --- a/tests/services/parsing/test_mineru_process_lifecycle.py +++ b/tests/services/parsing/test_mineru_process_lifecycle.py @@ -15,6 +15,15 @@ from deeptutor.services.parsing.engines.mineru import local from ._mineru_deadline_probe import run_deadline_probe CLEANUP_OBSERVATION_SECONDS = 20.0 +# Once child.pid appears, the parent only has to notice the configured 1.2s +# deadline and settle a fixture that polls its own stop file. Measured on this +# machine: a healthy run costs ~1.2s here and ~3s for the whole test, injected +# mid-loop stalls of 7.0s still pass while 7.8s fail, and 8s before the +# decision fails. The budget is therefore a real ceiling on cleanup latency, +# not generous headroom: adding 4s to each of production's own 5s allowances +# fails it. It is a stopgap, not a wall-clock guarantee -- see the discussion +# on #3126 about an external per-test timeout. +POST_MARKER_BUDGET_SECONDS = 8.0 CHILD_LIFETIME_SECONDS = 4 * CLEANUP_OBSERVATION_SECONDS @@ -195,10 +204,14 @@ def test_deadline_and_cancellation_settle_the_owned_tree( + f"time.sleep({CHILD_LIFETIME_SECONDS})" ) + first_marker_seen: list[float] = [] + def check(): # Let the descendant start before interrupting the CLI. The fake-clock # cases cover deadline precision independently of this startup wait. _await_marker(pid_file, timeout=CLEANUP_OBSERVATION_SECONDS) + if not first_marker_seen: + first_marker_seen.append(time.monotonic()) if cancelled: raise progress.IndexingCancelled("controlled cancellation") @@ -214,6 +227,15 @@ def test_deadline_and_cancellation_settle_the_owned_tree( local.LocalParseReason.CANCELLED if cancelled else local.LocalParseReason.TIMEOUT ), result.detail assert elapsed < CLEANUP_OBSERVATION_SECONDS, f"parse took {elapsed:.1f}s" + # The whole-run bound above is loose enough to hide a multi-second stall + # around the deadline, which is the case this test exists for. Once the + # fixture has published its pid, the parent only has to notice the + # configured timeout and settle the tree. + assert first_marker_seen, "the fixture never published child.pid" + after_marker = time.monotonic() - first_marker_seen[0] + assert after_marker < POST_MARKER_BUDGET_SECONDS, ( + f"parent took {after_marker:.1f}s after child.pid first appeared" + ) assert deadline_file.read_text(encoding="utf-8") == timeout child = int(pid_file.read_text(encoding="utf-8")) assert _await_settled(child), "Owned child survived cleanup"