Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/studyflow/platforms/ucampus/readonly_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ def tick(*, clock=None, reader_factory=UCampusProgressReader) -> dict[str, Any]:
if (type(previous_attempt) not in (int, float)
or previous_attempt < 0 or not float(previous_attempt) < float("inf")):
previous_attempt = 0
if now - float(previous_attempt) < _MIN_INTERVAL_SECONDS:
elapsed = now - float(previous_attempt)
# The persisted timestamp uses wall time across restarts. If Windows
# corrects the clock backwards, do not suppress progress checks until
# the old wall-clock value eventually comes around again.
if 0 <= elapsed < _MIN_INTERVAL_SECONDS:
return public
saved["last_attempt"] = now
_write(saved) # claim before GET: concurrent ticks do not duplicate it
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ucampus_readonly_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,27 @@ def list_tasks(self, course_id, unit_id):
assert not worker.is_alive()
assert monitor.status()["state"] == "verified"
assert len(calls) == 1


def test_clock_rollback_resumes_readonly_polling_without_request_storm(store):
monitor.configure("course-v2:eng+course+1", "U1", True)
calls = []

class Reader:
def list_tasks(self, course_id, unit_id):
calls.append((course_id, unit_id))
return [{"id": "v", "completed": True}]

first = monitor.tick(clock=lambda: 2_000_000, reader_factory=Reader)
assert first["state"] == "verified"
assert len(calls) == 1

# A Windows clock adjustment can make the persisted last_attempt appear
# to be in the future. One fresh GET must be allowed, rather than
# suppressing all reads until the old wall-clock timestamp is reached.
rolled_back = monitor.tick(clock=lambda: 1_999_000, reader_factory=Reader)
assert rolled_back["state"] == "verified"
assert len(calls) == 2

monitor.tick(clock=lambda: 1_999_010, reader_factory=Reader)
assert len(calls) == 2, "ordinary 60-second pacing must still apply"
Loading