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(utilities): replace time.sleep with freezegun in 3 TTL tests (#4101)

LearningCircuit7fd0a64e5e1a
diff --git a/tests/utilities/test_search_cache_stampede.py b/tests/utilities/test_search_cache_stampede.py
index 3b9e8667a5..0a63ab32d3 100644
--- a/tests/utilities/test_search_cache_stampede.py
+++ b/tests/utilities/test_search_cache_stampede.py
@@ -12,6 +12,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
from unittest.mock import patch
import pytest
+from freezegun import freeze_time
class TestStampedeProtectionConcurrency:
@@ -461,48 +462,57 @@ class TestTTLExpiration:
def test_expired_entry_not_returned(self, short_ttl_cache):
"""Expired entry returns None."""
- # Use ttl=4 to avoid flaky failures when put/get straddle a second
- # boundary (int(time.time()) truncation can cause off-by-one).
- short_ttl_cache.put("expiring", [{"title": "Temp"}], "engine1", ttl=4)
+ # SUT compares int(time.time()) against stored expires_at, so we can
+ # freeze the clock and tick forward instead of sleeping real time.
+ with freeze_time("2026-01-01 00:00:00") as frozen:
+ short_ttl_cache.put(
+ "expiring", [{"title": "Temp"}], "engine1", ttl=4
+ )
- # Immediately should work
- result = short_ttl_cache.get("expiring", "engine1")
- assert result is not None
+ # Immediately should work
+ result = short_ttl_cache.get("expiring", "engine1")
+ assert result is not None
- # Wait for expiration
- time.sleep(5) # allow: unmarked-sleep
+ # Advance past expiration
+ frozen.tick(5)
- result = short_ttl_cache.get("expiring", "engine1")
- assert result is None
+ result = short_ttl_cache.get("expiring", "engine1")
+ assert result is None
def test_expired_entry_removed_from_memory(self, short_ttl_cache):
"""Expired entry removed on access."""
- short_ttl_cache.put(
- "memory_expire", [{"title": "Temp"}], "engine1", ttl=2
- )
- query_hash = short_ttl_cache._get_query_hash("memory_expire", "engine1")
+ with freeze_time("2026-01-01 00:00:00") as frozen:
+ short_ttl_cache.put(
+ "memory_expire", [{"title": "Temp"}], "engine1", ttl=2
+ )
+ query_hash = short_ttl_cache._get_query_hash(
+ "memory_expire", "engine1"
+ )
- assert query_hash in short_ttl_cache._memory_cache
+ assert query_hash in short_ttl_cache._memory_cache
- time.sleep(2.5) # allow: unmarked-sleep
+ frozen.tick(3)
- # Access triggers removal
- short_ttl_cache.get("memory_expire", "engine1")
+ # Access triggers removal
+ short_ttl_cache.get("memory_expire", "engine1")
- assert query_hash not in short_ttl_cache._memory_cache
+ assert query_hash not in short_ttl_cache._memory_cache
def test_cleanup_removes_expired_from_database(self, short_ttl_cache):
"""_cleanup_expired removes DB entries."""
- short_ttl_cache.put("db_expire", [{"title": "DB"}], "engine1", ttl=2)
+ with freeze_time("2026-01-01 00:00:00") as frozen:
+ short_ttl_cache.put(
+ "db_expire", [{"title": "DB"}], "engine1", ttl=2
+ )
- time.sleep(2.5) # allow: unmarked-sleep
+ frozen.tick(3)
- # Run cleanup
- short_ttl_cache._cleanup_expired()
+ # Run cleanup
+ short_ttl_cache._cleanup_expired()
- # Should not be in database
- result = short_ttl_cache.get("db_expire", "engine1")
- assert result is None
+ # Should not be in database
+ result = short_ttl_cache.get("db_expire", "engine1")
+ assert result is None
def test_ttl_boundary_condition(self, short_ttl_cache):
"""Entry at exact TTL boundary."""