Answer stability round 1: 0 🔴, 5 🟡; test the lock-error and slot-folder exits and the default folder, and return a Failure from held_slot

This commit is contained in:
2026-10-23 11:06:55 +02:00
parent dde9caa172
commit 792fa2b8b0
3 changed files with 30 additions and 4 deletions
+19
View File
@@ -1,9 +1,11 @@
import errno
import fcntl import fcntl
import os import os
import pathlib import pathlib
import sys import sys
import tempfile import tempfile
import unittest import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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"))) 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): class WaitedSlot(unittest.TestCase):
def test_waits_while_every_slot_is_held_saying_so_once_then_takes_the_freed_one(self): def test_waits_while_every_slot_is_held_saying_so_once_then_takes_the_freed_one(self):
with tempfile.TemporaryDirectory() as folder: with tempfile.TemporaryDirectory() as folder:
+5 -4
View File
@@ -20,6 +20,7 @@ import pathlib
import sys import sys
import typing import typing
from failure import Failure
from machine_load.slots import slot_count, slot_folder, waited_slot 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. # 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 return OWN_FAILURE
if HELD not in env: if HELD not in env:
slot = held_slot(slot_folder(env), slot_count(cpus)) slot = held_slot(slot_folder(env), slot_count(cpus))
if isinstance(slot, str): if isinstance(slot, Failure):
print(f"run-heavy: {slot}", file=sys.stderr) print(f"run-heavy: {slot.reason}", file=sys.stderr)
return OWN_FAILURE return OWN_FAILURE
env = {**env, HELD: str(slot)} env = {**env, HELD: str(slot)}
return run(argv, env) 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.""" """The number of the slot this call holds, its lock left for the command to inherit, or why no slot could be taken."""
try: try:
folder.mkdir(parents=True, exist_ok=True) 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)) 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: 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. # The command inherits the locked file, so the slot stays held until it exits.
os.set_inheritable(fd, True) os.set_inheritable(fd, True)
return slot return slot
+6
View File
@@ -67,6 +67,12 @@ class RunHeavy(unittest.TestCase):
result = self.run_tool("echo", "nested", env={**self.env, main.HELD: "0"}) result = self.run_tool("echo", "nested", env={**self.env, main.HELD: "0"})
self.assertEqual((result.returncode, result.stdout), (0, "nested\n")) 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): 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") missing = self.run_tool("no-such-command-anywhere")
self.assertEqual((missing.returncode, missing.stdout), (127, "")) self.assertEqual((missing.returncode, missing.stdout), (127, ""))