Skip to content

Commit 052b1e2

Browse files
committed
test: cover empty-heap exit branch in _async_evict_oldest
``_async_evict_oldest`` falls through to a silent return if ``_expire_heap`` is empty. By the cache invariant every record counted in ``_total_records`` has a heap entry, so this branch is unreachable in normal operation — but it's a defense-in-depth path worth pinning. Force the broken state directly (``_total_records = MAX, _expire_heap = []``) and verify eviction returns gracefully while the subsequent insert still completes.
1 parent 7c6be63 commit 052b1e2

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tests/test_cache.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,19 @@ def test_cache_size_is_bounded() -> None:
519519
assert f"flood-{i}.local." in cache.cache
520520

521521

522+
def test_cache_eviction_empty_heap_returns_without_evicting() -> None:
523+
"""Eviction tolerates an empty ``_expire_heap`` (invariant-violation safety net)."""
524+
cache = r.DNSCache()
525+
# By the cache invariant every record in ``_total_records`` has a heap
526+
# entry, so eviction should never see an empty heap. Force the broken
527+
# state directly to pin the defensive behaviour: ``_async_evict_oldest``
528+
# returns without raising and the subsequent insert still lands.
529+
cache._total_records = const._MAX_CACHE_RECORDS
530+
cache._expire_heap = []
531+
cache.async_add_records([_addr("post-empty.local.", 0)])
532+
assert "post-empty.local." in cache.cache
533+
534+
522535
def test_cache_eviction_skips_stale_heap_entries() -> None:
523536
"""Eviction skips stale heap entries left by TTL re-adds."""
524537
cache = r.DNSCache()

0 commit comments

Comments
 (0)