This repository has been archived on 2026-10-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
NeteaseUrl/tests/test_http_client.py
T
snowy-yang 9f56f68b66 chore(async-infra): 异步基础层 + 工具链 + 清理
异步基础层(core/):
- db.py   aiosqlite + WAL:读路径无锁并发、写路径 asyncio.Lock 串行,
          busy_timeout 自旋——实现「读写同时进行」(旧实现用 RLock 全读串行)
- http_client.py  aiohttp 连接池 + 有界重试(5xx 重试 / 4xx 不重试)+
          响应体上限 + 流式接口;按事件循环隔离 session(支持迁移期双 loop)

工具链:
- pyproject.toml   ruff(E/F/W/I/B/UP/C4/ASYNC/S/RUF) + pyright(basic) + pytest(asyncio auto)
- requirements-fastapi.txt  FastAPI/aiohttp/aiosqlite/aiofiles 等目标栈
- requirements-dev.txt      开发依赖(pytest/ruff/pyright)

清理:删除无用脚本 _csscheck.py 与产物标记 _css_ok.txt/_pkg_ok.txt,同步 .gitignore

验证:pytest 10/10(含「写 200 条同时 4 读协程零异常」并发用例);
ruff All checks passed;pyright 0 errors。
2026-09-26 00:24:00 +08:00

114 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""异步 HTTP 客户端测试:连接复用、有界重试、4xx 不重试、响应体上限。"""
import pytest
from aiohttp import web
from core.http_client import AsyncHTTPClient, UpstreamHTTPError, UpstreamTooLarge
@pytest.fixture()
async def server():
"""本地 aiohttp 测试服务器。"""
hits = {'flaky': 0}
async def ok(request):
return web.json_response({'ok': True})
async def flaky(request):
# 首次 500,之后 200 —— 验证 5xx 退避重试
hits['flaky'] += 1
if hits['flaky'] == 1:
return web.Response(status=500, text='boom')
return web.json_response({'retried': True})
async def notfound(request):
return web.Response(status=404, text='nope')
async def big(request):
return web.Response(body=b'x' * (2 * 1024 * 1024))
app = web.Application()
app.router.add_get('/ok', ok)
app.router.add_get('/flaky', flaky)
app.router.add_get('/notfound', notfound)
app.router.add_get('/big', big)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '127.0.0.1', 0)
await site.start()
port = site._server.sockets[0].getsockname()[1]
base = f'http://127.0.0.1:{port}'
try:
yield base, hits
finally:
await runner.cleanup()
async def test_get_json(server):
base, _ = server
c = AsyncHTTPClient()
try:
assert await c.get_json(base + '/ok') == {'ok': True}
finally:
await c.close()
async def test_retry_on_5xx(server):
base, hits = server
c = AsyncHTTPClient()
try:
data = await c.get_json(base + '/flaky')
assert data == {'retried': True}
assert hits['flaky'] == 2 # 第一次 500 + 重试成功
finally:
await c.close()
async def test_no_retry_on_4xx(server):
base, _ = server
c = AsyncHTTPClient()
try:
with pytest.raises(UpstreamHTTPError) as ei:
await c.get_text(base + '/notfound')
assert ei.value.status == 404
finally:
await c.close()
async def test_response_size_limit(server):
base, _ = server
c = AsyncHTTPClient(max_bytes=1024 * 1024) # 1MB 上限,服务端返回 2MB
try:
with pytest.raises(UpstreamTooLarge):
await c.get_text(base + '/big')
finally:
await c.close()
async def test_session_reuse(server):
"""同一客户端多次请求复用同一(按 loop 隔离的)session —— 连接池生效。"""
base, _ = server
c = AsyncHTTPClient()
try:
await c.get_json(base + '/ok')
s1 = await c._session_for_loop()
await c.get_json(base + '/ok')
s2 = await c._session_for_loop()
assert s1 is s2 and not s1.closed
finally:
await c.close()
async def test_stream(server):
base, _ = server
c = AsyncHTTPClient()
try:
async with c.stream('GET', base + '/ok') as resp:
assert resp.status == 200
body = await resp.read()
assert b'ok' in body
finally:
await c.close()