Skip to content

Commit 56db161

Browse files
committed
add tests for relaxed cache coherency
1 parent 83539d3 commit 56db161

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

tests/test_store/test_cache_store.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,56 @@ async def test_infinity_max_age(self, cached_store: CacheStore) -> None:
190190
await asyncio.sleep(0.1)
191191
assert cached_store._is_key_fresh("eternal_key")
192192

193-
async def test_missing_key_cleanup(self, cached_store: CacheStore, source_store: Store) -> None:
194-
"""Test that accessing non-existent keys cleans up cache."""
193+
async def test_cache_returns_cached_data_for_performance(
194+
self, cached_store: CacheStore, source_store: Store
195+
) -> None:
196+
"""Test that cache returns cached data for performance, even if not in source."""
195197
# Skip test if key_insert_times attribute doesn't exist
196198
if not hasattr(cached_store, "key_insert_times"):
197199
pytest.skip("key_insert_times attribute not implemented")
198200

199-
# Put data in cache but not source
201+
# Put data in cache but not source (simulates orphaned cache entry)
200202
test_data = CPUBuffer.from_bytes(b"orphaned data")
201203
await cached_store._cache.set("orphan_key", test_data)
202204
cached_store.key_insert_times["orphan_key"] = time.monotonic()
203205

204-
# Access should clean up cache
206+
# Cache should return data for performance (no source verification)
205207
result = await cached_store.get("orphan_key", default_buffer_prototype())
206-
assert result is None
207-
assert not await cached_store._cache.exists("orphan_key")
208-
assert "orphan_key" not in cached_store.key_insert_times
208+
assert result is not None
209+
assert result.to_bytes() == b"orphaned data"
210+
211+
# Cache entry should remain (performance optimization)
212+
assert await cached_store._cache.exists("orphan_key")
213+
assert "orphan_key" in cached_store.key_insert_times
214+
215+
async def test_cache_coherency_through_expiration(self) -> None:
216+
"""Test that cache coherency is managed through cache expiration, not source verification."""
217+
source_store = MemoryStore()
218+
cache_store = MemoryStore()
219+
cached_store = CacheStore(
220+
source_store,
221+
cache_store=cache_store,
222+
max_age_seconds=1, # Short expiration for coherency
223+
)
224+
225+
# Add data to both stores
226+
test_data = CPUBuffer.from_bytes(b"original data")
227+
await cached_store.set("coherency_key", test_data)
228+
229+
# Remove from source (simulating external deletion)
230+
await source_store.delete("coherency_key")
231+
232+
# Cache should still return cached data (performance optimization)
233+
result = await cached_store.get("coherency_key", default_buffer_prototype())
234+
assert result is not None
235+
assert result.to_bytes() == b"original data"
236+
237+
# Wait for cache expiration
238+
await asyncio.sleep(1.1)
239+
240+
# Now stale cache should be refreshed from source
241+
result = await cached_store.get("coherency_key", default_buffer_prototype())
242+
assert result is None # Key no longer exists in source
209243

210244
async def test_cache_info(self, cached_store: CacheStore) -> None:
211245
"""Test cache_info method returns correct information."""

0 commit comments

Comments
 (0)