4

How to hit Sidekiq's death_handlers from RSpec?

config/initializers/sidekiq.rb

Sidekiq.configure_server do |config|
  # ...

  config.death_handlers << lambda { |job, ex|
    throw "Retries exhausted on #{job['class']} #{job['jid']}: #{ex.inspect}."
  }
end

Tried the recommendations for ActiveJob's retry_on but no success.

Also tried within_sidekiq_retries_exhausted_block of rspec-sidekiq but that failed saying the job class doesn't have this method.

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
  • 1
    What about extracting the handler logic in a separate class/module and test it in isolation? In my opinion, you shouldn't test death_handlers machinery itself - it is something that belongs to 3rd party library and is tested there... – Konstantin Strukov Aug 31 '21 at 12:35
  • 1
    The main reason would be to make sure this works in the correct context, for example, that `job` has `class` and `jid` fields. If I pass these arguments myself in a test that would defeat the purpose. I can look up how `job` looks like now but it can also change over time and my tests wouldn't catch that. – thisismydesign Aug 31 '21 at 13:29
  • @thismydesign Not sure I understand. The `job` that is being sent to the death handler is just a deserialized job definition as it was sent to sidekiq. So if you want to test its structure for some reason you can do it in other places (where the job was scheduled for example) - if the structure matches your expectation there it will match them on death handler call too, inevitably. – Konstantin Strukov Aug 31 '21 at 15:42
  • Yes, but I have to know that and then create the deserialized job by hand. I would also be unsure if that still works if the internals of a job or sidekiq change. Whereas it would be much more straightforward to just trigger a death handler on a job if that was possible. Hence the question. – thisismydesign Aug 31 '21 at 15:56

1 Answers1

0

To test death_handler behavior, I recommend failing a job locally, and checking your standard output to see if your death_handler proc runs as you expect.

You can do this by running Sidekiq locally, and then simultaneously running a job (perhaps via your rails console) that you know will fail (you could raise an exception in the perform method). You should see output from the death_handler proc in your rails console window.

Regarding your statement about:

Also tried within_sidekiq_retries_exhausted_block of rspec-sidekiq but that failed saying the job class doesn't have this method.

This is because ActiveJob does not support the sidekiq_retries_exhausted block. Therefore, within_sidekiq_retries_exhausted_block, a method designed to test the sidekiq_retries_exhausted block, will not test anything.