litellm/tests/unit/caching/redis_batch_double.py
yujonglee 4616174c45
refactor(redis): move per-request miss, settlement and shutdown-drain tracking from Python into Rust (#51384)
Rust already sent each request's Redis batches, but Python kept the rules about them. Three pieces of per-request
state now live on the Rust RequestRedis: the keys the request's MGET found absent, the flag saying its committed
spend increment has its reply, and the registry of requests whose post-call writes are still unsent at shutdown.
Python only passes the request to the store call, and the Python test doubles no longer re-implement those rules

Auth write-backs and prefetch reads always ride the request's pre-call batch, so a write inside a request cannot
leave its key marked absent. One post-call flush runs at a time, and the shutdown drain waits for it
2026-11-20 17:57:37 +09:00

107 lines
4.6 KiB
Python

"""Test doubles for the request's native Redis batches and for a ``RedisCache`` that never reaches a server."""
from __future__ import annotations
import uuid
from collections.abc import Callable, Sequence
from dataclasses import dataclass, field
from typing import Any, Final, Generic, NoReturn, TypeVar
from litellm._service_logger import ServiceLogging
from litellm.caching.redis_cache import RedisCache, RedisHealth
from litellm.redis.store_settings import RedisBreakerSettings, RedisStoreSettings
from litellm.rust_bridge._native import NativeRedisBreaker
_Answer = TypeVar("_Answer")
class FakeRedisCache(RedisCache):
"""A ``RedisCache`` the native stores connect to by its settings; Python itself must never open a client on it."""
def __init__(self, namespace: str | None = None, failure_threshold: int = 5) -> None: # super().__init__ needs a server
self.namespace = namespace
self.redis_kwargs = {"host": f"batch-{uuid.uuid4().hex}.invalid", "port": 1, "socket_timeout": 0.2}
self.service_logger_obj = ServiceLogging()
breaker: Final = RedisBreakerSettings(
failure_threshold=failure_threshold, recovery_timeout=30.0, timeout_min_duration=5.0
)
self._health = RedisHealth( # pyright: ignore[reportGeneralTypeIssues] # the fake skips RedisCache.__init__, which sets it
NativeRedisBreaker.shared(self.redis_kwargs, breaker), self.service_logger_obj
)
self.store_settings = RedisStoreSettings(
redis_kwargs=self.redis_kwargs, namespace=namespace, default_ttl=60, breaker=breaker
)
self.default_ttl = None
self.alone: list[tuple[str, Any]] = []
self.store: dict[str, Any] = {}
def init_async_client(self) -> NoReturn: # pyright: ignore[reportIncompatibleMethodOverride] # no server behind the fake
raise AssertionError("a Python Redis client was opened; Redis traffic goes through the native stores")
async def async_batch_get_cache(self, key_list: Sequence[str], **kwargs: object) -> dict[str, Any]: # pyright: ignore[reportIncompatibleMethodOverride] # records the direct read
self.alone.append(("MGET", tuple(key_list)))
return {key: self.store.get(key) for key in key_list}
async def async_increment(self, key: str, value: float, ttl: int | None = None, **kwargs: object) -> float: # pyright: ignore[reportIncompatibleMethodOverride] # records the direct write
self.alone.append(("INCRBYFLOAT", key, value))
self.store[key] = float(self.store.get(key, 0.0)) + value
return self.store[key]
@dataclass
class FakeNativeRequest:
"""Stands in for ``NativeRequestRedis``: records each flush in ``events`` next to what the test's pendings log.
``tracked_writes_settled`` answers what the test sets, since the native request owns that rule."""
post_call_deadline: float
events: list[str] = field(default_factory=list)
tracked_writes_settled: bool = False
async def flush_pre(self) -> None:
self.events.append("flush_pre")
async def flush_post(self) -> None:
self.events.append("flush_post")
@dataclass
class FakeNatives:
built: list[FakeNativeRequest] = field(default_factory=list)
def __call__(self, post_call_deadline: float) -> FakeNativeRequest:
self.built.append(FakeNativeRequest(post_call_deadline))
return self.built[-1]
@dataclass
class FakeNativePending(Generic[_Answer]):
"""A read declared on a ``FakeNativeRequest``; it only answers once that request's pre-call batch was flushed."""
request: FakeNativeRequest
answer: Callable[[], _Answer]
async def resolve(self) -> _Answer:
if "flush_pre" not in self.request.events:
raise ConnectionError("resolved before its batch was flushed")
self.request.events.append("resolve")
return self.answer()
@dataclass
class ReadyPending(Generic[_Answer]):
"""An op declared with ``NativeSend.Now()``: it answers whenever it is resolved."""
answer: Callable[[], _Answer]
async def resolve(self) -> _Answer:
return self.answer()
def fake_send(_phase: object, request: FakeNativeRequest) -> FakeNativeRequest:
"""A ``native_send_factory`` for ``FakeNatives``: the fake request stands in for its ``NativeSend``."""
return request
def fake_pending(send: object, answer: Callable[[], _Answer]) -> FakeNativePending[_Answer] | ReadyPending[_Answer]:
"""What a fake handle declares for ``send``: on the fake request's batch, or on its own for ``Now()``."""
return FakeNativePending(send, answer) if isinstance(send, FakeNativeRequest) else ReadyPending(answer)