litellm/tests/unit/caching/test_redis_batch.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

30 lines
1.1 KiB
Python

"""The request scope: one set of native batches per request, and nested scopes join it."""
from __future__ import annotations
import gc
import weakref
from typing import Final
from litellm.caching.redis_batch import RequestRedisBatches, active_request_redis_batches, request_redis_batch_scope
def test_nested_request_scopes_share_the_outer_request_and_close_with_it() -> None:
assert active_request_redis_batches() is None
with request_redis_batch_scope() as outer:
assert active_request_redis_batches() is outer
with request_redis_batch_scope() as inner:
assert inner is outer
assert active_request_redis_batches() is outer
assert active_request_redis_batches() is None
def test_a_request_scope_that_built_its_native_batches_is_freed_once_it_closes() -> None:
def serve_request() -> weakref.ref[RequestRedisBatches]:
with request_redis_batch_scope() as batches:
batches.native()
return weakref.ref(batches)
closed: Final = serve_request()
gc.collect()
assert closed() is None