refactor(fudai): 拆出预约抢占调度块(2429→2318,-111 行)
execute_reservation_attend_or_cleanup:根据 needswitch 分支处理: - 多间模式:调用 process_reservation_attend_window 抢占回退 - 单间模式:仅清理过期预约 新增 4 个单元测试覆盖正/负回报值、ref 同步、cleanup 异常吞噬。 1148 → 1152 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
28f91b2aaf
commit
03e509ac25
3 files changed
+212
-83
No files matched your search
+10
-82
@@ -1335,88 +1335,16 @@ class fudai_analyse:
|
||||
# 添加详细的调试日志,确认 needswitch 的值和预约信息
|
||||
fudai_run_blocks.log_needswitch_check_details(self, needswitch_cfg)
|
||||
|
||||
if needswitch_cfg:
|
||||
# 多间模式:需要回退到预约直播间
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 多间模式:开始调用 process_reservation_attend_window",
|
||||
reservations_count=(
|
||||
len(self.reservations) if self.reservations else 0
|
||||
),
|
||||
room_pos=int(getattr(self, "room_pos", 0) or 0),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
preempted = (
|
||||
douyin_reservation_logic.process_reservation_attend_window(
|
||||
analyser=self,
|
||||
reservations=self.reservations,
|
||||
room_pos=int(getattr(self, "room_pos", 0) or 0),
|
||||
reservation_focus_until_ts_ref=focus_ref,
|
||||
reservation_after_action_pending_ref=after_action_ref,
|
||||
reservation_back_swipe_delay_s=float(
|
||||
self.reservation_back_swipe_delay or 0.6
|
||||
),
|
||||
swipe_down_back_callback=_swipe_down_once,
|
||||
seek_reservation_room_callback=_seek_reservation_room,
|
||||
sort_reservations_callback=_sort_reservations,
|
||||
write_state_callback=_write_state,
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] process_reservation_attend_window 返回结果",
|
||||
preempted=preempted,
|
||||
focus_until=(
|
||||
int(focus_ref[0])
|
||||
if focus_ref and focus_ref[0] > 0
|
||||
else 0
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 同步回写 ref 值
|
||||
if focus_ref:
|
||||
self.reservation_focus_until_ts = float(focus_ref[0] or 0.0)
|
||||
if after_action_ref:
|
||||
self.reservation_after_action_pending = bool(
|
||||
after_action_ref[0]
|
||||
)
|
||||
|
||||
# 如果抢占成功,设置当前参与的福袋类型为预约福袋
|
||||
if preempted:
|
||||
self._current_attending_reservation_fudai = True
|
||||
|
||||
# 抢占逻辑已删除,直接继续处理
|
||||
else:
|
||||
# 单间模式:不需要回退,预约会在当前直播间自动识别和参与
|
||||
# 只需要清理过期的预约
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 单间模式(needswitch=False):不执行预约回退,预约会在当前直播间自动识别和参与",
|
||||
reservations_count=(
|
||||
len(self.reservations) if self.reservations else 0
|
||||
),
|
||||
)
|
||||
|
||||
# use module-level `douyin_reservation_logic`
|
||||
douyin_reservation_logic.cleanup_expired_reservations(
|
||||
self, self.reservations, _write_state
|
||||
)
|
||||
except Exception as exc:
|
||||
try:
|
||||
self._log_kv(
|
||||
"预约",
|
||||
"[预约] 单间模式下清理过期预约时出错",
|
||||
error=str(exc),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
fudai_run_blocks.execute_reservation_attend_or_cleanup(
|
||||
self,
|
||||
needswitch_cfg=needswitch_cfg,
|
||||
focus_ref=focus_ref,
|
||||
after_action_ref=after_action_ref,
|
||||
swipe_down_back_callback=_swipe_down_once,
|
||||
seek_reservation_room_callback=_seek_reservation_room,
|
||||
sort_reservations_callback=_sort_reservations,
|
||||
write_state_callback=_write_state,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -655,6 +655,99 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None:
|
||||
)
|
||||
|
||||
|
||||
def execute_reservation_attend_or_cleanup(
|
||||
analyser: Any,
|
||||
needswitch_cfg: bool,
|
||||
focus_ref: list,
|
||||
after_action_ref: list,
|
||||
swipe_down_back_callback: Any,
|
||||
seek_reservation_room_callback: Any,
|
||||
sort_reservations_callback: Any,
|
||||
write_state_callback: Any,
|
||||
) -> None:
|
||||
"""根据 needswitch_cfg 分支处理:
|
||||
- True:调用 process_reservation_attend_window 抢占回退到目标直播间
|
||||
- False(单间模式):仅清理过期预约
|
||||
"""
|
||||
from douyin.reservation import douyin_reservation_logic
|
||||
|
||||
if needswitch_cfg:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 多间模式:开始调用 process_reservation_attend_window",
|
||||
reservations_count=(
|
||||
len(analyser.reservations) if analyser.reservations else 0
|
||||
),
|
||||
room_pos=int(getattr(analyser, "room_pos", 0) or 0),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
preempted = douyin_reservation_logic.process_reservation_attend_window(
|
||||
analyser=analyser,
|
||||
reservations=analyser.reservations,
|
||||
room_pos=int(getattr(analyser, "room_pos", 0) or 0),
|
||||
reservation_focus_until_ts_ref=focus_ref,
|
||||
reservation_after_action_pending_ref=after_action_ref,
|
||||
reservation_back_swipe_delay_s=float(
|
||||
analyser.reservation_back_swipe_delay or 0.6
|
||||
),
|
||||
swipe_down_back_callback=swipe_down_back_callback,
|
||||
seek_reservation_room_callback=seek_reservation_room_callback,
|
||||
sort_reservations_callback=sort_reservations_callback,
|
||||
write_state_callback=write_state_callback,
|
||||
)
|
||||
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] process_reservation_attend_window 返回结果",
|
||||
preempted=preempted,
|
||||
focus_until=(
|
||||
int(focus_ref[0])
|
||||
if focus_ref and focus_ref[0] > 0
|
||||
else 0
|
||||
),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 同步回写 ref 值
|
||||
if focus_ref:
|
||||
analyser.reservation_focus_until_ts = float(focus_ref[0] or 0.0)
|
||||
if after_action_ref:
|
||||
analyser.reservation_after_action_pending = bool(after_action_ref[0])
|
||||
|
||||
# 如果抢占成功,设置当前参与的福袋类型为预约福袋
|
||||
if preempted:
|
||||
analyser._current_attending_reservation_fudai = True
|
||||
return
|
||||
|
||||
# 单间模式:仅清理过期预约
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 单间模式(needswitch=False):不执行预约回退,预约会在当前直播间自动识别和参与",
|
||||
reservations_count=(
|
||||
len(analyser.reservations) if analyser.reservations else 0
|
||||
),
|
||||
)
|
||||
|
||||
douyin_reservation_logic.cleanup_expired_reservations(
|
||||
analyser, analyser.reservations, write_state_callback
|
||||
)
|
||||
except Exception as exc:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 单间模式下清理过期预约时出错",
|
||||
error=str(exc),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def log_needswitch_check_details(analyser: Any, needswitch_cfg: bool) -> None:
|
||||
"""记录 needswitch 配置 + 第一个预约详情的诊断日志(纯日志辅助)。"""
|
||||
try:
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -735,3 +735,111 @@ def test_log_needswitch_empty_queue_logs_zero_count():
|
||||
assert call.kwargs["needswitch_cfg"] is False
|
||||
assert call.kwargs["reservations_count"] == 0
|
||||
assert call.kwargs["first_reservation"] == {}
|
||||
|
||||
|
||||
# ---- execute_reservation_attend_or_cleanup ---- #
|
||||
|
||||
def _make_reservation_analyser():
|
||||
a = MagicMock()
|
||||
a._log_kv = MagicMock()
|
||||
a.reservations = [{"future_ts": time.time() + 100, "prize_value_yuan": 5.0}]
|
||||
a.room_pos = 5
|
||||
a.reservation_back_swipe_delay = 0.6
|
||||
a.reservation_focus_until_ts = 0.0
|
||||
a.reservation_after_action_pending = False
|
||||
a._current_attending_reservation_fudai = False
|
||||
return a
|
||||
|
||||
|
||||
def test_execute_reservation_multi_room_calls_attend_window():
|
||||
"""needswitch=True 时调用 process_reservation_attend_window"""
|
||||
a = _make_reservation_analyser()
|
||||
from douyin.reservation import douyin_reservation_logic as drl
|
||||
|
||||
with patch.object(
|
||||
drl, "process_reservation_attend_window", return_value=True
|
||||
) as fake_attend:
|
||||
fudai_run_blocks.execute_reservation_attend_or_cleanup(
|
||||
a,
|
||||
needswitch_cfg=True,
|
||||
focus_ref=[123.0],
|
||||
after_action_ref=[True],
|
||||
swipe_down_back_callback=MagicMock(),
|
||||
seek_reservation_room_callback=MagicMock(),
|
||||
sort_reservations_callback=MagicMock(),
|
||||
write_state_callback=MagicMock(),
|
||||
)
|
||||
fake_attend.assert_called_once()
|
||||
# ref 值同步回写
|
||||
assert a.reservation_focus_until_ts == 123.0
|
||||
assert a.reservation_after_action_pending is True
|
||||
# preempted=True 时设置标志
|
||||
assert a._current_attending_reservation_fudai is True
|
||||
|
||||
|
||||
def test_execute_reservation_single_room_calls_cleanup():
|
||||
"""needswitch=False 时只调用 cleanup_expired_reservations"""
|
||||
a = _make_reservation_analyser()
|
||||
from douyin.reservation import douyin_reservation_logic as drl
|
||||
|
||||
write_cb = MagicMock()
|
||||
with patch.object(drl, "cleanup_expired_reservations") as fake_cleanup, patch.object(
|
||||
drl, "process_reservation_attend_window"
|
||||
) as fake_attend:
|
||||
fudai_run_blocks.execute_reservation_attend_or_cleanup(
|
||||
a,
|
||||
needswitch_cfg=False,
|
||||
focus_ref=[0.0],
|
||||
after_action_ref=[False],
|
||||
swipe_down_back_callback=MagicMock(),
|
||||
seek_reservation_room_callback=MagicMock(),
|
||||
sort_reservations_callback=MagicMock(),
|
||||
write_state_callback=write_cb,
|
||||
)
|
||||
fake_cleanup.assert_called_once_with(a, a.reservations, write_cb)
|
||||
fake_attend.assert_not_called()
|
||||
|
||||
|
||||
def test_execute_reservation_preempted_false_does_not_set_flag():
|
||||
"""preempted=False 时不应设置 _current_attending_reservation_fudai=True"""
|
||||
a = _make_reservation_analyser()
|
||||
from douyin.reservation import douyin_reservation_logic as drl
|
||||
|
||||
with patch.object(drl, "process_reservation_attend_window", return_value=False):
|
||||
fudai_run_blocks.execute_reservation_attend_or_cleanup(
|
||||
a,
|
||||
needswitch_cfg=True,
|
||||
focus_ref=[0.0],
|
||||
after_action_ref=[False],
|
||||
swipe_down_back_callback=MagicMock(),
|
||||
seek_reservation_room_callback=MagicMock(),
|
||||
sort_reservations_callback=MagicMock(),
|
||||
write_state_callback=MagicMock(),
|
||||
)
|
||||
assert a._current_attending_reservation_fudai is False
|
||||
|
||||
|
||||
def test_execute_reservation_single_room_swallows_cleanup_exception():
|
||||
"""单间模式 cleanup 抛错也不应崩"""
|
||||
a = _make_reservation_analyser()
|
||||
from douyin.reservation import douyin_reservation_logic as drl
|
||||
|
||||
with patch.object(
|
||||
drl, "cleanup_expired_reservations", side_effect=RuntimeError("boom")
|
||||
):
|
||||
fudai_run_blocks.execute_reservation_attend_or_cleanup(
|
||||
a,
|
||||
needswitch_cfg=False,
|
||||
focus_ref=[0.0],
|
||||
after_action_ref=[False],
|
||||
swipe_down_back_callback=MagicMock(),
|
||||
seek_reservation_room_callback=MagicMock(),
|
||||
sort_reservations_callback=MagicMock(),
|
||||
write_state_callback=MagicMock(),
|
||||
)
|
||||
# 应记录失败日志
|
||||
failure = [
|
||||
c for c in a._log_kv.call_args_list
|
||||
if len(c.args) >= 2 and "单间模式下清理过期预约时出错" in c.args[1]
|
||||
]
|
||||
assert len(failure) == 1
|
||||
Reference in new issue
Block a user