Skip to content

Commit abec1a4

Browse files
fix: address session-cache PR review feedback
1 parent 40cb91a commit abec1a4

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

tests/benchmarks/test_cache_bench.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ def _reset_cache() -> None:
1717
@pytest.mark.benchmark(group="cache")
1818
def test_cache_cold_parse(benchmark, parse_medium_file: Path) -> None:
1919
path = str(parse_medium_file)
20-
21-
def cold() -> None:
22-
clear_cache()
23-
get_cached_session(path)
24-
25-
benchmark(cold)
20+
benchmark.pedantic(get_cached_session, args=(path,), setup=clear_cache)
2621

2722

2823
@pytest.mark.benchmark(group="cache")

tests/test_session_cache.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from __future__ import annotations
44

5+
import os
56
import shutil
6-
import time
77
from pathlib import Path
88

99
import pytest
@@ -51,12 +51,38 @@ def counting_parse(p: str):
5151
def test_cache_invalidates_on_mtime_change(sample_session: Path) -> None:
5252
path = str(sample_session)
5353
first = get_cached_session(path)
54-
time.sleep(0.05)
55-
sample_session.touch()
54+
stat = sample_session.stat()
55+
os.utime(sample_session, (stat.st_mtime + 1, stat.st_mtime + 1))
5656
second = get_cached_session(path)
5757
assert first is not second
5858

5959

60+
def test_cache_normalizes_relative_and_absolute_paths(
61+
sample_session: Path,
62+
tmp_path: Path,
63+
monkeypatch: pytest.MonkeyPatch,
64+
) -> None:
65+
calls = 0
66+
67+
def counting_parse(p: str):
68+
nonlocal calls
69+
calls += 1
70+
return parse_session(p)
71+
72+
monkeypatch.setattr("utils.session_cache.parse_session", counting_parse)
73+
rel = "session.jsonl"
74+
abs_path = str(sample_session.resolve())
75+
original_cwd = os.getcwd()
76+
try:
77+
os.chdir(tmp_path)
78+
get_cached_session(rel)
79+
assert calls == 1
80+
get_cached_session(abs_path)
81+
assert calls == 1
82+
finally:
83+
os.chdir(original_cwd)
84+
85+
6086
def test_lru_eviction(sample_session: Path, tmp_path: Path) -> None:
6187
set_max_entries(2)
6288
content = sample_session.read_text(encoding="utf-8")
@@ -87,3 +113,8 @@ def counting_parse(p: str):
87113
assert calls == 1
88114
finally:
89115
monkeypatch.undo()
116+
117+
118+
def test_set_max_entries_rejects_negative() -> None:
119+
with pytest.raises(ValueError, match="non-negative"):
120+
set_max_entries(-1)

utils/session_cache.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def clear_cache() -> None:
4242

4343
def set_max_entries(max_entries: int) -> None:
4444
"""Override the LRU capacity (primarily for tests)."""
45+
if max_entries < 0:
46+
raise ValueError(f"max_entries must be non-negative, got {max_entries}")
4547
global _max_entries
4648
with _lock:
4749
_max_entries = max_entries

0 commit comments

Comments
 (0)