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.

tests/test_buildlock.py:67

Show on the map →

Called by none

Calls build_lock

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measuredtest_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
lines 79–79db = tmp_path / "clew.db"lines 80–80acquired = multiprocessing.Value("i", 0)lines 81–81child = multiprocessing.Process(target=_hold, args=(str(db), acquired, 5.0))lines 82–82child.start()lines 83–83trylines 84–84◇ for _ in range(100): # wait for the child to actually hold itlines 85–85◇ if acquired.valuelines 86–86breaklines 87–87time.sleep(0.05)lines 88–88assert acquired.value == 1, "the child never acquired the lock — test would be vacuous"lines 90–90started = time.monotonic()lines 91–91with build_lock(db, wait_seconds=0.5) as heldlines 92–92elapsed = time.monotonic() - startedlines 93–96assert held is False, ( "the lock was reported held while a child process held it — that would let two " "builds run believing each was exclusive" )lines 97–99assert elapsed < 4.0, ( f"waited {elapsed:.1f}s despite a 0.5s bound; an unbounded wait here is a hang" )

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.

tests/test_buildlock.py:113

Show on the map →

Called by none

Calls build_lock

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measuredtest_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
lines 126–126db = tmp_path / "clew.db"lines 127–127acquired = multiprocessing.Value("i", 0)lines 128–128child = multiprocessing.Process(target=_hold, args=(str(db), acquired, 60.0))lines 129–129child.start()lines 130–130trylines 131–131◇ for _ in range(100)lines 132–132◇ if acquired.valuelines 133–133breaklines 134–134time.sleep(0.05)lines 135–135assert acquired.value == 1, "the child never acquired the lock — test would be vacuous"lines 137–137child.kill()lines 138–138child.join(timeout=10)lines 141–141started = time.monotonic()lines 142–142with build_lock(db, wait_seconds=30.0) as heldlines 143–146assert 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" )lines 147–147assert time.monotonic() - started < 5.0, "acquisition after a kill should be immediate"

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.

tests/test_buildlock.py:51

Checks

  • assert held is True
  • assert lock_path(tmp_path / "clew.db").exists()

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measuredtest_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
lines 56–56with build_lock(tmp_path / "clew.db") as heldlines 57–57assert held is Truelines 58–58assert lock_path(tmp_path / "clew.db").exists()

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.

tests/test_buildlock.py:159

Show on the map →

Called by none

Calls build_lock

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

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measuredtest_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."
        )

Activity · 4 steps
lines 172–172obstacle = tmp_path / "not-a-dir"lines 173–173obstacle.write_text("", encoding="utf-8")lines 175–175with build_lock(obstacle / "clew.db") as heldlines 176–179assert 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." )