diff --git a/bin/machine_load/test_machine_load.py b/bin/machine_load/test_machine_load.py index 4f2ca9eb..7bb154b2 100644 --- a/bin/machine_load/test_machine_load.py +++ b/bin/machine_load/test_machine_load.py @@ -1,9 +1,11 @@ +import errno import fcntl import os import pathlib import sys import tempfile import unittest +import unittest.mock sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) @@ -37,6 +39,23 @@ class MemoryShare(unittest.TestCase): self.assertIsNone(memory.read_memory(pathlib.Path(temp, "gone"))) +class SlotFolder(unittest.TestCase): + def test_every_session_shares_the_slots_under_the_users_state_folder(self): + self.assertEqual(slots.slot_folder({}), pathlib.Path.home() / ".local" / "state" / "run-heavy") + self.assertEqual(slots.slot_folder({"XDG_STATE_HOME": "/s"}), pathlib.Path("/s/run-heavy")) + + +class TakenSlot(unittest.TestCase): + def test_a_lock_that_fails_for_another_reason_than_a_holder_closes_its_file_and_raises(self): + closed = [] + with tempfile.TemporaryDirectory() as folder, unittest.mock.patch("fcntl.flock", side_effect=OSError(errno.ENOLCK, "No locks available")), unittest.mock.patch("os.close", side_effect=closed.append): + with self.assertRaises(OSError): + slots.taken_slot(pathlib.Path(folder), 2) + self.assertEqual(len(closed), 1) + self.assertIsInstance(closed[0], int) + os.close(closed[0]) + + class WaitedSlot(unittest.TestCase): def test_waits_while_every_slot_is_held_saying_so_once_then_takes_the_freed_one(self): with tempfile.TemporaryDirectory() as folder: diff --git a/bin/run_heavy/main.py b/bin/run_heavy/main.py index ee60931c..dfde733e 100644 --- a/bin/run_heavy/main.py +++ b/bin/run_heavy/main.py @@ -20,6 +20,7 @@ import pathlib import sys import typing +from failure import Failure from machine_load.slots import slot_count, slot_folder, waited_slot # Exit codes for a command run-heavy never ran, apart from any the command exits with, as GNU env, nice and timeout use them. @@ -40,20 +41,20 @@ def main(argv: list[str], env: typing.Mapping[str, str], cpus: int | None) -> in return OWN_FAILURE if HELD not in env: slot = held_slot(slot_folder(env), slot_count(cpus)) - if isinstance(slot, str): - print(f"run-heavy: {slot}", file=sys.stderr) + if isinstance(slot, Failure): + print(f"run-heavy: {slot.reason}", file=sys.stderr) return OWN_FAILURE env = {**env, HELD: str(slot)} return run(argv, env) -def held_slot(folder: pathlib.Path, slots: int) -> int | str: +def held_slot(folder: pathlib.Path, slots: int) -> int | Failure: """The number of the slot this call holds, its lock left for the command to inherit, or why no slot could be taken.""" try: folder.mkdir(parents=True, exist_ok=True) slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for one of the machine's {slots} heavy slots", file=sys.stderr, flush=True)) except OSError as failure: - return f"cannot take a heavy slot in {folder}: {failure.strerror}" + return Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}") # The command inherits the locked file, so the slot stays held until it exits. os.set_inheritable(fd, True) return slot diff --git a/bin/run_heavy/test_run_heavy.py b/bin/run_heavy/test_run_heavy.py index cfabd18a..b3ebd623 100644 --- a/bin/run_heavy/test_run_heavy.py +++ b/bin/run_heavy/test_run_heavy.py @@ -67,6 +67,12 @@ 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_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, ""))