Files
NODEDC_MISSION_CORE/tests/test_cache_budget.py
T
DCCONSTRUCTIONS 70ef3f9f1c chore(storage): bound build retention, caches and test resources
Track build ownership, qualified pins and bounded cache/log lifecycles. Move Vite fixtures to a job-owned server helper and preserve private outputs outside Git.
2026-10-29 09:09:58 +03:00

112 lines
4.4 KiB
Python

import hashlib
from types import SimpleNamespace
import pytest
from k1link import cache_budget as budget
def test_quota_preserves_unknown_existing_and_counts_pending_files(tmp_path):
root = tmp_path / "cache"
root.mkdir()
(root / "unknown-result").write_bytes(b"evidence")
(root / ".ongoing.tmp").write_bytes(b"x" * 4)
with (
pytest.raises(budget.CacheCapacityError),
budget.cache_publication(root, 5, limit=16, reserve=0),
):
pytest.fail("admitted over quota")
assert (root / "unknown-result").read_bytes() == b"evidence"
assert (root / ".ongoing.tmp").read_bytes() == b"x" * 4
with budget.cache_publication(root, 4, limit=16, reserve=0):
(root / "new").write_bytes(b"y" * 4)
assert budget.usage(root) == (16, 3)
with pytest.raises(budget.CacheCapacityError):
budget.require_cache_room(root, 1, limit=16, reserve=0)
def test_quota_free_reserve_and_unsafe_paths(tmp_path, monkeypatch):
monkeypatch.setattr(budget.shutil, "disk_usage", lambda _: SimpleNamespace(free=10))
with pytest.raises(budget.CacheCapacityError):
budget.require_cache_room(tmp_path / "cache", 1, limit=100, reserve=10)
root = tmp_path / "unsafe"
root.mkdir()
outside = tmp_path / "source"
outside.write_text("keep")
(root / "data").symlink_to(outside)
with pytest.raises(budget.CacheCapacityError):
budget.require_cache_room(root, 0, limit=100, reserve=0)
assert outside.read_text() == "keep"
def test_quota_serializes_process_publications(tmp_path):
import subprocess
import sys
script = """from pathlib import Path
from k1link.cache_budget import cache_publication,CacheCapacityError
import sys,time
try:
with cache_publication(Path(sys.argv[1]),6,limit=10,reserve=0):
time.sleep(.1)
(Path(sys.argv[1])/sys.argv[2]).write_bytes(b"123456")
except CacheCapacityError:sys.exit(2)
"""
commands = [[sys.executable, "-c", script, str(tmp_path), str(i)] for i in range(2)]
children = [subprocess.Popen(c) for c in commands]
assert sorted(p.wait(timeout=10) for p in children) == [0, 2]
assert budget.usage(tmp_path)[0] == 6
def test_editor_reserves_full_pending_transfers_and_keeps_ready_on_quota(tmp_path, monkeypatch):
from k1link.simulation.ai_polygon.editor_cache import EditorCache
worlds = {
str(i): {
"world_id": str(i),
"sha256": str(i) * 64,
"status": "available",
"settings": {"prepared": True},
}
for i in (1, 2)
}
cache = EditorCache(tmp_path, SimpleNamespace(get=lambda key: worlds[key]))
rows = [cache.request(str(i)) for i in (1, 2)]
payload = b"a" * 8
files = {
n: {"byte_length": len(payload), "sha256": hashlib.sha256(payload).hexdigest()}
for n in ("scene.sog", "collision.glb")
}
before = budget.usage(cache.root)[0]
monkeypatch.setitem(budget.LIMITS, "editor-cache", before + 65536 + 30)
cache.update(rows[0]["key"], {"action": "manifest", "files": files})
# Second transfer cannot consume bytes already promised to the first.
with pytest.raises(budget.CacheCapacityError):
cache.update(rows[1]["key"], {"action": "manifest", "files": files})
assert cache.get(rows[0]["key"])["status"] == "uploading"
assert cache.get(rows[1]["key"])["status"] == "queued"
def test_camera_cache_capacity_does_not_fail_decoded_frame(tmp_path, monkeypatch):
# Exercise the actual extraction path, replacing only immutable media decoding.
import threading
from collections import OrderedDict
from k1link.sessions.camera_frame import RecordedCameraFrame, RecordedCameraFrameService
service = RecordedCameraFrameService.__new__(RecordedCameraFrameService)
service._cache_root = tmp_path
service._coordination = threading.Condition()
service._lanes = OrderedDict()
service._max_decode_lanes = 1
segment = SimpleNamespace(sha256="f" * 64)
manifest = SimpleNamespace(
generation_sha256="g", artifact_id="a", epochs=[SimpleNamespace(segments=[segment])]
)
monkeypatch.setattr(service, "_source_manifest", lambda *a, **k: manifest)
frame = RecordedCameraFrame(b"0123456789", "image/jpeg", 1, 1, "x")
monkeypatch.setattr(service, "_decode", lambda *a: frame)
monkeypatch.setitem(budget.LIMITS, "camera-frame-cache", 5)
assert service.extract("s", 0) == frame
assert not list(tmp_path.glob("*.jpg"))