Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions tests/test_unbounded_fs_scan_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import tempfile
import threading
import time
from collections.abc import Iterator
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from pathlib import Path
from typing import Any, cast
Expand Down Expand Up @@ -896,13 +896,39 @@ def test_what_keyword_arithmetic_reads_as_data_is_allowed(command: str) -> None:
#: The guard's last allow. The instrumented copy prints the keyword clock just before it.
_GUARD_ALLOW_EXIT = "\n exit 0\n} catch {"

#: Room past the budget for the one text, or the one view's bounds, a pass is reading when it
#: The LEAST room past the budget for the one text, or the one view's bounds, a pass is reading when it
#: expires. Measured locally on 2026-12-02 at a 2000 ms budget, the clock read 2029 to 2060 ms on the
#: quoted shape below and 2023 on the other; with no budget, 13361 to 13689 and 4291. A hosted
#: ubuntu runner read about 440 ms with no budget on the second, under the real budget, so the
#: control arm scales its own budget to what the runner measures rather than using the real one.
_KEYWORD_OVERRUN_MS = 500

#: The most room a slow runner gets. Hosted runs read 2519 to 2661 ms on 2026-12-02, on both shapes,
#: on changes nowhere near the guard. The unit being read at expiry takes longer on a slower runner,
#: which likely explains them, so the room follows the no-budget control. The cap keeps an absolute
#: bound however slow the runner is.
_KEYWORD_OVERRUN_CAP_MS = 1000


def _keyword_room_ms(control: int) -> int:
"""Room past the budget on a runner whose no-budget run spent ``control`` ms: a tenth of it, no
less than the floor and no more than the cap."""
return min(_KEYWORD_OVERRUN_CAP_MS, max(_KEYWORD_OVERRUN_MS, control // 10))


def test_the_keyword_line_is_under_any_control_over_2500_ms() -> None:
"""The room leaves its floor only once the control is past 5000 ms. From there the line, at the
2000 ms budget ceiling, is at most six tenths of the control, which is what a budget that does
nothing would read. With a control at or below 2500 ms the line does not catch such a budget,
and the scaled arm is what fails it."""
for control in range(200, 5001, 100):
assert _keyword_room_ms(control) == 500
for control in range(5010, 60_000, 10):
assert 500 < _keyword_room_ms(control) <= 1000
assert 2000 + _keyword_room_ms(control) <= control * 6 // 10
assert _keyword_room_ms(60_000) == 1000


#: The fewest milliseconds the no-budget copy must spend before a budget a fifth of it can be told
#: apart from noise. Under it the command is too small for this runner, and the test fails rather
#: than passing on a reading that proves nothing.
Expand Down Expand Up @@ -958,11 +984,14 @@ def _with_keyword_budget(source: str, budget: int, replacement: str) -> str:
return changed


# Three runs of at most 180 s each, under a cap the marker sets above CI's --timeout.
@pytest.mark.timeout(600)
# Five runs of at most 180 s each, under a cap the marker sets above CI's --timeout.
@pytest.mark.timeout(960)
@pytest.mark.parametrize("shape", sorted(_KEYWORD_GLUED_SHAPES))
def test_the_keyword_passes_stop_within_their_budget_on_a_64k_command(shape: str) -> None:
"""The keyword passes must stop within their budget on a 64K keyword-glued command.
def test_the_keyword_passes_stop_within_their_budget_on_a_64k_command(
shape: str, record_property: Callable[[str, object], None]
) -> None:
"""The keyword passes must stop within their budget, and the room ``_keyword_room_ms`` gives, on
a 64K keyword-glued command. The least of up to three readings is judged.

The real budget's reading alone proves nothing on a runner fast enough to finish under it, so
the control is scaled to the runner. A copy with no budget measures what the passes spend here;
Expand All @@ -975,15 +1004,29 @@ def test_the_keyword_passes_stop_within_their_budget_on_a_64k_command(shape: str
assert budget <= 2000, f"the keyword budget is {budget} ms, over the hold's ceiling"
command = _keyword_glued_command(shape)
assert 65_000 < len(command) <= 65_536
spent = _keyword_clock_ms(source, command)
assert spent < budget + _KEYWORD_OVERRUN_MS, (
f"the keyword passes ran {spent} ms, past {budget} ms and {_KEYWORD_OVERRUN_MS} ms of room"
)
control = _keyword_clock_ms(_with_keyword_budget(source, budget, "[long]::MaxValue"), command)
record_property("keyword_control_ms", control)
assert control >= _KEYWORD_CONTROL_FLOOR_MS, (
f"with no budget the keyword passes ran only {control} ms, under the "
f"{_KEYWORD_CONTROL_FLOOR_MS} ms floor, so the command does not test the budget here"
)
# Judge the least of up to three readings, since a stall after the budget expires only adds
# time. A guard that ignored its budget reads near the control every time, so it fails here where
# the control is well over the line, and the scaled arm below is what fails it elsewhere. An
# overrun on only some runs can pass.
room = _keyword_room_ms(control)
line = budget + room
readings: list[int] = []
for _ in range(3):
readings.append(_keyword_clock_ms(source, command))
if readings[-1] < line:
break
record_property("keyword_budget_readings_ms", readings)
record_property("keyword_line_ms", line)
assert min(readings) < line, (
f"the keyword passes ran {readings} ms, each past {budget} ms and {room} ms of room, "
f"against {control} ms with no budget"
)
scaled = control // 5
scaled_spent = _keyword_clock_ms(_with_keyword_budget(source, budget, str(scaled)), command)
assert scaled_spent < control // 2, (
Expand Down