litellm/tests/unit/proxy/auth/test_fallback_rate_limit.py

58 lines
2.5 KiB
Python

from typing import Final
import pytest
from litellm import Router
from litellm.caching.dual_cache import DualCache
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.auth import fallback_rate_limit
from litellm.proxy.auth.fallback_rate_limit import RouterFallbackRateLimitCheck
from litellm.proxy.hooks.parallel_request_limiter_v3 import PROXY_MaxParallelRequestsHandler_v3
from litellm.proxy.utils import InternalUsageCache
from litellm.router_api import RoutingContext
PRIMARY: Final = {"model_name": "primary", "litellm_params": {"model": "openai/gpt-4o", "api_key": "k"}}
FALLBACK: Final = {"model_name": "fallback", "litellm_params": {"model": "openai/gpt-4o", "api_key": "k"}}
def _router() -> Router:
return Router(model_list=[PRIMARY, FALLBACK], fallbacks=[{"primary": ["fallback"]}])
def _limiter() -> PROXY_MaxParallelRequestsHandler_v3:
return PROXY_MaxParallelRequestsHandler_v3(internal_usage_cache=InternalUsageCache(DualCache()))
def _request(token: UserAPIKeyAuth) -> dict[str, object]:
return {
"model": "primary",
"messages": [{"role": "user", "content": "hi"}],
"metadata": {"user_api_key_auth": token},
}
@pytest.mark.asyncio
async def test_request_without_a_key_is_unrestricted() -> None:
check: Final = RouterFallbackRateLimitCheck(limiter=_limiter)
assert await check(model="fallback", request_kwargs={"model": "primary"}, ctx=RoutingContext(_router())) is True
@pytest.mark.asyncio
async def test_proxy_without_the_v3_limiter_is_unrestricted() -> None:
check: Final = RouterFallbackRateLimitCheck(limiter=lambda: None)
token: Final = UserAPIKeyAuth(api_key="hashed", token="hashed", metadata={"model_rpm_limit": {"fallback": 0}})
assert await check(model="fallback", request_kwargs=_request(token), ctx=RoutingContext(_router())) is True
@pytest.mark.asyncio
async def test_a_failing_limiter_fails_closed(monkeypatch: pytest.MonkeyPatch) -> None:
async def broken_check(
limiter: PROXY_MaxParallelRequestsHandler_v3, user_api_key_dict: object, model: str, request_data: object
) -> bool:
raise RuntimeError("redis down")
monkeypatch.setattr(fallback_rate_limit, "fallback_target_allowed", broken_check)
limiter: Final = _limiter()
check: Final = RouterFallbackRateLimitCheck(limiter=lambda: limiter)
token: Final = UserAPIKeyAuth(api_key="hashed", token="hashed", metadata={"model_rpm_limit": {"fallback": 5}})
assert await check(model="fallback", request_kwargs=_request(token), ctx=RoutingContext(_router())) is False