2012-03-17 21:22:56 -04:00
|
|
|
require 'helper'
|
2012-05-25 23:21:42 -04:00
|
|
|
require 'sidekiq/scheduled'
|
2012-03-17 21:22:56 -04:00
|
|
|
require 'sidekiq/middleware/server/retry_jobs'
|
|
|
|
|
|
|
|
class TestRetry < MiniTest::Unit::TestCase
|
|
|
|
describe 'middleware' do
|
|
|
|
before do
|
|
|
|
@redis = MiniTest::Mock.new
|
|
|
|
# Ugh, this is terrible.
|
|
|
|
Sidekiq.instance_variable_set(:@redis, @redis)
|
|
|
|
|
|
|
|
def @redis.with; yield self; end
|
|
|
|
end
|
|
|
|
|
2012-04-01 22:53:45 -04:00
|
|
|
it 'allows disabling retry' do
|
|
|
|
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => false }
|
|
|
|
msg2 = msg.dup
|
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg2, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal msg, msg2
|
|
|
|
end
|
|
|
|
|
2012-11-10 00:18:02 -05:00
|
|
|
it 'allows a numeric retry' do
|
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
|
|
|
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => 2 }
|
|
|
|
msg2 = msg.dup
|
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg2, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
msg2.delete('failed_at')
|
|
|
|
assert_equal({"class"=>"Bob", "args"=>[1, 2, "foo"], "retry"=>2, "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "retry_count"=>0}, msg2)
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-04-27 23:25:46 -04:00
|
|
|
it 'saves backtraces' do
|
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
|
|
|
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true, 'backtrace' => true }
|
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
c = nil
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
c = caller(0); raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert msg["error_backtrace"]
|
2012-11-01 19:08:27 -04:00
|
|
|
assert_equal c[0], msg["error_backtrace"][0]
|
2012-11-10 00:18:02 -05:00
|
|
|
@redis.verify
|
2012-04-27 23:25:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves partial backtraces' do
|
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
|
|
|
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true, 'backtrace' => 3 }
|
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
c = nil
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
c = caller(0)[0..3]; raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert msg["error_backtrace"]
|
|
|
|
assert_equal c, msg["error_backtrace"]
|
|
|
|
end
|
|
|
|
|
2012-03-17 21:22:56 -04:00
|
|
|
it 'handles a new failed message' do
|
2012-03-18 02:04:31 -04:00
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
2012-04-01 22:53:45 -04:00
|
|
|
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true }
|
2012-03-17 21:22:56 -04:00
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 'default', msg["queue"]
|
|
|
|
assert_equal 'kerblammo!', msg["error_message"]
|
|
|
|
assert_equal 'RuntimeError', msg["error_class"]
|
|
|
|
assert_equal 0, msg["retry_count"]
|
2012-04-27 23:25:46 -04:00
|
|
|
refute msg["error_backtrace"]
|
2012-03-17 21:22:56 -04:00
|
|
|
assert msg["failed_at"]
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2013-01-15 20:28:52 -05:00
|
|
|
it 'allows a retry queue' do
|
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
|
|
|
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true, 'retry_queue' => 'retry' }
|
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 'retry', msg["queue"]
|
|
|
|
assert_equal 'kerblammo!', msg["error_message"]
|
|
|
|
assert_equal 'RuntimeError', msg["error_class"]
|
|
|
|
assert_equal 0, msg["retry_count"]
|
|
|
|
refute msg["error_backtrace"]
|
|
|
|
assert msg["failed_at"]
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-03-17 21:22:56 -04:00
|
|
|
it 'handles a recurring failed message' do
|
2012-03-18 02:04:31 -04:00
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
2012-03-17 21:22:56 -04:00
|
|
|
now = Time.now.utc
|
2012-04-01 22:53:45 -04:00
|
|
|
msg = {"class"=>"Bob", "args"=>[1, 2, "foo"], 'retry' => true, "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>now, "retry_count"=>10}
|
2012-03-17 21:22:56 -04:00
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 'default', msg["queue"]
|
|
|
|
assert_equal 'kerblammo!', msg["error_message"]
|
|
|
|
assert_equal 'RuntimeError', msg["error_class"]
|
|
|
|
assert_equal 11, msg["retry_count"]
|
|
|
|
assert msg["failed_at"]
|
|
|
|
@redis.verify
|
|
|
|
end
|
2012-03-18 02:04:31 -04:00
|
|
|
|
2012-10-17 18:51:26 -04:00
|
|
|
it 'handles a recurring failed message before reaching user-specifed max' do
|
|
|
|
@redis.expect :zadd, 1, ['retry', String, String]
|
|
|
|
now = Time.now.utc
|
2012-11-10 00:18:02 -05:00
|
|
|
msg = {"class"=>"Bob", "args"=>[1, 2, "foo"], 'retry' => 10, "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>now, "retry_count"=>8}
|
2012-10-17 18:51:26 -04:00
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 'default', msg["queue"]
|
|
|
|
assert_equal 'kerblammo!', msg["error_message"]
|
|
|
|
assert_equal 'RuntimeError', msg["error_class"]
|
2012-11-10 00:18:02 -05:00
|
|
|
assert_equal 9, msg["retry_count"]
|
2012-10-17 18:51:26 -04:00
|
|
|
assert msg["failed_at"]
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws away old messages after too many retries (using the default)' do
|
2012-03-18 02:04:31 -04:00
|
|
|
now = Time.now.utc
|
2012-10-16 09:55:04 -04:00
|
|
|
msg = {"class"=>"Bob", "args"=>[1, 2, "foo"], "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>now, "retry"=>true, "retry_count"=>25}
|
|
|
|
@redis.expect :zadd, 1, [ 'retry', String, String ]
|
2012-03-18 02:04:31 -04:00
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
2012-10-16 09:55:04 -04:00
|
|
|
# MiniTest can't assert that a method call did NOT happen!?
|
|
|
|
assert_raises(MockExpectationError) { @redis.verify }
|
2012-03-18 02:04:31 -04:00
|
|
|
end
|
2012-10-17 18:51:26 -04:00
|
|
|
|
|
|
|
it 'throws away old messages after too many retries (using user-specified max)' do
|
|
|
|
now = Time.now.utc
|
|
|
|
msg = {"class"=>"Bob", "args"=>[1, 2, "foo"], "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>now, "retry"=>3, "retry_count"=>3}
|
|
|
|
@redis.expect :zadd, 1, [ 'retry', String, String ]
|
|
|
|
handler = Sidekiq::Middleware::Server::RetryJobs.new
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise "kerblammo!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# MiniTest can't assert that a method call did NOT happen!?
|
|
|
|
assert_raises(MockExpectationError) { @redis.verify }
|
|
|
|
end
|
2013-03-18 17:20:28 -04:00
|
|
|
|
2013-03-21 14:16:07 -04:00
|
|
|
describe "retry exhaustion" do
|
|
|
|
let(:worker){ MiniTest::Mock.new }
|
|
|
|
let(:handler){ Sidekiq::Middleware::Server::RetryJobs.new }
|
|
|
|
let(:msg){ {"class"=>"Bob", "args"=>[1, 2, "foo"], "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>Time.now.utc, "retry"=>3, "retry_count"=>3} }
|
|
|
|
|
|
|
|
it 'calls worker retries_exhausted after too many retries' do
|
|
|
|
worker.expect(:retries_exhausted, true, [1,2,3])
|
|
|
|
task_misbehaving_worker
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles and logs retries_exhausted failures gracefully (drops them)' do
|
|
|
|
def worker.retries_exhausted(*args)
|
|
|
|
raise 'bam!'
|
|
|
|
end
|
|
|
|
|
|
|
|
e = task_misbehaving_worker
|
|
|
|
assert_equal e.message, "kerblammo!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def task_misbehaving_worker
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
handler.call('', msg, 'default') do
|
|
|
|
raise 'kerblammo!'
|
|
|
|
end
|
2013-03-18 17:20:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-17 21:22:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'poller' do
|
|
|
|
before do
|
|
|
|
@redis = MiniTest::Mock.new
|
|
|
|
Sidekiq.instance_variable_set(:@redis, @redis)
|
|
|
|
|
|
|
|
def @redis.with; yield self; end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should poll like a bad mother...SHUT YO MOUTH' do
|
2012-04-22 17:02:35 -04:00
|
|
|
fake_msg = Sidekiq.dump_json({ 'class' => 'Bob', 'args' => [1,2], 'queue' => 'someq' })
|
2012-03-18 02:04:31 -04:00
|
|
|
@redis.expect :multi, [[fake_msg], 1], []
|
2012-05-26 21:38:55 -04:00
|
|
|
@redis.expect :multi, [[], nil], []
|
2012-07-07 01:06:11 -04:00
|
|
|
@redis.expect :multi, [[], nil], []
|
|
|
|
@redis.expect :multi, [[], nil], []
|
2012-03-18 02:04:31 -04:00
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
inst = Sidekiq::Scheduled::Poller.new
|
2012-03-17 21:22:56 -04:00
|
|
|
inst.poll
|
2012-03-18 02:04:31 -04:00
|
|
|
|
2012-03-18 00:36:33 -04:00
|
|
|
@redis.verify
|
2012-03-17 21:22:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|