Search code examples
ruby-on-railsrubytestingsidekiqminitest

Testing Sidekiq's retry queue


We have a fairly mission-critical component that clears out the Sidekiq retry queue for a certain item if certain criteria are met. Sidekiq's testing API provided no way to add to the retry queue, so I we had to come up with something custom.


Solution

  • To add to the retry queue:

      def add_retry(object: @object, klass: 'HardWorker', at: Time.now.to_f)
        payload = Sidekiq.dump_json(
          class: klass,
          args: [object.id, object.class, 'sidekiq_retry_test'],
          queue: 'user_integration',
          jid: rand(1..10000),
          retry_count: 20,
          failed_at: Time.now.to_f
        )
    
        Sidekiq.redis do |conn|
          conn.zadd('retry', at.to_s, payload)
        end
      end
    

    You can change anything inside the payload hash. You might want to specify the queue, or add a more robust solution to pick a job id. This is just what worked for us.

    You will need to clean up the retry queue after the fact. You should do this within a teardown or after(:all) block.

      def cleanup_retry_queue
        Sidekiq::RetrySet.new.each do |job|
          job.delete if job.args[2] == 'sidekiq_retry_test'
        end
      end
    

    Remember, this will actually add to the queue, so be careful. Only use this if you have a testable component that relies on retry queues. Don't use this to test Sidekiq's queuing functionality itself.