import contextlib import fcntl import os import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from run_heavy import main from machine_load.slots import slot_count, usable_cpus TOOL = pathlib.Path(__file__).resolve().parents[1] / "run-heavy" class RunHeavy(unittest.TestCase): def setUp(self): state = tempfile.TemporaryDirectory() self.addCleanup(state.cleanup) self.env = {**os.environ, "XDG_STATE_HOME": state.name} self.env.pop(main.HELD, None) self.slots = pathlib.Path(state.name) / "run-heavy" def run_tool(self, *args: str, env=None) -> subprocess.CompletedProcess[str]: return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, text=True, env=env or self.env, timeout=30) def start_tool(self, *args: str, env=None) -> subprocess.Popen[str]: tool = subprocess.Popen([sys.executable, str(TOOL), *args], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env or self.env) self.addCleanup(tool.kill) return tool @contextlib.contextmanager def every_slot_held(self): self.slots.mkdir(parents=True, exist_ok=True) with contextlib.ExitStack() as held: 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(usable_cpus())): with open(self.slots / f"slot-{slot}", "a") as file: try: fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB) free += 1 except BlockingIOError: pass return free def test_runs_the_command_and_exits_with_its_code(self): result = self.run_tool("sh", "-c", "echo out; echo err >&2; exit 3") self.assertEqual((result.returncode, result.stdout, result.stderr), (3, "out\n", "err\n")) 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(usable_cpus()) - 1) tool.communicate("\n", timeout=30) 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(): result = self.run_tool("echo", "nested", env={**self.env, main.HELD: "0"}) self.assertEqual((result.returncode, result.stdout), (0, "nested\n")) def test_exits_125_saying_why_where_the_slot_folder_cannot_be_made(self): with tempfile.NamedTemporaryFile() as state: result = self.run_tool("echo", "ran", env={**self.env, "XDG_STATE_HOME": state.name}) self.assertEqual((result.returncode, result.stdout), (125, "")) self.assertIn(state.name, 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_125_with_the_usage_and_help_exits_0(self): bare = self.run_tool() self.assertEqual((bare.returncode, bare.stdout), (125, "")) self.assertIn("usage: run-heavy ", bare.stderr) helped = self.run_tool("--help") self.assertEqual(helped.returncode, 0) self.assertIn("usage: run-heavy ", helped.stdout) if __name__ == "__main__": unittest.main()