refactor(reservation): 拆出参与窗口入口日志 + 回退回调(988→937,-51 行)

- log_entering_attend_window:写"进入预约参与窗口"诊断日志
- execute_seek_reservation_callback:调用 seek_reservation_room_callback +
  前置日志 + 异常吞噬

新增 5 个单元测试,1302 → 1307 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 04:43:27 +08:00
1 parent 02e685ba20
commit 3192c8b5c7
3 files changed
+136 -62

No files matched your search

@@ -29,9 +29,11 @@ from .attend_window_helpers import (
check_waiting_for_reservation_lottery,
compute_dynamic_ahead,
discard_invalid_future_ts,
execute_seek_reservation_callback,
handle_lock_period_same_reservation_check,
handle_time_window_early_returns,
log_attend_window_check_details,
log_entering_attend_window,
log_target_reservation_details,
maybe_promote_locked_reservation,
sort_reservations_with_logging,
@@ -225,22 +227,10 @@ def process_reservation_attend_window(
return False
# 进入预约参与窗口:执行回退动作
try:
analyser._log_kv(
"预约",
"[预约] 进入预约参与窗口,准备回退到目标直播间",
future_ts=int(ft),
now_ts=int(now_ts),
room_pos=int(room_pos),
queue_len=len(reservations),
dynamic_ahead=int(dynamic_ahead),
est_back_times=int(est_back_times),
remain_seconds_to_open=int(ft - now_ts),
time_window_start=int(ft - dynamic_ahead),
time_window_end=int(ft + max(0.0, poll_before)),
)
except Exception:
pass
log_entering_attend_window(
analyser, ft, now_ts, room_pos, len(reservations),
dynamic_ahead, est_back_times, poll_before,
)
# 获取当前实际的 room_pos(可能已经被更新)
try:
@@ -255,52 +245,11 @@ def process_reservation_attend_window(
target_pos = 0
pos_diff = current_room_pos - target_pos
# 调用上层提供的"按预约信息回退到目标直播间"的回调
try:
analyser._log_kv(
"预约",
"[预约] 准备执行回退到预约直播间",
target_pos_seen=target_pos,
current_room_pos=current_room_pos,
pos_diff=pos_diff,
)
except Exception:
pass
# 直接调用回退回调,不需要预先下滑
# 原因:
# 1. 回退前已经进行了阶段诊断,处理了弹窗和福袋详情页等异常状态
# 2. 如果已经在直播间,不需要下滑
# 3. 预先下滑会导致 room_pos 减1,影响后续的回退计算,导致少回退一个直播间
ok = False
try:
ok = bool(seek_reservation_room_callback(target))
except Exception as e:
if _RESERVATION_ERROR_UTILS_AVAILABLE:
log_reservation_error(
analyser,
e,
format_reservation_context(
item=target,
reservations=reservations,
current_room_pos=current_room_pos,
target_pos=target_pos,
pos_diff=pos_diff,
),
level="ERROR",
stage="执行回退操作",
)
else:
try:
analyser._log_kv(
"预约",
"[预约] 执行回退时发生异常",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
ok = False
# 调用上层"按预约信息回退到目标直播间"的回调
ok = execute_seek_reservation_callback(
analyser, target, current_room_pos, target_pos, pos_diff,
seek_reservation_room_callback,
)
if ok:
# 获取目标直播间名字和福袋详情(用于日志和校验)
@@ -11,6 +11,73 @@ from datetime import datetime
from typing import Any, Dict, Tuple
def log_entering_attend_window(
analyser: Any,
ft: float,
now_ts: float,
room_pos: int,
queue_len: int,
dynamic_ahead: int,
est_back_times: int,
poll_before: float,
) -> None:
"""记录"进入预约参与窗口,准备回退到目标直播间"的诊断日志。"""
try:
analyser._log_kv(
"预约",
"[预约] 进入预约参与窗口,准备回退到目标直播间",
future_ts=int(ft),
now_ts=int(now_ts),
room_pos=int(room_pos),
queue_len=queue_len,
dynamic_ahead=int(dynamic_ahead),
est_back_times=int(est_back_times),
remain_seconds_to_open=int(ft - now_ts),
time_window_start=int(ft - dynamic_ahead),
time_window_end=int(ft + max(0.0, poll_before)),
)
except Exception:
pass
def execute_seek_reservation_callback(
analyser: Any,
target: Any,
current_room_pos: int,
target_pos: int,
pos_diff: int,
seek_reservation_room_callback: Any,
) -> bool:
"""调用上层提供的"按预约信息回退到目标直播间"的回调,含异常处理与日志。
返回回调成功与否(异常时返回 False)。
"""
try:
analyser._log_kv(
"预约",
"[预约] 准备执行回退到预约直播间",
target_pos_seen=target_pos,
current_room_pos=current_room_pos,
pos_diff=pos_diff,
)
except Exception:
pass
try:
return bool(seek_reservation_room_callback(target))
except Exception as e:
try:
analyser._log_kv(
"预约",
"[预约] 执行回退时发生异常",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
return False
def handle_lock_period_same_reservation_check(
analyser: Any,
target: Any,
+58
View File
@@ -598,3 +598,61 @@ def test_lock_check_same_reservation_not_at_target_returns_none():
)
result = h.handle_lock_period_same_reservation_check(**args)
assert result is None
# ---- log_entering_attend_window ---- #
def test_log_entering_attend_window_writes_one_line():
a = MagicMock()
h.log_entering_attend_window(
a, ft=1700000100.0, now_ts=1700000050.0,
room_pos=5, queue_len=3, dynamic_ahead=100, est_back_times=2, poll_before=10.0,
)
a._log_kv.assert_called_once()
_, kwargs = a._log_kv.call_args
assert kwargs["queue_len"] == 3
assert kwargs["est_back_times"] == 2
def test_log_entering_attend_window_swallows_exception():
a = MagicMock()
a._log_kv.side_effect = RuntimeError("boom")
h.log_entering_attend_window(
a, ft=0, now_ts=0, room_pos=0, queue_len=0,
dynamic_ahead=0, est_back_times=0, poll_before=0,
)
# ---- execute_seek_reservation_callback ---- #
def test_execute_seek_callback_returns_true():
a = MagicMock()
cb = MagicMock(return_value=True)
target = {"pos_seen": 5}
result = h.execute_seek_reservation_callback(
a, target, current_room_pos=10, target_pos=5, pos_diff=5,
seek_reservation_room_callback=cb,
)
assert result is True
cb.assert_called_once_with(target)
def test_execute_seek_callback_returns_false():
a = MagicMock()
cb = MagicMock(return_value=False)
result = h.execute_seek_reservation_callback(
a, {}, 0, 0, 0, cb,
)
assert result is False
def test_execute_seek_callback_exception_returns_false():
a = MagicMock()
cb = MagicMock(side_effect=RuntimeError("boom"))
result = h.execute_seek_reservation_callback(
a, {}, 0, 0, 0, cb,
)
assert result is False
# 应记录异常日志
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)