@@ -96,44 +96,46 @@ def main():
9696
9797 def test_semantic_cache_performance (self ):
9898 """Test that semantic cache improves performance."""
99+ import tempfile
99100 from vallm .validators .semantic_cache import SemanticCache
100101
101- cache = SemanticCache ()
102-
103- # Mock validation result
104- mock_result = Mock ()
105- mock_result .validator = "semantic"
106- mock_result .score = 0.8
107- mock_result .weight = 1.0
108- mock_result .issues = []
109- mock_result .details = {}
110-
111- code = "def test(): pass"
112- language = "python"
113- model = "test-model"
114-
115- # First call should miss cache
116- start_time = time .time ()
117- cached_result = cache .get (code , language , model )
118- first_call_time = time .time () - start_time
119-
120- assert cached_result is None
121-
122- # Store in cache
123- cache .set (code , language , model , mock_result )
124-
125- # Second call should hit cache
126- start_time = time .time ()
127- cached_result = cache .get (code , language , model )
128- second_call_time = time .time () - start_time
129-
130- assert cached_result is not None
131- assert cached_result .validator == "semantic"
132-
133- # Cache hit should be much faster
134- assert second_call_time < first_call_time * 0.1 , (
135- f"Cache hit ({ second_call_time :.6f} s) should be faster than miss ({ first_call_time :.6f} s)"
136- )
102+ with tempfile .TemporaryDirectory () as temp_dir :
103+ cache = SemanticCache (Path (temp_dir ))
104+
105+ # Mock validation result
106+ mock_result = Mock ()
107+ mock_result .validator = "semantic"
108+ mock_result .score = 0.8
109+ mock_result .weight = 1.0
110+ mock_result .issues = []
111+ mock_result .details = {}
112+
113+ code = "def test(): pass"
114+ language = "python"
115+ model = "test-model"
116+
117+ # First call should miss cache
118+ start_time = time .time ()
119+ cached_result = cache .get (code , language , model )
120+ first_call_time = time .time () - start_time
121+
122+ assert cached_result is None
123+
124+ # Store in cache
125+ cache .set (code , language , model , mock_result )
126+
127+ # Second call should hit cache
128+ start_time = time .time ()
129+ cached_result = cache .get (code , language , model )
130+ second_call_time = time .time () - start_time
131+
132+ assert cached_result is not None
133+ assert cached_result .validator == "semantic"
134+
135+ # Cache hit should be much faster
136+ assert second_call_time < first_call_time * 0.1 , (
137+ f"Cache hit ({ second_call_time :.6f} s) should be faster than miss ({ first_call_time :.6f} s)"
138+ )
137139
138140 def test_cache_key_generation (self ):
139141 """Test cache key generation is consistent."""
@@ -166,6 +168,7 @@ def test_cache_persistence(self):
166168 """Test cache persistence across instances."""
167169 import tempfile
168170 import json
171+ from vallm .validators .semantic_cache import SemanticCache
169172
170173 with tempfile .TemporaryDirectory () as temp_dir :
171174 cache_dir = Path (temp_dir )
0 commit comments