11

A job in sidekiq, upon exception, will be put on the retry queue.

Because of that, and because the task is run asynchronously, MyWorker.perform_async(...) can never throw an exception generated in the task code.

In testing, however, an exception that occurs in the task does not cause the task to be put in the retry queue. The exception bubbles up out of perform_async.

So what happens in tests is something that cannot possible occur when running the code.

What, then, is the best way to test code that triggers jobs that can fail and be put on the retry queue?

Note that the following seems to have no effect in testing:
Sidekiq.default_worker_options = { :retry => true}

Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129

1 Answers1

6

It's designed to be hard because you shouldn't do it. You should not be testing Sidekiq functionality. Test your own code, not features provided by other libraries/frameworks.

Mike Perham
  • 21,300
  • 6
  • 59
  • 61
  • 1
    I'm not testing sidekiq. My code does `a=f(x); Worker.perform_async(a); y(a)`. I can't run that code in a test environment with any data that causes the task to fail. Happy to discuss on github and create a pull request if you think it makes sense. If not, it's your project and I thank you for the work. – Mark Bolusmjak Sep 30 '15 at 23:26
  • 1
    That's because you are running `inline`. Use `fake` mode for that test. https://github.com/mperham/sidekiq/wiki/Testing – Mike Perham Sep 30 '15 at 23:48
  • 24
    @MikePerham along the same vein. Without retries in testing, how would you do an end to end test that has a external endpoint where timeouts must be retried until success. The success leads to other actions. This is not testing Sidekiq, but retries are part of the successful flow which would work outside of the testing environment. You could just test that Sidekiq is sent the right info, but then it wouldn't be an end to end test. – Gerry Jul 26 '17 at 07:46
  • 3
    There're multiple ways to define custom error handling with sidekiq which is definitely your code that you want to test. – thisismydesign May 06 '19 at 09:25