forked from sidekiq/sidekiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_retry.rb
125 lines (111 loc) · 4.05 KB
/
test_retry.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'helper'
require 'sidekiq/scheduled'
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
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
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"]
assert_equal c, msg["error_backtrace"]
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
it 'handles a new failed message' do
@redis.expect :zadd, 1, ['retry', String, String]
msg = { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => true }
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"]
refute msg["error_backtrace"]
assert msg["failed_at"]
@redis.verify
end
it 'handles a recurring failed message' do
@redis.expect :zadd, 1, ['retry', String, String]
now = Time.now.utc
msg = {"class"=>"Bob", "args"=>[1, 2, "foo"], 'retry' => true, "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>now, "retry_count"=>10}
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
it 'throws away old messages after too many retries' do
now = Time.now.utc
msg = {"class"=>"Bob", "args"=>[1, 2, "foo"], "queue"=>"default", "error_message"=>"kerblammo!", "error_class"=>"RuntimeError", "failed_at"=>now, "retry_count"=>25}
handler = Sidekiq::Middleware::Server::RetryJobs.new
assert_raises RuntimeError do
handler.call('', msg, 'default') do
raise "kerblammo!"
end
end
@redis.verify
end
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
fake_msg = Sidekiq.dump_json({ 'class' => 'Bob', 'args' => [1,2], 'queue' => 'someq' })
@redis.expect :multi, [[fake_msg], 1], []
@redis.expect :multi, [[], nil], []
@redis.expect :rpush, 1, ['queue:someq', fake_msg]
inst = Sidekiq::Scheduled::Poller.new
inst.poll
@redis.verify
end
end
end