Testing ActiveJobs with retry_on attempts: :unlimited

How do you folks test failure cases in unlimited-retry jobs?

For example my job has

retry_on StandardError, wait: :polynomially_longer, attempts: :unlimited

def perform(model)
  # work code
rescue => error
  # error handling code
  raise
end

I’d like to test “error handling code”. So I make “work code” fail, enqueue the job, and call perform_enqueued_jobs. This creates an infinite retry loop.

That’s actually surprising, I was expecting the retry to be enqueued, but not run until I call perform_enqueued_jobs again. Wondering if maybe it’s a bug.

Do ya’ll know if it’s intentional, and do you have ways to test it?

For anyone finding this:

Looks like if I don’t use perform_enqueued_jobs and just run perform_now it will not run repeatedly, but also not propagate the error to the test. So this does seem to work:

assert_enqueued_jobs 1 do # retry is enqueued
  MyJob.perform_now(model)
end

I don’t know why initially I thought this didn’t work.

1 Like