mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* test: make four flaky unit tests deterministic The skill archive fixtures stamped the current time into zip bytes used as parametrize ids, so xdist workers collected different ids and errored the whole shard. They now use a fixed timestamp. The usage-based v2 override test pinned no clock, so a minute boundary between rpm checks reset the counter. It now pins get_utc_datetime the way #49505 does. The stalled-burst DB lookup test asserted a 2s wall-clock bound. It now asserts every lookup was waiting at once, which fails if lookups are serialized. The long tool_search placement test asserted a 1s wall-clock bound. It keeps the placement assertion and drops the bound, as #49505 did for the json accumulator * test(skills): drop the archive fixture timestamp change now that #50118 removed its test
127 lines
3.9 KiB
Python
127 lines
3.9 KiB
Python
import asyncio
|
|
import time
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm.proxy.db.db_lookup_gate import DBLookupDeadlineExceeded, DBLookupStallTracker, bounded_db_lookup
|
|
|
|
|
|
async def _never_answers() -> None:
|
|
await asyncio.Event().wait()
|
|
|
|
|
|
class _FakeClock:
|
|
def __init__(self) -> None:
|
|
self.now = 1000.0
|
|
|
|
def __call__(self) -> float:
|
|
return self.now
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bounded_db_lookup_fails_a_stalled_lookup_at_the_deadline_and_records_the_hit():
|
|
tracker: Final = DBLookupStallTracker()
|
|
started: Final = time.monotonic()
|
|
|
|
with pytest.raises(DBLookupDeadlineExceeded) as exc_info:
|
|
await bounded_db_lookup(_never_answers(), name="team", deadline_seconds=0.05, tracker=tracker)
|
|
|
|
assert time.monotonic() - started < 2
|
|
assert exc_info.value.lookup == "team"
|
|
assert exc_info.value.deadline_seconds == 0.05
|
|
assert str(exc_info.value) == "team lookup did not answer within 0.05s"
|
|
assert isinstance(exc_info.value, asyncio.TimeoutError)
|
|
assert tracker.stalled_within(30) is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bounded_db_lookup_returns_a_prompt_answer_without_recording_a_stall():
|
|
tracker: Final = DBLookupStallTracker()
|
|
|
|
async def answers() -> str:
|
|
return "row"
|
|
|
|
assert await bounded_db_lookup(answers(), name="key", deadline_seconds=0.05, tracker=tracker) == "row"
|
|
assert tracker.stalled_within(30) is False
|
|
|
|
|
|
class _PendingLookups:
|
|
def __init__(self) -> None:
|
|
self.pending = 0
|
|
self.peak = 0
|
|
|
|
async def never_answers(self) -> None:
|
|
self.pending += 1
|
|
self.peak = max(self.peak, self.pending)
|
|
try:
|
|
await asyncio.Event().wait()
|
|
finally:
|
|
self.pending -= 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bounded_db_lookup_fails_a_whole_stalled_burst_within_one_deadline():
|
|
tracker: Final = DBLookupStallTracker()
|
|
burst: Final = 200
|
|
lookups: Final = _PendingLookups()
|
|
|
|
results: Final = await asyncio.gather(
|
|
*(
|
|
bounded_db_lookup(lookups.never_answers(), name=f"key-{i}", deadline_seconds=0.1, tracker=tracker)
|
|
for i in range(burst)
|
|
),
|
|
return_exceptions=True,
|
|
)
|
|
|
|
assert lookups.peak == burst, (
|
|
f"only {lookups.peak} of {burst} stalled lookups were waiting on their deadline together"
|
|
)
|
|
assert len(results) == burst
|
|
assert all(isinstance(result, DBLookupDeadlineExceeded) for result in results)
|
|
assert tracker.stalled_within(30) is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bounded_db_lookup_fails_at_the_deadline_even_when_the_lookup_absorbs_the_cancel():
|
|
tracker: Final = DBLookupStallTracker()
|
|
absorbed: Final = asyncio.Event()
|
|
let_go: Final = asyncio.Event()
|
|
|
|
async def absorbs_the_cancel() -> str:
|
|
try:
|
|
await asyncio.Event().wait()
|
|
except asyncio.CancelledError:
|
|
absorbed.set()
|
|
await let_go.wait()
|
|
return "late row"
|
|
|
|
started: Final = time.monotonic()
|
|
with pytest.raises(DBLookupDeadlineExceeded):
|
|
await asyncio.wait_for(
|
|
bounded_db_lookup(absorbs_the_cancel(), name="key", deadline_seconds=0.05, tracker=tracker),
|
|
timeout=2,
|
|
)
|
|
|
|
assert time.monotonic() - started < 1
|
|
assert tracker.stalled_within(30) is True
|
|
await asyncio.wait_for(absorbed.wait(), timeout=1)
|
|
let_go.set()
|
|
await asyncio.sleep(0)
|
|
|
|
|
|
def test_stall_tracker_reports_a_stall_only_inside_the_window():
|
|
clock: Final = _FakeClock()
|
|
tracker: Final = DBLookupStallTracker(clock=clock)
|
|
|
|
assert tracker.stalled_within(30) is False
|
|
tracker.record_hit()
|
|
assert tracker.stalled_within(30) is True
|
|
assert tracker.stalled_within(0) is False
|
|
clock.now += 29.9
|
|
assert tracker.stalled_within(30) is True
|
|
clock.now += 0.2
|
|
assert tracker.stalled_within(30) is False
|
|
tracker.record_hit()
|
|
tracker.clear()
|
|
assert tracker.stalled_within(30) is False
|