-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathscheduler_locking_test.rb
More file actions
320 lines (254 loc) · 8.13 KB
/
scheduler_locking_test.rb
File metadata and controls
320 lines (254 loc) · 8.13 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# vim:fileencoding=utf-8
require_relative 'test_helper'
module LockTestHelper
def lock_is_not_held(lock)
Resque.redis.set(lock.key, 'anothermachine:1234')
end
end
module LockSharedTests
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def self.included(mod)
mod.class_eval do
test 'you should not have the lock if someone else holds it' do
lock_is_not_held(@lock)
assert !@lock.locked?
end
test 'you should not be able to acquire the lock if someone ' \
'else holds it' do
lock_is_not_held(@lock)
assert !@lock.acquire!
end
test 'the lock should receive a TTL on acquiring' do
@lock.acquire!
assert Resque.redis.ttl(@lock.key) > 0, 'lock should expire'
end
test 'releasing should release the master lock' do
assert @lock.acquire!, 'should have acquired the master lock'
assert @lock.locked?, 'should be locked'
@lock.release!
assert !@lock.locked?, 'should not be locked'
end
test 'checking the lock should increase the TTL if we hold it' do
@lock.acquire!
Resque.redis.setex(@lock.key, 10, @lock.value)
@lock.locked?
assert Resque.redis.ttl(@lock.key) > 10, 'TTL should have been updated'
end
test 'checking the lock should not increase the TTL if we do not hold it' do
Resque.redis.setex(@lock.key, 10, @lock.value)
lock_is_not_held(@lock)
@lock.locked?
assert Resque.redis.ttl(@lock.key) <= 10,
'TTL should not have been updated'
end
test 'setting the lock timeout changes the key TTL if we hold it' do
@lock.acquire!
@lock.stubs(:locked?).returns(true)
@lock.timeout = 120
ttl = Resque.redis.ttl(@lock.key)
assert_send [ttl, :>, 100]
@lock.stubs(:locked?).returns(true)
@lock.timeout = 180
ttl = Resque.redis.ttl(@lock.key)
assert_send [ttl, :>, 120]
end
end
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
end
context '#master_lock_key' do
setup do
@subject = Class.new { extend Resque::Scheduler::Locking }
end
teardown do
Resque.redis.del(@subject.master_lock.key)
end
test 'should have resque prefix' do
assert_equal(
@subject.master_lock.key, 'resque:resque_scheduler_master_lock'
)
end
context 'with a prefix set via ENV' do
setup do
ENV['RESQUE_SCHEDULER_MASTER_LOCK_PREFIX'] = 'my.prefix'
@subject = Class.new { extend Resque::Scheduler::Locking }
end
teardown do
Resque.redis.del(@subject.master_lock.key)
end
test 'should have ENV prefix' do
assert_equal(
@subject.master_lock.key,
'resque:my.prefix:resque_scheduler_master_lock'
)
end
end
context 'with a namespace set for resque' do
setup do
Resque.redis.namespace = 'my.namespace'
@subject = Class.new { extend Resque::Scheduler::Locking }
end
teardown do
Resque.redis.namespace = 'resque'
Resque.redis.del(@subject.master_lock.key)
end
test 'should have resque prefix' do
assert_equal(
@subject.master_lock.key, 'my.namespace:resque_scheduler_master_lock'
)
end
context 'with a prefix set via ENV' do
setup do
Resque.redis.namespace = 'my.namespace'
ENV['RESQUE_SCHEDULER_MASTER_LOCK_PREFIX'] = 'my.prefix'
@subject = Class.new { extend Resque::Scheduler::Locking }
end
teardown do
Resque.redis.namespace = 'resque'
Resque.redis.del(@subject.master_lock.key)
end
test 'should have ENV prefix' do
assert_equal(
@subject.master_lock.key,
'my.namespace:my.prefix:resque_scheduler_master_lock'
)
end
end
end
end
context 'Resque::Scheduler::Locking' do
setup do
@subject = Class.new { extend Resque::Scheduler::Locking }
end
teardown do
Resque.redis.del(@subject.master_lock.key)
end
test 'should use the basic lock mechanism for <= Redis 2.4' do
Resque.data_store.redis.stubs(:info).returns('redis_version' => '2.4.16')
assert_equal @subject.master_lock.class, Resque::Scheduler::Lock::Basic
end
test 'should use the resilient lock mechanism for > Redis 2.4 and < 2.6.12' do
Resque.data_store.redis.stubs(:info).returns('redis_version' => '2.5.12')
assert_equal(
@subject.master_lock.class, Resque::Scheduler::Lock::Resilient
)
end
test 'should use the resilient lock mechanism for >= Redis 2.6.12' do
Resque.data_store.redis.stubs(:info).returns('redis_version' => '2.8.0')
assert_equal(
@subject.master_lock.class, Resque::Scheduler::Lock::ResilientModern
)
end
test 'should be the master if the lock is held' do
@subject.master_lock.acquire!
assert @subject.master?, 'should be master'
end
test 'should not be the master if the lock is held by someone else' do
Resque.redis.set(@subject.master_lock.key, 'somethingelse:1234')
assert !@subject.master?, 'should not be master'
end
test 'release_master_lock should delegate to master_lock' do
@subject.master_lock.expects(:release)
@subject.release_master_lock
end
test 'release_master_lock! should delegate to master_lock' do
@subject.expects(:warn)
@subject.master_lock.expects(:release!)
@subject.release_master_lock!
end
end
context 'Resque::Scheduler::Lock::Base' do
setup do
@lock = Resque::Scheduler::Lock::Base.new('test_lock_key')
end
test '#acquire! should be not implemented' do
assert_raises NotImplementedError do
@lock.acquire!
end
end
test '#locked? should be not implemented' do
assert_raises NotImplementedError do
@lock.locked?
end
end
end
context 'Resque::Scheduler::Lock::Basic' do
include LockTestHelper
setup do
@lock = Resque::Scheduler::Lock::Basic.new('test_lock_key')
end
teardown do
@lock.release!
end
include LockSharedTests
end
context 'Resque::Scheduler::Lock::Resilient' do
include LockTestHelper
if !Resque::Scheduler.supports_lua?
puts '*** Skipping Resque::Scheduler::Lock::Resilient ' \
'tests, as they require Redis >= 2.5.'
else
setup do
@lock = Resque::Scheduler::Lock::Resilient.new('test_resilient_lock')
end
teardown do
@lock.release!
end
include LockSharedTests
test 'refreshes sha cache when the sha cannot be found on ' \
'the redis server' do
assert @lock.acquire!
assert @lock.locked?
Resque.data_store.redis.script(:flush)
assert @lock.locked?
assert_false @lock.acquire!
end
test 'setting lock timeout is a noop if not held' do
@lock.acquire!
@lock.timeout = 100
@lock.stubs(:locked?).returns(false)
@lock.timeout = 120
assert_equal 100, @lock.timeout
end
test 'setting lock timeout nils out lock script' do
@lock.acquire!
@lock.timeout = 100
assert_equal nil, @lock.instance_variable_get(:@locked_sha)
end
test 'setting lock timeout does not nil out lock script if not held' do
@lock.acquire!
@lock.locked?
@lock.stubs(:locked?).returns(false)
@lock.timeout = 100
assert_not_nil @lock.instance_variable_get(:@locked_sha)
end
test 'setting lock timeout nils out acquire script' do
@lock.acquire!
@lock.timeout = 100
assert_equal nil, @lock.instance_variable_get(:@acquire_sha)
end
test 'setting lock timeout does not nil out acquire script if not held' do
@lock.acquire!
@lock.stubs(:locked?).returns(false)
@lock.timeout = 100
assert_not_nil @lock.instance_variable_get(:@acquire_sha)
end
end
end
context 'Resque::Scheduler::Lock::ResilientModern' do
include LockTestHelper
if !Resque::Scheduler.supports_get_x_options?
puts '*** Skipping Resque::Scheduler::Lock::ResilientModern ' \
'tests, as they require Redis >= 2.6.2.'
else
setup do
@lock = Resque::Scheduler::Lock::ResilientModern.new('test_resilient_modern_lock')
end
teardown do
@lock.release!
end
include LockSharedTests
end
end