test(parsing): 给清理用例补墙钟断言,并给夹具加真实耗时兜底

变更:
- 清理用例在夹具首次发布 `child.pid` 时记下墙钟时间,断言此后父进程在
  `POST_MARKER_BUDGET_SECONDS` 内返回,不再只靠整轮 20 秒的宽上限。
- 夹具的虚拟时间上限之外补一条真实墙钟兜底。虚拟时钟只在
  `local.time.sleep` 里推进,生产改成不调用它时虚拟时限永不触发,
  没有兜底就会无限空转。

验证(本机 Linux / CPython 3.13.5):
- 原 #3089 回归(时限 +8 秒):清理用例失败
- 时限判定处 8 秒真实停顿:清理用例失败
- 健康运行下 `after_marker` 约 1.2 秒,整轮约 3 秒
- 中途停顿 7.0 秒仍通过、7.8 秒失败;生产清理各环节各加 4 秒(均在
  其自身 5 秒预算内)时失败
- 轮询间隔 0.002 / 0.05 / 0.07 / 0.3:均 19 passed, 1 skipped
- pytest tests/services/parsing/:220 passed, 2 skipped
- pytest tests/runtime/test_launcher.py:67 passed, 1 skipped
- ruff check / ruff format --check tests/services/parsing/:PASS

已知局限:这条墙钟断言从「循环第一次调用 check()」起算,从进程启动到
首次 check 之间的耗时不在窗口内;兜底也只在 poll() 里检查,两次轮询
之间的阻塞观察不到。彻底解决需要外部超时机制,见 #3126 的讨论。
This commit is contained in:
AleFlyX
2026-12-01 16:57:07 +08:00
parent 65dcf1f0d0
commit 508a2bda35
2 changed files with 46 additions and 2 deletions

View File

@@ -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:

View File

@@ -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"