litellm/tests/unit/llms/sail/conftest.py
devin-ai-integration[bot] 784a52de79
test(router_strategy): keep foreign asyncio task noise out of the budget limiter caplog asserts (#43375)
* fix(tests): keep foreign asyncio task noise out of the budget limiter caplog asserts

The two gc.collect()-under-caplog tests in test_budget_limiter_hotpath.py asserted
on every captured record, so a LoggingWorker task another test had parked on the
session fixture loop got destroyed inside their window and asyncio's "Task was
destroyed but it is pending!" error failed them. They now assert only the limiter
logger's records and park a pending task on a closed loop in the window themselves,
so the noise is exercised on every run

The edenai and sail spend_capture fixtures no longer start the worker from the
session loop: the test's own call and its flush bind the worker to the test loop

* test(router_strategy): drop only asyncio records in the limiter caplog asserts and drain the response cache worker on the test loop

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-29 06:22:06 -07:00

38 lines
1.1 KiB
Python

import uuid
from collections.abc import Iterator
from typing import Final
import httpx
import pytest
import respx
import litellm
from tests.unit.llms.sail.helpers import SAIL_API_BASE, SpendCapture, chat_completion_body
@pytest.fixture
def sail_env(local_model_cost_map: None, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
monkeypatch.setenv("SAIL_API_KEY", "sail-test-key")
monkeypatch.delenv("SAIL_API_BASE", raising=False)
monkeypatch.setattr(
litellm,
"disable_aiohttp_transport",
True,
)
litellm.in_memory_llm_clients_cache.flush_cache()
yield
litellm.in_memory_llm_clients_cache.flush_cache()
@pytest.fixture
def spend_capture(monkeypatch: pytest.MonkeyPatch) -> SpendCapture:
capture: Final = SpendCapture(call_id=f"sail-{uuid.uuid4()}")
monkeypatch.setattr(litellm, "callbacks", [capture])
return capture
@pytest.fixture
def chat_route(respx_mock: respx.MockRouter) -> respx.Route:
return respx_mock.post(f"{SAIL_API_BASE}/chat/completions").mock(
return_value=httpx.Response(200, json=chat_completion_body())
)