Spaces:
Configuration error
Configuration error
File size: 10,591 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
import json
import os
import sys
from typing import Optional
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
# Adds the grandparent directory to sys.path to allow importing project modules
sys.path.insert(0, os.path.abspath("../.."))
from litellm.integrations.SlackAlerting.hanging_request_check import (
AlertingHangingRequestCheck,
)
from litellm.types.integrations.slack_alerting import HangingRequestData
class TestAlertingHangingRequestCheck:
"""Test suite for AlertingHangingRequestCheck class"""
@pytest.fixture
def mock_slack_alerting(self):
"""Create a mock SlackAlerting object for testing"""
mock_slack = MagicMock()
mock_slack.alerting_threshold = 300 # 5 minutes
mock_slack.send_alert = AsyncMock()
return mock_slack
@pytest.fixture
def hanging_request_checker(self, mock_slack_alerting):
"""Create an AlertingHangingRequestCheck instance for testing"""
return AlertingHangingRequestCheck(slack_alerting_object=mock_slack_alerting)
@pytest.mark.asyncio
async def test_init_creates_cache_with_correct_ttl(self, mock_slack_alerting):
"""
Test that initialization creates a hanging request cache with correct TTL.
The TTL should be alerting_threshold + buffer time.
"""
checker = AlertingHangingRequestCheck(slack_alerting_object=mock_slack_alerting)
# The cache should be created with TTL = alerting_threshold + buffer time
expected_ttl = (
mock_slack_alerting.alerting_threshold + 60
) # HANGING_ALERT_BUFFER_TIME_SECONDS
assert checker.hanging_request_cache.default_ttl == expected_ttl
@pytest.mark.asyncio
async def test_add_request_to_hanging_request_check_success(
self, hanging_request_checker
):
"""
Test successfully adding a request to the hanging request cache.
Should extract metadata and store HangingRequestData in cache.
"""
request_data = {
"litellm_call_id": "test_request_123",
"model": "gpt-4",
"deployment": {"litellm_params": {"api_base": "https://api.openai.com/v1"}},
"metadata": {
"user_api_key_alias": "test_key",
"user_api_key_team_alias": "test_team",
},
}
with patch("litellm.get_api_base", return_value="https://api.openai.com/v1"):
await hanging_request_checker.add_request_to_hanging_request_check(
request_data
)
# Verify the request was added to cache
cached_data = (
await hanging_request_checker.hanging_request_cache.async_get_cache(
key="test_request_123"
)
)
assert cached_data is not None
assert isinstance(cached_data, HangingRequestData)
assert cached_data.request_id == "test_request_123"
assert cached_data.model == "gpt-4"
assert cached_data.api_base == "https://api.openai.com/v1"
@pytest.mark.asyncio
async def test_add_request_to_hanging_request_check_none_request_data(
self, hanging_request_checker
):
"""
Test that passing None request_data returns early without error.
Should handle gracefully when no request data is provided.
"""
result = await hanging_request_checker.add_request_to_hanging_request_check(
None
)
assert result is None
@pytest.mark.asyncio
async def test_add_request_to_hanging_request_check_minimal_data(
self, hanging_request_checker
):
"""
Test adding request with minimal required data.
Should handle cases where optional fields are missing.
"""
request_data = {
"litellm_call_id": "minimal_request_456",
"model": "gpt-3.5-turbo",
}
await hanging_request_checker.add_request_to_hanging_request_check(request_data)
cached_data = (
await hanging_request_checker.hanging_request_cache.async_get_cache(
key="minimal_request_456"
)
)
assert cached_data is not None
assert cached_data.request_id == "minimal_request_456"
assert cached_data.model == "gpt-3.5-turbo"
assert cached_data.api_base is None
assert cached_data.key_alias == ""
assert cached_data.team_alias == ""
@pytest.mark.asyncio
async def test_send_hanging_request_alert(self, hanging_request_checker):
"""
Test sending a hanging request alert.
Should format the alert message correctly and call slack alerting.
"""
hanging_request_data = HangingRequestData(
request_id="test_hanging_request",
model="gpt-4",
api_base="https://api.openai.com/v1",
key_alias="test_key",
team_alias="test_team",
)
await hanging_request_checker.send_hanging_request_alert(hanging_request_data)
# Verify slack alert was called
hanging_request_checker.slack_alerting_object.send_alert.assert_called_once()
# Check the alert message format
call_args = hanging_request_checker.slack_alerting_object.send_alert.call_args
message = call_args[1]["message"]
assert "Requests are hanging - 300s+ request time" in message
assert "Request Model: `gpt-4`" in message
assert "API Base: `https://api.openai.com/v1`" in message
assert "Key Alias: `test_key`" in message
assert "Team Alias: `test_team`" in message
assert call_args[1]["level"] == "Medium"
@pytest.mark.asyncio
async def test_send_alerts_for_hanging_requests_no_proxy_logging(
self, hanging_request_checker
):
"""
Test send_alerts_for_hanging_requests when proxy_logging_obj.internal_usage_cache is None.
Should return early without processing when internal usage cache is unavailable.
"""
with patch("litellm.proxy.proxy_server.proxy_logging_obj") as mock_proxy:
mock_proxy.internal_usage_cache = None
result = await hanging_request_checker.send_alerts_for_hanging_requests()
assert result is None
@pytest.mark.asyncio
async def test_send_alerts_for_hanging_requests_with_completed_request(
self, hanging_request_checker
):
"""
Test send_alerts_for_hanging_requests when request has completed (not hanging).
Should remove completed requests from cache and not send alerts.
"""
# Add a request to the hanging cache
hanging_data = HangingRequestData(
request_id="completed_request_789",
model="gpt-4",
api_base="https://api.openai.com/v1",
)
await hanging_request_checker.hanging_request_cache.async_set_cache(
key="completed_request_789", value=hanging_data, ttl=300
)
with patch("litellm.proxy.proxy_server.proxy_logging_obj") as mock_proxy:
# Mock internal usage cache to return a request status (meaning request completed)
mock_internal_cache = AsyncMock()
mock_internal_cache.async_get_cache.return_value = {"status": "success"}
mock_proxy.internal_usage_cache = mock_internal_cache
# Mock the cache method to return our test request
hanging_request_checker.hanging_request_cache.async_get_oldest_n_keys = (
AsyncMock(return_value=["completed_request_789"])
)
await hanging_request_checker.send_alerts_for_hanging_requests()
# Verify no alert was sent since request completed
hanging_request_checker.slack_alerting_object.send_alert.assert_not_called()
@pytest.mark.asyncio
async def test_send_alerts_for_hanging_requests_with_actual_hanging_request(
self, hanging_request_checker
):
"""
Test send_alerts_for_hanging_requests when request is actually hanging.
Should send alert for requests that haven't completed within threshold.
"""
# Add a hanging request to the cache
hanging_data = HangingRequestData(
request_id="hanging_request_999",
model="gpt-4",
api_base="https://api.openai.com/v1",
key_alias="test_key",
team_alias="test_team",
)
await hanging_request_checker.hanging_request_cache.async_set_cache(
key="hanging_request_999", value=hanging_data, ttl=300
)
with patch("litellm.proxy.proxy_server.proxy_logging_obj") as mock_proxy:
# Mock internal usage cache to return None (meaning request is still hanging)
mock_internal_cache = AsyncMock()
mock_internal_cache.async_get_cache.return_value = None
mock_proxy.internal_usage_cache = mock_internal_cache
# Mock the cache method to return our test request
hanging_request_checker.hanging_request_cache.async_get_oldest_n_keys = (
AsyncMock(return_value=["hanging_request_999"])
)
await hanging_request_checker.send_alerts_for_hanging_requests()
# Verify alert was sent for hanging request
hanging_request_checker.slack_alerting_object.send_alert.assert_called_once()
@pytest.mark.asyncio
async def test_send_alerts_for_hanging_requests_with_missing_hanging_data(
self, hanging_request_checker
):
"""
Test send_alerts_for_hanging_requests when hanging request data is missing from cache.
Should continue processing other requests when individual request data is missing.
"""
with patch("litellm.proxy.proxy_server.proxy_logging_obj") as mock_proxy:
mock_internal_cache = AsyncMock()
mock_proxy.internal_usage_cache = mock_internal_cache
# Mock cache to return request ID but no data (simulating expired or missing data)
hanging_request_checker.hanging_request_cache.async_get_oldest_n_keys = (
AsyncMock(return_value=["missing_request_111"])
)
hanging_request_checker.hanging_request_cache.async_get_cache = AsyncMock(
return_value=None
)
await hanging_request_checker.send_alerts_for_hanging_requests()
# Should not crash and should not send any alerts
hanging_request_checker.slack_alerting_object.send_alert.assert_not_called()
|