@@ -7,6 +7,73 @@ def lock_is_not_held(lock)
7
7
end
8
8
end
9
9
10
+ module LockSharedTests
11
+ def self . included ( mod )
12
+ mod . class_eval do
13
+ test 'you should not have the lock if someone else holds it' do
14
+ lock_is_not_held ( @lock )
15
+
16
+ assert !@lock . locked?
17
+ end
18
+
19
+ test 'you should not be able to acquire the lock if someone ' \
20
+ 'else holds it' do
21
+ lock_is_not_held ( @lock )
22
+
23
+ assert !@lock . acquire!
24
+ end
25
+
26
+ test 'the lock should receive a TTL on acquiring' do
27
+ @lock . acquire!
28
+
29
+ assert Resque . redis . ttl ( @lock . key ) > 0 , 'lock should expire'
30
+ end
31
+
32
+ test 'releasing should release the master lock' do
33
+ assert @lock . acquire! , 'should have acquired the master lock'
34
+ assert @lock . locked? , 'should be locked'
35
+
36
+ @lock . release!
37
+
38
+ assert !@lock . locked? , 'should not be locked'
39
+ end
40
+
41
+ test 'checking the lock should increase the TTL if we hold it' do
42
+ @lock . acquire!
43
+ Resque . redis . setex ( @lock . key , 10 , @lock . value )
44
+
45
+ @lock . locked?
46
+
47
+ assert Resque . redis . ttl ( @lock . key ) > 10 , 'TTL should have been updated'
48
+ end
49
+
50
+ test 'checking the lock should not increase the TTL if we do not hold it' do
51
+ Resque . redis . setex ( @lock . key , 10 , @lock . value )
52
+ lock_is_not_held ( @lock )
53
+
54
+ @lock . locked?
55
+
56
+ assert Resque . redis . ttl ( @lock . key ) <= 10 ,
57
+ 'TTL should not have been updated'
58
+ end
59
+
60
+ test 'setting the lock timeout changes the key TTL if we hold it' do
61
+ @lock . acquire!
62
+
63
+ @lock . stubs ( :locked? ) . returns ( true )
64
+ @lock . timeout = 120
65
+ ttl = Resque . redis . ttl ( @lock . key )
66
+ assert_send [ ttl , :> , 100 ]
67
+
68
+ @lock . stubs ( :locked? ) . returns ( true )
69
+ @lock . timeout = 180
70
+ ttl = Resque . redis . ttl ( @lock . key )
71
+ assert_send [ ttl , :> , 120 ]
72
+ end
73
+ end
74
+ end
75
+ end
76
+
10
77
context '#master_lock_key' do
11
78
setup do
12
79
@subject = Class . new { extend Resque ::Scheduler ::Locking }
@@ -94,14 +161,22 @@ def lock_is_not_held(lock)
94
161
assert_equal @subject . master_lock . class , Resque ::Scheduler ::Lock ::Basic
95
162
end
96
163
97
- test 'should use the resilient lock mechanism for > Redis 2.4' do
98
- Resque . redis . stubs ( :info ) . returns ( 'redis_version' => '2.5.12' )
164
+ test 'should use the resilient lock mechanism for > Redis 2.4 and < 2.6.12 ' do
165
+ Resque . data_store . redis . stubs ( :info ) . returns ( 'redis_version' => '2.5.12' )
99
166
100
167
assert_equal (
101
168
@subject . master_lock . class , Resque ::Scheduler ::Lock ::Resilient
102
169
)
103
170
end
104
171
172
+ test 'should use the resilient lock mechanism for >= Redis 2.6.12' do
173
+ Resque . data_store . redis . stubs ( :info ) . returns ( 'redis_version' => '2.8.0' )
174
+
175
+ assert_equal (
176
+ @subject . master_lock . class , Resque ::Scheduler ::Lock ::ResilientModern
177
+ )
178
+ end
179
+
105
180
test 'should be the master if the lock is held' do
106
181
@subject . master_lock . acquire!
107
182
assert @subject . master? , 'should be master'
@@ -153,52 +228,7 @@ def lock_is_not_held(lock)
153
228
@lock . release!
154
229
end
155
230
156
- test 'you should not have the lock if someone else holds it' do
157
- lock_is_not_held ( @lock )
158
-
159
- assert !@lock . locked?
160
- end
161
-
162
- test 'you should not be able to acquire the lock if someone ' \
163
- 'else holds it' do
164
- lock_is_not_held ( @lock )
165
-
166
- assert !@lock . acquire!
167
- end
168
-
169
- test 'the lock should receive a TTL on acquiring' do
170
- @lock . acquire!
171
-
172
- assert Resque . redis . ttl ( @lock . key ) > 0 , 'lock should expire'
173
- end
174
-
175
- test 'releasing should release the master lock' do
176
- assert @lock . acquire! , 'should have acquired the master lock'
177
- assert @lock . locked? , 'should be locked'
178
-
179
- @lock . release!
180
-
181
- assert !@lock . locked? , 'should not be locked'
182
- end
183
-
184
- test 'checking the lock should increase the TTL if we hold it' do
185
- @lock . acquire!
186
- Resque . redis . setex ( @lock . key , 10 , @lock . value )
187
-
188
- @lock . locked?
189
-
190
- assert Resque . redis . ttl ( @lock . key ) > 10 , 'TTL should have been updated'
191
- end
192
-
193
- test 'checking the lock should not increase the TTL if we do not hold it' do
194
- Resque . redis . setex ( @lock . key , 10 , @lock . value )
195
- lock_is_not_held ( @lock )
196
-
197
- @lock . locked?
198
-
199
- assert Resque . redis . ttl ( @lock . key ) <= 10 ,
200
- 'TTL should not have been updated'
201
- end
231
+ include LockSharedTests
202
232
end
203
233
204
234
context 'Resque::Scheduler::Lock::Resilient' do
@@ -216,11 +246,7 @@ def lock_is_not_held(lock)
216
246
@lock . release!
217
247
end
218
248
219
- test 'you should not have the lock if someone else holds it' do
220
- lock_is_not_held ( @lock )
221
-
222
- assert !@lock . locked? , 'you should not have the lock'
223
- end
249
+ include LockSharedTests
224
250
225
251
test 'refreshes sha cache when the sha cannot be found on ' \
226
252
'the redis server' do
@@ -233,62 +259,6 @@ def lock_is_not_held(lock)
233
259
assert_false @lock . acquire!
234
260
end
235
261
236
- test 'you should not be able to acquire the lock if someone ' \
237
- 'else holds it' do
238
- lock_is_not_held ( @lock )
239
-
240
- assert !@lock . acquire!
241
- end
242
-
243
- test 'the lock should receive a TTL on acquiring' do
244
- @lock . acquire!
245
-
246
- assert Resque . redis . ttl ( @lock . key ) > 0 , 'lock should expire'
247
- end
248
-
249
- test 'releasing should release the master lock' do
250
- assert @lock . acquire! , 'should have acquired the master lock'
251
- assert @lock . locked? , 'should be locked'
252
-
253
- @lock . release!
254
-
255
- assert !@lock . locked? , 'should not be locked'
256
- end
257
-
258
- test 'checking the lock should increase the TTL if we hold it' do
259
- @lock . acquire!
260
- Resque . redis . setex ( @lock . key , 10 , @lock . value )
261
-
262
- @lock . locked?
263
-
264
- assert Resque . redis . ttl ( @lock . key ) > 10 , 'TTL should have been updated'
265
- end
266
-
267
- test 'checking the lock should not increase the TTL if we do ' \
268
- 'not hold it' do
269
- Resque . redis . setex ( @lock . key , 10 , @lock . value )
270
- lock_is_not_held ( @lock )
271
-
272
- @lock . locked?
273
-
274
- assert Resque . redis . ttl ( @lock . key ) <= 10 ,
275
- 'TTL should not have been updated'
276
- end
277
-
278
- test 'setting the lock timeout changes the key TTL if we hold it' do
279
- @lock . acquire!
280
-
281
- @lock . stubs ( :locked? ) . returns ( true )
282
- @lock . timeout = 120
283
- ttl = Resque . redis . ttl ( @lock . key )
284
- assert_send [ ttl , :> , 100 ]
285
-
286
- @lock . stubs ( :locked? ) . returns ( true )
287
- @lock . timeout = 180
288
- ttl = Resque . redis . ttl ( @lock . key )
289
- assert_send [ ttl , :> , 120 ]
290
- end
291
-
292
262
test 'setting lock timeout is a noop if not held' do
293
263
@lock . acquire!
294
264
@lock . timeout = 100
@@ -325,3 +295,22 @@ def lock_is_not_held(lock)
325
295
end
326
296
end
327
297
end
298
+
299
+ context 'Resque::Scheduler::Lock::ResilientModern' do
300
+ include LockTestHelper
301
+
302
+ if !Resque ::Scheduler . supports_get_x_options?
303
+ puts '*** Skipping Resque::Scheduler::Lock::ResilientModern ' \
304
+ 'tests, as they require Redis >= 2.6.2.'
305
+ else
306
+ setup do
307
+ @lock = Resque ::Scheduler ::Lock ::ResilientModern . new ( 'test_resilient_modern_lock' )
308
+ end
309
+
310
+ teardown do
311
+ @lock . release!
312
+ end
313
+
314
+ include LockSharedTests
315
+ end
316
+ end
0 commit comments