Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: migrate 3 search_cache TTL tests from time.sleep to freezegun (#4289)

LearningCircuitd4fdd1345a36
diff --git a/tests/utilities/test_search_cache.py b/tests/utilities/test_search_cache.py
index 5886b8c375..2a713f84f0 100644
--- a/tests/utilities/test_search_cache.py
+++ b/tests/utilities/test_search_cache.py
@@ -3,12 +3,14 @@ Comprehensive tests for search_cache module.
Tests caching, TTL, LRU eviction, stampede protection, and persistence.
"""
-import pytest
import tempfile
-import time
import threading
-from unittest.mock import patch
+import time
from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+from freezegun import freeze_time
class TestSearchCacheInit:
@@ -209,26 +211,27 @@ class TestSearchCachePutGet:
assert retrieved == results
def test_custom_ttl(self, cache):
- """Test that custom TTL is respected."""
+ """Test that custom TTL is respected.
+
+ SearchCache uses int(time.time()) for expires_at comparison
+ (search_cache.py:118, :160), so freezegun mocks the clock
+ deterministically — no real sleep needed.
+ """
results = [{"title": "Test"}]
- # Use short TTL of 2 seconds
- cache.put("test query", results, ttl=2)
+ with freeze_time("2026-01-01 00:00:00") as frozen:
+ cache.put("test query", results, ttl=2)
- # Should be available immediately
- retrieved = cache.get("test query")
- assert retrieved is not None, (
- "Cache should be available immediately after put"
- )
+ # Should be available immediately
+ assert cache.get("test query") is not None
- # Wait for expiration
- time.sleep(2.5) # allow: unmarked-sleep
+ # Advance past TTL
+ frozen.tick(2.5)
- # Clear memory cache to force DB check
- cache._memory_cache.clear()
+ # Clear memory cache to force DB check
+ cache._memory_cache.clear()
- # Should be expired
- assert cache.get("test query") is None
+ assert cache.get("test query") is None
class TestSearchCacheExpiration:
@@ -243,32 +246,42 @@ class TestSearchCacheExpiration:
yield SearchCache(cache_dir=tmpdir, default_ttl=1)
def test_expired_entries_not_returned(self, cache):
- """Test that expired entries are not returned."""
+ """Test that expired entries are not returned.
+
+ Migrated from real time.sleep(1.5) to freezegun. The cache's
+ default_ttl is 1s for this fixture (see fixture above).
+ """
results = [{"title": "Test"}]
- cache.put("test query", results)
- # Wait for expiration
- time.sleep(1.5) # allow: unmarked-sleep
+ with freeze_time("2026-01-01 00:00:00") as frozen:
+ cache.put("test query", results)
- # Clear memory cache to force DB check
- cache._memory_cache.clear()
+ # Advance past TTL
+ frozen.tick(1.5)
- result = cache.get("test query")
- assert result is None
+ # Clear memory cache to force DB check
+ cache._memory_cache.clear()
+
+ assert cache.get("test query") is None
def test_expired_memory_entries_removed(self, cache):
- """Test that expired memory entries are removed."""
+ """Test that expired memory entries are removed.
+
+ Migrated from real time.sleep(1.5) to freezegun.
+ """
results = [{"title": "Test"}]
- cache.put("test query", results)
- # Wait for expiration
- time.sleep(1.5) # allow: unmarked-sleep
+ with freeze_time("2026-01-01 00:00:00") as frozen:
+ cache.put("test query", results)
- # Access should remove expired entry from memory
- cache.get("test query")
+ # Advance past TTL
+ frozen.tick(1.5)
- query_hash = cache._get_query_hash("test query")
- assert query_hash not in cache._memory_cache
+ # Access should remove expired entry from memory
+ cache.get("test query")
+
+ query_hash = cache._get_query_hash("test query")
+ assert query_hash not in cache._memory_cache
class TestSearchCacheLRUEviction: