mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-18 06:26:16 +00:00
Fix/background health check (#10887)
* fix: improve health check logic by deep copying model list on each iteration * test: add async test for background health check reflecting model list changes * fix: validate health check interval before executing background health check * fix: specify type for health check results dictionary
This commit is contained in:
@@ -2194,6 +2194,50 @@ async def test_get_ui_settings_spend_logs_threshold():
|
||||
proxy_state.set_proxy_state_variable("spend_logs_row_count", 0)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_background_health_check_reflects_llm_model_list(monkeypatch):
|
||||
"""
|
||||
Test that _run_background_health_check reflects changes to llm_model_list in each health check iteration.
|
||||
"""
|
||||
import litellm.proxy.proxy_server as proxy_server
|
||||
import copy
|
||||
|
||||
test_model_list_1 = [{"model_name": "model-a"}]
|
||||
test_model_list_2 = [{"model_name": "model-b"}]
|
||||
called_model_lists = []
|
||||
|
||||
async def fake_perform_health_check(model_list, details):
|
||||
called_model_lists.append(copy.deepcopy(model_list))
|
||||
return (["healthy"], ["unhealthy"])
|
||||
|
||||
monkeypatch.setattr(proxy_server, "health_check_interval", 1)
|
||||
monkeypatch.setattr(proxy_server, "health_check_details", None)
|
||||
monkeypatch.setattr(proxy_server, "llm_model_list", copy.deepcopy(test_model_list_1))
|
||||
monkeypatch.setattr(proxy_server, "perform_health_check", fake_perform_health_check)
|
||||
monkeypatch.setattr(proxy_server, "health_check_results", {})
|
||||
|
||||
async def fake_sleep(interval):
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
monkeypatch.setattr(asyncio, "sleep", fake_sleep)
|
||||
|
||||
try:
|
||||
await proxy_server._run_background_health_check()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
monkeypatch.setattr(proxy_server, "llm_model_list", copy.deepcopy(test_model_list_2))
|
||||
|
||||
try:
|
||||
await proxy_server._run_background_health_check()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
assert len(called_model_lists) >= 2
|
||||
assert called_model_lists[0] == test_model_list_1
|
||||
assert called_model_lists[1] == test_model_list_2
|
||||
|
||||
|
||||
def test_get_timeout_from_request():
|
||||
from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup
|
||||
|
||||
|
||||
Reference in New Issue
Block a user