-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
while_executing_spec.rb
125 lines (104 loc) · 3.53 KB
/
while_executing_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
# frozen_string_literal: true
RSpec.describe SidekiqUniqueJobs::Lock::WhileExecuting do
let(:process_one) { described_class.new(item_one, callback_one) }
let(:process_two) { described_class.new(item_two, callback_two) }
let(:jid_one) { "jid one" }
let(:jid_two) { "jid two" }
let(:job_class) { WhileExecutingJob }
let(:unique) { :while_executing }
let(:queue) { :while_executing }
let(:args) { %w[array of arguments] }
let(:callback_one) { -> {} }
let(:callback_two) { -> {} }
let(:item_one) do
{ "jid" => jid_one,
"class" => job_class.to_s,
"queue" => queue,
"lock" => unique,
"args" => args }
end
let(:item_two) do
{ "jid" => jid_two,
"class" => job_class.to_s,
"queue" => queue,
"lock" => unique,
"args" => args }
end
before do
allow(callback_one).to receive(:call).and_call_original if callback_one
allow(callback_two).to receive(:call).and_call_original if callback_two
end
describe "#lock" do
it "does not lock jobs" do
expect(process_one.lock).to eq(jid_one)
expect(process_one).not_to be_locked
expect(process_two.lock).to eq(jid_two)
expect(process_two).not_to be_locked
end
end
describe "#execute" do
it "locks the process" do
process_one.execute do
expect(process_one).to be_locked
end
end
it "calls back" do
process_one.execute {}
expect(callback_one).to have_received(:call)
end
it "prevents other processes from executing" do
flush_redis
process_one.execute do
unset = true
process_two.execute { unset = false }
expect(unset).to be(true)
end
expect(callback_one).to have_received(:call).once
expect(callback_two).not_to have_received(:call)
end
it "yields without arguments" do
process_one.lock
process_one.execute {}
blk = -> {}
expect { process_one.execute(&blk) }.not_to raise_error
end
context "when no callback is defined" do
let(:job_class) { WhileExecutingRescheduleJob }
let(:callback_one) { -> { true } }
let(:callback_two) { -> { true } }
let(:strategy_one) { process_one.send(:server_strategy) }
let(:strategy_two) { process_two.send(:server_strategy) }
before do
allow(strategy_one).to receive(:call).and_call_original
allow(strategy_two).to receive(:call).and_call_original
allow(process_one).to receive(:reflect).and_call_original
allow(process_two).to receive(:reflect).and_call_original
end
it "reflects execution_failed" do
process_one.execute do
process_two.execute do
puts "BOGUS!"
nil
end
nil
end
expect(process_one).not_to have_received(:reflect).with(:execution_failed, item_one)
expect(callback_one).to have_received(:call).once
expect(strategy_one).not_to have_received(:call)
expect(process_two).to have_received(:reflect).with(:execution_failed, item_two)
expect(callback_two).not_to have_received(:call)
expect(strategy_two).to have_received(:call).once
end
end
context "when worker raises error" do
before do
allow(process_one.locksmith).to receive(:execute).and_raise(RuntimeError, "Hell")
end
it "always unlocks" do
expect { process_one.execute { raise "Hell" } }
.to raise_error(RuntimeError, "Hell")
expect(process_one).not_to be_locked
end
end
end
end