refactor(reservation): 拆出时间窗口早返回 + 队列满标志检查(1163→1118,-45 行)
- check_queue_full_immediate_attend:检查队列满立即参与模式 - handle_time_window_early_returns:时间窗口未到/已错过两种早返回(含 cleanup 调度) 新增 7 个单元测试,1289 → 1296 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
daddabd8cf
commit
f46f9dc23e
3 files changed
+187
-61
No files matched your search
@@ -24,10 +24,12 @@ from .helpers import (
|
||||
)
|
||||
from .attend_window_helpers import (
|
||||
check_already_participated,
|
||||
check_queue_full_immediate_attend,
|
||||
check_total_timeout,
|
||||
check_waiting_for_reservation_lottery,
|
||||
compute_dynamic_ahead,
|
||||
discard_invalid_future_ts,
|
||||
handle_time_window_early_returns,
|
||||
log_attend_window_check_details,
|
||||
log_target_reservation_details,
|
||||
maybe_promote_locked_reservation,
|
||||
@@ -335,68 +337,21 @@ def process_reservation_attend_window(
|
||||
pass
|
||||
|
||||
# 检查是否是队列满后立即参与的情况(使用标志判断,而不是锁定期)
|
||||
is_queue_full_immediate_attend = False
|
||||
try:
|
||||
# 检查队列满等待标志
|
||||
queue_full_waiting = bool(
|
||||
getattr(analyser, "queue_full_waiting_at_earliest", False) or False
|
||||
)
|
||||
if queue_full_waiting:
|
||||
is_queue_full_immediate_attend = True
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 检测到队列满后立即参与模式(通过标志判断),跳过时间窗口检查,直接参与",
|
||||
queue_full_waiting=queue_full_waiting,
|
||||
target_future_ts=int(ft),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
is_queue_full_immediate_attend = check_queue_full_immediate_attend(analyser)
|
||||
|
||||
if not is_queue_full_immediate_attend and now_ts < ft - dynamic_ahead:
|
||||
# 还没到窗口(非队列满立即参与模式)
|
||||
try:
|
||||
from datetime import datetime
|
||||
|
||||
time_window_start_ts = ft - dynamic_ahead
|
||||
time_window_start_str = (
|
||||
datetime.fromtimestamp(time_window_start_ts).strftime(
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
if time_window_start_ts > 0
|
||||
else "无效"
|
||||
)
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 时间窗口未到,跳过回退",
|
||||
now_ts=int(now_ts),
|
||||
now_str=datetime.fromtimestamp(now_ts).strftime("%Y-%m-%d %H:%M:%S"),
|
||||
time_window_start=int(time_window_start_ts),
|
||||
time_window_start_str=time_window_start_str,
|
||||
remain_seconds=int((ft - dynamic_ahead) - now_ts),
|
||||
target_future_ts=int(ft),
|
||||
dynamic_ahead=dynamic_ahead,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
if now_ts > ft + max(0.0, poll_before):
|
||||
# 已明显错过开奖时间,交给统一清理逻辑
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 已明显错过开奖时间,清理过期预约",
|
||||
future_ts=int(ft),
|
||||
now_ts=int(now_ts),
|
||||
poll_before=poll_before,
|
||||
missed_seconds=int(now_ts - (ft + max(0.0, poll_before))),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
cleanup_expired_reservations(analyser, reservations, write_state_callback)
|
||||
# 检查时间窗口的两种早返回:未到 / 已错过
|
||||
early_return = handle_time_window_early_returns(
|
||||
analyser,
|
||||
reservations,
|
||||
now_ts,
|
||||
ft,
|
||||
dynamic_ahead,
|
||||
poll_before,
|
||||
is_queue_full_immediate_attend,
|
||||
cleanup_expired_reservations,
|
||||
write_state_callback,
|
||||
)
|
||||
if early_return is not None:
|
||||
return False
|
||||
|
||||
# 进入预约参与窗口:执行回退动作
|
||||
|
||||
@@ -11,6 +11,93 @@ from datetime import datetime
|
||||
from typing import Any, Dict, Tuple
|
||||
|
||||
|
||||
def handle_time_window_early_returns(
|
||||
analyser: Any,
|
||||
reservations: list,
|
||||
now_ts: float,
|
||||
ft: float,
|
||||
dynamic_ahead: int,
|
||||
poll_before: float,
|
||||
is_queue_full_immediate_attend: bool,
|
||||
cleanup_expired_callback: Any,
|
||||
write_state_callback: Any,
|
||||
) -> bool | None:
|
||||
"""检查时间窗口的两种早返回:未到 / 已错过。
|
||||
|
||||
返回:
|
||||
- True:已错过窗口(已清理),主流程 return False
|
||||
- False:未到窗口,主流程 return False
|
||||
- None:在窗口内,继续执行回退动作
|
||||
"""
|
||||
# 时间窗口未到(非队列满立即参与模式)
|
||||
if not is_queue_full_immediate_attend and now_ts < ft - dynamic_ahead:
|
||||
try:
|
||||
time_window_start_ts = ft - dynamic_ahead
|
||||
time_window_start_str = (
|
||||
datetime.fromtimestamp(time_window_start_ts).strftime(
|
||||
"%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
if time_window_start_ts > 0
|
||||
else "无效"
|
||||
)
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 时间窗口未到,跳过回退",
|
||||
now_ts=int(now_ts),
|
||||
now_str=datetime.fromtimestamp(now_ts).strftime("%Y-%m-%d %H:%M:%S"),
|
||||
time_window_start=int(time_window_start_ts),
|
||||
time_window_start_str=time_window_start_str,
|
||||
remain_seconds=int((ft - dynamic_ahead) - now_ts),
|
||||
target_future_ts=int(ft),
|
||||
dynamic_ahead=dynamic_ahead,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
# 已明显错过开奖时间,交给统一清理逻辑
|
||||
if now_ts > ft + max(0.0, poll_before):
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 已明显错过开奖时间,清理过期预约",
|
||||
future_ts=int(ft),
|
||||
now_ts=int(now_ts),
|
||||
poll_before=poll_before,
|
||||
missed_seconds=int(now_ts - (ft + max(0.0, poll_before))),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
cleanup_expired_callback(analyser, reservations, write_state_callback)
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def check_queue_full_immediate_attend(analyser: Any) -> bool:
|
||||
"""检查是否处于队列满后立即参与模式(含日志)。"""
|
||||
try:
|
||||
queue_full_waiting = bool(
|
||||
getattr(analyser, "queue_full_waiting_at_earliest", False) or False
|
||||
)
|
||||
if queue_full_waiting:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 检测到队列满后立即参与模式(通过标志判断),跳过时间窗口检查,直接参与",
|
||||
queue_full_waiting=queue_full_waiting,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def sort_reservations_with_logging(
|
||||
analyser: Any, reservations: list, _sort_inplace_callback: Any
|
||||
) -> None:
|
||||
|
||||
@@ -411,3 +411,87 @@ def test_check_timeout_swallows_exception():
|
||||
# 应不抛错
|
||||
result = h.check_total_timeout(a, past, total_timeout_s=10.0, stage="测试")
|
||||
assert result is True
|
||||
|
||||
|
||||
# ---- check_queue_full_immediate_attend ---- #
|
||||
|
||||
def test_queue_full_immediate_attend_true():
|
||||
a = MagicMock()
|
||||
a.queue_full_waiting_at_earliest = True
|
||||
assert h.check_queue_full_immediate_attend(a) is True
|
||||
|
||||
|
||||
def test_queue_full_immediate_attend_false():
|
||||
a = MagicMock()
|
||||
a.queue_full_waiting_at_earliest = False
|
||||
assert h.check_queue_full_immediate_attend(a) is False
|
||||
|
||||
|
||||
def test_queue_full_immediate_swallows_attr_error():
|
||||
a = MagicMock()
|
||||
type(a).queue_full_waiting_at_earliest = property(
|
||||
lambda _self: (_ for _ in ()).throw(RuntimeError("boom"))
|
||||
)
|
||||
assert h.check_queue_full_immediate_attend(a) is False
|
||||
|
||||
|
||||
# ---- handle_time_window_early_returns ---- #
|
||||
|
||||
def test_time_window_not_yet_arrived_returns_false():
|
||||
a = MagicMock()
|
||||
cleanup = MagicMock()
|
||||
write_cb = MagicMock()
|
||||
now = 1700000000.0
|
||||
ft = now + 500 # 500s 后
|
||||
result = h.handle_time_window_early_returns(
|
||||
a, [], now, ft, dynamic_ahead=100,
|
||||
poll_before=10.0, is_queue_full_immediate_attend=False,
|
||||
cleanup_expired_callback=cleanup, write_state_callback=write_cb,
|
||||
)
|
||||
assert result is False
|
||||
cleanup.assert_not_called()
|
||||
|
||||
|
||||
def test_time_window_missed_calls_cleanup_and_returns_true():
|
||||
a = MagicMock()
|
||||
cleanup = MagicMock()
|
||||
write_cb = MagicMock()
|
||||
now = 1700000000.0
|
||||
ft = now - 1000 # 已过去
|
||||
result = h.handle_time_window_early_returns(
|
||||
a, [{"future_ts": ft}], now, ft, dynamic_ahead=100,
|
||||
poll_before=10.0, is_queue_full_immediate_attend=False,
|
||||
cleanup_expired_callback=cleanup, write_state_callback=write_cb,
|
||||
)
|
||||
assert result is True
|
||||
cleanup.assert_called_once()
|
||||
|
||||
|
||||
def test_time_window_in_range_returns_none():
|
||||
"""在窗口内 → 返回 None 让主流程继续"""
|
||||
a = MagicMock()
|
||||
cleanup = MagicMock()
|
||||
now = 1700000000.0
|
||||
ft = now + 50 # 50s 内
|
||||
result = h.handle_time_window_early_returns(
|
||||
a, [], now, ft, dynamic_ahead=100,
|
||||
poll_before=10.0, is_queue_full_immediate_attend=False,
|
||||
cleanup_expired_callback=cleanup, write_state_callback=MagicMock(),
|
||||
)
|
||||
assert result is None
|
||||
cleanup.assert_not_called()
|
||||
|
||||
|
||||
def test_time_window_queue_full_skips_not_yet_check():
|
||||
"""队列满立即参与模式 → 即使时间未到也不返回 False"""
|
||||
a = MagicMock()
|
||||
cleanup = MagicMock()
|
||||
now = 1700000000.0
|
||||
ft = now + 500 # 远未到
|
||||
result = h.handle_time_window_early_returns(
|
||||
a, [], now, ft, dynamic_ahead=100,
|
||||
poll_before=10.0, is_queue_full_immediate_attend=True,
|
||||
cleanup_expired_callback=cleanup, write_state_callback=MagicMock(),
|
||||
)
|
||||
# 队列满模式下时间未到也算"在窗口内"
|
||||
assert result is None
|
||||
Reference in new issue
Block a user