Files
Aether/tests/unit/test_curl_cffi_transport_pool.py
fawney19 e0286aebe3 refactor: 共享请求管道、按需懒加载、流式内存护栏与连接池治理
- 抽取 ApiRequestPipeline 单例,44 个路由文件共享同一实例
- Handler/Adapter 模块级 __getattr__ 延迟导入,减少启动时间
- 新增 ensure_stream_buffer_limit() 流式内存护栏(16MB 单行 / 32MB 总量)
- HTTP 空闲连接清理与 curl_cffi LRU 会话池
- ensure_providers_bootstrapped 按需引导指定 provider_types
- Usage 事件序列化迁移至 msgpack,Redis codec 隔离
- 启动预热任务(/readyz 就绪门控)与优雅关闭
- 通知邮件模块独立开关与 SMTP 配置校验
- CryptoService DCL 线程安全修复
- 通知模块开关 DB 查询 30s 内存缓存
- /readyz 对 unknown 状态返回 503
- 预热关闭 5s 超时保护
- 预热适配器逐个 try-except 容错
- FormatConversionRegistry 哨兵模式防并发重复物化
- 流式缓冲检查无条件执行

Closes #230

Co-authored-by: AAEE86 <ppk0227@hotmail.com>
2026-03-14 11:59:07 +08:00

41 lines
1.5 KiB
Python

from __future__ import annotations
from collections import OrderedDict
import pytest
import src.clients.curl_cffi_transport as transport_module
class _DummySession:
def __init__(self, **kwargs: object) -> None:
self.kwargs = kwargs
self.close_calls = 0
async def close(self) -> None:
self.close_calls += 1
@pytest.mark.asyncio
async def test_get_or_create_session_uses_lru_eviction(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(transport_module, "AsyncSession", _DummySession, raising=False)
monkeypatch.setattr(transport_module, "_MAX_SESSIONS", 2, raising=False)
monkeypatch.setattr(transport_module, "_session_pool", OrderedDict(), raising=False)
s1 = await transport_module._get_or_create_session("chrome120", "http://p1")
s2 = await transport_module._get_or_create_session("chrome120", "http://p2")
# 命中 s1 使其变成最近使用,随后新增 s3 应淘汰 s2。
s1_hit = await transport_module._get_or_create_session("chrome120", "http://p1")
assert s1_hit is s1
s3 = await transport_module._get_or_create_session("chrome120", "http://p3")
assert isinstance(s3, _DummySession)
assert len(transport_module._session_pool) == 2
assert "chrome120::http://p1" in transport_module._session_pool
assert "chrome120::http://p3" in transport_module._session_pool
assert "chrome120::http://p2" not in transport_module._session_pool
assert s2.close_calls == 1
assert s1.close_calls == 0