@@ -78,13 +78,13 @@ public void setupShutDown() {
78
78
}
79
79
80
80
private StringRedisTemplate createTemplate () {
81
- return new StringRedisTemplate (this . getConnectionFactoryForTest ());
81
+ return new StringRedisTemplate (getConnectionFactoryForTest ());
82
82
}
83
83
84
84
@ Test
85
85
@ RedisAvailable
86
- public void testLock () throws Exception {
87
- RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
86
+ public void testLock () {
87
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
88
88
for (int i = 0 ; i < 10 ; i ++) {
89
89
Lock lock = registry .obtain ("foo" );
90
90
lock .lock ();
@@ -102,7 +102,7 @@ public void testLock() throws Exception {
102
102
@ Test
103
103
@ RedisAvailable
104
104
public void testLockInterruptibly () throws Exception {
105
- RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
105
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
106
106
for (int i = 0 ; i < 10 ; i ++) {
107
107
Lock lock = registry .obtain ("foo" );
108
108
lock .lockInterruptibly ();
@@ -119,8 +119,8 @@ public void testLockInterruptibly() throws Exception {
119
119
120
120
@ Test
121
121
@ RedisAvailable
122
- public void testReentrantLock () throws Exception {
123
- RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
122
+ public void testReentrantLock () {
123
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
124
124
for (int i = 0 ; i < 10 ; i ++) {
125
125
Lock lock1 = registry .obtain ("foo" );
126
126
lock1 .lock ();
@@ -146,7 +146,7 @@ public void testReentrantLock() throws Exception {
146
146
@ Test
147
147
@ RedisAvailable
148
148
public void testReentrantLockInterruptibly () throws Exception {
149
- RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
149
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
150
150
for (int i = 0 ; i < 10 ; i ++) {
151
151
Lock lock1 = registry .obtain ("foo" );
152
152
lock1 .lockInterruptibly ();
@@ -172,7 +172,7 @@ public void testReentrantLockInterruptibly() throws Exception {
172
172
@ Test
173
173
@ RedisAvailable
174
174
public void testTwoLocks () throws Exception {
175
- RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
175
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
176
176
for (int i = 0 ; i < 10 ; i ++) {
177
177
Lock lock1 = registry .obtain ("foo" );
178
178
lock1 .lockInterruptibly ();
@@ -198,7 +198,7 @@ public void testTwoLocks() throws Exception {
198
198
@ Test
199
199
@ RedisAvailable
200
200
public void testTwoThreadsSecondFailsToGetLock () throws Exception {
201
- final RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
201
+ final RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
202
202
final Lock lock1 = registry .obtain ("foo" );
203
203
lock1 .lockInterruptibly ();
204
204
final AtomicBoolean locked = new AtomicBoolean ();
@@ -228,12 +228,12 @@ public void testTwoThreadsSecondFailsToGetLock() throws Exception {
228
228
@ Test
229
229
@ RedisAvailable
230
230
public void testTwoThreads () throws Exception {
231
- final RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
232
- final Lock lock1 = registry .obtain ("foo" );
233
- final AtomicBoolean locked = new AtomicBoolean ();
234
- final CountDownLatch latch1 = new CountDownLatch (1 );
235
- final CountDownLatch latch2 = new CountDownLatch (1 );
236
- final CountDownLatch latch3 = new CountDownLatch (1 );
231
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
232
+ Lock lock1 = registry .obtain ("foo" );
233
+ AtomicBoolean locked = new AtomicBoolean ();
234
+ CountDownLatch latch1 = new CountDownLatch (1 );
235
+ CountDownLatch latch2 = new CountDownLatch (1 );
236
+ CountDownLatch latch3 = new CountDownLatch (1 );
237
237
lock1 .lockInterruptibly ();
238
238
assertEquals (1 , TestUtils .getPropertyValue (registry , "locks" , Map .class ).size ());
239
239
Executors .newSingleThreadExecutor ().execute (() -> {
@@ -266,13 +266,13 @@ public void testTwoThreads() throws Exception {
266
266
@ Test
267
267
@ RedisAvailable
268
268
public void testTwoThreadsDifferentRegistries () throws Exception {
269
- final RedisLockRegistry registry1 = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
270
- final RedisLockRegistry registry2 = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
271
- final Lock lock1 = registry1 .obtain ("foo" );
272
- final AtomicBoolean locked = new AtomicBoolean ();
273
- final CountDownLatch latch1 = new CountDownLatch (1 );
274
- final CountDownLatch latch2 = new CountDownLatch (1 );
275
- final CountDownLatch latch3 = new CountDownLatch (1 );
269
+ RedisLockRegistry registry1 = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
270
+ RedisLockRegistry registry2 = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
271
+ Lock lock1 = registry1 .obtain ("foo" );
272
+ AtomicBoolean locked = new AtomicBoolean ();
273
+ CountDownLatch latch1 = new CountDownLatch (1 );
274
+ CountDownLatch latch2 = new CountDownLatch (1 );
275
+ CountDownLatch latch3 = new CountDownLatch (1 );
276
276
lock1 .lockInterruptibly ();
277
277
assertEquals (1 , TestUtils .getPropertyValue (registry1 , "locks" , Map .class ).size ());
278
278
Executors .newSingleThreadExecutor ().execute (() -> {
@@ -313,11 +313,11 @@ public void testTwoThreadsDifferentRegistries() throws Exception {
313
313
@ Test
314
314
@ RedisAvailable
315
315
public void testTwoThreadsWrongOneUnlocks () throws Exception {
316
- final RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey );
317
- final Lock lock = registry .obtain ("foo" );
316
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey );
317
+ Lock lock = registry .obtain ("foo" );
318
318
lock .lockInterruptibly ();
319
- final AtomicBoolean locked = new AtomicBoolean ();
320
- final CountDownLatch latch = new CountDownLatch (1 );
319
+ AtomicBoolean locked = new AtomicBoolean ();
320
+ CountDownLatch latch = new CountDownLatch (1 );
321
321
Future <Object > result = Executors .newSingleThreadExecutor ().submit (() -> {
322
322
try {
323
323
lock .unlock ();
@@ -341,8 +341,8 @@ public void testTwoThreadsWrongOneUnlocks() throws Exception {
341
341
@ Test
342
342
@ RedisAvailable
343
343
public void testExpireTwoRegistries () throws Exception {
344
- RedisLockRegistry registry1 = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey , 100 );
345
- RedisLockRegistry registry2 = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey , 100 );
344
+ RedisLockRegistry registry1 = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey , 1 );
345
+ RedisLockRegistry registry2 = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey , 1 );
346
346
Lock lock1 = registry1 .obtain ("foo" );
347
347
Lock lock2 = registry2 .obtain ("foo" );
348
348
assertTrue (lock1 .tryLock ());
@@ -354,8 +354,21 @@ public void testExpireTwoRegistries() throws Exception {
354
354
355
355
@ Test
356
356
@ RedisAvailable
357
- public void testEquals () throws Exception {
358
- RedisConnectionFactory connectionFactory = this .getConnectionFactoryForTest ();
357
+ public void testExceptionOnExpire () throws Exception {
358
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey , 1 );
359
+ Lock lock1 = registry .obtain ("foo" );
360
+ assertTrue (lock1 .tryLock ());
361
+ this .thrown .expect (IllegalStateException .class );
362
+ this .thrown .expectMessage ("Lock was released in the store due to expiration." );
363
+ waitForExpire ("foo" );
364
+ lock1 .unlock ();
365
+ }
366
+
367
+
368
+ @ Test
369
+ @ RedisAvailable
370
+ public void testEquals () {
371
+ RedisConnectionFactory connectionFactory = getConnectionFactoryForTest ();
359
372
RedisLockRegistry registry1 = new RedisLockRegistry (connectionFactory , this .registryKey );
360
373
RedisLockRegistry registry2 = new RedisLockRegistry (connectionFactory , this .registryKey );
361
374
RedisLockRegistry registry3 = new RedisLockRegistry (connectionFactory , this .registryKey2 );
@@ -388,7 +401,7 @@ public void testEquals() throws Exception {
388
401
@ Test
389
402
@ RedisAvailable
390
403
public void testThreadLocalListLeaks () {
391
- RedisLockRegistry registry = new RedisLockRegistry (this . getConnectionFactoryForTest (), this .registryKey , 100 );
404
+ RedisLockRegistry registry = new RedisLockRegistry (getConnectionFactoryForTest (), this .registryKey , 100 );
392
405
393
406
for (int i = 0 ; i < 10 ; i ++) {
394
407
registry .obtain ("foo" + i );
@@ -411,7 +424,7 @@ public void testThreadLocalListLeaks() {
411
424
@ Test
412
425
@ RedisAvailable
413
426
public void testExpireNotChanged () throws Exception {
414
- RedisConnectionFactory connectionFactory = this . getConnectionFactoryForTest ();
427
+ RedisConnectionFactory connectionFactory = getConnectionFactoryForTest ();
415
428
final RedisLockRegistry registry = new RedisLockRegistry (connectionFactory , this .registryKey , 10000 );
416
429
Lock lock = registry .obtain ("foo" );
417
430
lock .lock ();
@@ -429,13 +442,13 @@ public void testExpireNotChanged() throws Exception {
429
442
}
430
443
431
444
private Long getExpire (RedisLockRegistry registry , String lockKey ) {
432
- StringRedisTemplate template = this . createTemplate ();
445
+ StringRedisTemplate template = createTemplate ();
433
446
String registryKey = TestUtils .getPropertyValue (registry , "registryKey" , String .class );
434
447
return template .getExpire (registryKey + ":" + lockKey );
435
448
}
436
449
437
450
private void waitForExpire (String key ) throws Exception {
438
- StringRedisTemplate template = this . createTemplate ();
451
+ StringRedisTemplate template = createTemplate ();
439
452
int n = 0 ;
440
453
while (n ++ < 100 && template .keys (this .registryKey + ":" + key ).size () > 0 ) {
441
454
Thread .sleep (100 );
0 commit comments