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
+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)