- sort_reservations_with_logging:就地排序 + 前后写日志(含异常吞噬) - check_total_timeout:超时检查 + 写日志(含异常吞噬) 修复一个真实 bug:拆出 attend_window_helpers.py 时遗漏 import time,导致 check_total_timeout 内的 time.time() 抛 NameError,被外层 try/except 吞噬, 函数永远返回 False。原主文件不存在该 bug。 新增 6 个单元测试(含 import time 缺失场景的 swallow exception 验证), 1267 → 1273 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
414 lines
14 KiB
Python
414 lines
14 KiB
Python
"""单元测试:douyin/reservation/reservation_logic/attend_window_helpers.py"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import time
|
||
from unittest.mock import MagicMock
|
||
|
||
from douyin.reservation.reservation_logic import attend_window_helpers as h
|
||
|
||
|
||
def _make_analyser(**overrides):
|
||
a = MagicMock()
|
||
a.high_value_fudai_join_ahead_seconds = 100
|
||
a.high_value_fudai_max_swipe_back = 60
|
||
for k, v in overrides.items():
|
||
setattr(a, k, v)
|
||
return a
|
||
|
||
|
||
# ---- compute_dynamic_ahead ---- #
|
||
|
||
def test_compute_dynamic_ahead_zero_back_steps():
|
||
"""target_pos == room_pos → 不需要回退,提前量保持 join_ahead"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 5}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=5, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 0
|
||
assert da == 100 # join_ahead 默认值
|
||
|
||
|
||
def test_compute_dynamic_ahead_with_back_steps():
|
||
"""需要回退 50 步,每步 0.6s → 50*0.6+18 = 48s,仍小于 join_ahead=100,所以保持 100"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=50, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 50
|
||
assert da == 100
|
||
|
||
|
||
def test_compute_dynamic_ahead_back_steps_dominate():
|
||
"""需要回退 200 步但被 max_back=60 截断 → 60*0.6+18 = 54,仍 < 100"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=200, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 60 # 截断到 max_back
|
||
|
||
|
||
def test_compute_dynamic_ahead_huge_delay_dominates():
|
||
"""delay=5s 时 60*5+18=318 > join_ahead,dynamic_ahead 取大值"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=60, reservation_back_swipe_delay_s=5.0)
|
||
assert est == 60
|
||
assert da == 318 # 60*5+18
|
||
|
||
|
||
def test_compute_dynamic_ahead_no_max_back_uncapped():
|
||
"""high_value_fudai_max_swipe_back=0 时,由于 `or 60` 会被兜底成 60"""
|
||
a = _make_analyser(high_value_fudai_max_swipe_back=0)
|
||
target = {"pos_seen": 0}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=100, reservation_back_swipe_delay_s=2.0)
|
||
assert est == 60 # 0 被 `or 60` 兜底
|
||
assert da == 138 # 60*2+18
|
||
|
||
|
||
def test_compute_dynamic_ahead_handles_bad_target_pos():
|
||
"""target.pos_seen 缺失时按 0 处理"""
|
||
a = _make_analyser()
|
||
target = {}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=10, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 10
|
||
|
||
|
||
def test_compute_dynamic_ahead_negative_back_steps_clamped():
|
||
"""target_pos > room_pos(不正常)时 est_back_times 被钳到 0"""
|
||
a = _make_analyser()
|
||
target = {"pos_seen": 100}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=10, reservation_back_swipe_delay_s=0.6)
|
||
assert est == 0
|
||
|
||
|
||
def test_compute_dynamic_ahead_handles_bad_join_ahead():
|
||
"""getattr 抛错时回退到 100.0"""
|
||
a = MagicMock()
|
||
type(a).high_value_fudai_join_ahead_seconds = property(
|
||
lambda self: (_ for _ in ()).throw(RuntimeError("boom"))
|
||
)
|
||
a.high_value_fudai_max_swipe_back = 60
|
||
target = {"pos_seen": 5}
|
||
da, est = h.compute_dynamic_ahead(a, target, room_pos=5, reservation_back_swipe_delay_s=0.6)
|
||
# join_ahead 兜底为 100.0
|
||
assert da == 100
|
||
|
||
|
||
# ---- _ts_to_str ---- #
|
||
|
||
def test_ts_to_str_invalid():
|
||
assert h._ts_to_str(0) == "无效"
|
||
assert h._ts_to_str(-100) == "无效"
|
||
|
||
|
||
def test_ts_to_str_valid():
|
||
s = h._ts_to_str(1700000000) # 2023-11-14 22:13:20 UTC, 本地时区
|
||
assert len(s) == 19 # YYYY-MM-DD HH:MM:SS
|
||
|
||
|
||
# ---- log_attend_window_check_details ---- #
|
||
|
||
def test_log_attend_window_check_details_writes_one_line():
|
||
a = MagicMock()
|
||
h.log_attend_window_check_details(
|
||
a,
|
||
now_ts=1700000000,
|
||
focus_until=0,
|
||
ft=1700000100,
|
||
dynamic_ahead=100,
|
||
join_ahead=100.0,
|
||
poll_before=10.0,
|
||
est_back_times=0,
|
||
)
|
||
assert a._log_kv.call_count == 1
|
||
args, kwargs = a._log_kv.call_args
|
||
assert args[0] == "预约"
|
||
assert "process_reservation_attend_window 详细检查" in args[1]
|
||
assert kwargs["target_future_ts"] == 1700000100
|
||
assert kwargs["dynamic_ahead"] == 100
|
||
assert kwargs["in_lock_period"] is False # focus_until=0
|
||
|
||
|
||
def test_log_attend_window_check_details_in_lock_period():
|
||
a = MagicMock()
|
||
h.log_attend_window_check_details(
|
||
a,
|
||
now_ts=1700000000,
|
||
focus_until=1700000050, # > now_ts
|
||
ft=1700000100,
|
||
dynamic_ahead=100,
|
||
join_ahead=100.0,
|
||
poll_before=10.0,
|
||
est_back_times=0,
|
||
)
|
||
args, kwargs = a._log_kv.call_args
|
||
assert kwargs["in_lock_period"] is True
|
||
|
||
|
||
def test_log_attend_window_check_details_swallows_exceptions():
|
||
bad = MagicMock()
|
||
bad._log_kv.side_effect = RuntimeError("boom")
|
||
h.log_attend_window_check_details(
|
||
bad, now_ts=0, focus_until=0, ft=0, dynamic_ahead=0,
|
||
join_ahead=0.0, poll_before=0.0, est_back_times=0,
|
||
)
|
||
|
||
|
||
# ---- check_waiting_for_reservation_lottery ---- #
|
||
|
||
def test_waiting_lottery_with_reservation_returns_true():
|
||
a = MagicMock()
|
||
a._waiting_for_lottery_result = True
|
||
a._current_attending_reservation_fudai = True
|
||
assert h.check_waiting_for_reservation_lottery(a) is True
|
||
|
||
|
||
def test_waiting_lottery_only_one_flag_returns_false():
|
||
a = MagicMock()
|
||
a._waiting_for_lottery_result = True
|
||
a._current_attending_reservation_fudai = False
|
||
assert h.check_waiting_for_reservation_lottery(a) is False
|
||
|
||
a._waiting_for_lottery_result = False
|
||
a._current_attending_reservation_fudai = True
|
||
assert h.check_waiting_for_reservation_lottery(a) is False
|
||
|
||
|
||
def test_waiting_lottery_neither_flag_returns_false():
|
||
a = MagicMock()
|
||
a._waiting_for_lottery_result = False
|
||
a._current_attending_reservation_fudai = False
|
||
assert h.check_waiting_for_reservation_lottery(a) is False
|
||
|
||
|
||
# ---- maybe_promote_locked_reservation ---- #
|
||
|
||
def test_promote_locked_no_lock_period_returns_unchanged():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
ref = [0.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=0.0, now_ts=200.0, poll_before=10.0
|
||
)
|
||
assert result == 0.0
|
||
|
||
|
||
def test_promote_locked_expired_returns_unchanged():
|
||
"""now_ts >= focus_until → 锁定期已过,不修改"""
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
ref = [50.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=50.0, now_ts=100.0, poll_before=10.0
|
||
)
|
||
assert result == 50.0
|
||
|
||
|
||
def test_promote_locked_target_not_in_queue_clears_focus():
|
||
"""锁定期对应的预约已不在队列 → 清除锁定期"""
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 200}, {"future_ts": 300}]
|
||
# focus_until = 1000,但队列里没有 ft+10 = 1000 的预约
|
||
ref = [1000.0]
|
||
now = 500.0
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=1000.0, now_ts=now, poll_before=10.0
|
||
)
|
||
assert result == 0.0
|
||
assert ref[0] == 0.0
|
||
|
||
|
||
def test_promote_locked_already_at_head_no_swap():
|
||
"""锁定预约已在队头 → 不调整顺序"""
|
||
a = MagicMock()
|
||
# focus_until = 200 + 10 = 210,正好是第一个预约的"预期 focus"
|
||
reservations = [{"future_ts": 200}, {"future_ts": 300}]
|
||
ref = [210.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=210.0, now_ts=100.0, poll_before=10.0
|
||
)
|
||
assert result == 210.0
|
||
# 顺序未变
|
||
assert reservations[0]["future_ts"] == 200
|
||
|
||
|
||
def test_promote_locked_swaps_to_head():
|
||
"""锁定预约不在队头 → 移到队头"""
|
||
a = MagicMock()
|
||
# focus_until = 300 + 10 = 310,对应队列中第二个预约
|
||
reservations = [
|
||
{"future_ts": 200},
|
||
{"future_ts": 300},
|
||
]
|
||
ref = [310.0]
|
||
result = h.maybe_promote_locked_reservation(
|
||
a, reservations, ref, focus_until=310.0, now_ts=100.0, poll_before=10.0
|
||
)
|
||
assert result == 310.0
|
||
# 锁定预约(300)已被移到队头
|
||
assert reservations[0]["future_ts"] == 300
|
||
|
||
|
||
# ---- check_already_participated ---- #
|
||
|
||
def test_already_participated_with_match_pops_and_returns_true():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {100.0, 200.0}
|
||
reservations = [{"future_ts": 100.0}, {"future_ts": 300.0}]
|
||
target = reservations[0]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, target, 100.0, write_cb)
|
||
assert result is True
|
||
assert len(reservations) == 1 # 队头被弹出
|
||
write_cb.assert_called_once()
|
||
|
||
|
||
def test_already_participated_no_match_returns_false():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {500.0}
|
||
reservations = [{"future_ts": 100.0}]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 100.0, write_cb)
|
||
assert result is False
|
||
assert len(reservations) == 1
|
||
write_cb.assert_not_called()
|
||
|
||
|
||
def test_already_participated_within_5s_tolerance():
|
||
"""允许 5 秒误差 → 95-105 之间也算已参与"""
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {100.0}
|
||
reservations = [{"future_ts": 103.0}]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 103.0, write_cb)
|
||
assert result is True
|
||
|
||
|
||
def test_already_participated_outside_5s_returns_false():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = {100.0}
|
||
reservations = [{"future_ts": 110.0}] # 差 10 秒,超出误差
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 110.0, write_cb)
|
||
assert result is False
|
||
|
||
|
||
def test_already_participated_non_set_attr_treated_as_empty():
|
||
a = MagicMock()
|
||
a._participated_fudai_future_ts = "not_a_set"
|
||
reservations = [{"future_ts": 100.0}]
|
||
write_cb = MagicMock()
|
||
result = h.check_already_participated(a, reservations, reservations[0], 100.0, write_cb)
|
||
assert result is False
|
||
|
||
|
||
# ---- discard_invalid_future_ts ---- #
|
||
|
||
def test_discard_invalid_ft_zero_returns_true():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 0}, {"future_ts": 100}]
|
||
write_cb = MagicMock()
|
||
result = h.discard_invalid_future_ts(a, reservations, reservations[0], 0.0, write_cb)
|
||
assert result is True
|
||
assert len(reservations) == 1
|
||
write_cb.assert_called_once()
|
||
|
||
|
||
def test_discard_invalid_ft_negative_returns_true():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": -5}]
|
||
write_cb = MagicMock()
|
||
result = h.discard_invalid_future_ts(a, reservations, reservations[0], -5.0, write_cb)
|
||
assert result is True
|
||
|
||
|
||
def test_discard_invalid_ft_positive_returns_false():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
write_cb = MagicMock()
|
||
result = h.discard_invalid_future_ts(a, reservations, reservations[0], 100.0, write_cb)
|
||
assert result is False
|
||
assert len(reservations) == 1
|
||
write_cb.assert_not_called()
|
||
|
||
|
||
# ---- log_target_reservation_details ---- #
|
||
|
||
def test_log_target_reservation_details_writes_one_line():
|
||
a = MagicMock()
|
||
target = {"prize_value_yuan": 5.0, "pos_seen": 3, "content": "test"}
|
||
h.log_target_reservation_details(a, target, ft=1700000000.0)
|
||
a._log_kv.assert_called_once()
|
||
args, kwargs = a._log_kv.call_args
|
||
assert args[0] == "预约"
|
||
assert kwargs["target_future_ts"] == 1700000000
|
||
assert kwargs["target_value"] == 5.0
|
||
|
||
|
||
def test_log_target_reservation_details_invalid_ft():
|
||
a = MagicMock()
|
||
h.log_target_reservation_details(a, {}, ft=0.0)
|
||
a._log_kv.assert_called_once()
|
||
_, kwargs = a._log_kv.call_args
|
||
assert kwargs["target_future_str"] == "无效"
|
||
|
||
|
||
def test_log_target_reservation_details_swallows_exception():
|
||
a = MagicMock()
|
||
a._log_kv.side_effect = RuntimeError("boom")
|
||
# 不应抛错
|
||
h.log_target_reservation_details(a, {}, ft=100.0)
|
||
|
||
|
||
# ---- sort_reservations_with_logging ---- #
|
||
|
||
def test_sort_reservations_logs_before_and_after():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 200}, {"future_ts": 100}]
|
||
sort_cb = MagicMock(side_effect=lambda lst: lst.sort(key=lambda x: x["future_ts"]))
|
||
h.sort_reservations_with_logging(a, reservations, sort_cb)
|
||
# 至少 2 次日志:开始排序 / 排序完成
|
||
assert a._log_kv.call_count >= 2
|
||
# 排序后队头是 100
|
||
assert reservations[0]["future_ts"] == 100
|
||
|
||
|
||
def test_sort_reservations_swallows_sort_exception():
|
||
a = MagicMock()
|
||
reservations = [{"future_ts": 100}]
|
||
sort_cb = MagicMock(side_effect=RuntimeError("boom"))
|
||
# 不应抛错
|
||
h.sort_reservations_with_logging(a, reservations, sort_cb)
|
||
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
|
||
assert any("队列排序异常" in m for m in msgs)
|
||
|
||
|
||
def test_sort_reservations_empty_queue():
|
||
a = MagicMock()
|
||
sort_cb = MagicMock()
|
||
h.sort_reservations_with_logging(a, [], sort_cb)
|
||
sort_cb.assert_called_once_with([])
|
||
|
||
|
||
# ---- check_total_timeout ---- #
|
||
|
||
def test_check_timeout_not_exceeded_returns_false():
|
||
a = MagicMock()
|
||
now = time.time()
|
||
assert h.check_total_timeout(a, now, total_timeout_s=10.0, stage="测试") is False
|
||
|
||
|
||
def test_check_timeout_exceeded_returns_true():
|
||
a = MagicMock()
|
||
past = time.time() - 100
|
||
assert h.check_total_timeout(a, past, total_timeout_s=10.0, stage="测试") is True
|
||
msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2]
|
||
assert any("超时退出(测试)" in m for m in msgs)
|
||
|
||
|
||
def test_check_timeout_swallows_exception():
|
||
a = MagicMock()
|
||
a._log_kv.side_effect = RuntimeError("boom")
|
||
past = time.time() - 100
|
||
# 应不抛错
|
||
result = h.check_total_timeout(a, past, total_timeout_s=10.0, stage="测试")
|
||
assert result is True
|