-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
while_executing_spec.rb
107 lines (90 loc) · 3.02 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
# frozen_string_literal: true
require "spec_helper"
RSpec.describe SidekiqUniqueJobs::Lock::WhileExecuting, redis: :redis do
include SidekiqHelpers
let(:process_one) { described_class.new(item_one, callback_one) }
let(:process_two) { described_class.new(item_two, callback_two) }
let(:strategy_one) { nil }
let(:strategy_two) { nil }
let(:jid_one) { "jid one" }
let(:jid_two) { "jid two" }
let(:worker_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" => worker_class.to_s,
"queue" => queue,
"lock" => unique,
"args" => args }
end
let(:item_two) do
{ "jid" => jid_two,
"class" => worker_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
allow(process_one).to receive(:strategy).and_return(strategy_one)
allow(process_two).to receive(:strategy).and_return(strategy_two)
end
describe "#lock" do
it "does not lock jobs" do
expect(process_one.lock).to eq(true)
expect(process_one).not_to be_locked
expect(process_two.lock).to eq(true)
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
process_one.execute do
unset = true
process_two.execute { unset = false }
expect(unset).to eq(true)
end
expect(callback_one).to have_received(:call).once
expect(callback_two).not_to have_received(:call)
end
context "when no callback is defined" do
let(:callback_one) { -> { true } }
let(:callback_two) { nil }
let(:strategy_one) { SidekiqUniqueJobs::OnConflict::Reschedule.new(item_one) }
let(:strategy_two) { SidekiqUniqueJobs::OnConflict::Reschedule.new(item_two) }
before do
allow(strategy_one).to receive(:call).and_call_original
allow(strategy_two).to receive(:call).and_call_original
end
it "works" do
process_one.execute do
process_two.execute { puts "BOGUS!" }
end
expect(callback_one).to have_received(:call).once
expect(strategy_one).not_to have_received(:call)
expect(strategy_two).to have_received(:call).once
end
end
context "when worker raises error" do
it "always unlocks" do
expect { process_one.execute { raise "Hell" } }
.to raise_error(RuntimeError, "Hell")
expect(process_one.locked?).to eq(false)
end
end
end
end