How to test if Oban job is retried or attempted upon failture

How do I wirte test to check if job is retried for Oban worker?

# This job will be failed
assert {:error, result} = perform_job(MyWorker, wrong_args)

I can check whether job failed or not
But How can I check if failed job is retried?

# Failed job
my_job = MyWorker.new(wrong_args) |> Oban.insert!()
assert %{failture: 1} = Oban.drain_queue(queue: :my_job)

# How do I check `job.attempts` value?
assert my_job.attempts == 1

And 1 more thing.
If I set with_safety: false

assert %{failture: 10} = Oban.drain_queue(queue: :my_job, with_safety: false)

Why number in failure is changing randomly?

Retrying failures is handled by Oban and not something you need to unit test in your application.

The with_safety flag controls whether errors bubble up into your tests. The number of failures (not failture) depends on how many jobs were drained. If you’re not using the sandbox, or other jobs were inserted during the same test run, then you could get more than 1 job drained.

1 Like