import typing as typ
from fractions import Fraction
from coverage_lanes import coverage_jobs_in
from nextest_budgets import global_timeout, termination_allowance
from timeout_budgets import (
COLD_BUILD_ALLOWANCE_SECONDS,
COVERAGE_ACTION,
OUTSIDE_WATCHDOG_ALLOWANCE_SECONDS,
WATCHDOG_VARIABLE,
required_ceiling,
seconds,
)
HUGE_SECONDS: typ.Final[int] = 2**60
def _workflow(watchdog: int, timeout_minutes: int) -> dict[str, dict[str, object]]:
return {
"ci.yml": {
"jobs": {
"coverage": {
"timeout-minutes": timeout_minutes,
"env": {WATCHDOG_VARIABLE: watchdog},
"steps": [{"uses": f"{COVERAGE_ACTION}@" + "0" * 40}],
}
}
}
}
def _watchdog_read(watchdog: int) -> Fraction:
(job,) = coverage_jobs_in(_workflow(watchdog, timeout_minutes=66))
(budget,) = job.watchdogs
assert budget is not None, "the job declares a watchdog at job level"
return budget
def _job_timeout_read(timeout_minutes: int) -> Fraction:
(job,) = coverage_jobs_in(_workflow(1800, timeout_minutes=timeout_minutes))
assert job.job_timeout is not None, "the job declares timeout-minutes"
return job.job_timeout
def test_the_job_ceiling_reader_keeps_a_minute_a_float_would_lose() -> None:
larger = _job_timeout_read(HUGE_SECONDS + 1)
smaller = _job_timeout_read(HUGE_SECONDS)
assert larger > smaller, "ceilings one minute apart must order strictly"
assert float(larger) == float(smaller), (
"the float collapse this guards against must still be real; if these "
"differ, the case no longer exercises what it was written for"
)
def test_the_lane_reader_keeps_a_second_a_float_would_lose() -> None:
larger = _watchdog_read(HUGE_SECONDS + 1)
smaller = _watchdog_read(HUGE_SECONDS)
assert larger > smaller, "watchdogs one second apart must order strictly"
assert float(larger) == float(smaller), (
"the float collapse this guards against must still be real; if these "
"differ, the case no longer exercises what it was written for"
)
def test_the_job_ceiling_arithmetic_stays_exact() -> None:
allowance = OUTSIDE_WATCHDOG_ALLOWANCE_SECONDS
larger = required_ceiling([Fraction(HUGE_SECONDS + 1)], allowance)
smaller = required_ceiling([Fraction(HUGE_SECONDS)], allowance)
assert larger > smaller, "ceilings one second apart must order strictly"
assert float(larger) == float(smaller), (
"the float collapse this guards against must still be real; if these "
"differ, the case no longer exercises what it was written for"
)
def _watchdog_floor(whole_run: int, *, grace_period: bool = True) -> Fraction:
grace = ', grace-period = "5s"' if grace_period else ""
config_text = (
"[profile.default]\n"
f'global-timeout = "{whole_run}s"\n'
f'slow-timeout = {{ period = "60s", terminate-after = 1{grace} }}\n'
)
budget = global_timeout(config_text)
assert budget is not None, "the profile declares a global-timeout"
return budget + termination_allowance(config_text) + COLD_BUILD_ALLOWANCE_SECONDS
def test_the_watchdog_floor_stays_exact() -> None:
for grace_period in (True, False):
larger = _watchdog_floor(HUGE_SECONDS + 1, grace_period=grace_period)
smaller = _watchdog_floor(HUGE_SECONDS, grace_period=grace_period)
assert larger > smaller, (
f"floors one second apart must order strictly "
f"(grace-period declared: {grace_period})"
)
assert float(larger) == float(smaller), (
"the float collapse this guards against must still be real; if "
"these differ, the case no longer exercises what it was written "
f"for (grace-period declared: {grace_period})"
)
def test_the_configured_tiers_are_exact_values() -> None:
assert isinstance(seconds("600s"), Fraction), (
"a nextest duration must arrive exact"
)
assert isinstance(_watchdog_read(1800), Fraction), (
"a workflow watchdog must arrive exact"
)
assert isinstance(_job_timeout_read(66), Fraction), (
"a job ceiling must arrive exact through its conversion to seconds"
)
assert isinstance(required_ceiling([Fraction(1800)], Fraction(1200)), Fraction), (
"the ceiling must stay exact through its sum"
)