test_watch_refresh.py

tests/test_watch_refresh.py

A save wakes the refresh loop; a query never waits long behind someone else's build.

test_a_query_waits_at_most_a_second_behind_another_build

None test_watch_refresh.test_a_query_waits_at_most_a_second_behind_another_build(Path tmp_path, pytest.MonkeyPatch monkeypatch)

A stale query behind a held build lock returns within ~1 s.

tests/test_watch_refresh.py:79

Checks

  • assert elapsed and elapsed[0] < 2.0, f"the query waited {elapsed} s behind another build"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-006measured
REQ-DDB-MCP-001measuredtest_a_query_waits_at_most_a_second_behind_another_build → build_server
REQ-DDB-MCP-002measured
REQ-DDB-MCP-003measured
REQ-DDB-MCP-004measuredtest_a_query_waits_at_most_a_second_behind_another_build → build_server → _refusal_as_tool_error
REQ-DDB-WIKI-034measuredtest_a_query_waits_at_most_a_second_behind_another_build → build_server → _register_prompts
REQ-DDB-WIKI-054measured
Source · 39 lines
async def test_a_query_waits_at_most_a_second_behind_another_build(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """THE BACKGROUND BUILD MAY HOLD THE LOCK FOR SECONDS; the query answers from the index as it
    stands — with its staleness notice — after one.

    @brief A stale query behind a held build lock returns within ~1 s.
    @version 1
    """
    monkeypatch.setattr(server_module, "_LOCK_WAIT_SECONDS", 120)
    reg = st.TargetRegistry(tmp_path / "state")
    _mcp, state = build_server(reg)
    repo = tmp_path / "busy"
    repo.mkdir()
    (repo / "a.c").write_text("int main(void){return 0;}\n", encoding="utf-8")
    target = state.resolve_target(str(repo))
    monkeypatch.setattr(state, "_data_stale", lambda _t: True)
    elapsed: list[float] = []

    async def _hold() -> None:
        async with state._build_lock(target.repo_path):
            await anyio.sleep(6)

    async def _query() -> None:
        started = time.monotonic()
        await state._auto_refresh(str(repo))
        elapsed.append(time.monotonic() - started)

    with anyio.move_on_after(5):
        async with anyio.create_task_group() as tg:
            tg.start_soon(_hold)
            await anyio.sleep(0.1)
            tg.start_soon(_query)
            while not elapsed:
                await anyio.sleep(0.05)
            tg.cancel_scope.cancel()
    assert elapsed and elapsed[0] < 2.0, f"the query waited {elapsed} s behind another build"

Activity · 20 steps
lines 88–88monkeypatch.setattr(server_module, "_LOCK_WAIT_SECONDS", 120)lines 89–89reg = st.TargetRegistry(tmp_path / "state")lines 90–90_mcp, state = build_server(reg)lines 91–91repo = tmp_path / "busy"lines 92–92repo.mkdir()lines 93–93(repo / "a.c").write_text("int main(void){return 0;}\n", encoding="utf-8")lines 94–94target = state.resolve_target(str(repo))lines 95–95monkeypatch.setattr(state, "_data_stale", lambda _t: True)lines 96–96elapsed: list[float] = []lines 98–100async def _hold() -> None: async with state._build_lock(target.repo_path): await anyio.sleep(6)lines 102–105async def _query() -> None: started = time.monotonic() await state._auto_refresh(str(repo)) elapsed.append(time.monotonic() - started)lines 107–107with anyio.move_on_after(5)lines 108–108async with anyio.create_task_group() as tglines 109–109tg.start_soon(_hold)lines 110–110await anyio.sleep(0.1)lines 111–111tg.start_soon(_query)lines 112–112◇ while not elapsedlines 113–113await anyio.sleep(0.05)lines 114–114tg.cancel_scope.cancel()lines 115–115assert elapsed and elapsed[0] < 2.0, f"the query waited {elapsed} s behind another build"

test_a_saved_file_wakes_the_refresh_loop

None test_watch_refresh.test_a_saved_file_wakes_the_refresh_loop(Path tmp_path, pytest.MonkeyPatch monkeypatch)

A file event wakes keep_current.

tests/test_watch_refresh.py:44

Checks

  • assert passes, "no refresh pass followed the save"
  • assert passes[0] - started < 2.0, f"the pass came {passes[0] - started:.2f} s after the save"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-006measured
REQ-DDB-MCP-001measuredtest_a_saved_file_wakes_the_refresh_loop → build_server
REQ-DDB-MCP-003measured
REQ-DDB-MCP-004measuredtest_a_saved_file_wakes_the_refresh_loop → build_server → _refusal_as_tool_error
REQ-DDB-WIKI-034measuredtest_a_saved_file_wakes_the_refresh_loop → build_server → _register_prompts
REQ-DDB-WIKI-054measured
Source · 35 lines
async def test_a_saved_file_wakes_the_refresh_loop(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """WITHIN A SECOND, NOT AN INTERVAL: the loop runs with a 60 s interval, and a file written
    into a watched repository must trigger a pass long before that.

    @brief A file event wakes keep_current.
    @version 1
    """
    registry = st.TargetRegistry(tmp_path / "state")
    _mcp, state = build_server(registry)
    repo = tmp_path / "repo"
    repo.mkdir()
    state._touched["t"] = registry.register(repo)
    passes: list[float] = []

    async def _record() -> list[str]:
        passes.append(time.monotonic())
        return []

    monkeypatch.setattr(state, "refresh_touched", _record)
    with anyio.move_on_after(10):
        async with anyio.create_task_group() as tg:
            tg.start_soon(state.keep_current, 60)
            await anyio.sleep(1.0)  # let the watch start
            started = time.monotonic()
            (repo / "saved.c").write_text("int x;\n")
            while not passes:
                await anyio.sleep(0.05)
            tg.cancel_scope.cancel()
    assert passes, "no refresh pass followed the save"
    assert passes[0] - started < 2.0, f"the pass came {passes[0] - started:.2f} s after the save"


@pytest.mark.anyio