Skip to content

Commit 4f368b1

Browse files
committed
test(core.utils): cover FileStreamProgramCache size cap
Exercises the opportunistic mtime-based eviction sweep that runs on each write and confirms that omitting the cap keeps the cache unbounded. Part of issue #178.
1 parent f9d90f9 commit 4f368b1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cuda_core/tests/test_program_cache.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,33 @@ def test_filestream_cache_accepts_str_keys(tmp_path):
559559
cache["my-key"] = _fake_object_code(b"v")
560560
assert "my-key" in cache
561561
assert b"my-key" in cache
562+
563+
564+
def test_filestream_cache_size_cap_evicts_oldest(tmp_path):
565+
from cuda.core.utils import FileStreamProgramCache
566+
567+
# Big payloads, small cap; after the third entry, the cap is exceeded and
568+
# the oldest file (a) must be evicted.
569+
cap = 3000
570+
root = tmp_path / "fc"
571+
with FileStreamProgramCache(root, max_size_bytes=cap) as cache:
572+
cache[b"a"] = _fake_object_code(b"A" * 2000, name="a")
573+
time.sleep(0.02)
574+
cache[b"b"] = _fake_object_code(b"B" * 2000, name="b")
575+
time.sleep(0.02)
576+
cache[b"c"] = _fake_object_code(b"C" * 2000, name="c")
577+
578+
with FileStreamProgramCache(root, max_size_bytes=cap) as cache:
579+
# 'a' was oldest by mtime; the opportunistic sweep on the third write
580+
# must have removed it. 'c' is the newest and must remain.
581+
assert b"a" not in cache
582+
assert b"c" in cache
583+
584+
585+
def test_filestream_cache_unbounded_by_default(tmp_path):
586+
from cuda.core.utils import FileStreamProgramCache
587+
588+
with FileStreamProgramCache(tmp_path / "fc") as cache:
589+
for i in range(20):
590+
cache[f"k{i}".encode()] = _fake_object_code(b"X" * 1024, name=f"n{i}")
591+
assert len(cache) == 20

0 commit comments

Comments
 (0)