-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
reaper_spec.rb
222 lines (181 loc) · 6.75 KB
/
reaper_spec.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# frozen_string_literal: true
RSpec.describe SidekiqUniqueJobs::Orphans::Reaper do
let(:service) { redis { |conn| described_class.new(conn) } }
let(:digest) { "uniquejobs:digest" }
let(:job_id) { "job_id" }
let(:item) { raw_item }
let(:lock) { SidekiqUniqueJobs::Lock.create(digest, job_id, lock_info) }
let(:raw_item) { { "class" => MyUniqueJob, "args" => [], "jid" => job_id, "lock_digest" => digest } }
let(:lock_info) do
{
"job_id" => job_id,
"limit" => 1,
"lock" => :while_executing,
"time" => now_f,
"timeout" => nil,
"ttl" => nil,
"lock_args" => [],
"worker" => "MyUniqueJob",
}
end
before do
# Create the lock key without having a matching digest in a job hash
# (this is needed because we want to setup the test conditions manually)
# (find this to be more sensible defaults)
SidekiqUniqueJobs.disable!
lock
end
after do
SidekiqUniqueJobs.enable!
end
describe ".call" do
subject(:call) { described_class.call(conn) }
let(:conn) { nil }
around do |example|
SidekiqUniqueJobs.use_config(reaper: reaper) do
example.run
end
end
context "when given a connection" do
let(:conn) { instance_spy(ConnectionPool) }
let(:reaper) { :ruby }
let(:reaper_spy) { instance_spy("SidekiqUniqueJobs::Orphans::Reaper") }
before do
allow(reaper_spy).to receive(:call)
allow(described_class).to receive(:new).and_return(reaper_spy)
end
it "calls the reaper with the given connection" do
call
expect(reaper_spy).to have_received(:call)
expect(described_class).to have_received(:new).with(conn)
end
end
shared_examples "deletes orphans" do
context "when scheduled" do
let(:item) { raw_item.merge("at" => Time.now.to_f + 3_600) }
context "without scheduled job" do
it "deletes the digest" do
expect { call }.to change { digests.count }.by(-1)
expect(unique_keys).to match_array([])
end
end
context "with scheduled job" do
before { push_item(item) }
it "keeps the digest" do
expect { call }.not_to change { digests.count }.from(1)
expect(unique_keys).not_to match_array([])
end
end
end
context "when retried" do
let(:item) { raw_item.merge("retry_count" => 2, "failed_at" => now_f) }
context "without job in retry" do
it "deletes the digest" do
expect { call }.to change { digests.count }.by(-1)
expect(unique_keys).to match_array([])
end
end
context "with job in retry" do
before { zadd("retry", Time.now.to_f.to_s, dump_json(item)) }
it "keeps the digest" do
expect { call }.not_to change { digests.count }.from(1)
expect(unique_keys).not_to match_array([])
end
end
end
context "when digest exists in a queue" do
context "without enqueued job" do
it "deletes the digest" do
expect { call }.to change { digests.count }.by(-1)
expect(unique_keys).to match_array([])
end
end
context "with enqueued job" do
before { push_item(item) }
it "keeps the digest" do
expect { call }.not_to change { digests.count }.from(1)
expect(unique_keys).not_to match_array([])
end
end
end
context "when processing" do
context "without job in process" do
it "deletes the digest" do
expect { call }.to change { digests.count }.by(-1)
expect(unique_keys).to match_array([])
end
end
context "with job in process" do
let(:process_key) { "process-id" }
let(:thread_id) { "thread-id" }
let(:worker_key) { "#{process_key}:workers" }
let(:created_at) { (Time.now - reaper_timeout).to_f }
let(:reaper_timeout) { SidekiqUniqueJobs.config.reaper_timeout }
before do
SidekiqUniqueJobs.redis do |conn|
conn.multi do
conn.sadd("processes", process_key)
conn.set(process_key, "bogus")
conn.hset(worker_key, thread_id, dump_json(created_at: created_at, payload: item))
conn.expire(process_key, 60)
conn.expire(worker_key, 60)
end
end
end
context "that matches current digest", sidekiq_ver: ">= 5.0" do
context "and created_at is old" do # rubocop:disable RSpec/NestedGroups
let(:created_at) { (Time.now - (reaper_timeout + 100)).to_f }
it "keeps the digest" do
expect { call }.not_to change { digests.count }.from(1)
expect(unique_keys).not_to match_array([])
end
end
context "and created_at is recent" do # rubocop:disable RSpec/NestedGroups
let(:created_at) { Time.now.to_f }
it "keeps the digest" do
expect { call }.not_to change { digests.count }.from(1)
expect(unique_keys).not_to match_array([])
end
end
end
context "that does not match current digest" do
let(:item) { { "class" => MyUniqueJob, "args" => [], "jid" => job_id, "lock_digest" => "uniquejobs:d2" } }
context "and created_at is old" do # rubocop:disable RSpec/NestedGroups
let(:created_at) { (Time.now - (reaper_timeout + 100)).to_f }
it "deletes the digest" do
expect { call }.to change { digests.count }.by(-1)
expect(unique_keys).to match_array([])
end
end
context "and created_at is recent" do # rubocop:disable RSpec/NestedGroups
let(:created_at) { Time.now.to_f }
it "keeps the digest" do
expect { call }.not_to change { digests.count }.from(1)
expect(unique_keys).not_to match_array([])
end
end
end
end
end
end
context "when config.reaper = :ruby" do
let(:reaper) { :ruby }
it_behaves_like "deletes orphans"
end
context "when config.reaper = :lua" do
let(:reaper) { :lua }
it_behaves_like "deletes orphans"
end
context "when config.reaper = :bogus" do
let(:reaper) { :bogus }
before do
allow(service).to receive(:log_fatal)
end
specify do
service.call
expect(service).to have_received(:log_fatal)
.with(":#{reaper} is invalid for `SidekiqUnqiueJobs.config.reaper`")
end
end
end
end