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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-006 | measured | |
| REQ-DDB-MCP-001 | measured | test_a_query_waits_at_most_a_second_behind_another_build → build_server |
| REQ-DDB-MCP-002 | measured | |
| REQ-DDB-MCP-003 | measured | |
| REQ-DDB-MCP-004 | measured | test_a_query_waits_at_most_a_second_behind_another_build → build_server → _refusal_as_tool_error |
| REQ-DDB-WIKI-034 | measured | test_a_query_waits_at_most_a_second_behind_another_build → build_server → _register_prompts |
| REQ-DDB-WIKI-054 | measured |
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
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
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-006 | measured | |
| REQ-DDB-MCP-001 | measured | test_a_saved_file_wakes_the_refresh_loop → build_server |
| REQ-DDB-MCP-003 | measured | |
| REQ-DDB-MCP-004 | measured | test_a_saved_file_wakes_the_refresh_loop → build_server → _refusal_as_tool_error |
| REQ-DDB-WIKI-034 | measured | test_a_saved_file_wakes_the_refresh_loop → build_server → _register_prompts |
| REQ-DDB-WIKI-054 | measured |
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