mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* test(integration): hand every harness server a port from one per-worker block
About 45 places in the integration harness picked a port by binding port 0,
reading the number back, closing the socket and starting a server on that
number later. Another process could take the port in between, which surfaced
as "Address already in use" flakes. Earlier fixes retried around the race in
single call sites without removing it.
tests/integration/_support/ports.py now owns every port the harness picks
ahead of a server. Each pytest process claims a 200-port block between 10000
and 32767 by taking an exclusive flock on a per-block lock file in the system
temp directory, so parallel runs on one machine never share a block and the
lock goes away with the process. The range is above the fixed service ports
and below the Linux and macOS ephemeral ranges. Ports go out in order, are
never reused in a run, skip anything an outside process already holds, and
a process that exhausts its block claims the next free one.
The three _free_port copies, worker_endpoints' port-0 binds,
free_loopback_port, the owned proxy's lost-port-race retry and the owned
Redis retry loop are gone. Tests that restart a server on the same port now
allocate that port up front instead of reading it back from the first
server. TQ012 in scripts/check_test_quality.py rejects a port-0 bind read
back with getsockname anywhere under tests/integration.
* ci(integration): record listening sockets and the ephemeral port range
* Revert "ci(integration): record listening sockets and the ephemeral port range"
This reverts commit d3fafc3da2.
102 lines
3.7 KiB
Python
102 lines
3.7 KiB
Python
import asyncio
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
from typing import Final
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from tests._master_key import MASTER_KEY
|
|
from tests.integration._support.process import stop_child
|
|
from tests.integration._support.ports import allocate_port
|
|
|
|
MAX_IN_FLIGHT: Final = 8
|
|
MAX_QUEUED: Final = 8
|
|
REQUESTS: Final = 200
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_granian_admission_control_saturation(tmp_path: Path) -> None:
|
|
fake_port: Final = allocate_port()
|
|
proxy_port: Final = allocate_port()
|
|
fake_script: Final = Path(__file__).parents[2] / "_fake_openai_endpoint_server.py"
|
|
config_path: Final = tmp_path / "saturation_config.yaml"
|
|
proxy_env: Final = {**{k: v for k, v in os.environ.items() if k != "DATABASE_URL"}, "LITELLM_MASTER_KEY": MASTER_KEY}
|
|
config_path.write_text(
|
|
f"""model_list:
|
|
- model_name: slow-endpoint
|
|
litellm_params:
|
|
model: openai/slow-endpoint
|
|
api_base: http://127.0.0.1:{fake_port}/v1
|
|
api_key: fake-upstream-key
|
|
general_settings:
|
|
master_key: os.environ/LITELLM_MASTER_KEY
|
|
max_in_flight_requests_per_worker: {MAX_IN_FLIGHT}
|
|
max_queued_requests_per_worker: {MAX_QUEUED}
|
|
admission_queue_timeout_seconds: 0.5
|
|
"""
|
|
)
|
|
fake_process: Final = subprocess.Popen(
|
|
[sys.executable, str(fake_script), "--host", "127.0.0.1", "--port", str(fake_port)],
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
try:
|
|
proxy_process: Final = subprocess.Popen(
|
|
[
|
|
sys.executable,
|
|
"-m",
|
|
"litellm.proxy.proxy_cli",
|
|
"--config",
|
|
str(config_path),
|
|
"--run_granian",
|
|
"--num_workers",
|
|
"1",
|
|
"--port",
|
|
str(proxy_port),
|
|
],
|
|
env=proxy_env,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
try:
|
|
async with httpx.AsyncClient(base_url=f"http://127.0.0.1:{proxy_port}") as client:
|
|
deadline: Final = time.monotonic() + 60
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
response: Final = await client.get("/health/liveliness", timeout=2)
|
|
if response.status_code == 200:
|
|
break
|
|
except httpx.HTTPError:
|
|
pass
|
|
await asyncio.sleep(0.25)
|
|
else:
|
|
raise AssertionError("Granian proxy did not become healthy")
|
|
|
|
async def send_completion() -> tuple[int, bool]:
|
|
response = await client.post(
|
|
"/chat/completions",
|
|
headers={"Authorization": f"Bearer {MASTER_KEY}"},
|
|
json={
|
|
"model": "slow-endpoint",
|
|
"messages": [{"role": "user", "content": "hello"}],
|
|
},
|
|
timeout=10,
|
|
)
|
|
return response.status_code, "retry-after" in response.headers
|
|
|
|
results: Final = await asyncio.gather(*(send_completion() for _ in range(REQUESTS)))
|
|
|
|
statuses: Final = [result[0] for result in results]
|
|
rejected: Final = [result for result in results if result[0] == 503]
|
|
assert set(statuses) <= {200, 503}, statuses
|
|
assert 1 <= statuses.count(200) <= MAX_IN_FLIGHT + MAX_QUEUED, statuses
|
|
assert len(rejected) >= REQUESTS - MAX_IN_FLIGHT - MAX_QUEUED, statuses
|
|
assert all(result[1] for result in rejected), rejected
|
|
finally:
|
|
stop_child(proxy_process)
|
|
finally:
|
|
stop_child(fake_process)
|