Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s

This commit is contained in:
2026-11-19 12:42:03 +01:00
parent 8ca612b7a4
commit cb3135bc8c
124 changed files with 1437 additions and 793 deletions
+10 -7
View File
@@ -2,7 +2,6 @@
import fcntl
import os
import pathlib
import time
import typing
CPUS_PER_SLOT = 4
@@ -26,12 +25,16 @@ def slot_folder(env: typing.Mapping[str, str], home: pathlib.Path) -> pathlib.Pa
return pathlib.Path(state) / "run-heavy"
def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
"""The number and open file of the lowest slot this call locked; None while every slot is held. Raises OSError where a slot file cannot be opened or locked."""
# Locks an open file, as fcntl.flock does.
Lock = typing.Callable[[int, int], None]
def taken_slot(folder: pathlib.Path, slots: int, lock: Lock) -> tuple[int, int] | None:
"""The number and open file of the lowest slot this call locked, through lock; None while every slot is held. Raises OSError where a slot file cannot be opened or locked."""
for slot in range(slots):
fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644)
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
lock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
os.close(fd)
continue
@@ -42,10 +45,10 @@ def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
return None
def waited_slot(folder: pathlib.Path, slots: int, say: typing.Callable[[], None], sleep: typing.Callable[[float], None] = time.sleep) -> tuple[int, int]:
"""The number and open file of a slot this call locked, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS."""
def waited_slot(folder: pathlib.Path, slots: int, say: typing.Callable[[], None], sleep: typing.Callable[[float], None], lock: Lock) -> tuple[int, int]:
"""The number and open file of a slot this call locked, through lock, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS."""
said = False
while (taken := taken_slot(folder, slots)) is None:
while (taken := taken_slot(folder, slots, lock)) is None:
if not said:
say()
said = True
+14 -9
View File
@@ -5,7 +5,6 @@ import pathlib
import sys
import tempfile
import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -57,13 +56,19 @@ class SlotFolder(unittest.TestCase):
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):
locked = []
def lock(fd, operation):
locked.append(fd)
raise OSError(errno.ENOLCK, "No locks available")
with tempfile.TemporaryDirectory() as folder:
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])
slots.taken_slot(pathlib.Path(folder), 2, lock)
self.assertEqual(len(locked), 1)
with self.assertRaises(OSError) as closed:
os.fstat(locked[0])
self.assertEqual(closed.exception.errno, errno.EBADF)
class WaitedSlot(unittest.TestCase):
@@ -79,14 +84,14 @@ class WaitedSlot(unittest.TestCase):
if len(slept) == 2:
held[1].close()
slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: said.append(True), sleep)
slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: said.append(True), sleep, fcntl.flock)
os.close(fd)
held[0].close()
self.assertEqual((slot, said, slept), (1, [True], [slots.POLL_SECONDS] * 2))
def test_takes_a_free_slot_at_once(self):
with tempfile.TemporaryDirectory() as folder:
slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: self.fail("said it waits"), lambda seconds: self.fail("slept"))
slot, fd = slots.waited_slot(pathlib.Path(folder), 2, lambda: self.fail("said it waits"), lambda seconds: self.fail("slept"), fcntl.flock)
os.close(fd)
self.assertEqual(slot, 0)