fix: 平台丢掉过快心跳时不再把课程判成失败
This commit is contained in:
@@ -25,6 +25,7 @@ from euniv.autostudy import (
|
||||
_set_user_status,
|
||||
_set_worker_status,
|
||||
_Stall,
|
||||
_pace_after,
|
||||
_study_document,
|
||||
_study_video,
|
||||
autostudy_status,
|
||||
@@ -139,6 +140,108 @@ def test_stall_gives_up_after_five_minutes_without_progress() -> None:
|
||||
assert credited.stuck(10.41, 30, 5) is False
|
||||
|
||||
|
||||
def test_rejected_heartbeat_waits_for_platform_chunk_instead_of_failing() -> None:
|
||||
stall = _Stall()
|
||||
for offset in range(12):
|
||||
landed, give_up, pause = _pace_after(
|
||||
stall, 16.67, 16.67, 2, 2, {},
|
||||
video=True, step=30, interval=1, now=1000 + offset,
|
||||
)
|
||||
assert landed is False
|
||||
assert give_up is False
|
||||
assert pause == 30
|
||||
landed, give_up, pause = _pace_after(
|
||||
stall, 16.67, 25, 2, 3, {"studySchedule": 25},
|
||||
video=True, step=30, interval=1, now=1400,
|
||||
)
|
||||
assert landed is True
|
||||
assert give_up is False
|
||||
assert pause == 1
|
||||
landed, give_up, pause = _pace_after(
|
||||
stall, 25, 25, 3, 3, {},
|
||||
video=True, step=30, interval=1, now=1401,
|
||||
)
|
||||
assert give_up is False
|
||||
assert pause == 30
|
||||
assert stall.quiet(1701) is True
|
||||
|
||||
|
||||
def test_accepted_but_flat_heartbeat_still_gives_up() -> None:
|
||||
stall = _Stall()
|
||||
for _ in range(9):
|
||||
landed, give_up, pause = _pace_after(
|
||||
stall, 10, 10, 1, 1, {"studySchedule": 10},
|
||||
video=True, step=30, interval=1, now=1000,
|
||||
)
|
||||
assert landed is True
|
||||
assert give_up is False
|
||||
assert pause == 1
|
||||
landed, give_up, _pause = _pace_after(
|
||||
stall, 10, 10, 1, 1, {"studySchedule": 10},
|
||||
video=True, step=30, interval=1, now=1000,
|
||||
)
|
||||
assert landed is True
|
||||
assert give_up is True
|
||||
|
||||
|
||||
def test_fast_video_survives_a_burst_of_empty_heartbeats(monkeypatch) -> None:
|
||||
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda *_args: None)
|
||||
waits: list[int] = []
|
||||
|
||||
async def capture_wait(_worker, interval, _stop, _hours, _progress) -> bool:
|
||||
waits.append(interval)
|
||||
return False
|
||||
|
||||
monkeypatch.setattr("euniv.autostudy._heartbeat_wait", capture_wait)
|
||||
|
||||
class Session:
|
||||
def __init__(self) -> None:
|
||||
self.submits = 0
|
||||
|
||||
async def update_progress(self, _kid: str, _pkg: str) -> dict:
|
||||
if self.submits >= 10:
|
||||
return {
|
||||
"studyschedule": 100,
|
||||
"status": "Completed",
|
||||
"actualstudyhours": 3,
|
||||
"viewSchedule": 300,
|
||||
}
|
||||
return {
|
||||
"studyschedule": 16.67,
|
||||
"status": "Studying",
|
||||
"actualstudyhours": 2,
|
||||
"viewSchedule": 89,
|
||||
}
|
||||
|
||||
async def submit_video_heartbeat(self, _kid: str, _pkg: str, _study_time: int, _view: int) -> dict:
|
||||
self.submits += 1
|
||||
if self.submits >= 10:
|
||||
return {"studySchedule": 100}
|
||||
return {}
|
||||
|
||||
async def month_hours_text(self) -> str:
|
||||
return "10"
|
||||
|
||||
async def main() -> None:
|
||||
session = Session()
|
||||
watcher = HoursWatcher(session, "u-burst", 600, asyncio.Event())
|
||||
await _study_video(
|
||||
session, "kid", "", 1, asyncio.Event(), watcher,
|
||||
study_time=30,
|
||||
snapshot={
|
||||
"studyschedule": 16.67,
|
||||
"status": "Studying",
|
||||
"actualstudyhours": 2,
|
||||
"viewSchedule": 89,
|
||||
},
|
||||
video_cap=720,
|
||||
)
|
||||
assert session.submits == 10
|
||||
assert waits == [30] * 9
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
def test_jitter_never_sends_early() -> None:
|
||||
assert _jitter(0) == 1
|
||||
assert _jitter(-1) == 1
|
||||
|
||||
Reference in New Issue
Block a user