|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Exercise benchmark client readiness against real HTTP rejection and retry responses.""" |
| 16 | + |
| 17 | +import importlib.util |
| 18 | +import json |
| 19 | +import threading |
| 20 | +import urllib.error |
| 21 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 22 | +from pathlib import Path |
| 23 | +from types import ModuleType |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +pytestmark = pytest.mark.cpu_only |
| 28 | + |
| 29 | +CLIENT_PATH = Path(__file__).resolve().parents[3] / "tests/integration/defs/perf/agentx_client.py" |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def client(monkeypatch: pytest.MonkeyPatch) -> ModuleType: |
| 34 | + spec = importlib.util.spec_from_file_location("perf_benchmark_client", CLIENT_PATH) |
| 35 | + assert spec is not None and spec.loader is not None |
| 36 | + module = importlib.util.module_from_spec(spec) |
| 37 | + spec.loader.exec_module(module) |
| 38 | + monkeypatch.setenv("AGENTX_READY_TIMEOUT", "60") |
| 39 | + monkeypatch.setattr(module.time, "sleep", lambda _: None) |
| 40 | + return module |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def endpoint(): |
| 45 | + responses = [] |
| 46 | + requests = [] |
| 47 | + |
| 48 | + class Handler(BaseHTTPRequestHandler): |
| 49 | + def do_POST(self) -> None: |
| 50 | + requests.append( |
| 51 | + (self.path, json.loads(self.rfile.read(int(self.headers["Content-Length"])))) |
| 52 | + ) |
| 53 | + status, body = responses.pop(0) if responses else (503, "not ready") |
| 54 | + self.send_response(status) |
| 55 | + self.end_headers() |
| 56 | + self.wfile.write(body.encode()) |
| 57 | + |
| 58 | + def log_message(self, *args: object) -> None: |
| 59 | + pass |
| 60 | + |
| 61 | + server = HTTPServer(("127.0.0.1", 0), Handler) |
| 62 | + thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 63 | + thread.start() |
| 64 | + try: |
| 65 | + yield f"http://127.0.0.1:{server.server_port}", responses, requests |
| 66 | + finally: |
| 67 | + server.shutdown() |
| 68 | + server.server_close() |
| 69 | + thread.join(timeout=5) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("status", [400, 401, 403, 404, 422]) |
| 73 | +def test_rejection_preserves_server_reason(client: ModuleType, endpoint, status: int) -> None: |
| 74 | + url, responses, requests = endpoint |
| 75 | + responses.append((status, '{"error":{"message":"unsupported chat template"}}')) |
| 76 | + with pytest.raises(RuntimeError, match=f"HTTP {status}.*unsupported chat template"): |
| 77 | + client.verify_endpoint(url, "model") |
| 78 | + assert len(requests) == 1 |
| 79 | + assert requests[0][0] == "/v1/chat/completions" |
| 80 | + assert requests[0][1]["model"] == "model" |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("status", [408, 429, 500, 503]) |
| 84 | +def test_transient_http_failure_still_retries(client: ModuleType, endpoint, status: int) -> None: |
| 85 | + url, responses, requests = endpoint |
| 86 | + responses.extend([(status, "temporarily unavailable"), (200, '{"choices":[{}]}')]) |
| 87 | + client.verify_endpoint(url, "model") |
| 88 | + assert len(requests) == 2 |
| 89 | + |
| 90 | + |
| 91 | +def test_heartbeat_request_remains_best_effort(client: ModuleType, endpoint) -> None: |
| 92 | + url, responses, requests = endpoint |
| 93 | + responses.append((400, "invalid request")) |
| 94 | + assert client.post_one_token(url, "model", timeout=1) is None |
| 95 | + assert len(requests) == 1 |
| 96 | + |
| 97 | + |
| 98 | +def test_transport_failure_remains_retryable(client: ModuleType, monkeypatch) -> None: |
| 99 | + def unavailable(*args: object, **kwargs: object) -> None: |
| 100 | + raise urllib.error.URLError("connection refused") |
| 101 | + |
| 102 | + monkeypatch.setattr(client.urllib.request, "urlopen", unavailable) |
| 103 | + assert client.post_one_token("http://unused", "model", timeout=1) is None |
| 104 | + |
| 105 | + |
| 106 | +def test_rejection_body_is_bounded(client: ModuleType, endpoint) -> None: |
| 107 | + url, responses, _ = endpoint |
| 108 | + responses.append((400, "x" * 10000)) |
| 109 | + with pytest.raises(RuntimeError, match="HTTP 400") as error: |
| 110 | + client.verify_endpoint(url, "model") |
| 111 | + assert len(str(error.value)) < 2300 |
0 commit comments