litellm/tests/unit/redis/stores/test_active_teams.py
yujonglee be7859a9c0
feat(redis): serve the dynamic rate limiter's active teams from a native store (#52995)
* feat(redis): serve the dynamic rate limiter's active teams from a native store

The registry of callers active on a model, which dynamic_rate_limiter_v3 splits a priority
class's share among, ran its own Lua through redis-py. It now goes through a redis-store
family like the limiter's counters, so the Rust gateway can use the same registry.

The store sends the script main sends, byte for byte, and keeps the memory fallback. A Redis
failure from the store is treated as the registry being unavailable, as a redis-py error was

* test(redis): cover a store outage with in-process doubles, not a refused socket

The unit suite runs without network access, so the two outage tests no longer bind a port and
dial it. A failing handle and a failing registry raise what the Rust store raises instead. The
real refused connection stays covered by the redis-store test against real Redis.

The store's error class is imported where it is checked, since the type gate cannot resolve the
extension module from source and counted an unrelated unknown type with the import at the top
2026-11-20 10:58:19 +09:00

63 lines
2.2 KiB
Python

from collections.abc import Sequence
from dataclasses import dataclass
from typing import Final
import pytest
from litellm.proxy.hooks.priority_class_share import MEMBER_SEPARATOR, team_member
from litellm.redis.stores.active_teams import ActiveTeamStore
from litellm.redis.stores.active_teams.handle import ActiveMemberRecord
from litellm.rust_bridge._native import NativeRedisStoreError
@dataclass(frozen=True, slots=True)
class _WireTouch:
"""One touch as main's ``ActiveTeamRegistry`` sent it through redis-py: the sorted set member and the script's
score arguments, which redis-py encodes with ``repr``. The same literals sit in
``litellm/rust/crates/redis-store/tests/active_teams.rs``, where real Redis is checked to hold them after the
Rust store writes."""
team: str
member: str
now: float
window: int
cutoff_arg: str
now_arg: str
_WIRE_TOUCH: Final = _WireTouch(
team="team\x1ft1",
member="team\x1ft1\x1finteractive",
now=1000.0,
window=60,
cutoff_arg="940.0",
now_arg="1000.0",
)
def test_the_wire_case_is_what_main_sends() -> None:
assert team_member("t1") == _WIRE_TOUCH.team
assert f"{_WIRE_TOUCH.team}{MEMBER_SEPARATOR}interactive" == _WIRE_TOUCH.member
assert repr(_WIRE_TOUCH.now - _WIRE_TOUCH.window) == _WIRE_TOUCH.cutoff_arg
assert repr(_WIRE_TOUCH.now) == _WIRE_TOUCH.now_arg
class _DownRedis:
"""A handle on a Redis that refuses every call, raising what the Rust store raises for one."""
has_redis: Final = True
async def touch(
self, model: str, member: str, priority_class: str, window_seconds: int
) -> Sequence[ActiveMemberRecord]:
raise NativeRedisStoreError("transport", "redis is down")
def touch_local(
self, model: str, member: str, priority_class: str, window_seconds: int
) -> Sequence[ActiveMemberRecord]:
raise AssertionError("memory answered for a store that has Redis")
async def test_a_touch_fails_while_redis_is_down_and_never_answers_from_memory() -> None:
with pytest.raises(NativeRedisStoreError):
await ActiveTeamStore(_DownRedis()).touch("chat", _WIRE_TOUCH.team, "interactive", _WIRE_TOUCH.window)