Testing result of #sidekiq_retries_exhausted

1,951 views
Skip to first unread message

Kevin Ross

unread,
May 19, 2015, 5:55:56 PM5/19/15
to sid...@googlegroups.com
I have a worker that acts on a Subscription model with a state machine.  On success, it calls Subscription#renew, after retries are exhausted, it calls Subscription#renew_fail.  This is coded, now I just need to test the retries exhausted case.

I'm expecting the following to be succeed:
FailingHardWorker.perform_async(1)
assert_equal 1, FailingHardWorker.jobs.size
FailingHardWorker.drain
assert_equal 1, FailingHardWorker.jobs.size

But it appears that #drain removes the job from the test queue after one execution without retries.  As expected from that behavior, #drain does not execute the #sidekiq_retries_exhausted, so the expected result of failed retries cannot be asserted on my model (which is a critical problem in my case).

How can I execute the job to execute according to it's configuration?  I would like it to retry x times then execute #sidekiq_retries_exhausted.

Mike Perham

unread,
May 19, 2015, 6:16:01 PM5/19/15
to sid...@googlegroups.com
You're writing an integration test and Sidekiq does not provide facilities to do this.  Call renew_fail manually if you want to test your own method.

--
You received this message because you are subscribed to the Google Groups "Sidekiq" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sidekiq+u...@googlegroups.com.
To post to this group, send email to sid...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sidekiq/e40fe8fb-302b-4864-b150-becb5234f135%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mike Perham – CEO, Contributed Systems
Smart, effective open source infrastructure for your apps.

Kevin Ross

unread,
May 19, 2015, 6:20:15 PM5/19/15
to sid...@googlegroups.com
The #renew_fail method is tested and does work.  The fact is that I have ~5 untested lines of code that are critical to the business, and they are located in #sidekiq_retries_exhausted.

It's sounds trivial, but I just found a typo in that code.  I can't really afford to screw up these few lines which simply connect the dots so it's pretty important that I can test the signature, even if I call it directly.

--
Kevin Ross, Founder and CEO
AlienFast | www.alienfast.com

--
You received this message because you are subscribed to a topic in the Google Groups "Sidekiq" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sidekiq/phnSFwkmTt8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sidekiq+u...@googlegroups.com.

To post to this group, send email to sid...@googlegroups.com.

Kevin Ross

unread,
May 19, 2015, 6:22:36 PM5/19/15
to sid...@googlegroups.com
Here is the code, plenty of possibilities to screw it up, and it's critical to the business:

 
   sidekiq_retries_exhausted do |msg|
      subscription_id
= nil
     
begin
        args
= msg['args']
        subscription_id
= args[0]
        subscription
= Subscription.lock.find(subscription_id)
       
Subscription.transaction do
          subscription
.renew_fail!
       
end
     
rescue => e
       
Sidekiq.logger.warn "Retries exhausted: #{msg['class']} failed to mark subscription##{subscription_id} as renew_fail!"
     
end
   
end

Mike Perham

unread,
May 19, 2015, 6:25:51 PM5/19/15
to sid...@googlegroups.com
class MyWorker
  FailureHandler = lambda { |msg|
    # your 5 lines
  }
  sidekiq_retries_exhausted(&FailureHandler)

  def perform(...)
end

Your test:
MyWorker::FailureHandler.call(some_fake_msg)

Mike


For more options, visit https://groups.google.com/d/optout.

Kevin Ross

unread,
May 19, 2015, 8:08:41 PM5/19/15
to sid...@googlegroups.com

Is there a way to derive the msg from the worker instance?

Reply all
Reply to author
Forward
0 new messages