重新按英文命名,修改成相对路径

This commit is contained in:
lianjianwei2024
2026-10-11 10:52:01 +08:00
parent eea5d511dc
commit 9bb23ac13b
64 changed files with 913 additions and 1427 deletions

View File

@@ -0,0 +1,117 @@
# -*- coding: utf-8 -*-
"""决策 31 加固项:can_run 结果缓存、高水位节流写、product_id 绑定、心跳并发串行、
缓存失效(心跳接受新租约后门禁立即看到)。"""
import json
import threading
import time
from datetime import timedelta
from lic_helpers import (NOW, FakeTransport, envelope, lease_payload,
make_client, cert_payload, z)
def active_env(priv_key, server=NOW):
return (200, {}, {"data": envelope(lease_payload(server=server), priv_key, "lease")})
def test_can_run_cache_within_ttl(priv_key, client):
client.cache_ttl = 600
client.mono_fn = (lambda: 1000.0)
client._tp.script.append(active_env(priv_key))
client.heartbeat()
assert client.can_run()[0] is True
# TTL 内就算本地租约被篡改,也吃到缓存(这正是缓存的意义:不每请求验签)
p = client.state_store.path
st = json.load(open(p, encoding="utf-8"))
st["lease_envelope"]["payload"]["state"] = "frozen"
json.dump(st, open(p, "w", encoding="utf-8"), ensure_ascii=False)
client.mono_fn = (lambda: 1001.0)
assert client.can_run()[0] is True # 缓存命中
client.mono_fn = (lambda: 1000.0 + 601) # TTL 过 → 重新验签发现篡改
ok, reason = client.can_run()
assert ok is False and "校验失败" in reason
def test_cache_invalidated_by_new_lease(priv_key, client):
client.cache_ttl = 600
client.mono_fn = (lambda: 1000.0)
client._tp.script.append((200, {}, {"data": envelope(lease_payload(state="frozen"),
priv_key, "lease")}))
client.heartbeat()
assert client.can_run()[0] is False # 负结果也进缓存
client._tp.script.append(active_env(priv_key))
client.heartbeat() # 新租约必须作废缓存
assert client.can_run()[0] is True
def test_high_water_throttled_write(priv_key, client):
"""55 秒内不重复落盘高水位;跨过阈值才前进;回拨检测不受影响。"""
client.cache_ttl = 0 # 关缓存,专测节流
t = {"now": NOW}
client.now_fn = lambda: t["now"]
client._tp.script.append(active_env(priv_key))
client.heartbeat()
assert client.can_run()[0] is True
assert client.state_store.load()["local_high_water"] == z(NOW)
t["now"] = NOW + timedelta(seconds=30)
assert client.can_run()[0] is True
assert client.state_store.load()["local_high_water"] == z(NOW) # 未落盘
t["now"] = NOW + timedelta(seconds=60)
assert client.can_run()[0] is True
assert client.state_store.load()["local_high_water"] == z(NOW + timedelta(seconds=60))
t["now"] = NOW + timedelta(seconds=20) # 回拨 40 秒:立即锁
ok, reason = client.can_run()
assert ok is False and "回拨" in reason
def test_product_binding(priv_key, tmp_path, keyring):
c = make_client(tmp_path, keyring)
c.expected_product_id = 3
c._tp.script.append((200, {}, {"data": {"instance_id": c.state_store.load()["instance_id"],
"status": "activated",
"certificate": envelope(
cert_payload(product_id=9), priv_key, "cert")}}))
r = c.activate()
assert r["ok"] is False and r["code"] == "PRODUCT_MISMATCH"
assert c.state_store.load().get("status") != "activated"
c._tp.script.append((200, {}, {"data": {"instance_id": c.state_store.load()["instance_id"],
"status": "activated",
"certificate": envelope(
cert_payload(product_id=3), priv_key, "cert")}}))
assert c.activate()["ok"] is True
assert c.status()["product_id"] == 3
def test_concurrent_heartbeats_serialized(priv_key, client):
"""循环/自愈/手动三方并发:必须串行(各自完整操作、键不互踩、状态不撕)。"""
base = client._tp.__call__
def slow(method, url, body, headers):
time.sleep(0.05)
return base(method, url, body, headers)
client._send_fn = slow
client._tp.script.append(active_env(priv_key))
client._tp.script.append(active_env(priv_key))
rs = []
def beat():
rs.append(client.heartbeat())
ts = [threading.Thread(target=beat) for _ in range(2)]
[t.start() for t in ts]
[t.join() for t in ts]
assert all(r["ok"] for r in rs)
keys = [c["headers"].get("Idempotency-Key") for c in client._tp.calls]
assert len(keys) == 2 and len(set(keys)) == 2 # 两个独立操作、两把键
st = client.state_store.load()
assert st["enabled"] is True and "heartbeat_op" not in st
def test_gate_can_run_safe_uses_cache(priv_key, client):
"""门禁入口 can_run_safe 走同一缓存:TTL 内重复调用不再触发落盘。"""
client.cache_ttl = 600
client.mono_fn = (lambda: 1000.0)
client._tp.script.append(active_env(priv_key))
client.heartbeat()
a = client.can_run_safe()
b = client.can_run_safe()
assert a[0] is True and b[0] is True