Expect-testing a job scheduler
A critical piece of a job scheduler is the algorithm that takes an abstract schedule ("run every hour from 9am to 5pm") and determines the concrete times when the job should run (2025-11-21 09:00, 2025-11-21 10:00, …).
A good scheduling algorithm must be robust to job overruns and scheduler crashes, and sensitive to complications like uneven month lengths and daylight saving time transitions. That is to say, a good scheduling algorithm must be well-tested.
Suppose the scheduler exposes a function get_next_scheduled_time(schedule, now). The kind of test I would like to write calls this function in a loop and produces a list of concrete times, something like:
self.assertEqual(timetable(schedule, start_time), """\
2025-11-22 00:00:00: next at 2025-11-22 04:00:00
2025-11-22 04:01:00: next at 2025-11-22 05:00:00
2025-11-22 05:01:00: next at 2025-11-22 06:00:00
""")
It would be possible though tedious to write such a test in a traditional unit-testing framework. Expect tests make it more tractable: instead of writing out the expected result by hand, you can have the test runner produce the output for you. The steps are:
- Write the test inputs and leave the output blank.
- Run the test and accept the corrected output.
- Examine the output. If it is wrong, fix the bug and return to step 2.
My scheduling algorithm tests use the expecttest library for Python.
Expect tests are not just about convenience: they also free you to include as much diagnostic information as you like in the test output. In the example above, the test displays not just the run time itself, but also the time at which it was calculated.