-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
RedissonLockTest.java
311 lines (255 loc) · 8.73 KB
/
RedissonLockTest.java
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
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import static org.awaitility.Awaitility.*;
public class RedissonLockTest extends BaseConcurrentTest {
@Test
public void testTryLockWait() throws InterruptedException {
testSingleInstanceConcurrency(1, r -> {
RLock lock = r.getLock("lock");
lock.lock();
});
RLock lock = redisson.getLock("lock");
long startTime = System.currentTimeMillis();
lock.tryLock(3, TimeUnit.SECONDS);
assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3100L);
}
@Test
public void testDelete() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.delete());
lock.lock();
Assert.assertTrue(lock.delete());
}
@Test
public void testForceUnlock() {
RLock lock = redisson.getLock("lock");
lock.lock();
lock.forceUnlock();
Assert.assertFalse(lock.isLocked());
lock = redisson.getLock("lock");
Assert.assertFalse(lock.isLocked());
}
@Test
public void testExpire() throws InterruptedException {
RLock lock = redisson.getLock("lock");
lock.lock(2, TimeUnit.SECONDS);
final long startTime = System.currentTimeMillis();
Thread t = new Thread() {
public void run() {
RLock lock1 = redisson.getLock("lock");
lock1.lock();
long spendTime = System.currentTimeMillis() - startTime;
Assert.assertTrue(spendTime < 2020);
lock1.unlock();
};
};
t.start();
t.join();
lock.unlock();
}
@Test
public void testAutoExpire() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
RedissonClient r = createInstance();
Thread t = new Thread() {
@Override
public void run() {
RLock lock = r.getLock("lock");
lock.lock();
latch.countDown();
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
RLock lock = redisson.getLock("lock");
t.join();
r.shutdown();
await().atMost(redisson.getConfig().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS).until(() -> !lock.isLocked());
}
@Test
public void testGetHoldCount() {
RLock lock = redisson.getLock("lock");
Assert.assertEquals(0, lock.getHoldCount());
lock.lock();
Assert.assertEquals(1, lock.getHoldCount());
lock.unlock();
Assert.assertEquals(0, lock.getHoldCount());
lock.lock();
lock.lock();
Assert.assertEquals(2, lock.getHoldCount());
lock.unlock();
Assert.assertEquals(1, lock.getHoldCount());
lock.unlock();
Assert.assertEquals(0, lock.getHoldCount());
}
@Test
public void testIsHeldByCurrentThreadOtherThread() throws InterruptedException {
RLock lock = redisson.getLock("lock");
lock.lock();
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.isHeldByCurrentThread());
};
};
t.start();
t.join();
lock.unlock();
Thread t2 = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.isHeldByCurrentThread());
};
};
t2.start();
t2.join();
}
@Test
public void testIsHeldByCurrentThread() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.isHeldByCurrentThread());
lock.lock();
Assert.assertTrue(lock.isHeldByCurrentThread());
lock.unlock();
Assert.assertFalse(lock.isHeldByCurrentThread());
}
@Test
public void testIsLockedOtherThread() throws InterruptedException {
RLock lock = redisson.getLock("lock");
lock.lock();
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
Assert.assertTrue(lock.isLocked());
};
};
t.start();
t.join();
lock.unlock();
Thread t2 = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.isLocked());
};
};
t2.start();
t2.join();
}
@Test
public void testIsLocked() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.isLocked());
lock.lock();
Assert.assertTrue(lock.isLocked());
lock.unlock();
Assert.assertFalse(lock.isLocked());
}
@Test(expected = IllegalMonitorStateException.class)
public void testUnlockFail() throws InterruptedException {
RLock lock = redisson.getLock("lock");
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
lock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lock.unlock();
};
};
t.start();
t.join(400);
try {
lock.unlock();
} catch (IllegalMonitorStateException e) {
t.join();
throw e;
}
}
@Test
public void testLockUnlock() {
Lock lock = redisson.getLock("lock1");
lock.lock();
lock.unlock();
lock.lock();
lock.unlock();
}
@Test
public void testReentrancy() throws InterruptedException {
Lock lock = redisson.getLock("lock1");
Assert.assertTrue(lock.tryLock());
Assert.assertTrue(lock.tryLock());
lock.unlock();
// next row for test renew expiration tisk.
//Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(RedissonLock.LOCK_EXPIRATION_INTERVAL_SECONDS*2));
Thread thread1 = new Thread() {
@Override
public void run() {
RLock lock1 = redisson.getLock("lock1");
Assert.assertFalse(lock1.tryLock());
}
};
thread1.start();
thread1.join();
lock.unlock();
}
@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
final AtomicInteger lockedCounter = new AtomicInteger();
int iterations = 15;
testSingleInstanceConcurrency(iterations, r -> {
Lock lock = r.getLock("testConcurrency_SingleInstance");
lock.lock();
lockedCounter.incrementAndGet();
lock.unlock();
});
Assert.assertEquals(iterations, lockedCounter.get());
}
@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
final int iterations = 100;
final AtomicInteger lockedCounter = new AtomicInteger();
testMultiInstanceConcurrency(16, r -> {
for (int i = 0; i < iterations; i++) {
r.getLock("testConcurrency_MultiInstance1").lock();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
lockedCounter.incrementAndGet();
r.getLock("testConcurrency_MultiInstance1").unlock();
}
});
Assert.assertEquals(16 * iterations, lockedCounter.get());
}
@Test
public void testConcurrency_MultiInstance() throws InterruptedException {
int iterations = 100;
final AtomicInteger lockedCounter = new AtomicInteger();
testMultiInstanceConcurrency(iterations, r -> {
Lock lock = r.getLock("testConcurrency_MultiInstance2");
lock.lock();
lockedCounter.incrementAndGet();
lock.unlock();
});
Assert.assertEquals(iterations, lockedCounter.get());
}
}