|
| 1 | +from typing import Any, Dict |
| 2 | +from unittest.mock import AsyncMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from vllm_router.routers.routing_logic import ( |
| 7 | + PrefixAwareRouter, |
| 8 | + cleanup_routing_logic, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def cleanup_router(): |
| 14 | + cleanup_routing_logic() |
| 15 | + yield |
| 16 | + cleanup_routing_logic() |
| 17 | + |
| 18 | + |
| 19 | +class EndpointInfo: |
| 20 | + def __init__(self, url: str): |
| 21 | + self.url = url |
| 22 | + |
| 23 | + |
| 24 | +class RequestStats: |
| 25 | + def __init__(self, qps: float): |
| 26 | + self.qps = qps |
| 27 | + |
| 28 | + |
| 29 | +class Request: |
| 30 | + def __init__(self, headers: Dict[str, str], body: Dict[str, Any] = None): |
| 31 | + self.headers = headers |
| 32 | + self.body = body |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_route_falls_back_to_qps_when_match_below_threshold(): |
| 37 | + """ |
| 38 | + When the longest prefix match is shorter than prefix_min_match_length, |
| 39 | + the request should NOT use the matched endpoint. It should fall back to |
| 40 | + QPS-based routing and pick the engine with the lowest QPS. |
| 41 | + """ |
| 42 | + |
| 43 | + endpoints = [ |
| 44 | + EndpointInfo(url="http://engine1.com"), |
| 45 | + EndpointInfo(url="http://engine2.com"), |
| 46 | + ] |
| 47 | + request_stats = { |
| 48 | + "http://engine1.com": RequestStats(qps=10), |
| 49 | + "http://engine2.com": RequestStats(qps=5), |
| 50 | + } |
| 51 | + request = Request(headers={}) |
| 52 | + request_json = {"prompt": "some prompt text"} |
| 53 | + |
| 54 | + router = PrefixAwareRouter(prefix_min_match_length=4096) |
| 55 | + |
| 56 | + fake_hashtrie = AsyncMock() |
| 57 | + fake_hashtrie.longest_prefix_match.return_value = ( |
| 58 | + 0, |
| 59 | + {"http://engine1.com"}, |
| 60 | + ) |
| 61 | + router.hashtrie = fake_hashtrie |
| 62 | + |
| 63 | + url = await router.route_request( |
| 64 | + endpoints, None, request_stats, request, request_json |
| 65 | + ) |
| 66 | + |
| 67 | + assert url == "http://engine2.com" |
| 68 | + |
| 69 | + fake_hashtrie.insert.assert_not_awaited() |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.asyncio |
| 73 | +async def test_route_uses_matched_endpoint_when_match_above_threshold(): |
| 74 | + """ |
| 75 | + When the longest prefix match is no shorter than prefix_min_match_length, |
| 76 | + the request should use the matched endpoint. |
| 77 | + """ |
| 78 | + |
| 79 | + endpoints = [ |
| 80 | + EndpointInfo(url="http://engine1.com"), |
| 81 | + EndpointInfo(url="http://engine2.com"), |
| 82 | + ] |
| 83 | + request_stats = {} |
| 84 | + request = Request(headers={}) |
| 85 | + request_json = {"prompt": "some prompt text"} |
| 86 | + |
| 87 | + router = PrefixAwareRouter(prefix_min_match_length=128) |
| 88 | + |
| 89 | + fake_hashtrie = AsyncMock() |
| 90 | + fake_hashtrie.longest_prefix_match.return_value = ( |
| 91 | + 4096, |
| 92 | + {"http://engine1.com"}, |
| 93 | + ) |
| 94 | + router.hashtrie = fake_hashtrie |
| 95 | + |
| 96 | + url = await router.route_request( |
| 97 | + endpoints, None, request_stats, request, request_json |
| 98 | + ) |
| 99 | + |
| 100 | + assert url == "http://engine1.com" |
| 101 | + |
| 102 | + fake_hashtrie.insert.assert_awaited_once() |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_default_threshold_zero_preserves_original_behavior(): |
| 107 | + """ |
| 108 | + When --prefix-min-match-length is not provided, prefix_min_match_length |
| 109 | + defaults to 0. Even when there is no prefix match at all (match_length 0), |
| 110 | + the request still uses the matched endpoint instead of falling back, |
| 111 | + preserving the original behavior before this change. |
| 112 | + """ |
| 113 | + |
| 114 | + endpoints = [ |
| 115 | + EndpointInfo(url="http://engine1.com"), |
| 116 | + EndpointInfo(url="http://engine2.com"), |
| 117 | + ] |
| 118 | + request_stats = {} |
| 119 | + request = Request(headers={}) |
| 120 | + request_json = {"prompt": "some prompt text"} |
| 121 | + |
| 122 | + router = PrefixAwareRouter() |
| 123 | + |
| 124 | + fake_hashtrie = AsyncMock() |
| 125 | + fake_hashtrie.longest_prefix_match.return_value = ( |
| 126 | + 0, |
| 127 | + {"http://engine1.com"}, |
| 128 | + ) |
| 129 | + router.hashtrie = fake_hashtrie |
| 130 | + |
| 131 | + url = await router.route_request( |
| 132 | + endpoints, None, request_stats, request, request_json |
| 133 | + ) |
| 134 | + |
| 135 | + assert url == "http://engine1.com" |
| 136 | + |
| 137 | + fake_hashtrie.insert.assert_awaited_once() |
0 commit comments