test_buildlock.py¶
tests/test_buildlock.py
The cross-process build lock, and above all the ways it must NOT block (#497).
test_a_contended_build_gives_up_and_proceeds
None test_buildlock.test_a_contended_build_gives_up_and_proceeds(Path tmp_path)
A second process must not be blocked indefinitely by the first.
Checks
assert acquired.value == 1, "the child never acquired the lock — test would be vacuous"assert held is False, ("the lock was reported held while a child process held it — that would let two " "builds run believing each was exclusive" )assert elapsed < 4.0, (f"waited {elapsed:.1f}s despite a 0.5s bound; an unbounded wait here is a hang" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | test_a_contended_build_gives_up_and_proceeds → build_lock |
Source · 40 lines
def test_a_contended_build_gives_up_and_proceeds(tmp_path: Path) -> None:
"""THE LOAD-BEARING TEST, because this is the failure this lock could introduce. A holder that
outlasts the wait must leave the second process free to BUILD — reporting `held=False` — not
waiting on it. The caller treats False as "proceed", so a lock that never returned would be a
hang, and hangs are what the three releases before this were about.
The wait is shortened to keep the test fast; the production bound is a backstop.
@brief A waiter past the bound proceeds unlocked.
@return None.
@version 1
"""
db = tmp_path / "clew.db"
acquired = multiprocessing.Value("i", 0)
child = multiprocessing.Process(target=_hold, args=(str(db), acquired, 5.0))
child.start()
try:
for _ in range(100): # wait for the child to actually hold it
if acquired.value:
break
time.sleep(0.05)
assert acquired.value == 1, "the child never acquired the lock — test would be vacuous"
started = time.monotonic()
with build_lock(db, wait_seconds=0.5) as held:
elapsed = time.monotonic() - started
assert held is False, (
"the lock was reported held while a child process held it — that would let two "
"builds run believing each was exclusive"
)
assert elapsed < 4.0, (
f"waited {elapsed:.1f}s despite a 0.5s bound; an unbounded wait here is a hang"
)
finally:
child.join(timeout=10)
if child.is_alive(): # pragma: no cover
child.kill()
child.join()
Activity · 15 steps
test_a_killed_holder_leaves_no_stale_lock
None test_buildlock.test_a_killed_holder_leaves_no_stale_lock(Path tmp_path)
A killed holder must not leave a lock that wedges the next build.
Checks
assert acquired.value == 1, "the child never acquired the lock — test would be vacuous"assert held is True, ("the lock was still unavailable after the holder was SIGKILLed — that is a stale " "lock, and it would wedge every later build of this target" )assert time.monotonic() - started < 5.0, "acquisition after a kill should be immediate"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | test_a_killed_holder_leaves_no_stale_lock → build_lock |
Source · 41 lines
def test_a_killed_holder_leaves_no_stale_lock(tmp_path: Path) -> None:
"""WHY flock RATHER THAN AN O_EXCL LOCKFILE, proved rather than asserted. A lockfile needs a
stale-lock story — a builder killed mid-run leaves the file, and every later process has to
decide whether the holder still lives. Getting that wrong wedges a target permanently, which
is exactly the class of failure this sequence of releases has been about.
`flock` has no such story to get wrong: the kernel drops the lock when the descriptor closes,
including on SIGKILL. So the next process acquires immediately.
@brief SIGKILL releases the lock.
@return None.
@version 1
"""
db = tmp_path / "clew.db"
acquired = multiprocessing.Value("i", 0)
child = multiprocessing.Process(target=_hold, args=(str(db), acquired, 60.0))
child.start()
try:
for _ in range(100):
if acquired.value:
break
time.sleep(0.05)
assert acquired.value == 1, "the child never acquired the lock — test would be vacuous"
child.kill()
child.join(timeout=10)
## Immediately acquirable. A short wait proves it is not merely the bound expiring.
started = time.monotonic()
with build_lock(db, wait_seconds=30.0) as held:
assert held is True, (
"the lock was still unavailable after the holder was SIGKILLed — that is a stale "
"lock, and it would wedge every later build of this target"
)
assert time.monotonic() - started < 5.0, "acquisition after a kill should be immediate"
finally:
if child.is_alive(): # pragma: no cover
child.kill()
child.join()
Activity · 16 steps
test_an_uncontended_build_holds_the_lock
None test_buildlock.test_an_uncontended_build_holds_the_lock(Path tmp_path)
An uncontended build takes the lock.
Checks
assert held is Trueassert lock_path(tmp_path / "clew.db").exists()
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | test_an_uncontended_build_holds_the_lock → build_lock |
Source · 10 lines
def test_an_uncontended_build_holds_the_lock(tmp_path: Path) -> None:
"""@brief The ordinary case reports the lock held.
@return None.
@version 1
"""
with build_lock(tmp_path / "clew.db") as held:
assert held is True
assert lock_path(tmp_path / "clew.db").exists()
Activity · 3 steps
test_an_unwritable_location_proceeds_unlocked
None test_buildlock.test_an_unwritable_location_proceeds_unlocked(Path tmp_path)
An unusable lock location must not stop a build.
Checks
assert held is False, ("an unusable lock location must report not-held rather than raise. This component can " "only ever save a duplicate build, so it must never be able to prevent one." )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | test_an_unwritable_location_proceeds_unlocked → build_lock |
Source · 23 lines
def test_an_unwritable_location_proceeds_unlocked(tmp_path: Path) -> None:
"""FAILS OPEN. An unwritable state directory is a real deployment, and this component can only
ever save a duplicate build — so it must degrade to "no lock" rather than to an error. Asserted
by pointing at a path that cannot be created.
@brief An unopenable lock path yields False rather than raising.
@return None.
@version 1
"""
## A REGULAR FILE WHERE A DIRECTORY MUST BE, rather than a chmod. The first version of this
## test made the OUTER directory read-only and still got the lock: traversal only needs +x, and
## the inner directory it actually wrote to was untouched. ENOTDIR needs no permission games and
## cannot be bypassed by running as root.
obstacle = tmp_path / "not-a-dir"
obstacle.write_text("", encoding="utf-8")
with build_lock(obstacle / "clew.db") as held:
assert held is False, (
"an unusable lock location must report not-held rather than raise. This component can "
"only ever save a duplicate build, so it must never be able to prevent one."
)