Answer principles round 1: 0 🔴, 5 🟡; enforce run-heavy in rule-hook, take slots through one tested function, exit 125 on lock errors, fake meminfo in black-box tests

This commit is contained in:
2026-10-23 10:55:55 +02:00
parent 6725ee4c9d
commit c7dbd56f39
14 changed files with 136 additions and 59 deletions
+5 -15
View File
@@ -5,13 +5,12 @@ import pathlib
import subprocess
import sys
import tempfile
import time
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from machine_load.slots import slot_count
from run_heavy import main
from machine_load.slots import slot_count, usable_cpus
TOOL = pathlib.Path(__file__).resolve().parents[1] / "run-heavy"
@@ -36,14 +35,14 @@ class RunHeavy(unittest.TestCase):
def every_slot_held(self):
self.slots.mkdir(parents=True, exist_ok=True)
with contextlib.ExitStack() as held:
for slot in range(slot_count(os.cpu_count())):
for slot in range(slot_count(usable_cpus())):
file = held.enter_context(open(self.slots / f"slot-{slot}", "a"))
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
yield
def free_slots(self) -> int:
free = 0
for slot in range(slot_count(os.cpu_count())):
for slot in range(slot_count(usable_cpus())):
with open(self.slots / f"slot-{slot}", "a") as file:
try:
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
@@ -59,18 +58,9 @@ class RunHeavy(unittest.TestCase):
def test_holds_a_slot_while_the_command_runs_and_frees_it_after(self):
tool = self.start_tool("sh", "-c", "echo started; read line")
self.assertEqual(tool.stdout.readline(), "started\n")
self.assertEqual(self.free_slots(), slot_count(os.cpu_count()) - 1)
self.assertEqual(self.free_slots(), slot_count(usable_cpus()) - 1)
tool.communicate("\n", timeout=30)
self.assertEqual(self.free_slots(), slot_count(os.cpu_count()))
def test_waits_while_every_slot_is_held_and_says_so_once(self):
with self.every_slot_held():
tool = self.start_tool("echo", "ran")
time.sleep(3 * main.POLL_SECONDS)
self.assertIsNone(tool.poll())
out, err = tool.communicate(timeout=30)
self.assertEqual((tool.returncode, out), (0, "ran\n"))
self.assertEqual(err.count("waiting"), 1, err)
self.assertEqual(self.free_slots(), slot_count(usable_cpus()))
def test_a_command_inside_a_held_slot_runs_at_once(self):
with self.every_slot_held():