Recreate-AAA / tests /test_builder_progress_memory.py
CharlieDreemur's picture
RecreateAAA 0.1.0 from Recreate-AAA/Recreate-AAA@7da1a3d
966b234 verified
Raw History Blame Contribute Delete
4.56 kB
"""History polling keeps one current projection and streams large file reads."""
import hashlib
import json
import os
from pathlib import Path
import pytest
from bench_builder import builder_progress as progress
def _row(number: int) -> bytes:
return json.dumps({"snapshot_id": f"s{number:06d}", "summary": "中文😀"}, ensure_ascii=False).encode()
@pytest.fixture(autouse=True)
def clear_history_cache():
progress._HISTORY_CACHE.clear()
yield
progress._HISTORY_CACHE.clear()
@pytest.mark.parametrize("suffix", [b"", _row(2), _row(2) + b"\r", _row(2) + b"\r" + _row(3) + b"\n"])
@pytest.mark.parametrize("ending", [b"\n", b"\r\n"])
def test_history_keeps_exact_committed_prefix(tmp_path: Path, suffix: bytes, ending: bytes) -> None:
path = tmp_path / "history.jsonl"
first = _row(1) + ending
path.write_bytes(first + suffix)
rows, valid_end = progress._read_history(str(path))
assert rows == [json.loads(_row(1))]
assert valid_end == len(first)
@pytest.mark.parametrize("invalid", [b"not-json\n", b"[]\n", _row(3) + b"\n", b'"\xff"\n'])
def test_history_still_rejects_invalid_committed_records(tmp_path: Path, invalid: bytes) -> None:
path = tmp_path / "history.jsonl"
path.write_bytes(_row(1) + b"\n" + invalid)
with pytest.raises(progress.BuilderProgressError, match="invalid committed record"):
progress._read_history(str(path))
assert str(path) not in progress._HISTORY_CACHE
def test_append_replaces_cached_version_without_mutating_prior_result(tmp_path: Path, monkeypatch) -> None:
path = tmp_path / "history.jsonl"
path.write_bytes(_row(1) + b"\n")
first = progress._read_history(str(path))
loads = progress.json.loads
calls = []
def tracked_loads(value):
calls.append(value)
return loads(value)
monkeypatch.setattr(progress.json, "loads", tracked_loads)
assert progress._read_history(str(path)) is first
assert calls == []
for number in range(2, 20):
with path.open("ab") as stream:
stream.write(_row(number) + b"\n")
current = progress._read_history(str(path))
assert len(current[0]) == number
assert len(progress._HISTORY_CACHE) == 1
assert len(first[0]) == 1
def test_replaced_file_with_same_size_and_mtime_is_reloaded(tmp_path: Path) -> None:
path = tmp_path / "history.jsonl"
path.write_bytes(b'{"snapshot_id":"s000001","value":"old"}\n')
first = progress._read_history(str(path))
original = path.stat()
replacement = tmp_path / "replacement"
replacement.write_bytes(b'{"snapshot_id":"s000001","value":"new"}\n')
os.utime(replacement, ns=(original.st_atime_ns, original.st_mtime_ns))
replacement.replace(path)
assert progress._read_history(str(path))[0][0]["value"] == "new"
assert first[0][0]["value"] == "old"
def test_history_cache_bounds_distinct_files(tmp_path: Path) -> None:
for number in range(20):
path = tmp_path / str(number)
path.write_bytes(_row(1) + b"\n")
progress._read_history(str(path))
assert len(progress._HISTORY_CACHE) == 16
assert str(tmp_path / "0") not in progress._HISTORY_CACHE
def test_history_changed_during_read_is_not_cached(tmp_path: Path, monkeypatch) -> None:
path = tmp_path / "history.jsonl"
path.write_bytes(_row(1) + b"\n")
loads = progress.json.loads
appended = False
def append_during_parse(value):
nonlocal appended
if not appended:
appended = True
with path.open("ab") as stream:
stream.write(_row(2) + b"\n")
return loads(value)
monkeypatch.setattr(progress.json, "loads", append_during_parse)
progress._read_history(str(path))
assert str(path) not in progress._HISTORY_CACHE
assert len(progress._read_history(str(path))[0]) == 2
def test_progress_hash_and_history_do_not_materialize_whole_files(tmp_path: Path, monkeypatch) -> None:
binary = tmp_path / "asset.bin"
binary.write_bytes(bytes(range(256)) * 20_000)
expected = hashlib.sha256(binary.read_bytes()).hexdigest()
history = tmp_path / "history.jsonl"
history.write_bytes(_row(1) + b"\n")
def no_read_bytes(*args):
raise AssertionError("Progress must stream file reads")
monkeypatch.setattr(Path, "read_bytes", no_read_bytes)
stat = binary.stat()
assert progress._file_hash(str(binary), stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns) == expected
assert progress._read_history(str(history))[0] == [json.loads(_row(1))]