mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
feat(pool): 引入多维评分调度策略与账号状态检测
- 新增 multi_score 调度模式,支持 LRU/延迟/健康度/剩余额度多维加权评分 - 新增调度预设维度系统(free_team_first, quota_balanced, recent_refresh, single_account),支持有序对象列表配置格式并兼容旧字符串列表 - 新增 account_state 模块,统一账号封禁/受限检测逻辑,替代分散在 routes 中的判断代码 - 新增 health_cache 模块和 latency 采样(redis_ops.record_latency / batch_get_latency_avgs) - RequestDispatcher 返回 ttfb_ms,PoolManager.on_request_success 记录延迟样本 - 前端:PoolConfigDialog 替换为 PoolSchedulingDialog,支持预设维度可视化配置;号池管理页增加调度模式标签与账号异常 Badge 显示 - 提取前端 accountBlock 工具函数,ProviderDetailDrawer 复用统一判断 - scheduling_dimensions 增加 account_state 和 latency 维度评估 - 补充 account_state、health_cache、multi_score 策略、preset 维度、redis latency 等测试
This commit is contained in:
88
tests/services/test_pool_redis_latency_ops.py
Normal file
88
tests/services/test_pool_redis_latency_ops.py
Normal file
@@ -0,0 +1,88 @@
|
||||
"""Tests for pool redis latency operations."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.services.provider.pool import redis_ops
|
||||
|
||||
|
||||
class _FakePipe:
|
||||
def __init__(self, execute_result: list[object] | None = None) -> None:
|
||||
self.ops: list[tuple] = []
|
||||
self._execute_result = execute_result or []
|
||||
|
||||
def zadd(self, key: str, mapping: dict[str, float]):
|
||||
self.ops.append(("zadd", key, mapping))
|
||||
return self
|
||||
|
||||
def zremrangebyscore(self, key: str, start: str, stop: float):
|
||||
self.ops.append(("zremrangebyscore", key, start, stop))
|
||||
return self
|
||||
|
||||
def zremrangebyrank(self, key: str, start: int, stop: int):
|
||||
self.ops.append(("zremrangebyrank", key, start, stop))
|
||||
return self
|
||||
|
||||
def expire(self, key: str, ttl: int):
|
||||
self.ops.append(("expire", key, ttl))
|
||||
return self
|
||||
|
||||
def eval(self, script: str, numkeys: int, key: str, window_start: str):
|
||||
self.ops.append(("eval", script, numkeys, key, window_start))
|
||||
return self
|
||||
|
||||
async def execute(self):
|
||||
return self._execute_result
|
||||
|
||||
|
||||
class _FakeRedis:
|
||||
def __init__(self, pipe: _FakePipe) -> None:
|
||||
self._pipe = pipe
|
||||
|
||||
def pipeline(self) -> _FakePipe:
|
||||
return self._pipe
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_record_latency_writes_sample_and_trims() -> None:
|
||||
pipe = _FakePipe()
|
||||
fake_redis = _FakeRedis(pipe)
|
||||
with patch(
|
||||
"src.services.provider.pool.redis_ops._get_redis",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_redis,
|
||||
):
|
||||
await redis_ops.record_latency(
|
||||
provider_id="prov-1",
|
||||
key_id="key-1",
|
||||
ttfb_ms=250,
|
||||
window_seconds=3600,
|
||||
sample_limit=50,
|
||||
)
|
||||
|
||||
op_names = [item[0] for item in pipe.ops]
|
||||
assert op_names == ["zadd", "zremrangebyscore", "zremrangebyrank", "expire"]
|
||||
assert pipe.ops[0][1] == "ap:prov-1:latency:key-1"
|
||||
assert pipe.ops[2][2:] == (0, -51)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_batch_get_latency_avgs_returns_numeric_results_only() -> None:
|
||||
pipe = _FakePipe(execute_result=[120.5, None, "330"])
|
||||
fake_redis = _FakeRedis(pipe)
|
||||
with patch(
|
||||
"src.services.provider.pool.redis_ops._get_redis",
|
||||
new_callable=AsyncMock,
|
||||
return_value=fake_redis,
|
||||
):
|
||||
result = await redis_ops.batch_get_latency_avgs(
|
||||
provider_id="prov-1",
|
||||
key_ids=["k1", "k2", "k3"],
|
||||
window_seconds=3600,
|
||||
)
|
||||
|
||||
assert result == {"k1": 120.5, "k3": 330.0}
|
||||
assert len([item for item in pipe.ops if item[0] == "eval"]) == 3
|
||||
Reference in New Issue
Block a user