From 67e905f0d0cc2dd9ead3d1bac24918e31e8b096e Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Sun, 15 Mar 2026 15:18:22 -0700 Subject: [PATCH] Fix flaky encrypted_content_affinity tests: clear HTTP client cache, disable retries Tests failed intermittently in CI (-n 8 workers) because cached AsyncHTTPHandler instances from other tests bypassed the class-level mock on AsyncHTTPHandler.post, causing real requests to OpenAI with mock API keys. Router retries (default 2) masked the root cause. - Add autouse fixture to flush litellm.in_memory_llm_clients_cache before/after each test so mocks always apply to fresh clients - Set num_retries=0 on all Router instances to surface mock failures immediately instead of silently retrying Co-Authored-By: Claude Opus 4.6 --- .../test_encrypted_content_affinity_check.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_litellm/router_utils/pre_call_checks/test_encrypted_content_affinity_check.py b/tests/test_litellm/router_utils/pre_call_checks/test_encrypted_content_affinity_check.py index 6e845e9d050..5f629f1fb32 100644 --- a/tests/test_litellm/router_utils/pre_call_checks/test_encrypted_content_affinity_check.py +++ b/tests/test_litellm/router_utils/pre_call_checks/test_encrypted_content_affinity_check.py @@ -28,6 +28,22 @@ import json import litellm from litellm.responses.utils import ResponsesAPIRequestUtils + +@pytest.fixture(autouse=True) +def _clear_http_client_cache(): + """ + Clear the shared HTTP client cache before each test so that cached clients + from other tests (running in the same pytest-xdist worker) do not bypass + class-level mocks on AsyncHTTPHandler.post. + """ + cache = getattr(litellm, "in_memory_llm_clients_cache", None) + if cache is not None: + cache.flush_cache() + yield + cache = getattr(litellm, "in_memory_llm_clients_cache", None) + if cache is not None: + cache.flush_cache() + # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- @@ -301,6 +317,7 @@ async def test_encrypted_content_affinity_tracks_and_routes(): }, ], optional_pre_call_checks=["encrypted_content_affinity"], + num_retries=0, ) selected_deployments = [] @@ -376,6 +393,7 @@ async def test_encrypted_content_affinity_no_effect_on_chat_completions(): }, ], optional_pre_call_checks=["encrypted_content_affinity"], + num_retries=0, ) response1 = await router.acompletion( @@ -435,6 +453,7 @@ async def test_encrypted_content_affinity_bypasses_rpm_limits(): ], optional_pre_call_checks=["encrypted_content_affinity"], routing_strategy="usage-based-routing-v2", + num_retries=0, ) selected_deployments = [] @@ -527,6 +546,7 @@ async def test_encrypted_content_affinity_no_match_normal_routing(): }, ], optional_pre_call_checks=["encrypted_content_affinity"], + num_retries=0, ) with patch( @@ -588,6 +608,7 @@ async def test_encrypted_content_affinity_with_wrapped_content_no_id(): }, ], optional_pre_call_checks=["encrypted_content_affinity"], + num_retries=0, ) selected_deployments = []