From 452ee4dc0884d226b14e99be8d6e84f38e1e05e6 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 15 Oct 2026 05:53:30 -0700 Subject: [PATCH] test(caching): baseline breaker metrics from live state after gc (#46652) * test(caching): baseline breaker metrics from live state after gc Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(caching): collect garbage before the services logger gauge test reads live breakers --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com> --- tests/unit/caching/test_redis_cache.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/unit/caching/test_redis_cache.py b/tests/unit/caching/test_redis_cache.py index 377527497cc..2bdc7b9549b 100644 --- a/tests/unit/caching/test_redis_cache.py +++ b/tests/unit/caching/test_redis_cache.py @@ -1,4 +1,5 @@ import asyncio +import gc import time import types from collections.abc import Iterator @@ -978,7 +979,11 @@ async def test_breaker_metrics_track_state_and_failure_class(monkeypatch): from redis.exceptions import TimeoutError as RedisTimeoutError from litellm._service_logger import ServiceLogging - from litellm.caching.redis_cache import RedisCircuitBreaker, is_redis_timeout_failure + from litellm.caching.redis_cache import ( + RedisCircuitBreaker, + is_redis_timeout_failure, + redis_breaker_state_counts, + ) monkeypatch.setattr(litellm, "service_callback", ["prometheus_system"]) service_logger: Final = ServiceLogging() @@ -986,13 +991,15 @@ async def test_breaker_metrics_track_state_and_failure_class(monkeypatch): def sample(name, labels=None): return REGISTRY.get_sample_value(name, labels) or 0.0 - timeout_before = sample("litellm_redis_circuit_breaker_failures_total", {"failure_class": "timeout"}) - hard_before = sample("litellm_redis_circuit_breaker_failures_total", {"failure_class": "connectivity"}) - opened_before = sample("litellm_redis_circuit_breaker_transitions_total", {"state": "open"}) - open_gauge_before = sample("litellm_redis_circuit_breaker_state", {"state": "open"}) - closed_gauge_before = sample("litellm_redis_circuit_breaker_state", {"state": "closed"}) + gc.collect() + timeout_before: Final = sample("litellm_redis_circuit_breaker_failures_total", {"failure_class": "timeout"}) + hard_before: Final = sample("litellm_redis_circuit_breaker_failures_total", {"failure_class": "connectivity"}) + opened_before: Final = sample("litellm_redis_circuit_breaker_transitions_total", {"state": "open"}) + state_counts_before: Final = redis_breaker_state_counts() + open_gauge_before: Final = state_counts_before.get(RedisCircuitBreaker.OPEN, 0) + closed_gauge_before: Final = state_counts_before.get(RedisCircuitBreaker.CLOSED, 0) - breaker = RedisCircuitBreaker( + breaker: Final = RedisCircuitBreaker( failure_threshold=2, recovery_timeout=60, timeout_min_duration=5.0, service_logger=service_logger ) breaker.record_failure(is_timeout=is_redis_timeout_failure(RedisTimeoutError("t"))) @@ -1024,6 +1031,7 @@ def test_prometheus_services_logger_starts_from_the_breakers_already_built(monke assert early.is_open() is False monkeypatch.setattr(litellm, "service_callback", ["prometheus_system"]) + gc.collect() ServiceLogging() assert REGISTRY.get_sample_value("litellm_redis_circuit_breaker_state", {"state": "closed"}) == ( redis_breaker_state_counts()["closed"]