1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.concurrent.locks.*; |
11 |
import java.util.concurrent.*; |
12 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
import java.io.*; |
14 |
import java.util.*; |
15 |
|
16 |
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
17 |
public static void main(String[] args) { |
18 |
junit.textui.TestRunner.run (suite()); |
19 |
} |
20 |
public static Test suite() { |
21 |
return new TestSuite(ReentrantReadWriteLockTest.class); |
22 |
} |
23 |
|
24 |
/** |
25 |
* A runnable calling lockInterruptibly |
26 |
*/ |
27 |
class InterruptibleLockRunnable extends CheckedRunnable { |
28 |
final ReentrantReadWriteLock lock; |
29 |
InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
30 |
public void realRun() throws InterruptedException { |
31 |
lock.writeLock().lockInterruptibly(); |
32 |
} |
33 |
} |
34 |
|
35 |
|
36 |
/** |
37 |
* A runnable calling lockInterruptibly that expects to be |
38 |
* interrupted |
39 |
*/ |
40 |
class InterruptedLockRunnable extends CheckedInterruptedRunnable { |
41 |
final ReentrantReadWriteLock lock; |
42 |
InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; } |
43 |
public void realRun() throws InterruptedException { |
44 |
lock.writeLock().lockInterruptibly(); |
45 |
} |
46 |
} |
47 |
|
48 |
/** |
49 |
* Subclass to expose protected methods |
50 |
*/ |
51 |
static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock { |
52 |
PublicReentrantReadWriteLock() { super(); } |
53 |
public Collection<Thread> getQueuedThreads() { |
54 |
return super.getQueuedThreads(); |
55 |
} |
56 |
public Collection<Thread> getWaitingThreads(Condition c) { |
57 |
return super.getWaitingThreads(c); |
58 |
} |
59 |
} |
60 |
|
61 |
/** |
62 |
* Constructor sets given fairness, and is in unlocked state |
63 |
*/ |
64 |
public void testConstructor() { |
65 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
66 |
assertFalse(rl.isFair()); |
67 |
assertFalse(rl.isWriteLocked()); |
68 |
assertEquals(0, rl.getReadLockCount()); |
69 |
ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true); |
70 |
assertTrue(r2.isFair()); |
71 |
assertFalse(r2.isWriteLocked()); |
72 |
assertEquals(0, r2.getReadLockCount()); |
73 |
ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false); |
74 |
assertFalse(r3.isFair()); |
75 |
assertFalse(r3.isWriteLocked()); |
76 |
assertEquals(0, r3.getReadLockCount()); |
77 |
} |
78 |
|
79 |
/** |
80 |
* write-locking and read-locking an unlocked lock succeed |
81 |
*/ |
82 |
public void testLock() { |
83 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
84 |
rl.writeLock().lock(); |
85 |
assertTrue(rl.isWriteLocked()); |
86 |
assertTrue(rl.isWriteLockedByCurrentThread()); |
87 |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
88 |
assertEquals(0, rl.getReadLockCount()); |
89 |
rl.writeLock().unlock(); |
90 |
assertFalse(rl.isWriteLocked()); |
91 |
assertFalse(rl.isWriteLockedByCurrentThread()); |
92 |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
93 |
assertEquals(0, rl.getReadLockCount()); |
94 |
rl.readLock().lock(); |
95 |
assertFalse(rl.isWriteLocked()); |
96 |
assertFalse(rl.isWriteLockedByCurrentThread()); |
97 |
assertEquals(1, rl.getReadLockCount()); |
98 |
rl.readLock().unlock(); |
99 |
assertFalse(rl.isWriteLocked()); |
100 |
assertFalse(rl.isWriteLockedByCurrentThread()); |
101 |
assertEquals(0, rl.getReadLockCount()); |
102 |
} |
103 |
|
104 |
|
105 |
/** |
106 |
* locking an unlocked fair lock succeeds |
107 |
*/ |
108 |
public void testFairLock() { |
109 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true); |
110 |
rl.writeLock().lock(); |
111 |
assertTrue(rl.isWriteLocked()); |
112 |
assertTrue(rl.isWriteLockedByCurrentThread()); |
113 |
assertTrue(rl.writeLock().isHeldByCurrentThread()); |
114 |
assertEquals(0, rl.getReadLockCount()); |
115 |
rl.writeLock().unlock(); |
116 |
assertFalse(rl.isWriteLocked()); |
117 |
assertFalse(rl.isWriteLockedByCurrentThread()); |
118 |
assertFalse(rl.writeLock().isHeldByCurrentThread()); |
119 |
assertEquals(0, rl.getReadLockCount()); |
120 |
rl.readLock().lock(); |
121 |
assertFalse(rl.isWriteLocked()); |
122 |
assertFalse(rl.isWriteLockedByCurrentThread()); |
123 |
assertEquals(1, rl.getReadLockCount()); |
124 |
rl.readLock().unlock(); |
125 |
assertFalse(rl.isWriteLocked()); |
126 |
assertFalse(rl.isWriteLockedByCurrentThread()); |
127 |
assertEquals(0, rl.getReadLockCount()); |
128 |
} |
129 |
|
130 |
/** |
131 |
* getWriteHoldCount returns number of recursive holds |
132 |
*/ |
133 |
public void testGetWriteHoldCount() { |
134 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
135 |
for (int i = 1; i <= SIZE; i++) { |
136 |
lock.writeLock().lock(); |
137 |
assertEquals(i,lock.getWriteHoldCount()); |
138 |
} |
139 |
for (int i = SIZE; i > 0; i--) { |
140 |
lock.writeLock().unlock(); |
141 |
assertEquals(i-1,lock.getWriteHoldCount()); |
142 |
} |
143 |
} |
144 |
|
145 |
/** |
146 |
* WriteLock.getHoldCount returns number of recursive holds |
147 |
*/ |
148 |
public void testGetHoldCount() { |
149 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
150 |
for (int i = 1; i <= SIZE; i++) { |
151 |
lock.writeLock().lock(); |
152 |
assertEquals(i,lock.writeLock().getHoldCount()); |
153 |
} |
154 |
for (int i = SIZE; i > 0; i--) { |
155 |
lock.writeLock().unlock(); |
156 |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
157 |
} |
158 |
} |
159 |
|
160 |
/** |
161 |
* getReadHoldCount returns number of recursive holds |
162 |
*/ |
163 |
public void testGetReadHoldCount() { |
164 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
165 |
for (int i = 1; i <= SIZE; i++) { |
166 |
lock.readLock().lock(); |
167 |
assertEquals(i,lock.getReadHoldCount()); |
168 |
} |
169 |
for (int i = SIZE; i > 0; i--) { |
170 |
lock.readLock().unlock(); |
171 |
assertEquals(i-1,lock.getReadHoldCount()); |
172 |
} |
173 |
} |
174 |
|
175 |
|
176 |
/** |
177 |
* write-unlocking an unlocked lock throws IllegalMonitorStateException |
178 |
*/ |
179 |
public void testUnlock_IllegalMonitorStateException() { |
180 |
ReentrantReadWriteLock rl = new ReentrantReadWriteLock(); |
181 |
try { |
182 |
rl.writeLock().unlock(); |
183 |
shouldThrow(); |
184 |
} catch (IllegalMonitorStateException success) {} |
185 |
} |
186 |
|
187 |
|
188 |
/** |
189 |
* write-lockInterruptibly is interruptible |
190 |
*/ |
191 |
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
192 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
193 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
194 |
public void realRun() throws InterruptedException { |
195 |
lock.writeLock().lockInterruptibly(); |
196 |
lock.writeLock().unlock(); |
197 |
lock.writeLock().lockInterruptibly(); |
198 |
lock.writeLock().unlock(); |
199 |
}}); |
200 |
|
201 |
lock.writeLock().lock(); |
202 |
t.start(); |
203 |
Thread.sleep(SHORT_DELAY_MS); |
204 |
t.interrupt(); |
205 |
Thread.sleep(SHORT_DELAY_MS); |
206 |
lock.writeLock().unlock(); |
207 |
t.join(); |
208 |
} |
209 |
|
210 |
/** |
211 |
* timed write-tryLock is interruptible |
212 |
*/ |
213 |
public void testWriteTryLock_Interrupted() throws InterruptedException { |
214 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
215 |
lock.writeLock().lock(); |
216 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
217 |
public void realRun() throws InterruptedException { |
218 |
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
219 |
}}); |
220 |
|
221 |
t.start(); |
222 |
Thread.sleep(SHORT_DELAY_MS); |
223 |
t.interrupt(); |
224 |
lock.writeLock().unlock(); |
225 |
t.join(); |
226 |
} |
227 |
|
228 |
/** |
229 |
* read-lockInterruptibly is interruptible |
230 |
*/ |
231 |
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
232 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
233 |
lock.writeLock().lock(); |
234 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
235 |
public void realRun() throws InterruptedException { |
236 |
lock.readLock().lockInterruptibly(); |
237 |
}}); |
238 |
|
239 |
t.start(); |
240 |
Thread.sleep(SHORT_DELAY_MS); |
241 |
t.interrupt(); |
242 |
Thread.sleep(SHORT_DELAY_MS); |
243 |
lock.writeLock().unlock(); |
244 |
t.join(); |
245 |
} |
246 |
|
247 |
/** |
248 |
* timed read-tryLock is interruptible |
249 |
*/ |
250 |
public void testReadTryLock_Interrupted() throws InterruptedException { |
251 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
252 |
lock.writeLock().lock(); |
253 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
public void realRun() throws InterruptedException { |
255 |
lock.readLock().tryLock(1000,MILLISECONDS); |
256 |
}}); |
257 |
|
258 |
t.start(); |
259 |
t.interrupt(); |
260 |
t.join(); |
261 |
} |
262 |
|
263 |
|
264 |
/** |
265 |
* write-tryLock fails if locked |
266 |
*/ |
267 |
public void testWriteTryLockWhenLocked() throws InterruptedException { |
268 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
269 |
lock.writeLock().lock(); |
270 |
Thread t = new Thread(new CheckedRunnable() { |
271 |
public void realRun() { |
272 |
threadAssertFalse(lock.writeLock().tryLock()); |
273 |
}}); |
274 |
|
275 |
t.start(); |
276 |
t.join(); |
277 |
lock.writeLock().unlock(); |
278 |
} |
279 |
|
280 |
/** |
281 |
* read-tryLock fails if locked |
282 |
*/ |
283 |
public void testReadTryLockWhenLocked() throws InterruptedException { |
284 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
285 |
lock.writeLock().lock(); |
286 |
Thread t = new Thread(new CheckedRunnable() { |
287 |
public void realRun() { |
288 |
threadAssertFalse(lock.readLock().tryLock()); |
289 |
}}); |
290 |
|
291 |
t.start(); |
292 |
t.join(); |
293 |
lock.writeLock().unlock(); |
294 |
} |
295 |
|
296 |
/** |
297 |
* Multiple threads can hold a read lock when not write-locked |
298 |
*/ |
299 |
public void testMultipleReadLocks() throws InterruptedException { |
300 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
301 |
lock.readLock().lock(); |
302 |
Thread t = new Thread(new CheckedRunnable() { |
303 |
public void realRun() { |
304 |
threadAssertTrue(lock.readLock().tryLock()); |
305 |
lock.readLock().unlock(); |
306 |
}}); |
307 |
|
308 |
t.start(); |
309 |
t.join(); |
310 |
lock.readLock().unlock(); |
311 |
} |
312 |
|
313 |
/** |
314 |
* A writelock succeeds after reading threads unlock |
315 |
*/ |
316 |
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
317 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
318 |
lock.readLock().lock(); |
319 |
Thread t1 = new Thread(new CheckedRunnable() { |
320 |
public void realRun() { |
321 |
lock.readLock().lock(); |
322 |
lock.readLock().unlock(); |
323 |
}}); |
324 |
Thread t2 = new Thread(new CheckedRunnable() { |
325 |
public void realRun() { |
326 |
lock.writeLock().lock(); |
327 |
lock.writeLock().unlock(); |
328 |
}}); |
329 |
|
330 |
t1.start(); |
331 |
t2.start(); |
332 |
Thread.sleep(SHORT_DELAY_MS); |
333 |
lock.readLock().unlock(); |
334 |
t1.join(MEDIUM_DELAY_MS); |
335 |
t2.join(MEDIUM_DELAY_MS); |
336 |
assertTrue(!t1.isAlive()); |
337 |
assertTrue(!t2.isAlive()); |
338 |
} |
339 |
|
340 |
/** |
341 |
* Readlocks succeed after a writing thread unlocks |
342 |
*/ |
343 |
public void testReadAfterWriteLock() throws InterruptedException { |
344 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
345 |
lock.writeLock().lock(); |
346 |
Thread t1 = new Thread(new CheckedRunnable() { |
347 |
public void realRun() { |
348 |
lock.readLock().lock(); |
349 |
lock.readLock().unlock(); |
350 |
}}); |
351 |
Thread t2 = new Thread(new CheckedRunnable() { |
352 |
public void realRun() { |
353 |
lock.readLock().lock(); |
354 |
lock.readLock().unlock(); |
355 |
}}); |
356 |
|
357 |
t1.start(); |
358 |
t2.start(); |
359 |
Thread.sleep(SHORT_DELAY_MS); |
360 |
lock.writeLock().unlock(); |
361 |
t1.join(MEDIUM_DELAY_MS); |
362 |
t2.join(MEDIUM_DELAY_MS); |
363 |
assertTrue(!t1.isAlive()); |
364 |
assertTrue(!t2.isAlive()); |
365 |
} |
366 |
|
367 |
/** |
368 |
* Read trylock succeeds if write locked by current thread |
369 |
*/ |
370 |
public void testReadHoldingWriteLock() { |
371 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
372 |
lock.writeLock().lock(); |
373 |
assertTrue(lock.readLock().tryLock()); |
374 |
lock.readLock().unlock(); |
375 |
lock.writeLock().unlock(); |
376 |
} |
377 |
|
378 |
/** |
379 |
* Read lock succeeds if write locked by current thread even if |
380 |
* other threads are waiting for readlock |
381 |
*/ |
382 |
public void testReadHoldingWriteLock2() throws InterruptedException { |
383 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
384 |
lock.writeLock().lock(); |
385 |
Thread t1 = new Thread(new CheckedRunnable() { |
386 |
public void realRun() { |
387 |
lock.readLock().lock(); |
388 |
lock.readLock().unlock(); |
389 |
}}); |
390 |
Thread t2 = new Thread(new CheckedRunnable() { |
391 |
public void realRun() { |
392 |
lock.readLock().lock(); |
393 |
lock.readLock().unlock(); |
394 |
}}); |
395 |
|
396 |
t1.start(); |
397 |
t2.start(); |
398 |
lock.readLock().lock(); |
399 |
lock.readLock().unlock(); |
400 |
Thread.sleep(SHORT_DELAY_MS); |
401 |
lock.readLock().lock(); |
402 |
lock.readLock().unlock(); |
403 |
lock.writeLock().unlock(); |
404 |
t1.join(MEDIUM_DELAY_MS); |
405 |
t2.join(MEDIUM_DELAY_MS); |
406 |
assertTrue(!t1.isAlive()); |
407 |
assertTrue(!t2.isAlive()); |
408 |
} |
409 |
|
410 |
/** |
411 |
* Read lock succeeds if write locked by current thread even if |
412 |
* other threads are waiting for writelock |
413 |
*/ |
414 |
public void testReadHoldingWriteLock3() throws InterruptedException { |
415 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
416 |
lock.writeLock().lock(); |
417 |
Thread t1 = new Thread(new CheckedRunnable() { |
418 |
public void realRun() { |
419 |
lock.writeLock().lock(); |
420 |
lock.writeLock().unlock(); |
421 |
}}); |
422 |
Thread t2 = new Thread(new CheckedRunnable() { |
423 |
public void realRun() { |
424 |
lock.writeLock().lock(); |
425 |
lock.writeLock().unlock(); |
426 |
}}); |
427 |
|
428 |
t1.start(); |
429 |
t2.start(); |
430 |
lock.readLock().lock(); |
431 |
lock.readLock().unlock(); |
432 |
Thread.sleep(SHORT_DELAY_MS); |
433 |
lock.readLock().lock(); |
434 |
lock.readLock().unlock(); |
435 |
lock.writeLock().unlock(); |
436 |
t1.join(MEDIUM_DELAY_MS); |
437 |
t2.join(MEDIUM_DELAY_MS); |
438 |
assertTrue(!t1.isAlive()); |
439 |
assertTrue(!t2.isAlive()); |
440 |
} |
441 |
|
442 |
|
443 |
/** |
444 |
* Write lock succeeds if write locked by current thread even if |
445 |
* other threads are waiting for writelock |
446 |
*/ |
447 |
public void testWriteHoldingWriteLock4() throws InterruptedException { |
448 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
449 |
lock.writeLock().lock(); |
450 |
Thread t1 = new Thread(new CheckedRunnable() { |
451 |
public void realRun() { |
452 |
lock.writeLock().lock(); |
453 |
lock.writeLock().unlock(); |
454 |
}}); |
455 |
Thread t2 = new Thread(new CheckedRunnable() { |
456 |
public void realRun() { |
457 |
lock.writeLock().lock(); |
458 |
lock.writeLock().unlock(); |
459 |
}}); |
460 |
|
461 |
t1.start(); |
462 |
t2.start(); |
463 |
lock.writeLock().lock(); |
464 |
lock.writeLock().unlock(); |
465 |
Thread.sleep(SHORT_DELAY_MS); |
466 |
lock.writeLock().lock(); |
467 |
lock.writeLock().unlock(); |
468 |
lock.writeLock().unlock(); |
469 |
t1.join(MEDIUM_DELAY_MS); |
470 |
t2.join(MEDIUM_DELAY_MS); |
471 |
assertTrue(!t1.isAlive()); |
472 |
assertTrue(!t2.isAlive()); |
473 |
} |
474 |
|
475 |
|
476 |
/** |
477 |
* Fair Read trylock succeeds if write locked by current thread |
478 |
*/ |
479 |
public void testReadHoldingWriteLockFair() { |
480 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
481 |
lock.writeLock().lock(); |
482 |
assertTrue(lock.readLock().tryLock()); |
483 |
lock.readLock().unlock(); |
484 |
lock.writeLock().unlock(); |
485 |
} |
486 |
|
487 |
/** |
488 |
* Fair Read lock succeeds if write locked by current thread even if |
489 |
* other threads are waiting for readlock |
490 |
*/ |
491 |
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
492 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
493 |
lock.writeLock().lock(); |
494 |
Thread t1 = new Thread(new CheckedRunnable() { |
495 |
public void realRun() { |
496 |
lock.readLock().lock(); |
497 |
lock.readLock().unlock(); |
498 |
}}); |
499 |
Thread t2 = new Thread(new CheckedRunnable() { |
500 |
public void realRun() { |
501 |
lock.readLock().lock(); |
502 |
lock.readLock().unlock(); |
503 |
}}); |
504 |
|
505 |
t1.start(); |
506 |
t2.start(); |
507 |
lock.readLock().lock(); |
508 |
lock.readLock().unlock(); |
509 |
Thread.sleep(SHORT_DELAY_MS); |
510 |
lock.readLock().lock(); |
511 |
lock.readLock().unlock(); |
512 |
lock.writeLock().unlock(); |
513 |
t1.join(MEDIUM_DELAY_MS); |
514 |
t2.join(MEDIUM_DELAY_MS); |
515 |
assertTrue(!t1.isAlive()); |
516 |
assertTrue(!t2.isAlive()); |
517 |
} |
518 |
|
519 |
|
520 |
/** |
521 |
* Fair Read lock succeeds if write locked by current thread even if |
522 |
* other threads are waiting for writelock |
523 |
*/ |
524 |
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
525 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
526 |
lock.writeLock().lock(); |
527 |
Thread t1 = new Thread(new CheckedRunnable() { |
528 |
public void realRun() { |
529 |
lock.writeLock().lock(); |
530 |
lock.writeLock().unlock(); |
531 |
}}); |
532 |
Thread t2 = new Thread(new CheckedRunnable() { |
533 |
public void realRun() { |
534 |
lock.writeLock().lock(); |
535 |
lock.writeLock().unlock(); |
536 |
}}); |
537 |
|
538 |
t1.start(); |
539 |
t2.start(); |
540 |
lock.readLock().lock(); |
541 |
lock.readLock().unlock(); |
542 |
Thread.sleep(SHORT_DELAY_MS); |
543 |
lock.readLock().lock(); |
544 |
lock.readLock().unlock(); |
545 |
lock.writeLock().unlock(); |
546 |
t1.join(MEDIUM_DELAY_MS); |
547 |
t2.join(MEDIUM_DELAY_MS); |
548 |
assertTrue(!t1.isAlive()); |
549 |
assertTrue(!t2.isAlive()); |
550 |
} |
551 |
|
552 |
|
553 |
/** |
554 |
* Fair Write lock succeeds if write locked by current thread even if |
555 |
* other threads are waiting for writelock |
556 |
*/ |
557 |
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
558 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
559 |
lock.writeLock().lock(); |
560 |
Thread t1 = new Thread(new CheckedRunnable() { |
561 |
public void realRun() { |
562 |
lock.writeLock().lock(); |
563 |
lock.writeLock().unlock(); |
564 |
}}); |
565 |
Thread t2 = new Thread(new CheckedRunnable() { |
566 |
public void realRun() { |
567 |
lock.writeLock().lock(); |
568 |
lock.writeLock().unlock(); |
569 |
}}); |
570 |
|
571 |
t1.start(); |
572 |
t2.start(); |
573 |
Thread.sleep(SHORT_DELAY_MS); |
574 |
assertTrue(lock.isWriteLockedByCurrentThread()); |
575 |
assertTrue(lock.getWriteHoldCount() == 1); |
576 |
lock.writeLock().lock(); |
577 |
assertTrue(lock.getWriteHoldCount() == 2); |
578 |
lock.writeLock().unlock(); |
579 |
lock.writeLock().lock(); |
580 |
lock.writeLock().unlock(); |
581 |
lock.writeLock().unlock(); |
582 |
t1.join(MEDIUM_DELAY_MS); |
583 |
t2.join(MEDIUM_DELAY_MS); |
584 |
assertTrue(!t1.isAlive()); |
585 |
assertTrue(!t2.isAlive()); |
586 |
} |
587 |
|
588 |
|
589 |
/** |
590 |
* Read tryLock succeeds if readlocked but not writelocked |
591 |
*/ |
592 |
public void testTryLockWhenReadLocked() throws InterruptedException { |
593 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
594 |
lock.readLock().lock(); |
595 |
Thread t = new Thread(new CheckedRunnable() { |
596 |
public void realRun() { |
597 |
threadAssertTrue(lock.readLock().tryLock()); |
598 |
lock.readLock().unlock(); |
599 |
}}); |
600 |
|
601 |
t.start(); |
602 |
t.join(); |
603 |
lock.readLock().unlock(); |
604 |
} |
605 |
|
606 |
|
607 |
|
608 |
/** |
609 |
* write tryLock fails when readlocked |
610 |
*/ |
611 |
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
612 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
613 |
lock.readLock().lock(); |
614 |
Thread t = new Thread(new CheckedRunnable() { |
615 |
public void realRun() { |
616 |
threadAssertFalse(lock.writeLock().tryLock()); |
617 |
}}); |
618 |
|
619 |
t.start(); |
620 |
t.join(); |
621 |
lock.readLock().unlock(); |
622 |
} |
623 |
|
624 |
|
625 |
/** |
626 |
* Fair Read tryLock succeeds if readlocked but not writelocked |
627 |
*/ |
628 |
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
629 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
630 |
lock.readLock().lock(); |
631 |
Thread t = new Thread(new CheckedRunnable() { |
632 |
public void realRun() { |
633 |
threadAssertTrue(lock.readLock().tryLock()); |
634 |
lock.readLock().unlock(); |
635 |
}}); |
636 |
|
637 |
t.start(); |
638 |
t.join(); |
639 |
lock.readLock().unlock(); |
640 |
} |
641 |
|
642 |
|
643 |
|
644 |
/** |
645 |
* Fair write tryLock fails when readlocked |
646 |
*/ |
647 |
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
648 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
649 |
lock.readLock().lock(); |
650 |
Thread t = new Thread(new CheckedRunnable() { |
651 |
public void realRun() { |
652 |
threadAssertFalse(lock.writeLock().tryLock()); |
653 |
}}); |
654 |
|
655 |
t.start(); |
656 |
t.join(); |
657 |
lock.readLock().unlock(); |
658 |
} |
659 |
|
660 |
|
661 |
|
662 |
/** |
663 |
* write timed tryLock times out if locked |
664 |
*/ |
665 |
public void testWriteTryLock_Timeout() throws InterruptedException { |
666 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
667 |
lock.writeLock().lock(); |
668 |
Thread t = new Thread(new CheckedRunnable() { |
669 |
public void realRun() throws InterruptedException { |
670 |
threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
671 |
}}); |
672 |
|
673 |
t.start(); |
674 |
t.join(); |
675 |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
676 |
lock.writeLock().unlock(); |
677 |
} |
678 |
|
679 |
/** |
680 |
* read timed tryLock times out if write-locked |
681 |
*/ |
682 |
public void testReadTryLock_Timeout() throws InterruptedException { |
683 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
684 |
lock.writeLock().lock(); |
685 |
Thread t = new Thread(new CheckedRunnable() { |
686 |
public void realRun() throws InterruptedException { |
687 |
threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
688 |
}}); |
689 |
|
690 |
t.start(); |
691 |
t.join(); |
692 |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
693 |
lock.writeLock().unlock(); |
694 |
} |
695 |
|
696 |
|
697 |
/** |
698 |
* write lockInterruptibly succeeds if lock free else is interruptible |
699 |
*/ |
700 |
public void testWriteLockInterruptibly() throws InterruptedException { |
701 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
702 |
lock.writeLock().lockInterruptibly(); |
703 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
704 |
public void realRun() throws InterruptedException { |
705 |
lock.writeLock().lockInterruptibly(); |
706 |
}}); |
707 |
|
708 |
t.start(); |
709 |
Thread.sleep(SHORT_DELAY_MS); |
710 |
t.interrupt(); |
711 |
Thread.sleep(SHORT_DELAY_MS); |
712 |
t.join(); |
713 |
lock.writeLock().unlock(); |
714 |
} |
715 |
|
716 |
/** |
717 |
* read lockInterruptibly succeeds if lock free else is interruptible |
718 |
*/ |
719 |
public void testReadLockInterruptibly() throws InterruptedException { |
720 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
721 |
lock.writeLock().lockInterruptibly(); |
722 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
723 |
public void realRun() throws InterruptedException { |
724 |
lock.readLock().lockInterruptibly(); |
725 |
}}); |
726 |
|
727 |
t.start(); |
728 |
Thread.sleep(SHORT_DELAY_MS); |
729 |
t.interrupt(); |
730 |
t.join(); |
731 |
lock.writeLock().unlock(); |
732 |
} |
733 |
|
734 |
/** |
735 |
* Calling await without holding lock throws IllegalMonitorStateException |
736 |
*/ |
737 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
738 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
739 |
final Condition c = lock.writeLock().newCondition(); |
740 |
try { |
741 |
c.await(); |
742 |
shouldThrow(); |
743 |
} catch (IllegalMonitorStateException success) {} |
744 |
} |
745 |
|
746 |
/** |
747 |
* Calling signal without holding lock throws IllegalMonitorStateException |
748 |
*/ |
749 |
public void testSignal_IllegalMonitor() { |
750 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
751 |
final Condition c = lock.writeLock().newCondition(); |
752 |
try { |
753 |
c.signal(); |
754 |
shouldThrow(); |
755 |
} catch (IllegalMonitorStateException success) {} |
756 |
} |
757 |
|
758 |
/** |
759 |
* awaitNanos without a signal times out |
760 |
*/ |
761 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
762 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
763 |
final Condition c = lock.writeLock().newCondition(); |
764 |
|
765 |
lock.writeLock().lock(); |
766 |
long t = c.awaitNanos(100); |
767 |
assertTrue(t <= 0); |
768 |
lock.writeLock().unlock(); |
769 |
} |
770 |
|
771 |
|
772 |
/** |
773 |
* timed await without a signal times out |
774 |
*/ |
775 |
public void testAwait_Timeout() throws InterruptedException { |
776 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
777 |
final Condition c = lock.writeLock().newCondition(); |
778 |
lock.writeLock().lock(); |
779 |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
780 |
lock.writeLock().unlock(); |
781 |
} |
782 |
|
783 |
/** |
784 |
* awaitUntil without a signal times out |
785 |
*/ |
786 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
787 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
788 |
final Condition c = lock.writeLock().newCondition(); |
789 |
lock.writeLock().lock(); |
790 |
java.util.Date d = new java.util.Date(); |
791 |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
792 |
lock.writeLock().unlock(); |
793 |
} |
794 |
|
795 |
/** |
796 |
* await returns when signalled |
797 |
*/ |
798 |
public void testAwait() throws InterruptedException { |
799 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
800 |
final Condition c = lock.writeLock().newCondition(); |
801 |
Thread t = new Thread(new CheckedRunnable() { |
802 |
public void realRun() throws InterruptedException { |
803 |
lock.writeLock().lock(); |
804 |
c.await(); |
805 |
lock.writeLock().unlock(); |
806 |
}}); |
807 |
|
808 |
t.start(); |
809 |
Thread.sleep(SHORT_DELAY_MS); |
810 |
lock.writeLock().lock(); |
811 |
c.signal(); |
812 |
lock.writeLock().unlock(); |
813 |
t.join(SHORT_DELAY_MS); |
814 |
assertFalse(t.isAlive()); |
815 |
} |
816 |
|
817 |
/** A helper class for uninterruptible wait tests */ |
818 |
class UninterruptableThread extends Thread { |
819 |
private Lock lock; |
820 |
private Condition c; |
821 |
|
822 |
public volatile boolean canAwake = false; |
823 |
public volatile boolean interrupted = false; |
824 |
public volatile boolean lockStarted = false; |
825 |
|
826 |
public UninterruptableThread(Lock lock, Condition c) { |
827 |
this.lock = lock; |
828 |
this.c = c; |
829 |
} |
830 |
|
831 |
public synchronized void run() { |
832 |
lock.lock(); |
833 |
lockStarted = true; |
834 |
|
835 |
while (!canAwake) { |
836 |
c.awaitUninterruptibly(); |
837 |
} |
838 |
|
839 |
interrupted = isInterrupted(); |
840 |
lock.unlock(); |
841 |
} |
842 |
} |
843 |
|
844 |
/** |
845 |
* awaitUninterruptibly doesn't abort on interrupt |
846 |
*/ |
847 |
public void testAwaitUninterruptibly() throws InterruptedException { |
848 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
849 |
final Condition c = lock.writeLock().newCondition(); |
850 |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
851 |
|
852 |
thread.start(); |
853 |
|
854 |
while (!thread.lockStarted) { |
855 |
Thread.sleep(100); |
856 |
} |
857 |
|
858 |
lock.writeLock().lock(); |
859 |
try { |
860 |
thread.interrupt(); |
861 |
thread.canAwake = true; |
862 |
c.signal(); |
863 |
} finally { |
864 |
lock.writeLock().unlock(); |
865 |
} |
866 |
|
867 |
thread.join(); |
868 |
assertTrue(thread.interrupted); |
869 |
assertFalse(thread.isAlive()); |
870 |
} |
871 |
|
872 |
/** |
873 |
* await is interruptible |
874 |
*/ |
875 |
public void testAwait_Interrupt() throws InterruptedException { |
876 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
877 |
final Condition c = lock.writeLock().newCondition(); |
878 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
879 |
public void realRun() throws InterruptedException { |
880 |
lock.writeLock().lock(); |
881 |
c.await(); |
882 |
lock.writeLock().unlock(); |
883 |
}}); |
884 |
|
885 |
t.start(); |
886 |
Thread.sleep(SHORT_DELAY_MS); |
887 |
t.interrupt(); |
888 |
t.join(SHORT_DELAY_MS); |
889 |
assertFalse(t.isAlive()); |
890 |
} |
891 |
|
892 |
/** |
893 |
* awaitNanos is interruptible |
894 |
*/ |
895 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
896 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
897 |
final Condition c = lock.writeLock().newCondition(); |
898 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
899 |
public void realRun() throws InterruptedException { |
900 |
lock.writeLock().lock(); |
901 |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
902 |
lock.writeLock().unlock(); |
903 |
}}); |
904 |
|
905 |
t.start(); |
906 |
Thread.sleep(SHORT_DELAY_MS); |
907 |
t.interrupt(); |
908 |
t.join(SHORT_DELAY_MS); |
909 |
assertFalse(t.isAlive()); |
910 |
} |
911 |
|
912 |
/** |
913 |
* awaitUntil is interruptible |
914 |
*/ |
915 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
916 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
917 |
final Condition c = lock.writeLock().newCondition(); |
918 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
919 |
public void realRun() throws InterruptedException { |
920 |
lock.writeLock().lock(); |
921 |
java.util.Date d = new java.util.Date(); |
922 |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
923 |
lock.writeLock().unlock(); |
924 |
}}); |
925 |
|
926 |
t.start(); |
927 |
Thread.sleep(SHORT_DELAY_MS); |
928 |
t.interrupt(); |
929 |
t.join(SHORT_DELAY_MS); |
930 |
assertFalse(t.isAlive()); |
931 |
} |
932 |
|
933 |
/** |
934 |
* signalAll wakes up all threads |
935 |
*/ |
936 |
public void testSignalAll() throws InterruptedException { |
937 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
938 |
final Condition c = lock.writeLock().newCondition(); |
939 |
Thread t1 = new Thread(new CheckedRunnable() { |
940 |
public void realRun() throws InterruptedException { |
941 |
lock.writeLock().lock(); |
942 |
c.await(); |
943 |
lock.writeLock().unlock(); |
944 |
}}); |
945 |
|
946 |
Thread t2 = new Thread(new CheckedRunnable() { |
947 |
public void realRun() throws InterruptedException { |
948 |
lock.writeLock().lock(); |
949 |
c.await(); |
950 |
lock.writeLock().unlock(); |
951 |
}}); |
952 |
|
953 |
t1.start(); |
954 |
t2.start(); |
955 |
Thread.sleep(SHORT_DELAY_MS); |
956 |
lock.writeLock().lock(); |
957 |
c.signalAll(); |
958 |
lock.writeLock().unlock(); |
959 |
t1.join(SHORT_DELAY_MS); |
960 |
t2.join(SHORT_DELAY_MS); |
961 |
assertFalse(t1.isAlive()); |
962 |
assertFalse(t2.isAlive()); |
963 |
} |
964 |
|
965 |
/** |
966 |
* A serialized lock deserializes as unlocked |
967 |
*/ |
968 |
public void testSerialization() throws Exception { |
969 |
ReentrantReadWriteLock l = new ReentrantReadWriteLock(); |
970 |
l.readLock().lock(); |
971 |
l.readLock().unlock(); |
972 |
|
973 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
974 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
975 |
out.writeObject(l); |
976 |
out.close(); |
977 |
|
978 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
979 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
980 |
ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject(); |
981 |
r.readLock().lock(); |
982 |
r.readLock().unlock(); |
983 |
} |
984 |
|
985 |
/** |
986 |
* hasQueuedThreads reports whether there are waiting threads |
987 |
*/ |
988 |
public void testhasQueuedThreads() throws InterruptedException { |
989 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
990 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
991 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
992 |
assertFalse(lock.hasQueuedThreads()); |
993 |
lock.writeLock().lock(); |
994 |
t1.start(); |
995 |
Thread.sleep(SHORT_DELAY_MS); |
996 |
assertTrue(lock.hasQueuedThreads()); |
997 |
t2.start(); |
998 |
Thread.sleep(SHORT_DELAY_MS); |
999 |
assertTrue(lock.hasQueuedThreads()); |
1000 |
t1.interrupt(); |
1001 |
Thread.sleep(SHORT_DELAY_MS); |
1002 |
assertTrue(lock.hasQueuedThreads()); |
1003 |
lock.writeLock().unlock(); |
1004 |
Thread.sleep(SHORT_DELAY_MS); |
1005 |
assertFalse(lock.hasQueuedThreads()); |
1006 |
t1.join(); |
1007 |
t2.join(); |
1008 |
} |
1009 |
|
1010 |
/** |
1011 |
* hasQueuedThread(null) throws NPE |
1012 |
*/ |
1013 |
public void testHasQueuedThreadNPE() { |
1014 |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1015 |
try { |
1016 |
sync.hasQueuedThread(null); |
1017 |
shouldThrow(); |
1018 |
} catch (NullPointerException success) {} |
1019 |
} |
1020 |
|
1021 |
/** |
1022 |
* hasQueuedThread reports whether a thread is queued. |
1023 |
*/ |
1024 |
public void testHasQueuedThread() throws InterruptedException { |
1025 |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1026 |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1027 |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1028 |
assertFalse(sync.hasQueuedThread(t1)); |
1029 |
assertFalse(sync.hasQueuedThread(t2)); |
1030 |
sync.writeLock().lock(); |
1031 |
t1.start(); |
1032 |
Thread.sleep(SHORT_DELAY_MS); |
1033 |
assertTrue(sync.hasQueuedThread(t1)); |
1034 |
t2.start(); |
1035 |
Thread.sleep(SHORT_DELAY_MS); |
1036 |
assertTrue(sync.hasQueuedThread(t1)); |
1037 |
assertTrue(sync.hasQueuedThread(t2)); |
1038 |
t1.interrupt(); |
1039 |
Thread.sleep(SHORT_DELAY_MS); |
1040 |
assertFalse(sync.hasQueuedThread(t1)); |
1041 |
assertTrue(sync.hasQueuedThread(t2)); |
1042 |
sync.writeLock().unlock(); |
1043 |
Thread.sleep(SHORT_DELAY_MS); |
1044 |
assertFalse(sync.hasQueuedThread(t1)); |
1045 |
Thread.sleep(SHORT_DELAY_MS); |
1046 |
assertFalse(sync.hasQueuedThread(t2)); |
1047 |
t1.join(); |
1048 |
t2.join(); |
1049 |
} |
1050 |
|
1051 |
|
1052 |
/** |
1053 |
* getQueueLength reports number of waiting threads |
1054 |
*/ |
1055 |
public void testGetQueueLength() throws InterruptedException { |
1056 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1057 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1058 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1059 |
assertEquals(0, lock.getQueueLength()); |
1060 |
lock.writeLock().lock(); |
1061 |
t1.start(); |
1062 |
Thread.sleep(SHORT_DELAY_MS); |
1063 |
assertEquals(1, lock.getQueueLength()); |
1064 |
t2.start(); |
1065 |
Thread.sleep(SHORT_DELAY_MS); |
1066 |
assertEquals(2, lock.getQueueLength()); |
1067 |
t1.interrupt(); |
1068 |
Thread.sleep(SHORT_DELAY_MS); |
1069 |
assertEquals(1, lock.getQueueLength()); |
1070 |
lock.writeLock().unlock(); |
1071 |
Thread.sleep(SHORT_DELAY_MS); |
1072 |
assertEquals(0, lock.getQueueLength()); |
1073 |
t1.join(); |
1074 |
t2.join(); |
1075 |
} |
1076 |
|
1077 |
/** |
1078 |
* getQueuedThreads includes waiting threads |
1079 |
*/ |
1080 |
public void testGetQueuedThreads() throws InterruptedException { |
1081 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1082 |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1083 |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1084 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1085 |
lock.writeLock().lock(); |
1086 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1087 |
t1.start(); |
1088 |
Thread.sleep(SHORT_DELAY_MS); |
1089 |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1090 |
t2.start(); |
1091 |
Thread.sleep(SHORT_DELAY_MS); |
1092 |
assertTrue(lock.getQueuedThreads().contains(t1)); |
1093 |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1094 |
t1.interrupt(); |
1095 |
Thread.sleep(SHORT_DELAY_MS); |
1096 |
assertFalse(lock.getQueuedThreads().contains(t1)); |
1097 |
assertTrue(lock.getQueuedThreads().contains(t2)); |
1098 |
lock.writeLock().unlock(); |
1099 |
Thread.sleep(SHORT_DELAY_MS); |
1100 |
assertTrue(lock.getQueuedThreads().isEmpty()); |
1101 |
t1.join(); |
1102 |
t2.join(); |
1103 |
} |
1104 |
|
1105 |
/** |
1106 |
* hasWaiters throws NPE if null |
1107 |
*/ |
1108 |
public void testHasWaitersNPE() { |
1109 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1110 |
try { |
1111 |
lock.hasWaiters(null); |
1112 |
shouldThrow(); |
1113 |
} catch (NullPointerException success) {} |
1114 |
} |
1115 |
|
1116 |
/** |
1117 |
* getWaitQueueLength throws NPE if null |
1118 |
*/ |
1119 |
public void testGetWaitQueueLengthNPE() { |
1120 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1121 |
try { |
1122 |
lock.getWaitQueueLength(null); |
1123 |
shouldThrow(); |
1124 |
} catch (NullPointerException success) {} |
1125 |
} |
1126 |
|
1127 |
|
1128 |
/** |
1129 |
* getWaitingThreads throws NPE if null |
1130 |
*/ |
1131 |
public void testGetWaitingThreadsNPE() { |
1132 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1133 |
try { |
1134 |
lock.getWaitingThreads(null); |
1135 |
shouldThrow(); |
1136 |
} catch (NullPointerException success) {} |
1137 |
} |
1138 |
|
1139 |
/** |
1140 |
* hasWaiters throws IAE if not owned |
1141 |
*/ |
1142 |
public void testHasWaitersIAE() { |
1143 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1144 |
final Condition c = lock.writeLock().newCondition(); |
1145 |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1146 |
try { |
1147 |
lock2.hasWaiters(c); |
1148 |
shouldThrow(); |
1149 |
} catch (IllegalArgumentException success) {} |
1150 |
} |
1151 |
|
1152 |
/** |
1153 |
* hasWaiters throws IMSE if not locked |
1154 |
*/ |
1155 |
public void testHasWaitersIMSE() { |
1156 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1157 |
final Condition c = lock.writeLock().newCondition(); |
1158 |
try { |
1159 |
lock.hasWaiters(c); |
1160 |
shouldThrow(); |
1161 |
} catch (IllegalMonitorStateException success) {} |
1162 |
} |
1163 |
|
1164 |
|
1165 |
/** |
1166 |
* getWaitQueueLength throws IAE if not owned |
1167 |
*/ |
1168 |
public void testGetWaitQueueLengthIAE() { |
1169 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1170 |
final Condition c = lock.writeLock().newCondition(); |
1171 |
final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(); |
1172 |
try { |
1173 |
lock2.getWaitQueueLength(c); |
1174 |
shouldThrow(); |
1175 |
} catch (IllegalArgumentException success) {} |
1176 |
} |
1177 |
|
1178 |
/** |
1179 |
* getWaitQueueLength throws IMSE if not locked |
1180 |
*/ |
1181 |
public void testGetWaitQueueLengthIMSE() { |
1182 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1183 |
final Condition c = lock.writeLock().newCondition(); |
1184 |
try { |
1185 |
lock.getWaitQueueLength(c); |
1186 |
shouldThrow(); |
1187 |
} catch (IllegalMonitorStateException success) {} |
1188 |
} |
1189 |
|
1190 |
|
1191 |
/** |
1192 |
* getWaitingThreads throws IAE if not owned |
1193 |
*/ |
1194 |
public void testGetWaitingThreadsIAE() { |
1195 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1196 |
final Condition c = lock.writeLock().newCondition(); |
1197 |
final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock(); |
1198 |
try { |
1199 |
lock2.getWaitingThreads(c); |
1200 |
shouldThrow(); |
1201 |
} catch (IllegalArgumentException success) {} |
1202 |
} |
1203 |
|
1204 |
/** |
1205 |
* getWaitingThreads throws IMSE if not locked |
1206 |
*/ |
1207 |
public void testGetWaitingThreadsIMSE() { |
1208 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1209 |
final Condition c = lock.writeLock().newCondition(); |
1210 |
try { |
1211 |
lock.getWaitingThreads(c); |
1212 |
shouldThrow(); |
1213 |
} catch (IllegalMonitorStateException success) {} |
1214 |
} |
1215 |
|
1216 |
|
1217 |
/** |
1218 |
* hasWaiters returns true when a thread is waiting, else false |
1219 |
*/ |
1220 |
public void testHasWaiters() throws InterruptedException { |
1221 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1222 |
final Condition c = lock.writeLock().newCondition(); |
1223 |
Thread t = new Thread(new CheckedRunnable() { |
1224 |
public void realRun() throws InterruptedException { |
1225 |
lock.writeLock().lock(); |
1226 |
threadAssertFalse(lock.hasWaiters(c)); |
1227 |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1228 |
c.await(); |
1229 |
lock.writeLock().unlock(); |
1230 |
}}); |
1231 |
|
1232 |
t.start(); |
1233 |
Thread.sleep(SHORT_DELAY_MS); |
1234 |
lock.writeLock().lock(); |
1235 |
assertTrue(lock.hasWaiters(c)); |
1236 |
assertEquals(1, lock.getWaitQueueLength(c)); |
1237 |
c.signal(); |
1238 |
lock.writeLock().unlock(); |
1239 |
Thread.sleep(SHORT_DELAY_MS); |
1240 |
lock.writeLock().lock(); |
1241 |
assertFalse(lock.hasWaiters(c)); |
1242 |
assertEquals(0, lock.getWaitQueueLength(c)); |
1243 |
lock.writeLock().unlock(); |
1244 |
t.join(SHORT_DELAY_MS); |
1245 |
assertFalse(t.isAlive()); |
1246 |
} |
1247 |
|
1248 |
/** |
1249 |
* getWaitQueueLength returns number of waiting threads |
1250 |
*/ |
1251 |
public void testGetWaitQueueLength() throws InterruptedException { |
1252 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1253 |
final Condition c = lock.writeLock().newCondition(); |
1254 |
Thread t = new Thread(new CheckedRunnable() { |
1255 |
public void realRun() throws InterruptedException { |
1256 |
lock.writeLock().lock(); |
1257 |
threadAssertFalse(lock.hasWaiters(c)); |
1258 |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1259 |
c.await(); |
1260 |
lock.writeLock().unlock(); |
1261 |
}}); |
1262 |
|
1263 |
t.start(); |
1264 |
Thread.sleep(SHORT_DELAY_MS); |
1265 |
lock.writeLock().lock(); |
1266 |
assertTrue(lock.hasWaiters(c)); |
1267 |
assertEquals(1, lock.getWaitQueueLength(c)); |
1268 |
c.signal(); |
1269 |
lock.writeLock().unlock(); |
1270 |
Thread.sleep(SHORT_DELAY_MS); |
1271 |
lock.writeLock().lock(); |
1272 |
assertFalse(lock.hasWaiters(c)); |
1273 |
assertEquals(0, lock.getWaitQueueLength(c)); |
1274 |
lock.writeLock().unlock(); |
1275 |
t.join(SHORT_DELAY_MS); |
1276 |
assertFalse(t.isAlive()); |
1277 |
} |
1278 |
|
1279 |
|
1280 |
/** |
1281 |
* getWaitingThreads returns only and all waiting threads |
1282 |
*/ |
1283 |
public void testGetWaitingThreads() throws InterruptedException { |
1284 |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1285 |
final Condition c = lock.writeLock().newCondition(); |
1286 |
Thread t1 = new Thread(new CheckedRunnable() { |
1287 |
public void realRun() throws InterruptedException { |
1288 |
lock.writeLock().lock(); |
1289 |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1290 |
c.await(); |
1291 |
lock.writeLock().unlock(); |
1292 |
}}); |
1293 |
|
1294 |
Thread t2 = new Thread(new CheckedRunnable() { |
1295 |
public void realRun() throws InterruptedException { |
1296 |
lock.writeLock().lock(); |
1297 |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1298 |
c.await(); |
1299 |
lock.writeLock().unlock(); |
1300 |
}}); |
1301 |
|
1302 |
lock.writeLock().lock(); |
1303 |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1304 |
lock.writeLock().unlock(); |
1305 |
t1.start(); |
1306 |
Thread.sleep(SHORT_DELAY_MS); |
1307 |
t2.start(); |
1308 |
Thread.sleep(SHORT_DELAY_MS); |
1309 |
lock.writeLock().lock(); |
1310 |
assertTrue(lock.hasWaiters(c)); |
1311 |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
1312 |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
1313 |
c.signalAll(); |
1314 |
lock.writeLock().unlock(); |
1315 |
Thread.sleep(SHORT_DELAY_MS); |
1316 |
lock.writeLock().lock(); |
1317 |
assertFalse(lock.hasWaiters(c)); |
1318 |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1319 |
lock.writeLock().unlock(); |
1320 |
t1.join(SHORT_DELAY_MS); |
1321 |
t2.join(SHORT_DELAY_MS); |
1322 |
assertFalse(t1.isAlive()); |
1323 |
assertFalse(t2.isAlive()); |
1324 |
} |
1325 |
|
1326 |
/** |
1327 |
* toString indicates current lock state |
1328 |
*/ |
1329 |
public void testToString() { |
1330 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1331 |
String us = lock.toString(); |
1332 |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1333 |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1334 |
lock.writeLock().lock(); |
1335 |
String ws = lock.toString(); |
1336 |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1337 |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1338 |
lock.writeLock().unlock(); |
1339 |
lock.readLock().lock(); |
1340 |
lock.readLock().lock(); |
1341 |
String rs = lock.toString(); |
1342 |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1343 |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1344 |
} |
1345 |
|
1346 |
/** |
1347 |
* readLock.toString indicates current lock state |
1348 |
*/ |
1349 |
public void testReadLockToString() { |
1350 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1351 |
String us = lock.readLock().toString(); |
1352 |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1353 |
lock.readLock().lock(); |
1354 |
lock.readLock().lock(); |
1355 |
String rs = lock.readLock().toString(); |
1356 |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1357 |
} |
1358 |
|
1359 |
/** |
1360 |
* writeLock.toString indicates current lock state |
1361 |
*/ |
1362 |
public void testWriteLockToString() { |
1363 |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1364 |
String us = lock.writeLock().toString(); |
1365 |
assertTrue(us.indexOf("Unlocked") >= 0); |
1366 |
lock.writeLock().lock(); |
1367 |
String ls = lock.writeLock().toString(); |
1368 |
assertTrue(ls.indexOf("Locked") >= 0); |
1369 |
} |
1370 |
|
1371 |
} |