mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 16:32:26 +01:00
The narrow pull, the log write and the publish each read, merge and write the food log. Run at once (a publish in flight while a second request logs) they lose the append: the phone is told "logged" and the only copy is gone. The new race test lost an entry before this change (1 of 2) and keeps both after it; the shared temp-file name of the log writer also collided between threads. One asyncio lock now covers the pull, "resolve slot + log" and the publish. A request arriving during a publish waits for it (up to one full tick); the model call stays outside the lock. Also: a test that the relay unlocks the header diet_guard's devices store wrote (same key, a blob sealed by one opens on the other), the skill returns grams 0 for nothing to estimate (parse fails closed, nothing is logged), and the doc's session note is corrected: the pull and publish refresh the gate's session file as the CLI's publish does. Failure paths: a lost append is now impossible in-process (fail closed by waiting); the pull failure, locked key, estimator and cleanup paths are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
"""Two close requests must never lose an entry to a pull or a publish.
|
|
|
|
The narrow pull and the full tick each read the food log, merge and write it
|
|
back; ``log_meal_as`` appends to it. Two of those at once lose the append: the
|
|
phone is told "logged" and the only copy is gone. conftest replaces pull and
|
|
publish with mocks, so this test gives them the read-pause-write shape of the
|
|
real ones.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import time
|
|
from typing import TYPE_CHECKING, Any
|
|
from unittest.mock import AsyncMock
|
|
|
|
from diet_guard import _claude_sync
|
|
from diet_guard._state_sync import read_raw_log, write_raw_log
|
|
from diet_guard.tests._claude_worker_fakes import (
|
|
BODY,
|
|
EST,
|
|
RID,
|
|
RID2,
|
|
entries,
|
|
new_server,
|
|
put,
|
|
request_fake,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from diet_guard._claude_schema import Estimate
|
|
|
|
|
|
def _merge_like_a_sync_tick(*_args: object) -> None:
|
|
"""Read the log, take a while merging, write it back: what a tick does."""
|
|
log = read_raw_log()
|
|
time.sleep(0.2)
|
|
write_raw_log(log)
|
|
|
|
|
|
async def _slow_for_b(text: str | None, photo: bytes | None) -> Estimate:
|
|
"""The second request's estimate finishes while the first one publishes."""
|
|
del photo
|
|
if text == "slow":
|
|
await asyncio.sleep(0.05)
|
|
return EST
|
|
|
|
|
|
def test_a_publish_in_flight_does_not_swallow_the_next_request() -> None:
|
|
"""Both entries survive when request B logs during request A's publish."""
|
|
_claude_sync.pull_peers.side_effect = _merge_like_a_sync_tick
|
|
_claude_sync.publish.side_effect = _merge_like_a_sync_tick
|
|
fake = request_fake({**BODY, "text": "fast"}, rid=RID)
|
|
put(fake, RID2, {**BODY, "text": "slow"})
|
|
|
|
async def go() -> None:
|
|
db = fake.rtdb()
|
|
server = new_server(db, AsyncMock(side_effect=_slow_for_b))
|
|
tree: dict[str, Any] = await server._raw.get("requests")
|
|
server.dispatch(tree)
|
|
while server.tasks:
|
|
await asyncio.gather(*list(server.tasks))
|
|
await db.aclose()
|
|
|
|
asyncio.run(go())
|
|
assert len(entries()) == 2
|