litellm/tests/unit/proxy/hooks/test_dynamic_rate_limiter.py
devin-ai-integration[bot] 39e31958f8
test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy (#43998)
* test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): stub HIBP through respx by disabling the aiohttp transport

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): share the httpx transport fixture across proxy unit tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): restore proxy globals without a missing-value sentinel

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): package moved dirs and stub the login breach check at the HTTP boundary

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(proxy): isolate the mcp server manager per test

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-01 10:11:45 -07:00

44 lines
1.6 KiB
Python

from datetime import datetime, timezone
import pytest
from litellm.caching.caching import DualCache
from litellm.proxy.hooks.dynamic_rate_limiter import (
DynamicRateLimiterCache,
_PROXY_DynamicRateLimitHandler,
)
@pytest.mark.asyncio
async def test_sadd_and_get_share_injected_clock_window():
dual_cache = DualCache()
cache = DynamicRateLimiterCache(
cache=dual_cache,
time_fn=lambda: datetime(2024, 1, 1, 10, 30, 0, tzinfo=timezone.utc),
)
await cache.async_set_cache_sadd(model="my-fake-model", value=["p1", "p2", "p3"])
assert await cache.async_get_cache(model="my-fake-model") == 3
assert await dual_cache.async_get_cache(key="10-30:my-fake-model") is not None
@pytest.mark.asyncio
async def test_minute_rollover_between_sadd_and_get_reads_empty_window():
ticks = iter(
(
datetime(2024, 1, 1, 10, 30, 59, 999999, tzinfo=timezone.utc),
datetime(2024, 1, 1, 10, 31, 0, 0, tzinfo=timezone.utc),
)
)
cache = DynamicRateLimiterCache(cache=DualCache(), time_fn=lambda: next(ticks))
await cache.async_set_cache_sadd(model="my-fake-model", value=["p1"])
assert await cache.async_get_cache(model="my-fake-model") is None
@pytest.mark.asyncio
async def test_handler_threads_time_fn_to_internal_cache():
handler = _PROXY_DynamicRateLimitHandler(
internal_usage_cache=DualCache(),
time_fn=lambda: datetime(2024, 1, 1, 10, 30, 0, tzinfo=timezone.utc),
)
await handler.internal_usage_cache.async_set_cache_sadd(model="my-fake-model", value=["p1", "p2"])
assert await handler.internal_usage_cache.async_get_cache(model="my-fake-model") == 2