Files
perf-harness-demo/tests/test_mark_price_service.py

117 lines
4.7 KiB
Python

"""标记价缓存服务的两条硬语义:断流回退 + 滑动窗口去重。
这是「压测脚本失真」判断的落点:缓存服务一旦写错(断流就崩 / 去重形同虚设),
下游所有触发率数据悄悄作废,而接口照常返回 200。
"""
import mark_price_service as mps
class FakeClock:
def __init__(self):
self.t = 0.0
def __call__(self):
return self.t
def advance(self, dt):
self.t += dt
def make_cache(ttl=120.0):
clock = FakeClock()
cache = mps.PriceCache(contract_id=1, ttl=ttl, tick_size=1.0,
window_size=2, now=clock)
return cache, clock
class TestSlidingWindowDedup:
def test_same_price_repeated_is_deduped(self):
"""同一价格连推多次:只接受第一笔,其余判重复。"""
cache, _ = make_cache()
assert cache.ingest_tick(31000.2) is True
for _ in range(5):
# 量化到 tick=1 后同为 31000 → 重复推送
assert cache.ingest_tick(31000.4) is False
assert cache.stats["accepted"] == 1
assert cache.stats["dups"] == 5
def test_alternating_prices_are_deduped(self):
"""A-B-A-B 交替抖动也要判重复。
★ 这是双元素窗口存在的理由:只存「最后一个值」的伪窗口在
100→105→100 时会把第三笔误判成新变化(只和 105 比),去重形同虚设。
"""
cache, _ = make_cache()
assert cache.ingest_tick(31000.0) is True # 窗口 [31000]
assert cache.ingest_tick(31005.0) is True # 窗口 [31000, 31005]
assert cache.ingest_tick(31000.0) is False # 在窗口内 → 重复
assert cache.ingest_tick(31005.0) is False # 在窗口内 → 重复
assert cache.stats["accepted"] == 2
assert cache.stats["dups"] == 2
def test_real_change_is_accepted(self):
"""价格真实变化(落在窗口外)必须被识别接受,不能被去重误杀。"""
cache, _ = make_cache()
assert cache.ingest_tick(31000.0) is True
assert cache.ingest_tick(31005.0) is True
assert cache.ingest_tick(31003.0) is True # 新值,不在窗口内
assert cache.stats["accepted"] == 3
def test_accepted_price_is_what_get_served(self):
"""接受的新值要成为对外出价的值(不能去重把价格「冻」在旧值上)。"""
cache, _ = make_cache()
cache.ingest_tick(31000.0)
cache.ingest_tick(31050.0)
assert cache.get_price()["price"] == 31050.0
class TestFallbackOnSourceOutage:
"""断流回退:源不可用时压测不中断 —— 接口照常出价,绝不抛异常。"""
def test_never_seen_source_serves_default(self):
"""行情源从未可用过:回退默认价(与 mock 基准同式),标记 stale。"""
cache, _ = make_cache()
out = cache.get_price()
assert out["price"] == mps.default_price_for(1) # 31000.0
assert out["stale"] is True
assert out["source"] == "default"
def test_fresh_cache_served_within_ttl(self):
cache, _ = make_cache(ttl=120.0)
cache.ingest_tick(31234.5)
out = cache.get_price()
assert out["price"] == 31234.5
assert out["stale"] is False
assert out["source"] == "cache"
def test_outage_within_ttl_still_serves_cache(self):
"""断流但缓存还在 TTL 内:继续用缓存价,压测不中断。"""
cache, clock = make_cache(ttl=120.0)
cache.ingest_tick(31234.5)
clock.advance(30) # 源挂了 30s(无新 tick),仍在 TTL 内
out = cache.get_price()
assert out["price"] == 31234.5
assert out["stale"] is False
def test_outage_beyond_ttl_falls_back_to_default(self):
"""断流超过 TTL:回退默认价,接口正常返回(不抛异常、不退出)。"""
cache, clock = make_cache(ttl=120.0)
cache.ingest_tick(31234.5)
clock.advance(121) # 缓存已过期
out = cache.get_price()
assert out["price"] == mps.default_price_for(1)
assert out["stale"] is True
assert out["source"] == "default"
assert cache.stats["served_default"] == 1
def test_dead_source_counts_error_and_still_serves(self):
"""源进程整个不在(连接被拒):poll 只计数不抛出,出价走回退。"""
cache, _ = make_cache()
poller = mps.Poller("http://127.0.0.1:9", cache,
interval=0.05, timeout=0.2)
poller.poll_once() # 端口 9 连接被拒 —— 必须被吞掉
assert cache.stats["fetch_errors"] == 1
out = cache.get_price()
assert out["source"] == "default" and out["stale"] is True