Files
2026Technology-Competition/tests/test_inference_client.py
T
lhl 8239a37a99 feat(inference): LLM 客户端全异步化(T8 架构审查整改)
- Issue9: client.py 由同步 httpx.Client 全异步化
  - LLMClient Protocol / HttpLLMClient.chat → async;httpx.AsyncClient + asyncio.sleep 退避
  - __enter__/__exit__ → __aenter__/__aexit__(async with 生命周期闭环)
- engine.py chat/chat_structured/_call 全部 async + await
- FakeLLMClient.chat → async;测试用 anyio pytest 插件转换(engine 32 + client 10 用例)
- 同步 inference-engine spec 与 milestone3 review 的 httpx 描述
- 全量 182 passed / 100.00%(987 stmts/252 br)
2026-08-12 09:50:37 +08:00

169 lines
5.2 KiB
Python

import pytest
import httpx
from genesis.inference.client import HttpLLMClient, LLMClient
from genesis.inference.exceptions import (
LLMNetworkError,
LLMNotConfiguredError,
LLMResponseError,
LLMTimeoutError,
)
from genesis.inference.types import ChatMessage
def make_client(handler, *, api_key="sk-test", retry_backoff=(0.0, 0.0)):
return HttpLLMClient(
base_url="https://api.test.local",
api_key=api_key,
timeout_sec=0.1,
retry_backoff=retry_backoff,
transport=httpx.MockTransport(handler),
)
def _ok_handler(request):
return httpx.Response(200, json={
"choices": [{"message": {"content": "Hello"}}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5},
})
def test_client_implements_protocol():
# 结构性断言:HttpLLMClient.chat 的关键字参数签名与 LLMClient Protocol 一致
import inspect
proto_params = set(inspect.signature(LLMClient.chat).parameters)
impl_params = set(inspect.signature(HttpLLMClient.chat).parameters)
assert proto_params.issubset(impl_params)
# ---------- T8: 全异步化(async 接口) ----------
@pytest.mark.anyio
async def test_chat_success_async():
"""chat 为 async 接口,返回文本与用量(T8)。"""
client = make_client(_ok_handler)
text, usage = await client.chat(
model="deepseek-chat",
messages=[ChatMessage(role="user", content="hi")],
temperature=0.2,
max_tokens=100,
)
assert text == "Hello"
assert usage.input_tokens == 10 and usage.output_tokens == 5
@pytest.mark.anyio
async def test_client_async_context_manager_closes_transport():
"""async with 块退出后底层 httpx.AsyncClient 应被关闭(T8)。"""
async with HttpLLMClient(
base_url="https://api.test.local",
api_key="sk-test",
transport=httpx.MockTransport(_ok_handler),
) as client:
assert isinstance(client, HttpLLMClient)
text, _ = await client.chat(
model="deepseek-chat",
messages=[ChatMessage(role="user", content="hi")],
temperature=0.2,
max_tokens=100,
)
assert text == "Hello"
assert client._client.is_closed is True
def test_chat_requires_api_key():
with pytest.raises(LLMNotConfiguredError):
HttpLLMClient(base_url="https://x", api_key="")
@pytest.mark.anyio
async def test_chat_timeout():
def slow(request):
raise httpx.ReadTimeout("slow")
with pytest.raises(LLMTimeoutError):
await make_client(slow, retry_backoff=(0, 0)).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
@pytest.mark.anyio
async def test_chat_5xx_retry_then_network_error():
calls = {"n": 0}
def handler(request):
calls["n"] += 1
return httpx.Response(500, text="boom")
with pytest.raises(LLMNetworkError):
await make_client(handler, retry_backoff=(0, 0)).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
assert calls["n"] == 3 # 初始 + 2 次退避重试(间隔 0/0.01)
@pytest.mark.anyio
async def test_chat_network_error_retry_then_network_error():
# 非超时的网络错误走 httpx.HTTPError 分支,重试耗尽后抛 LLMNetworkError
def handler(request):
raise httpx.ConnectError("connection refused")
with pytest.raises(LLMNetworkError):
await make_client(handler, retry_backoff=(0, 0)).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
@pytest.mark.anyio
async def test_chat_4xx_no_retry():
calls = {"n": 0}
def handler(request):
calls["n"] += 1
return httpx.Response(429, text="rate limit")
with pytest.raises(LLMNetworkError):
await make_client(handler).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
assert calls["n"] == 1 # 4xx 不重试
@pytest.mark.anyio
async def test_chat_3xx_no_retry():
# 3xx(重定向,httpx 不自动跟随)不得被当作成功,应立即抛 LLMNetworkError
calls = {"n": 0}
def handler(request):
calls["n"] += 1
return httpx.Response(302, headers={"Location": "https://elsewhere"})
with pytest.raises(LLMNetworkError):
await make_client(handler).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)
assert calls["n"] == 1 # 3xx 不重试
@pytest.mark.anyio
async def test_chat_malformed_response_raises_llm_response_error():
# 2xx 但响应体结构损坏(非 JSON / 缺 choices/message/content)→ LLMResponseError
def not_json(request):
return httpx.Response(200, text="not-json")
def missing_key(request):
return httpx.Response(200, json={"choices": []})
for handler in (not_json, missing_key):
with pytest.raises(LLMResponseError):
await make_client(handler).chat(
model="m", messages=[ChatMessage(role="user", content="x")],
temperature=0.2, max_tokens=100,
)