Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Add e2e tests: httpx clients survive LLMClientCache eviction

Ryan Crabbe0b7e9a197191
diff --git a/tests/test_litellm/caching/test_llm_client_cache_e2e.py b/tests/test_litellm/caching/test_llm_client_cache_e2e.py
new file mode 100644
index 00000000000..b1f8be3b484
--- /dev/null
+++ b/tests/test_litellm/caching/test_llm_client_cache_e2e.py
@@ -0,0 +1,44 @@
+"""e2e tests: httpx clients obtained via get_async_httpx_client must remain
+usable after LLMClientCache evicts their cache entry."""
+
+import pytest
+
+import litellm
+from litellm.caching.llm_caching_handler import LLMClientCache
+from litellm.llms.custom_httpx.http_handler import get_async_httpx_client
+
+
[email protected](autouse=True)
+def _tiny_client_cache(monkeypatch):
+ """Replace the global client cache with a size-1 cache so eviction
+ triggers on the second insert."""
+ cache = LLMClientCache(max_size_in_memory=1, default_ttl=600)
+ monkeypatch.setattr(litellm, "in_memory_llm_clients_cache", cache)
+ yield cache
+
+
+async def test_evicted_client_is_not_closed():
+ """Get a client via get_async_httpx_client, evict it by caching a second
+ one, then verify the first client's transport is still open."""
+ client_a = get_async_httpx_client(llm_provider="provider_a")
+ # This evicts client_a from cache (capacity=1)
+ get_async_httpx_client(llm_provider="provider_b")
+
+ assert not client_a.client.is_closed
+
+
+async def test_expired_client_is_not_closed():
+ """Get a client, expire it via TTL, then verify the client is still open."""
+ cache = litellm.in_memory_llm_clients_cache
+ client = get_async_httpx_client(llm_provider="provider_ttl")
+
+ # Force the entry to expire and trigger eviction
+ for key in list(cache.ttl_dict.keys()):
+ cache.ttl_dict[key] = 0
+ # Also fix the heap entry so evict_cache finds it
+ cache.expiration_heap = [(0, key) for _, key in cache.expiration_heap]
+ cache.evict_cache()
+
+ assert not client.client.is_closed