blob: 37f44f98cdabaaec2e5edd9041eefe87a49c6ce8 (
plain)
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
|
# frozen_string_literal: true
# Expects `subject` to be a job/worker instance
RSpec.shared_examples 'reenqueuer' do
before do
allow(subject).to receive(:sleep) # faster tests
end
it 'implements lease_timeout' do
expect(subject.lease_timeout).to be_a(ActiveSupport::Duration)
end
describe '#perform' do
it 'tries to obtain a lease' do
expect_to_obtain_exclusive_lease(subject.lease_key)
subject.perform
end
end
end
# Expects `subject` to be a job/worker instance
RSpec.shared_examples '#perform is rate limited to 1 call per' do |minimum_duration|
before do
# Allow Timecop freeze and travel without the block form
Timecop.safe_mode = false
Timecop.freeze
time_travel_during_perform(actual_duration)
end
after do
Timecop.return
Timecop.safe_mode = true
end
context 'when the work finishes in 0 seconds' do
let(:actual_duration) { 0 }
it 'sleeps exactly the minimum duration' do
expect(subject).to receive(:sleep).with(a_value_within(0.01).of(minimum_duration))
subject.perform
end
end
context 'when the work finishes in 10% of minimum duration' do
let(:actual_duration) { 0.1 * minimum_duration }
it 'sleeps 90% of minimum duration' do
expect(subject).to receive(:sleep).with(a_value_within(0.01).of(0.9 * minimum_duration))
subject.perform
end
end
context 'when the work finishes in 90% of minimum duration' do
let(:actual_duration) { 0.9 * minimum_duration }
it 'sleeps 10% of minimum duration' do
expect(subject).to receive(:sleep).with(a_value_within(0.01).of(0.1 * minimum_duration))
subject.perform
end
end
context 'when the work finishes exactly at minimum duration' do
let(:actual_duration) { minimum_duration }
it 'does not sleep' do
expect(subject).not_to receive(:sleep)
subject.perform
end
end
context 'when the work takes 10% longer than minimum duration' do
let(:actual_duration) { 1.1 * minimum_duration }
it 'does not sleep' do
expect(subject).not_to receive(:sleep)
subject.perform
end
end
context 'when the work takes twice as long as minimum duration' do
let(:actual_duration) { 2 * minimum_duration }
it 'does not sleep' do
expect(subject).not_to receive(:sleep)
subject.perform
end
end
def time_travel_during_perform(actual_duration)
# Save the original implementation of ensure_minimum_duration
original_ensure_minimum_duration = subject.method(:ensure_minimum_duration)
allow(subject).to receive(:ensure_minimum_duration) do |minimum_duration, &block|
original_ensure_minimum_duration.call(minimum_duration) do
# Time travel inside the block inside ensure_minimum_duration
Timecop.travel(actual_duration) if actual_duration && actual_duration > 0
end
end
end
end
|