Files
alzeo/tests/test_offline_step.py
T
Hasan BasbunarandClaude Opus 5.5 d8766bb228 CI: each test file runs once for the offline step and the environment check (tests/run_suite.py)
A CI run ran the suite about four times (counted on 2026-10-24): the offline step ran every file
on its own, tests/test_suite_isolation.py then ran every unittest module again in a process of
its own to see whether it left an environment variable changed, and that file ran twice, in the
offline step and in the single-process pass. tests/run_suite.py runs each file once, as a human
runs it, up to four at a time, the longest first, and the files that take a port number out of a
socket they close one at a time at the end, as the step's script did; a sitecustomize.py loaded
into every python it starts compares the test process's environment at its start and at its
exit (no test file touches the environment in its __main__ block, checked on the 246 files), and
records a connect() to a serving port with the test line, as the step's own hook did.
test_suite_isolation.py keeps its two quick probes (HOME at import, the proxy fixtures' key).

tests/test_run_suite.py plants each defect in a throwaway repository and sees it caught: a failing
test named with its output, a file that runs none of its declared tests, a variable left changed
(and one set then removed not named), a contact with a serving port recorded with its line, a
file that takes a port run alone, a longest file that is gone stopping the run before anything
runs. tests/test_offline_step.py now drives run_suite.py over its fake tree: at most four files
at a time and at least two, nothing beside a file that frees a port, the longest first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 11:51:55 +01:00

127 lines
6.1 KiB
Python

#!/usr/bin/env python3
"""The offline step runs the test files up to four at a time, and the ones that free a port alone.
One after the other, the files of dashboard/tests and tests/ took 20 minutes of the offline
step on the reference box (2026-10-18). They now run up to four at a time, the longest first,
except the files that take a port number out of a socket they close, to start a server on it
later or to have a port where nothing listens: a file running beside one of them can be handed
that port meanwhile, so they run one at a time once the others are over.
The step's runner, tests/run_suite.py since 2026-10-24 (a script ci.yml wrote before), runs here
over a tree of files whose tests record when they ran: every file still runs and has its count
checked, every failure is named with the end of its output, and nothing runs beside a file that
frees a port."""
import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
# spelled in parts: the CI counts the first string to know how many tests a file declares,
# and takes a file holding the second for one that frees a port
TEST_DEF = "def " + "test_"
FREES_A_PORT = "getsockname" + "()[1]"
LONGEST = ("tests/test_x_long.py", "tests/test_text_lane.py", "tests/test_check_pins.py")
FAKE = """import os, sys, time, unittest
# {mark}
start = time.time()
class Fake(unittest.TestCase):
{tests}
{main}"""
MAIN = """if __name__ == "__main__":
ok = unittest.main(exit=False).result.wasSuccessful()
with open(os.environ["FAKE_LOG"], "a") as f:
f.write(f"{{start}} {{time.time()}} {path}\\n")
sys.exit(0 if ok else 1)
"""
class TheOfflineStep(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = pathlib.Path(self.tmp.name) / "repo"
self.log = pathlib.Path(self.tmp.name) / "ran.log"
self.files = {}
for path in LONGEST:
self.fake(path)
for path in ("dashboard/tests/test_one.py", "dashboard/tests/test_two.py", "tests/test_a.py",
"tests/test_b.py", "tests/test_c.py", "tests/test_d.py"):
self.fake(path)
for path in ("tests/test_port_e.py", "tests/test_port_f.py"):
self.fake(path, frees_a_port=True)
def fake(self, path, frees_a_port=False, fails=None, main=True):
"""A test file of one test that sleeps, then records when it ran."""
test = f" {TEST_DEF}it(self):\n time.sleep(0.8)\n"
if fails:
test += f" self.fail({fails!r})\n"
self.files[path] = FAKE.format(mark=FREES_A_PORT if frees_a_port else "", tests=test,
main=MAIN.format(path=path) if main else "")
def run_step(self):
for path, text in self.files.items():
(self.root / path).parent.mkdir(parents=True, exist_ok=True)
(self.root / path).write_text(text)
r = subprocess.run([sys.executable, str(REPO / "tests" / "run_suite.py"), "--repo", str(self.root),
"--first", ",".join(LONGEST)], capture_output=True, text=True, timeout=120,
env={**os.environ, "FAKE_LOG": str(self.log)})
ran = {}
if self.log.exists():
for ln in self.log.read_text().splitlines():
start, end, path = ln.split()
ran[path] = (float(start), float(end))
return r.returncode, r.stdout + r.stderr, ran
def test_every_file_runs_and_the_ones_that_free_a_port_run_alone(self):
rc, out, ran = self.run_step()
self.assertEqual(rc, 0, out)
self.assertEqual(set(ran), set(self.files), out)
self.assertIn("every test file ran on its own: 9 of them", out)
self.assertIn("then 2 one at a time", out)
self.assertRegex(out, r"the longest: \S+ \d+ s, \S+ \d+ s, \S+ \d+ s\n")
alone = {p: ran[p] for p in ("tests/test_port_e.py", "tests/test_port_f.py")}
for path, (start, end) in alone.items():
for other, (s, e) in ran.items():
if other != path:
self.assertTrue(e <= start or s >= end, f"{other} ran beside {path}")
together = [span for p, span in ran.items() if p not in alone]
most = max(sum(s <= t < e for s, e in together) for t, _ in together)
self.assertLessEqual(most, 4)
if len(os.sched_getaffinity(0)) >= 2: # the CPUs nproc counts, which the step sizes from
self.assertGreaterEqual(most, 2, "the files ran one after the other")
first_four = sorted(together)[:4]
for path in LONGEST:
self.assertIn(ran[path], first_four, f"{path} did not start among the first")
def test_every_failure_is_named_with_the_end_of_its_output(self):
self.fake("tests/test_b.py", fails="b went wrong")
self.fake("tests/test_port_f.py", frees_a_port=True, fails="f went wrong")
rc, out, ran = self.run_step()
self.assertEqual(rc, 1, out)
self.assertEqual(set(ran), set(self.files), "a failure kept the other files from running")
for path, why in (("tests/test_b.py", "b went wrong"), ("tests/test_port_f.py", "f went wrong")):
said = out.split(f"{path} failed, the end of its output:", 1)
self.assertEqual(len(said), 2, out)
self.assertIn(why, said[1], out)
def test_a_file_that_runs_fewer_tests_than_it_declares_fails(self):
self.fake("tests/test_c.py", main=False) # exits 0 having run nothing
rc, out, _ = self.run_step()
self.assertEqual(rc, 1, out)
self.assertIn("tests/test_c.py ran 0 of its 1 declared tests when invoked directly", out)
def test_a_longest_file_that_is_gone_is_named(self):
del self.files["tests/test_check_pins.py"]
rc, out, ran = self.run_step()
self.assertEqual(rc, 1, out)
self.assertIn("['tests/test_check_pins.py'], named as the longest, are gone", out)
self.assertEqual(ran, {}, "the step ran files with a stale list")
if __name__ == "__main__":
unittest.main()