Testing scheduled Oban jobs

I’m currently trying to integration test that a scheduled Oban job is run after said time. Lets just say 2 seconds. But even if i wait 2 seconds, the job is still in “scheduled” state.

I can manually run the job, but the defeats the purpose of the integration test.

I’m trying to :timer.sleep(5000) to give time for the job to run and then I check on the effects of the job. I can see that the job is still enqueued with all_enqueued\1.

Personally, I’d argue that checking if it’s enqueued for the future is enough, otherwise you’re testing functionality that has probably been thoroughly tested in the library itself

4 Likes

true, I can also make two tests.

  1. Check that the job is enqueued properly with the right scheduled_at.
  2. Check the effects of performing the job are correct.

There are a few ways to go about this:

  1. You can make two tests, one that’s integration and uses assert_enqueued while another unit tests with perform_job/3.
  2. Keep it an integration test and use assert_enqueued followed by Oban.drain_queue to run the jobs.
  3. Wrap the insertion with with_testing_mode(:inline, fn -> ... to have it run inline rather than hitting the database.

The various approaches are discussed in the testing guides, and there’s also a talk on Oban testing from a few years ago.

2 Likes