test(scheduler): runs started in a row start in that order

The scheduler tests open several runs within microseconds and read them back newest first, by start time. A container's clock on a VM can step back between two readings, which sorted the third of three runs before the second; the tests now run on a clock that never repeats or goes back.
This commit is contained in:
2026-11-13 00:31:57 +01:00
parent fe6af1445f
commit 5bf13c8701
+25
View File
@@ -22,6 +22,31 @@ from app.runstatus import RunStatus, RunTrigger
_JOB = "unit_test_claim" # not a real job id — exercises the primitive without touching live jobs
class _StrictlyLater(datetime):
"""A clock that never repeats or goes back: each reading at least a microsecond after the last."""
last: datetime | None = None
clock: type[datetime] = datetime
@classmethod
def now(cls, tz=None):
t = cls.clock.now(tz)
if cls.last is not None and t <= cls.last:
t = cls.last + timedelta(microseconds=1)
cls.last = t
return t
@pytest.fixture(autouse=True)
def _runs_start_in_order(monkeypatch):
"""Runs started in a row start in that order. These tests open several runs within microseconds
and read them back newest first, by start time; the wall clock of a container on a VM can step
back between two readings (seen: the third of three runs sorted before the second)."""
_StrictlyLater.last = None
_StrictlyLater.clock = jobruns.datetime
monkeypatch.setattr(jobruns, "datetime", _StrictlyLater)
def _release():
scheduler._record(_JOB, running=False)