test: the app's clock never goes back in the test suite
The docker VM the suite runs in steps its wall clock back now and then (measured: 2 steps in 20 s, up to 0.5 s), so rows a test made one after another could read out of order (a files list sorted newest first, a scheduler's last runs). Every app module that took datetime from the datetime module reads a clock that never repeats or goes back; the scheduler test's own fixture for this goes.
This commit is contained in:
@@ -22,31 +22,6 @@ 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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user