Files
vectorcraft/scripts/ci-clear-rolling-cache-tests.py
T
dotCooCoo 6927affc24 CI: the cold cache clear waits for the warm leg's own save and reads every cache page
scripts/ci-clear-rolling-cache.py:
- The 160-minute wait is timed from the cache warm leg's start, not from
  the script's start. The clear job starts with the run, and the leg
  starts after gate / select legs and a free runner. The job's timeout
  goes from 170 to 200 minutes to leave room for that delay.
- The leg counts as saving when its Post Setup step (the gate-setup
  action's cache save) starts, not when any Post step starts. A leg that
  ends canceled or failed before that step succeeds deletes nothing and
  exits 1, as a leg that failed before its save already did.
- actions/caches is read one page at a time, sorted by creation, until a
  page holds fewer than 100 entries.

scripts/ci-clear-rolling-cache-tests.py checks the script with recorded
API answers, and Nightly check runs it when either file changes.
2026-11-27 19:55:45 -08:00

143 lines
7.4 KiB
Python

#!/usr/bin/env python3
"""Checks for ci-clear-rolling-cache.py with recorded GitHub API answers; no network.
python3 scripts/ci-clear-rolling-cache-tests.py
"""
from datetime import datetime, timedelta, timezone
import importlib.util
from pathlib import Path
import unittest
_spec = importlib.util.spec_from_file_location("clear", Path(__file__).with_name("ci-clear-rolling-cache.py"))
clear = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(clear)
WARM = "gate / cache warm (ubuntu-24.04)"
ME = [{"name": "cold cache clear", "status": "in_progress"}]
def warm_job(status, conclusion=None, steps=(), started_at="2026-11-28T03:40:00Z"):
return {
"name": WARM,
"status": status,
"conclusion": conclusion,
"started_at": started_at,
"steps": [{"name": n, "status": s, "conclusion": c} for n, s, c in steps],
}
def earlier(conclusion):
return [{"name": WARM, "conclusion": conclusion}, {"name": "cold cache clear", "conclusion": "failure"}]
class SaveStep(unittest.TestCase):
def test_the_leg_is_saving_once_its_save_step_starts(self):
self.assertFalse(clear.saving(warm_job("in_progress", steps=[("Setup", "completed", "success"), ("Gate", "in_progress", None)])))
self.assertTrue(clear.saving(warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Post Setup", "in_progress", None)])))
def test_other_post_steps_are_not_the_save(self):
job = warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Post Harden runner", "in_progress", None)])
self.assertFalse(clear.saving(job))
def test_a_leg_canceled_during_its_save_saves_nothing(self):
job = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "completed", "cancelled")])
self.assertTrue(clear.saving(job))
self.assertTrue(clear.saves_nothing(job))
def test_a_leg_that_ended_with_its_save_still_running_saves_nothing(self):
job = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "in_progress", None)])
self.assertTrue(clear.saves_nothing(job))
def test_a_running_or_finished_save_counts_as_saved(self):
running = warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Post Setup", "in_progress", None)])
done = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "success"),
("Post Harden runner", "completed", "failure")])
self.assertFalse(clear.saves_nothing(running))
self.assertFalse(clear.saves_nothing(done))
def test_a_failed_step_before_the_save_saves_nothing(self):
job = lambda c: warm_job("in_progress", steps=[("Gate", "completed", c), ("Post Setup", "in_progress", None)])
self.assertTrue(clear.saves_nothing(job("failure")))
self.assertTrue(clear.saves_nothing(job("cancelled")))
self.assertFalse(clear.saves_nothing(job("success")))
tolerated = warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Trim the cache entry to this build", "completed", "failure"),
("Post Setup", "in_progress", None)])
self.assertFalse(clear.saves_nothing(tolerated))
class Deadline(unittest.TestCase):
began = datetime(2026, 11, 28, 3, 37, tzinfo=timezone.utc)
def test_the_wait_is_timed_from_the_leg_start(self):
late = warm_job("in_progress", started_at="2026-11-28T04:00:00Z")
before_limit = clear.parse_time("2026-11-28T04:00:00Z") + timedelta(seconds=clear.GIVE_UP_SECONDS - 60)
after_limit = clear.parse_time("2026-11-28T04:00:00Z") + timedelta(seconds=clear.GIVE_UP_SECONDS + 60)
self.assertGreater((before_limit - self.began).total_seconds(), clear.GIVE_UP_SECONDS)
self.assertFalse(clear.gave_up(late, before_limit, self.began))
self.assertTrue(clear.gave_up(late, after_limit, self.began))
def test_a_leg_that_never_starts_is_timed_from_the_script_start(self):
queued = warm_job("queued", started_at="2026-11-28T03:38:00Z")
later = self.began + timedelta(seconds=clear.GIVE_UP_SECONDS + 60)
self.assertFalse(clear.gave_up(None, self.began + timedelta(minutes=10), self.began))
self.assertTrue(clear.gave_up(None, later, self.began))
self.assertTrue(clear.gave_up(queued, later, self.began))
class Jobs(unittest.TestCase):
def test_only_the_warm_leg_counts(self):
jobs = [{"name": "gate / test 1/5 (ubuntu-24.04)"}, {"name": "gate / build (${{ matrix.os }})"}, {"name": WARM}]
self.assertEqual(clear.warm_job(jobs), jobs[2])
def test_a_rerun_of_this_job_alone_skips_a_leg_that_succeeded(self):
self.assertTrue(clear.not_in_attempt(ME, {1: earlier("success")}))
self.assertTrue(clear.not_in_attempt(ME, {1: earlier("success"), 2: [{"name": "cold cache clear"}]}))
self.assertTrue(clear.not_in_attempt(ME, {1: earlier("failure"), 2: [{"name": "cold cache clear"}], 3: earlier("success")}))
def test_a_leg_that_did_not_succeed_is_waited_for(self):
self.assertFalse(clear.not_in_attempt(ME, {1: earlier("cancelled")}))
self.assertFalse(clear.not_in_attempt(ME, {}))
self.assertFalse(clear.not_in_attempt(ME, {1: earlier("success"), 2: earlier("failure")}))
def test_a_rerun_of_all_jobs_is_waited_for(self):
self.assertFalse(clear.not_in_attempt(ME + [{"name": "cache prune", "status": "in_progress"}], {1: earlier("success")}))
def test_a_failed_select_or_build_stops_the_wait(self):
done = lambda n, c: {"name": n, "status": "completed", "conclusion": c}
self.assertTrue(clear.legs_stopped([done("gate / select legs", "failure")]))
self.assertTrue(clear.legs_stopped([done("gate / select legs", "success"), done("gate / build (ubuntu-24.04)", "cancelled")]))
self.assertFalse(clear.legs_stopped([done("gate / select legs", "success"), done("gate / build (${{ matrix.os }})", "skipped")]))
self.assertFalse(clear.legs_stopped([done("gate / test 1/5 (ubuntu-24.04)", "failure")]))
class Caches(unittest.TestCase):
def test_every_page_is_read(self):
entries = [{"id": n, "key": f"v2-rust-a013-gate-crates-Linux-x64-{n}"} for n in range(2 * clear.PAGE_SIZE + 3)]
pages = []
def fetch(page):
pages.append(page)
start = (page - 1) * clear.PAGE_SIZE
return {"actions_caches": entries[start:start + clear.PAGE_SIZE]}
self.assertEqual(clear.all_caches(fetch), entries)
self.assertEqual(pages, [1, 2, 3])
def test_only_rolling_crates_entries_are_deleted(self):
caches = [
{"key": "v2-rust-a013-gate-crates-Linux-x64-46055934-01d60c35"},
{"key": "v2-rust-a013-gate-Linux-x64-46055934-01d60c35"},
{"key": "v1-affinity-rust-a013-gate-Linux-x64-b541f517-01d60c35"},
{"key": "v0-rust-build-Linux-x64-1fd829d9-01d60c35"},
]
self.assertEqual(clear.rolling_entries(caches), caches[:1])
def test_entries_created_after_the_run_started_are_kept(self):
rolling = lambda created: {"key": "v2-rust-ab-gate-crates-Linux-x64-1", "created_at": created}
old, same_minute_push = rolling("2026-11-27T03:00:00.5Z"), rolling("2026-11-28T03:40:00Z")
self.assertEqual(clear.created_before([old, same_minute_push], "2026-11-28T03:37:10Z"), [old])
if __name__ == "__main__":
unittest.main(verbosity=2)