diff --git a/backend/tests/test_scheduler.py b/backend/tests/test_scheduler.py index cab4b7bf..2465ed30 100644 --- a/backend/tests/test_scheduler.py +++ b/backend/tests/test_scheduler.py @@ -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)