Answer architecture round 1: 0 🔴, 3 🟡; keep the slots and the memory share in machine_load, exit 125/126/127 as GNU wrappers do, point --help at the module

This commit is contained in:
2026-10-23 10:47:01 +02:00
parent 7c05bf414c
commit 66af309e71
12 changed files with 113 additions and 67 deletions
+15 -16
View File
@@ -10,17 +10,12 @@ 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
TOOL = pathlib.Path(__file__).resolve().parents[1] / "run-heavy"
class SlotCount(unittest.TestCase):
def test_one_slot_per_four_cpus_and_never_none(self):
for cpus, slots in ((16, 4), (8, 2), (7, 1), (4, 1), (1, 1), (None, 1)):
self.assertEqual(main.slot_count(cpus), slots, cpus)
class RunHeavy(unittest.TestCase):
def setUp(self):
state = tempfile.TemporaryDirectory()
@@ -41,14 +36,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(main.slot_count(os.cpu_count())):
for slot in range(slot_count(os.cpu_count())):
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(main.slot_count(os.cpu_count())):
for slot in range(slot_count(os.cpu_count())):
with open(self.slots / f"slot-{slot}", "a") as file:
try:
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
@@ -64,9 +59,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(), main.slot_count(os.cpu_count()) - 1)
self.assertEqual(self.free_slots(), slot_count(os.cpu_count()) - 1)
tool.communicate("\n", timeout=30)
self.assertEqual(self.free_slots(), main.slot_count(os.cpu_count()))
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():
@@ -82,14 +77,18 @@ class RunHeavy(unittest.TestCase):
result = self.run_tool("echo", "nested", env={**self.env, main.HELD: "0"})
self.assertEqual((result.returncode, result.stdout), (0, "nested\n"))
def test_a_command_that_cannot_start_exits_127_saying_why(self):
result = self.run_tool("no-such-command-anywhere")
self.assertEqual((result.returncode, result.stdout), (127, ""))
self.assertIn("no-such-command-anywhere", result.stderr)
def test_a_command_not_found_exits_127_and_one_that_cannot_run_126_saying_why(self):
missing = self.run_tool("no-such-command-anywhere")
self.assertEqual((missing.returncode, missing.stdout), (127, ""))
self.assertIn("no-such-command-anywhere", missing.stderr)
with tempfile.NamedTemporaryFile(suffix=".sh") as unrunnable:
denied = self.run_tool(unrunnable.name)
self.assertEqual((denied.returncode, denied.stdout), (126, ""))
self.assertIn(unrunnable.name, denied.stderr)
def test_no_command_exits_1_with_the_usage_and_help_exits_0(self):
def test_no_command_exits_125_with_the_usage_and_help_exits_0(self):
bare = self.run_tool()
self.assertEqual((bare.returncode, bare.stdout), (1, ""))
self.assertEqual((bare.returncode, bare.stdout), (125, ""))
self.assertIn("usage: run-heavy <command>", bare.stderr)
helped = self.run_tool("--help")
self.assertEqual(helped.returncode, 0)