ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
Revision: 1.72
Committed: Fri Feb 27 22:06:24 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.71: +6 -10 lines
Log Message:
catch clause readability

File Contents

# Content
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/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.locks.Condition;
17 import java.util.concurrent.locks.Lock;
18 import java.util.concurrent.locks.ReentrantReadWriteLock;
19
20 import junit.framework.AssertionFailedError;
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 public class ReentrantReadWriteLockTest extends JSR166TestCase {
25 public static void main(String[] args) {
26 junit.textui.TestRunner.run(suite());
27 }
28 public static Test suite() {
29 return new TestSuite(ReentrantReadWriteLockTest.class);
30 }
31
32 /**
33 * A runnable calling lockInterruptibly
34 */
35 class InterruptibleLockRunnable extends CheckedRunnable {
36 final ReentrantReadWriteLock lock;
37 InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
38 public void realRun() throws InterruptedException {
39 lock.writeLock().lockInterruptibly();
40 }
41 }
42
43 /**
44 * A runnable calling lockInterruptibly that expects to be
45 * interrupted
46 */
47 class InterruptedLockRunnable extends CheckedInterruptedRunnable {
48 final ReentrantReadWriteLock lock;
49 InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
50 public void realRun() throws InterruptedException {
51 lock.writeLock().lockInterruptibly();
52 }
53 }
54
55 /**
56 * Subclass to expose protected methods
57 */
58 static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
59 PublicReentrantReadWriteLock() { super(); }
60 PublicReentrantReadWriteLock(boolean fair) { super(fair); }
61 public Thread getOwner() {
62 return super.getOwner();
63 }
64 public Collection<Thread> getQueuedThreads() {
65 return super.getQueuedThreads();
66 }
67 public Collection<Thread> getWaitingThreads(Condition c) {
68 return super.getWaitingThreads(c);
69 }
70 }
71
72 /**
73 * Releases write lock, checking that it had a hold count of 1.
74 */
75 void releaseWriteLock(PublicReentrantReadWriteLock lock) {
76 ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
77 assertWriteLockedByMoi(lock);
78 assertEquals(1, lock.getWriteHoldCount());
79 writeLock.unlock();
80 assertNotWriteLocked(lock);
81 }
82
83 /**
84 * Spin-waits until lock.hasQueuedThread(t) becomes true.
85 */
86 void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
87 long startTime = System.nanoTime();
88 while (!lock.hasQueuedThread(t)) {
89 if (millisElapsedSince(startTime) > LONG_DELAY_MS)
90 throw new AssertionFailedError("timed out");
91 Thread.yield();
92 }
93 assertTrue(t.isAlive());
94 assertNotSame(t, lock.getOwner());
95 }
96
97 /**
98 * Checks that lock is not write-locked.
99 */
100 void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
101 assertFalse(lock.isWriteLocked());
102 assertFalse(lock.isWriteLockedByCurrentThread());
103 assertFalse(lock.writeLock().isHeldByCurrentThread());
104 assertEquals(0, lock.getWriteHoldCount());
105 assertEquals(0, lock.writeLock().getHoldCount());
106 assertNull(lock.getOwner());
107 }
108
109 /**
110 * Checks that lock is write-locked by the given thread.
111 */
112 void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
113 assertTrue(lock.isWriteLocked());
114 assertSame(t, lock.getOwner());
115 assertEquals(t == Thread.currentThread(),
116 lock.isWriteLockedByCurrentThread());
117 assertEquals(t == Thread.currentThread(),
118 lock.writeLock().isHeldByCurrentThread());
119 assertEquals(t == Thread.currentThread(),
120 lock.getWriteHoldCount() > 0);
121 assertEquals(t == Thread.currentThread(),
122 lock.writeLock().getHoldCount() > 0);
123 assertEquals(0, lock.getReadLockCount());
124 }
125
126 /**
127 * Checks that lock is write-locked by the current thread.
128 */
129 void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
130 assertWriteLockedBy(lock, Thread.currentThread());
131 }
132
133 /**
134 * Checks that condition c has no waiters.
135 */
136 void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
137 assertHasWaiters(lock, c, new Thread[] {});
138 }
139
140 /**
141 * Checks that condition c has exactly the given waiter threads.
142 */
143 void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
144 Thread... threads) {
145 lock.writeLock().lock();
146 assertEquals(threads.length > 0, lock.hasWaiters(c));
147 assertEquals(threads.length, lock.getWaitQueueLength(c));
148 assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
149 assertEquals(threads.length, lock.getWaitingThreads(c).size());
150 assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
151 new HashSet<Thread>(Arrays.asList(threads)));
152 lock.writeLock().unlock();
153 }
154
155 enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
156
157 /**
158 * Awaits condition using the specified AwaitMethod.
159 */
160 void await(Condition c, AwaitMethod awaitMethod)
161 throws InterruptedException {
162 switch (awaitMethod) {
163 case await:
164 c.await();
165 break;
166 case awaitTimed:
167 assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
168 break;
169 case awaitNanos:
170 long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
171 assertTrue(nanosRemaining > 0);
172 break;
173 case awaitUntil:
174 java.util.Date d = new java.util.Date();
175 assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
176 break;
177 default:
178 throw new AssertionError();
179 }
180 }
181
182 /**
183 * Constructor sets given fairness, and is in unlocked state
184 */
185 public void testConstructor() {
186 PublicReentrantReadWriteLock lock;
187
188 lock = new PublicReentrantReadWriteLock();
189 assertFalse(lock.isFair());
190 assertNotWriteLocked(lock);
191 assertEquals(0, lock.getReadLockCount());
192
193 lock = new PublicReentrantReadWriteLock(true);
194 assertTrue(lock.isFair());
195 assertNotWriteLocked(lock);
196 assertEquals(0, lock.getReadLockCount());
197
198 lock = new PublicReentrantReadWriteLock(false);
199 assertFalse(lock.isFair());
200 assertNotWriteLocked(lock);
201 assertEquals(0, lock.getReadLockCount());
202 }
203
204 /**
205 * write-locking and read-locking an unlocked lock succeed
206 */
207 public void testLock() { testLock(false); }
208 public void testLock_fair() { testLock(true); }
209 public void testLock(boolean fair) {
210 PublicReentrantReadWriteLock lock =
211 new PublicReentrantReadWriteLock(fair);
212 assertNotWriteLocked(lock);
213 lock.writeLock().lock();
214 assertWriteLockedByMoi(lock);
215 lock.writeLock().unlock();
216 assertNotWriteLocked(lock);
217 assertEquals(0, lock.getReadLockCount());
218 lock.readLock().lock();
219 assertNotWriteLocked(lock);
220 assertEquals(1, lock.getReadLockCount());
221 lock.readLock().unlock();
222 assertNotWriteLocked(lock);
223 assertEquals(0, lock.getReadLockCount());
224 }
225
226 /**
227 * getWriteHoldCount returns number of recursive holds
228 */
229 public void testGetWriteHoldCount() { testGetWriteHoldCount(false); }
230 public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
231 public void testGetWriteHoldCount(boolean fair) {
232 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
233 for (int i = 1; i <= SIZE; i++) {
234 lock.writeLock().lock();
235 assertEquals(i,lock.getWriteHoldCount());
236 }
237 for (int i = SIZE; i > 0; i--) {
238 lock.writeLock().unlock();
239 assertEquals(i-1,lock.getWriteHoldCount());
240 }
241 }
242
243 /**
244 * writelock.getHoldCount returns number of recursive holds
245 */
246 public void testGetHoldCount() { testGetHoldCount(false); }
247 public void testGetHoldCount_fair() { testGetHoldCount(true); }
248 public void testGetHoldCount(boolean fair) {
249 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
250 for (int i = 1; i <= SIZE; i++) {
251 lock.writeLock().lock();
252 assertEquals(i,lock.writeLock().getHoldCount());
253 }
254 for (int i = SIZE; i > 0; i--) {
255 lock.writeLock().unlock();
256 assertEquals(i-1,lock.writeLock().getHoldCount());
257 }
258 }
259
260 /**
261 * getReadHoldCount returns number of recursive holds
262 */
263 public void testGetReadHoldCount() { testGetReadHoldCount(false); }
264 public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
265 public void testGetReadHoldCount(boolean fair) {
266 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
267 for (int i = 1; i <= SIZE; i++) {
268 lock.readLock().lock();
269 assertEquals(i,lock.getReadHoldCount());
270 }
271 for (int i = SIZE; i > 0; i--) {
272 lock.readLock().unlock();
273 assertEquals(i-1,lock.getReadHoldCount());
274 }
275 }
276
277 /**
278 * write-unlocking an unlocked lock throws IllegalMonitorStateException
279 */
280 public void testWriteUnlock_IMSE() { testWriteUnlock_IMSE(false); }
281 public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
282 public void testWriteUnlock_IMSE(boolean fair) {
283 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
284 try {
285 lock.writeLock().unlock();
286 shouldThrow();
287 } catch (IllegalMonitorStateException success) {}
288 }
289
290 /**
291 * read-unlocking an unlocked lock throws IllegalMonitorStateException
292 */
293 public void testReadUnlock_IMSE() { testReadUnlock_IMSE(false); }
294 public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
295 public void testReadUnlock_IMSE(boolean fair) {
296 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
297 try {
298 lock.readLock().unlock();
299 shouldThrow();
300 } catch (IllegalMonitorStateException success) {}
301 }
302
303 /**
304 * write-lockInterruptibly is interruptible
305 */
306 public void testWriteLockInterruptibly_Interruptible() { testWriteLockInterruptibly_Interruptible(false); }
307 public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
308 public void testWriteLockInterruptibly_Interruptible(boolean fair) {
309 final PublicReentrantReadWriteLock lock =
310 new PublicReentrantReadWriteLock(fair);
311 lock.writeLock().lock();
312 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
313 public void realRun() throws InterruptedException {
314 lock.writeLock().lockInterruptibly();
315 }});
316
317 waitForQueuedThread(lock, t);
318 t.interrupt();
319 awaitTermination(t);
320 releaseWriteLock(lock);
321 }
322
323 /**
324 * timed write-tryLock is interruptible
325 */
326 public void testWriteTryLock_Interruptible() { testWriteTryLock_Interruptible(false); }
327 public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
328 public void testWriteTryLock_Interruptible(boolean fair) {
329 final PublicReentrantReadWriteLock lock =
330 new PublicReentrantReadWriteLock(fair);
331 lock.writeLock().lock();
332 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
333 public void realRun() throws InterruptedException {
334 lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
335 }});
336
337 waitForQueuedThread(lock, t);
338 t.interrupt();
339 awaitTermination(t);
340 releaseWriteLock(lock);
341 }
342
343 /**
344 * read-lockInterruptibly is interruptible
345 */
346 public void testReadLockInterruptibly_Interruptible() { testReadLockInterruptibly_Interruptible(false); }
347 public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
348 public void testReadLockInterruptibly_Interruptible(boolean fair) {
349 final PublicReentrantReadWriteLock lock =
350 new PublicReentrantReadWriteLock(fair);
351 lock.writeLock().lock();
352 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
353 public void realRun() throws InterruptedException {
354 lock.readLock().lockInterruptibly();
355 }});
356
357 waitForQueuedThread(lock, t);
358 t.interrupt();
359 awaitTermination(t);
360 releaseWriteLock(lock);
361 }
362
363 /**
364 * timed read-tryLock is interruptible
365 */
366 public void testReadTryLock_Interruptible() { testReadTryLock_Interruptible(false); }
367 public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
368 public void testReadTryLock_Interruptible(boolean fair) {
369 final PublicReentrantReadWriteLock lock =
370 new PublicReentrantReadWriteLock(fair);
371 lock.writeLock().lock();
372 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
373 public void realRun() throws InterruptedException {
374 lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
375 }});
376
377 waitForQueuedThread(lock, t);
378 t.interrupt();
379 awaitTermination(t);
380 releaseWriteLock(lock);
381 }
382
383 /**
384 * write-tryLock on an unlocked lock succeeds
385 */
386 public void testWriteTryLock() { testWriteTryLock(false); }
387 public void testWriteTryLock_fair() { testWriteTryLock(true); }
388 public void testWriteTryLock(boolean fair) {
389 final PublicReentrantReadWriteLock lock =
390 new PublicReentrantReadWriteLock(fair);
391 assertTrue(lock.writeLock().tryLock());
392 assertWriteLockedByMoi(lock);
393 assertTrue(lock.writeLock().tryLock());
394 assertWriteLockedByMoi(lock);
395 lock.writeLock().unlock();
396 releaseWriteLock(lock);
397 }
398
399 /**
400 * write-tryLock fails if locked
401 */
402 public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); }
403 public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
404 public void testWriteTryLockWhenLocked(boolean fair) {
405 final PublicReentrantReadWriteLock lock =
406 new PublicReentrantReadWriteLock(fair);
407 lock.writeLock().lock();
408 Thread t = newStartedThread(new CheckedRunnable() {
409 public void realRun() {
410 assertFalse(lock.writeLock().tryLock());
411 }});
412
413 awaitTermination(t);
414 releaseWriteLock(lock);
415 }
416
417 /**
418 * read-tryLock fails if locked
419 */
420 public void testReadTryLockWhenLocked() { testReadTryLockWhenLocked(false); }
421 public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
422 public void testReadTryLockWhenLocked(boolean fair) {
423 final PublicReentrantReadWriteLock lock =
424 new PublicReentrantReadWriteLock(fair);
425 lock.writeLock().lock();
426 Thread t = newStartedThread(new CheckedRunnable() {
427 public void realRun() {
428 assertFalse(lock.readLock().tryLock());
429 }});
430
431 awaitTermination(t);
432 releaseWriteLock(lock);
433 }
434
435 /**
436 * Multiple threads can hold a read lock when not write-locked
437 */
438 public void testMultipleReadLocks() { testMultipleReadLocks(false); }
439 public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
440 public void testMultipleReadLocks(boolean fair) {
441 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
442 lock.readLock().lock();
443 Thread t = newStartedThread(new CheckedRunnable() {
444 public void realRun() throws InterruptedException {
445 assertTrue(lock.readLock().tryLock());
446 lock.readLock().unlock();
447 assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
448 lock.readLock().unlock();
449 lock.readLock().lock();
450 lock.readLock().unlock();
451 }});
452
453 awaitTermination(t);
454 lock.readLock().unlock();
455 }
456
457 /**
458 * A writelock succeeds only after a reading thread unlocks
459 */
460 public void testWriteAfterReadLock() { testWriteAfterReadLock(false); }
461 public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
462 public void testWriteAfterReadLock(boolean fair) {
463 final PublicReentrantReadWriteLock lock =
464 new PublicReentrantReadWriteLock(fair);
465 lock.readLock().lock();
466 Thread t = newStartedThread(new CheckedRunnable() {
467 public void realRun() {
468 assertEquals(1, lock.getReadLockCount());
469 lock.writeLock().lock();
470 assertEquals(0, lock.getReadLockCount());
471 lock.writeLock().unlock();
472 }});
473 waitForQueuedThread(lock, t);
474 assertNotWriteLocked(lock);
475 assertEquals(1, lock.getReadLockCount());
476 lock.readLock().unlock();
477 assertEquals(0, lock.getReadLockCount());
478 awaitTermination(t);
479 assertNotWriteLocked(lock);
480 }
481
482 /**
483 * A writelock succeeds only after reading threads unlock
484 */
485 public void testWriteAfterMultipleReadLocks() { testWriteAfterMultipleReadLocks(false); }
486 public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
487 public void testWriteAfterMultipleReadLocks(boolean fair) {
488 final PublicReentrantReadWriteLock lock =
489 new PublicReentrantReadWriteLock(fair);
490 lock.readLock().lock();
491 lock.readLock().lock();
492 Thread t1 = newStartedThread(new CheckedRunnable() {
493 public void realRun() {
494 lock.readLock().lock();
495 assertEquals(3, lock.getReadLockCount());
496 lock.readLock().unlock();
497 }});
498 awaitTermination(t1);
499
500 Thread t2 = newStartedThread(new CheckedRunnable() {
501 public void realRun() {
502 assertEquals(2, lock.getReadLockCount());
503 lock.writeLock().lock();
504 assertEquals(0, lock.getReadLockCount());
505 lock.writeLock().unlock();
506 }});
507 waitForQueuedThread(lock, t2);
508 assertNotWriteLocked(lock);
509 assertEquals(2, lock.getReadLockCount());
510 lock.readLock().unlock();
511 lock.readLock().unlock();
512 assertEquals(0, lock.getReadLockCount());
513 awaitTermination(t2);
514 assertNotWriteLocked(lock);
515 }
516
517 /**
518 * A thread that tries to acquire a fair read lock (non-reentrantly)
519 * will block if there is a waiting writer thread
520 */
521 public void testReaderWriterReaderFairFifo() {
522 final PublicReentrantReadWriteLock lock =
523 new PublicReentrantReadWriteLock(true);
524 final AtomicBoolean t1GotLock = new AtomicBoolean(false);
525
526 lock.readLock().lock();
527 Thread t1 = newStartedThread(new CheckedRunnable() {
528 public void realRun() {
529 assertEquals(1, lock.getReadLockCount());
530 lock.writeLock().lock();
531 assertEquals(0, lock.getReadLockCount());
532 t1GotLock.set(true);
533 lock.writeLock().unlock();
534 }});
535 waitForQueuedThread(lock, t1);
536
537 Thread t2 = newStartedThread(new CheckedRunnable() {
538 public void realRun() {
539 assertEquals(1, lock.getReadLockCount());
540 lock.readLock().lock();
541 assertEquals(1, lock.getReadLockCount());
542 assertTrue(t1GotLock.get());
543 lock.readLock().unlock();
544 }});
545 waitForQueuedThread(lock, t2);
546 assertTrue(t1.isAlive());
547 assertNotWriteLocked(lock);
548 assertEquals(1, lock.getReadLockCount());
549 lock.readLock().unlock();
550 awaitTermination(t1);
551 awaitTermination(t2);
552 assertNotWriteLocked(lock);
553 }
554
555 /**
556 * Readlocks succeed only after a writing thread unlocks
557 */
558 public void testReadAfterWriteLock() { testReadAfterWriteLock(false); }
559 public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
560 public void testReadAfterWriteLock(boolean fair) {
561 final PublicReentrantReadWriteLock lock =
562 new PublicReentrantReadWriteLock(fair);
563 lock.writeLock().lock();
564 Thread t1 = newStartedThread(new CheckedRunnable() {
565 public void realRun() {
566 lock.readLock().lock();
567 lock.readLock().unlock();
568 }});
569 Thread t2 = newStartedThread(new CheckedRunnable() {
570 public void realRun() {
571 lock.readLock().lock();
572 lock.readLock().unlock();
573 }});
574
575 waitForQueuedThread(lock, t1);
576 waitForQueuedThread(lock, t2);
577 releaseWriteLock(lock);
578 awaitTermination(t1);
579 awaitTermination(t2);
580 }
581
582 /**
583 * Read trylock succeeds if write locked by current thread
584 */
585 public void testReadHoldingWriteLock() { testReadHoldingWriteLock(false); }
586 public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
587 public void testReadHoldingWriteLock(boolean fair) {
588 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
589 lock.writeLock().lock();
590 assertTrue(lock.readLock().tryLock());
591 lock.readLock().unlock();
592 lock.writeLock().unlock();
593 }
594
595 /**
596 * Read trylock succeeds (barging) even in the presence of waiting
597 * readers and/or writers
598 */
599 public void testReadTryLockBarging() { testReadTryLockBarging(false); }
600 public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
601 public void testReadTryLockBarging(boolean fair) {
602 final PublicReentrantReadWriteLock lock =
603 new PublicReentrantReadWriteLock(fair);
604 lock.readLock().lock();
605
606 Thread t1 = newStartedThread(new CheckedRunnable() {
607 public void realRun() {
608 lock.writeLock().lock();
609 lock.writeLock().unlock();
610 }});
611
612 waitForQueuedThread(lock, t1);
613
614 Thread t2 = newStartedThread(new CheckedRunnable() {
615 public void realRun() {
616 lock.readLock().lock();
617 lock.readLock().unlock();
618 }});
619
620 if (fair)
621 waitForQueuedThread(lock, t2);
622
623 Thread t3 = newStartedThread(new CheckedRunnable() {
624 public void realRun() {
625 lock.readLock().tryLock();
626 lock.readLock().unlock();
627 }});
628
629 assertTrue(lock.getReadLockCount() > 0);
630 awaitTermination(t3);
631 assertTrue(t1.isAlive());
632 if (fair) assertTrue(t2.isAlive());
633 lock.readLock().unlock();
634 awaitTermination(t1);
635 awaitTermination(t2);
636 }
637
638 /**
639 * Read lock succeeds if write locked by current thread even if
640 * other threads are waiting for readlock
641 */
642 public void testReadHoldingWriteLock2() { testReadHoldingWriteLock2(false); }
643 public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
644 public void testReadHoldingWriteLock2(boolean fair) {
645 final PublicReentrantReadWriteLock lock =
646 new PublicReentrantReadWriteLock(fair);
647 lock.writeLock().lock();
648 lock.readLock().lock();
649 lock.readLock().unlock();
650
651 Thread t1 = newStartedThread(new CheckedRunnable() {
652 public void realRun() {
653 lock.readLock().lock();
654 lock.readLock().unlock();
655 }});
656 Thread t2 = newStartedThread(new CheckedRunnable() {
657 public void realRun() {
658 lock.readLock().lock();
659 lock.readLock().unlock();
660 }});
661
662 waitForQueuedThread(lock, t1);
663 waitForQueuedThread(lock, t2);
664 assertWriteLockedByMoi(lock);
665 lock.readLock().lock();
666 lock.readLock().unlock();
667 releaseWriteLock(lock);
668 awaitTermination(t1);
669 awaitTermination(t2);
670 }
671
672 /**
673 * Read lock succeeds if write locked by current thread even if
674 * other threads are waiting for writelock
675 */
676 public void testReadHoldingWriteLock3() { testReadHoldingWriteLock3(false); }
677 public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
678 public void testReadHoldingWriteLock3(boolean fair) {
679 final PublicReentrantReadWriteLock lock =
680 new PublicReentrantReadWriteLock(fair);
681 lock.writeLock().lock();
682 lock.readLock().lock();
683 lock.readLock().unlock();
684
685 Thread t1 = newStartedThread(new CheckedRunnable() {
686 public void realRun() {
687 lock.writeLock().lock();
688 lock.writeLock().unlock();
689 }});
690 Thread t2 = newStartedThread(new CheckedRunnable() {
691 public void realRun() {
692 lock.writeLock().lock();
693 lock.writeLock().unlock();
694 }});
695
696 waitForQueuedThread(lock, t1);
697 waitForQueuedThread(lock, t2);
698 assertWriteLockedByMoi(lock);
699 lock.readLock().lock();
700 lock.readLock().unlock();
701 assertWriteLockedByMoi(lock);
702 lock.writeLock().unlock();
703 awaitTermination(t1);
704 awaitTermination(t2);
705 }
706
707 /**
708 * Write lock succeeds if write locked by current thread even if
709 * other threads are waiting for writelock
710 */
711 public void testWriteHoldingWriteLock4() { testWriteHoldingWriteLock4(false); }
712 public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
713 public void testWriteHoldingWriteLock4(boolean fair) {
714 final PublicReentrantReadWriteLock lock =
715 new PublicReentrantReadWriteLock(fair);
716 lock.writeLock().lock();
717 lock.writeLock().lock();
718 lock.writeLock().unlock();
719
720 Thread t1 = newStartedThread(new CheckedRunnable() {
721 public void realRun() {
722 lock.writeLock().lock();
723 lock.writeLock().unlock();
724 }});
725 Thread t2 = newStartedThread(new CheckedRunnable() {
726 public void realRun() {
727 lock.writeLock().lock();
728 lock.writeLock().unlock();
729 }});
730
731 waitForQueuedThread(lock, t1);
732 waitForQueuedThread(lock, t2);
733 assertWriteLockedByMoi(lock);
734 assertEquals(1, lock.getWriteHoldCount());
735 lock.writeLock().lock();
736 assertWriteLockedByMoi(lock);
737 assertEquals(2, lock.getWriteHoldCount());
738 lock.writeLock().unlock();
739 assertWriteLockedByMoi(lock);
740 assertEquals(1, lock.getWriteHoldCount());
741 lock.writeLock().unlock();
742 awaitTermination(t1);
743 awaitTermination(t2);
744 }
745
746 /**
747 * Read tryLock succeeds if readlocked but not writelocked
748 */
749 public void testTryLockWhenReadLocked() { testTryLockWhenReadLocked(false); }
750 public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
751 public void testTryLockWhenReadLocked(boolean fair) {
752 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
753 lock.readLock().lock();
754 Thread t = newStartedThread(new CheckedRunnable() {
755 public void realRun() {
756 assertTrue(lock.readLock().tryLock());
757 lock.readLock().unlock();
758 }});
759
760 awaitTermination(t);
761 lock.readLock().unlock();
762 }
763
764 /**
765 * write tryLock fails when readlocked
766 */
767 public void testWriteTryLockWhenReadLocked() { testWriteTryLockWhenReadLocked(false); }
768 public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
769 public void testWriteTryLockWhenReadLocked(boolean fair) {
770 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
771 lock.readLock().lock();
772 Thread t = newStartedThread(new CheckedRunnable() {
773 public void realRun() {
774 assertFalse(lock.writeLock().tryLock());
775 }});
776
777 awaitTermination(t);
778 lock.readLock().unlock();
779 }
780
781 /**
782 * write timed tryLock times out if locked
783 */
784 public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); }
785 public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
786 public void testWriteTryLock_Timeout(boolean fair) {
787 final PublicReentrantReadWriteLock lock =
788 new PublicReentrantReadWriteLock(fair);
789 lock.writeLock().lock();
790 Thread t = newStartedThread(new CheckedRunnable() {
791 public void realRun() throws InterruptedException {
792 long startTime = System.nanoTime();
793 long timeoutMillis = 10;
794 assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
795 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
796 }});
797
798 awaitTermination(t);
799 releaseWriteLock(lock);
800 }
801
802 /**
803 * read timed tryLock times out if write-locked
804 */
805 public void testReadTryLock_Timeout() { testReadTryLock_Timeout(false); }
806 public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
807 public void testReadTryLock_Timeout(boolean fair) {
808 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
809 lock.writeLock().lock();
810 Thread t = newStartedThread(new CheckedRunnable() {
811 public void realRun() throws InterruptedException {
812 long startTime = System.nanoTime();
813 long timeoutMillis = 10;
814 assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
815 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
816 }});
817
818 awaitTermination(t);
819 assertTrue(lock.writeLock().isHeldByCurrentThread());
820 lock.writeLock().unlock();
821 }
822
823 /**
824 * write lockInterruptibly succeeds if unlocked, else is interruptible
825 */
826 public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); }
827 public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
828 public void testWriteLockInterruptibly(boolean fair) {
829 final PublicReentrantReadWriteLock lock =
830 new PublicReentrantReadWriteLock(fair);
831 try {
832 lock.writeLock().lockInterruptibly();
833 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
834 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
835 public void realRun() throws InterruptedException {
836 lock.writeLock().lockInterruptibly();
837 }});
838
839 waitForQueuedThread(lock, t);
840 t.interrupt();
841 assertTrue(lock.writeLock().isHeldByCurrentThread());
842 awaitTermination(t);
843 releaseWriteLock(lock);
844 }
845
846 /**
847 * read lockInterruptibly succeeds if lock free else is interruptible
848 */
849 public void testReadLockInterruptibly() { testReadLockInterruptibly(false); }
850 public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
851 public void testReadLockInterruptibly(boolean fair) {
852 final PublicReentrantReadWriteLock lock =
853 new PublicReentrantReadWriteLock(fair);
854 try {
855 lock.readLock().lockInterruptibly();
856 lock.readLock().unlock();
857 lock.writeLock().lockInterruptibly();
858 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
859 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
860 public void realRun() throws InterruptedException {
861 lock.readLock().lockInterruptibly();
862 }});
863
864 waitForQueuedThread(lock, t);
865 t.interrupt();
866 awaitTermination(t);
867 releaseWriteLock(lock);
868 }
869
870 /**
871 * Calling await without holding lock throws IllegalMonitorStateException
872 */
873 public void testAwait_IMSE() { testAwait_IMSE(false); }
874 public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
875 public void testAwait_IMSE(boolean fair) {
876 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
877 final Condition c = lock.writeLock().newCondition();
878 for (AwaitMethod awaitMethod : AwaitMethod.values()) {
879 long startTime = System.nanoTime();
880 try {
881 await(c, awaitMethod);
882 shouldThrow();
883 } catch (IllegalMonitorStateException success) {
884 } catch (InterruptedException fail) {
885 threadUnexpectedException(fail);
886 }
887 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
888 }
889 }
890
891 /**
892 * Calling signal without holding lock throws IllegalMonitorStateException
893 */
894 public void testSignal_IMSE() { testSignal_IMSE(false); }
895 public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
896 public void testSignal_IMSE(boolean fair) {
897 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
898 final Condition c = lock.writeLock().newCondition();
899 try {
900 c.signal();
901 shouldThrow();
902 } catch (IllegalMonitorStateException success) {}
903 }
904
905 /**
906 * Calling signalAll without holding lock throws IllegalMonitorStateException
907 */
908 public void testSignalAll_IMSE() { testSignalAll_IMSE(false); }
909 public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
910 public void testSignalAll_IMSE(boolean fair) {
911 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
912 final Condition c = lock.writeLock().newCondition();
913 try {
914 c.signalAll();
915 shouldThrow();
916 } catch (IllegalMonitorStateException success) {}
917 }
918
919 /**
920 * awaitNanos without a signal times out
921 */
922 public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); }
923 public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
924 public void testAwaitNanos_Timeout(boolean fair) {
925 try {
926 final ReentrantReadWriteLock lock =
927 new ReentrantReadWriteLock(fair);
928 final Condition c = lock.writeLock().newCondition();
929 lock.writeLock().lock();
930 long startTime = System.nanoTime();
931 long timeoutMillis = 10;
932 long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
933 long nanosRemaining = c.awaitNanos(timeoutNanos);
934 assertTrue(nanosRemaining <= 0);
935 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
936 lock.writeLock().unlock();
937 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
938 }
939
940 /**
941 * timed await without a signal times out
942 */
943 public void testAwait_Timeout() { testAwait_Timeout(false); }
944 public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
945 public void testAwait_Timeout(boolean fair) {
946 try {
947 final ReentrantReadWriteLock lock =
948 new ReentrantReadWriteLock(fair);
949 final Condition c = lock.writeLock().newCondition();
950 lock.writeLock().lock();
951 long startTime = System.nanoTime();
952 long timeoutMillis = 10;
953 assertFalse(c.await(timeoutMillis, MILLISECONDS));
954 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
955 lock.writeLock().unlock();
956 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
957 }
958
959 /**
960 * awaitUntil without a signal times out
961 */
962 public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); }
963 public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
964 public void testAwaitUntil_Timeout(boolean fair) {
965 try {
966 final ReentrantReadWriteLock lock =
967 new ReentrantReadWriteLock(fair);
968 final Condition c = lock.writeLock().newCondition();
969 lock.writeLock().lock();
970 long startTime = System.nanoTime();
971 long timeoutMillis = 10;
972 java.util.Date d = new java.util.Date();
973 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
974 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
975 lock.writeLock().unlock();
976 } catch (InterruptedException fail) { threadUnexpectedException(fail); }
977 }
978
979 /**
980 * await returns when signalled
981 */
982 public void testAwait() { testAwait(false); }
983 public void testAwait_fair() { testAwait(true); }
984 public void testAwait(boolean fair) {
985 final PublicReentrantReadWriteLock lock =
986 new PublicReentrantReadWriteLock(fair);
987 final Condition c = lock.writeLock().newCondition();
988 final CountDownLatch locked = new CountDownLatch(1);
989 Thread t = newStartedThread(new CheckedRunnable() {
990 public void realRun() throws InterruptedException {
991 lock.writeLock().lock();
992 locked.countDown();
993 c.await();
994 lock.writeLock().unlock();
995 }});
996
997 await(locked);
998 lock.writeLock().lock();
999 assertHasWaiters(lock, c, t);
1000 c.signal();
1001 assertHasNoWaiters(lock, c);
1002 assertTrue(t.isAlive());
1003 lock.writeLock().unlock();
1004 awaitTermination(t);
1005 }
1006
1007 /**
1008 * awaitUninterruptibly is uninterruptible
1009 */
1010 public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); }
1011 public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1012 public void testAwaitUninterruptibly(boolean fair) {
1013 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1014 final Condition c = lock.writeLock().newCondition();
1015 final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1016
1017 Thread t1 = newStartedThread(new CheckedRunnable() {
1018 public void realRun() {
1019 // Interrupt before awaitUninterruptibly
1020 lock.writeLock().lock();
1021 pleaseInterrupt.countDown();
1022 Thread.currentThread().interrupt();
1023 c.awaitUninterruptibly();
1024 assertTrue(Thread.interrupted());
1025 lock.writeLock().unlock();
1026 }});
1027
1028 Thread t2 = newStartedThread(new CheckedRunnable() {
1029 public void realRun() {
1030 // Interrupt during awaitUninterruptibly
1031 lock.writeLock().lock();
1032 pleaseInterrupt.countDown();
1033 c.awaitUninterruptibly();
1034 assertTrue(Thread.interrupted());
1035 lock.writeLock().unlock();
1036 }});
1037
1038 await(pleaseInterrupt);
1039 lock.writeLock().lock();
1040 lock.writeLock().unlock();
1041 t2.interrupt();
1042
1043 assertThreadStaysAlive(t1);
1044 assertTrue(t2.isAlive());
1045
1046 lock.writeLock().lock();
1047 c.signalAll();
1048 lock.writeLock().unlock();
1049
1050 awaitTermination(t1);
1051 awaitTermination(t2);
1052 }
1053
1054 /**
1055 * await/awaitNanos/awaitUntil is interruptible
1056 */
1057 public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); }
1058 public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); }
1059 public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); }
1060 public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); }
1061 public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); }
1062 public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); }
1063 public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); }
1064 public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); }
1065 public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1066 final PublicReentrantReadWriteLock lock =
1067 new PublicReentrantReadWriteLock(fair);
1068 final Condition c = lock.writeLock().newCondition();
1069 final CountDownLatch locked = new CountDownLatch(1);
1070 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1071 public void realRun() throws InterruptedException {
1072 lock.writeLock().lock();
1073 assertWriteLockedByMoi(lock);
1074 assertHasNoWaiters(lock, c);
1075 locked.countDown();
1076 try {
1077 await(c, awaitMethod);
1078 } finally {
1079 assertWriteLockedByMoi(lock);
1080 assertHasNoWaiters(lock, c);
1081 lock.writeLock().unlock();
1082 assertFalse(Thread.interrupted());
1083 }
1084 }});
1085
1086 await(locked);
1087 assertHasWaiters(lock, c, t);
1088 t.interrupt();
1089 awaitTermination(t);
1090 assertNotWriteLocked(lock);
1091 }
1092
1093 /**
1094 * signalAll wakes up all threads
1095 */
1096 public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); }
1097 public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); }
1098 public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); }
1099 public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); }
1100 public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); }
1101 public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); }
1102 public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); }
1103 public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); }
1104 public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1105 final PublicReentrantReadWriteLock lock =
1106 new PublicReentrantReadWriteLock(fair);
1107 final Condition c = lock.writeLock().newCondition();
1108 final CountDownLatch locked = new CountDownLatch(2);
1109 final Lock writeLock = lock.writeLock();
1110 class Awaiter extends CheckedRunnable {
1111 public void realRun() throws InterruptedException {
1112 writeLock.lock();
1113 locked.countDown();
1114 await(c, awaitMethod);
1115 writeLock.unlock();
1116 }
1117 }
1118
1119 Thread t1 = newStartedThread(new Awaiter());
1120 Thread t2 = newStartedThread(new Awaiter());
1121
1122 await(locked);
1123 writeLock.lock();
1124 assertHasWaiters(lock, c, t1, t2);
1125 c.signalAll();
1126 assertHasNoWaiters(lock, c);
1127 writeLock.unlock();
1128 awaitTermination(t1);
1129 awaitTermination(t2);
1130 }
1131
1132 /**
1133 * signal wakes up waiting threads in FIFO order
1134 */
1135 public void testSignalWakesFifo() { testSignalWakesFifo(false); }
1136 public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1137 public void testSignalWakesFifo(boolean fair) {
1138 final PublicReentrantReadWriteLock lock =
1139 new PublicReentrantReadWriteLock(fair);
1140 final Condition c = lock.writeLock().newCondition();
1141 final CountDownLatch locked1 = new CountDownLatch(1);
1142 final CountDownLatch locked2 = new CountDownLatch(1);
1143 final Lock writeLock = lock.writeLock();
1144 Thread t1 = newStartedThread(new CheckedRunnable() {
1145 public void realRun() throws InterruptedException {
1146 writeLock.lock();
1147 locked1.countDown();
1148 c.await();
1149 writeLock.unlock();
1150 }});
1151
1152 await(locked1);
1153
1154 Thread t2 = newStartedThread(new CheckedRunnable() {
1155 public void realRun() throws InterruptedException {
1156 writeLock.lock();
1157 locked2.countDown();
1158 c.await();
1159 writeLock.unlock();
1160 }});
1161
1162 await(locked2);
1163
1164 writeLock.lock();
1165 assertHasWaiters(lock, c, t1, t2);
1166 assertFalse(lock.hasQueuedThreads());
1167 c.signal();
1168 assertHasWaiters(lock, c, t2);
1169 assertTrue(lock.hasQueuedThread(t1));
1170 assertFalse(lock.hasQueuedThread(t2));
1171 c.signal();
1172 assertHasNoWaiters(lock, c);
1173 assertTrue(lock.hasQueuedThread(t1));
1174 assertTrue(lock.hasQueuedThread(t2));
1175 writeLock.unlock();
1176 awaitTermination(t1);
1177 awaitTermination(t2);
1178 }
1179
1180 /**
1181 * await after multiple reentrant locking preserves lock count
1182 */
1183 public void testAwaitLockCount() { testAwaitLockCount(false); }
1184 public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1185 public void testAwaitLockCount(boolean fair) {
1186 final PublicReentrantReadWriteLock lock =
1187 new PublicReentrantReadWriteLock(fair);
1188 final Condition c = lock.writeLock().newCondition();
1189 final CountDownLatch locked = new CountDownLatch(2);
1190 Thread t1 = newStartedThread(new CheckedRunnable() {
1191 public void realRun() throws InterruptedException {
1192 lock.writeLock().lock();
1193 assertWriteLockedByMoi(lock);
1194 assertEquals(1, lock.writeLock().getHoldCount());
1195 locked.countDown();
1196 c.await();
1197 assertWriteLockedByMoi(lock);
1198 assertEquals(1, lock.writeLock().getHoldCount());
1199 lock.writeLock().unlock();
1200 }});
1201
1202 Thread t2 = newStartedThread(new CheckedRunnable() {
1203 public void realRun() throws InterruptedException {
1204 lock.writeLock().lock();
1205 lock.writeLock().lock();
1206 assertWriteLockedByMoi(lock);
1207 assertEquals(2, lock.writeLock().getHoldCount());
1208 locked.countDown();
1209 c.await();
1210 assertWriteLockedByMoi(lock);
1211 assertEquals(2, lock.writeLock().getHoldCount());
1212 lock.writeLock().unlock();
1213 lock.writeLock().unlock();
1214 }});
1215
1216 await(locked);
1217 lock.writeLock().lock();
1218 assertHasWaiters(lock, c, t1, t2);
1219 c.signalAll();
1220 assertHasNoWaiters(lock, c);
1221 lock.writeLock().unlock();
1222 awaitTermination(t1);
1223 awaitTermination(t2);
1224 }
1225
1226 /**
1227 * A serialized lock deserializes as unlocked
1228 */
1229 public void testSerialization() { testSerialization(false); }
1230 public void testSerialization_fair() { testSerialization(true); }
1231 public void testSerialization(boolean fair) {
1232 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1233 lock.writeLock().lock();
1234 lock.readLock().lock();
1235
1236 ReentrantReadWriteLock clone = serialClone(lock);
1237 assertEquals(lock.isFair(), clone.isFair());
1238 assertTrue(lock.isWriteLocked());
1239 assertFalse(clone.isWriteLocked());
1240 assertEquals(1, lock.getReadLockCount());
1241 assertEquals(0, clone.getReadLockCount());
1242 clone.writeLock().lock();
1243 clone.readLock().lock();
1244 assertTrue(clone.isWriteLocked());
1245 assertEquals(1, clone.getReadLockCount());
1246 clone.readLock().unlock();
1247 clone.writeLock().unlock();
1248 assertFalse(clone.isWriteLocked());
1249 assertEquals(1, lock.getReadLockCount());
1250 assertEquals(0, clone.getReadLockCount());
1251 }
1252
1253 /**
1254 * hasQueuedThreads reports whether there are waiting threads
1255 */
1256 public void testHasQueuedThreads() { testHasQueuedThreads(false); }
1257 public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
1258 public void testHasQueuedThreads(boolean fair) {
1259 final PublicReentrantReadWriteLock lock =
1260 new PublicReentrantReadWriteLock(fair);
1261 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1262 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1263 assertFalse(lock.hasQueuedThreads());
1264 lock.writeLock().lock();
1265 assertFalse(lock.hasQueuedThreads());
1266 t1.start();
1267 waitForQueuedThread(lock, t1);
1268 assertTrue(lock.hasQueuedThreads());
1269 t2.start();
1270 waitForQueuedThread(lock, t2);
1271 assertTrue(lock.hasQueuedThreads());
1272 t1.interrupt();
1273 awaitTermination(t1);
1274 assertTrue(lock.hasQueuedThreads());
1275 lock.writeLock().unlock();
1276 awaitTermination(t2);
1277 assertFalse(lock.hasQueuedThreads());
1278 }
1279
1280 /**
1281 * hasQueuedThread(null) throws NPE
1282 */
1283 public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); }
1284 public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
1285 public void testHasQueuedThreadNPE(boolean fair) {
1286 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1287 try {
1288 lock.hasQueuedThread(null);
1289 shouldThrow();
1290 } catch (NullPointerException success) {}
1291 }
1292
1293 /**
1294 * hasQueuedThread reports whether a thread is queued
1295 */
1296 public void testHasQueuedThread() { testHasQueuedThread(false); }
1297 public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
1298 public void testHasQueuedThread(boolean fair) {
1299 final PublicReentrantReadWriteLock lock =
1300 new PublicReentrantReadWriteLock(fair);
1301 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1302 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1303 assertFalse(lock.hasQueuedThread(t1));
1304 assertFalse(lock.hasQueuedThread(t2));
1305 lock.writeLock().lock();
1306 t1.start();
1307 waitForQueuedThread(lock, t1);
1308 assertTrue(lock.hasQueuedThread(t1));
1309 assertFalse(lock.hasQueuedThread(t2));
1310 t2.start();
1311 waitForQueuedThread(lock, t2);
1312 assertTrue(lock.hasQueuedThread(t1));
1313 assertTrue(lock.hasQueuedThread(t2));
1314 t1.interrupt();
1315 awaitTermination(t1);
1316 assertFalse(lock.hasQueuedThread(t1));
1317 assertTrue(lock.hasQueuedThread(t2));
1318 lock.writeLock().unlock();
1319 awaitTermination(t2);
1320 assertFalse(lock.hasQueuedThread(t1));
1321 assertFalse(lock.hasQueuedThread(t2));
1322 }
1323
1324 /**
1325 * getQueueLength reports number of waiting threads
1326 */
1327 public void testGetQueueLength() { testGetQueueLength(false); }
1328 public void testGetQueueLength_fair() { testGetQueueLength(true); }
1329 public void testGetQueueLength(boolean fair) {
1330 final PublicReentrantReadWriteLock lock =
1331 new PublicReentrantReadWriteLock(fair);
1332 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1333 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1334 assertEquals(0, lock.getQueueLength());
1335 lock.writeLock().lock();
1336 t1.start();
1337 waitForQueuedThread(lock, t1);
1338 assertEquals(1, lock.getQueueLength());
1339 t2.start();
1340 waitForQueuedThread(lock, t2);
1341 assertEquals(2, lock.getQueueLength());
1342 t1.interrupt();
1343 awaitTermination(t1);
1344 assertEquals(1, lock.getQueueLength());
1345 lock.writeLock().unlock();
1346 awaitTermination(t2);
1347 assertEquals(0, lock.getQueueLength());
1348 }
1349
1350 /**
1351 * getQueuedThreads includes waiting threads
1352 */
1353 public void testGetQueuedThreads() { testGetQueuedThreads(false); }
1354 public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
1355 public void testGetQueuedThreads(boolean fair) {
1356 final PublicReentrantReadWriteLock lock =
1357 new PublicReentrantReadWriteLock(fair);
1358 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1359 Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1360 assertTrue(lock.getQueuedThreads().isEmpty());
1361 lock.writeLock().lock();
1362 assertTrue(lock.getQueuedThreads().isEmpty());
1363 t1.start();
1364 waitForQueuedThread(lock, t1);
1365 assertEquals(1, lock.getQueuedThreads().size());
1366 assertTrue(lock.getQueuedThreads().contains(t1));
1367 t2.start();
1368 waitForQueuedThread(lock, t2);
1369 assertEquals(2, lock.getQueuedThreads().size());
1370 assertTrue(lock.getQueuedThreads().contains(t1));
1371 assertTrue(lock.getQueuedThreads().contains(t2));
1372 t1.interrupt();
1373 awaitTermination(t1);
1374 assertFalse(lock.getQueuedThreads().contains(t1));
1375 assertTrue(lock.getQueuedThreads().contains(t2));
1376 assertEquals(1, lock.getQueuedThreads().size());
1377 lock.writeLock().unlock();
1378 awaitTermination(t2);
1379 assertTrue(lock.getQueuedThreads().isEmpty());
1380 }
1381
1382 /**
1383 * hasWaiters throws NPE if null
1384 */
1385 public void testHasWaitersNPE() { testHasWaitersNPE(false); }
1386 public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
1387 public void testHasWaitersNPE(boolean fair) {
1388 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1389 try {
1390 lock.hasWaiters(null);
1391 shouldThrow();
1392 } catch (NullPointerException success) {}
1393 }
1394
1395 /**
1396 * getWaitQueueLength throws NPE if null
1397 */
1398 public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); }
1399 public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
1400 public void testGetWaitQueueLengthNPE(boolean fair) {
1401 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1402 try {
1403 lock.getWaitQueueLength(null);
1404 shouldThrow();
1405 } catch (NullPointerException success) {}
1406 }
1407
1408 /**
1409 * getWaitingThreads throws NPE if null
1410 */
1411 public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); }
1412 public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
1413 public void testGetWaitingThreadsNPE(boolean fair) {
1414 final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair);
1415 try {
1416 lock.getWaitingThreads(null);
1417 shouldThrow();
1418 } catch (NullPointerException success) {}
1419 }
1420
1421 /**
1422 * hasWaiters throws IllegalArgumentException if not owned
1423 */
1424 public void testHasWaitersIAE() { testHasWaitersIAE(false); }
1425 public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
1426 public void testHasWaitersIAE(boolean fair) {
1427 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1428 final Condition c = lock.writeLock().newCondition();
1429 final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1430 try {
1431 lock2.hasWaiters(c);
1432 shouldThrow();
1433 } catch (IllegalArgumentException success) {}
1434 }
1435
1436 /**
1437 * hasWaiters throws IllegalMonitorStateException if not locked
1438 */
1439 public void testHasWaitersIMSE() { testHasWaitersIMSE(false); }
1440 public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
1441 public void testHasWaitersIMSE(boolean fair) {
1442 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1443 final Condition c = lock.writeLock().newCondition();
1444 try {
1445 lock.hasWaiters(c);
1446 shouldThrow();
1447 } catch (IllegalMonitorStateException success) {}
1448 }
1449
1450 /**
1451 * getWaitQueueLength throws IllegalArgumentException if not owned
1452 */
1453 public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); }
1454 public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
1455 public void testGetWaitQueueLengthIAE(boolean fair) {
1456 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1457 final Condition c = lock.writeLock().newCondition();
1458 final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1459 try {
1460 lock2.getWaitQueueLength(c);
1461 shouldThrow();
1462 } catch (IllegalArgumentException success) {}
1463 }
1464
1465 /**
1466 * getWaitQueueLength throws IllegalMonitorStateException if not locked
1467 */
1468 public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); }
1469 public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
1470 public void testGetWaitQueueLengthIMSE(boolean fair) {
1471 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1472 final Condition c = lock.writeLock().newCondition();
1473 try {
1474 lock.getWaitQueueLength(c);
1475 shouldThrow();
1476 } catch (IllegalMonitorStateException success) {}
1477 }
1478
1479 /**
1480 * getWaitingThreads throws IllegalArgumentException if not owned
1481 */
1482 public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); }
1483 public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
1484 public void testGetWaitingThreadsIAE(boolean fair) {
1485 final PublicReentrantReadWriteLock lock =
1486 new PublicReentrantReadWriteLock(fair);
1487 final Condition c = lock.writeLock().newCondition();
1488 final PublicReentrantReadWriteLock lock2 =
1489 new PublicReentrantReadWriteLock(fair);
1490 try {
1491 lock2.getWaitingThreads(c);
1492 shouldThrow();
1493 } catch (IllegalArgumentException success) {}
1494 }
1495
1496 /**
1497 * getWaitingThreads throws IllegalMonitorStateException if not locked
1498 */
1499 public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); }
1500 public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
1501 public void testGetWaitingThreadsIMSE(boolean fair) {
1502 final PublicReentrantReadWriteLock lock =
1503 new PublicReentrantReadWriteLock(fair);
1504 final Condition c = lock.writeLock().newCondition();
1505 try {
1506 lock.getWaitingThreads(c);
1507 shouldThrow();
1508 } catch (IllegalMonitorStateException success) {}
1509 }
1510
1511 /**
1512 * hasWaiters returns true when a thread is waiting, else false
1513 */
1514 public void testHasWaiters() { testHasWaiters(false); }
1515 public void testHasWaiters_fair() { testHasWaiters(true); }
1516 public void testHasWaiters(boolean fair) {
1517 final PublicReentrantReadWriteLock lock =
1518 new PublicReentrantReadWriteLock(fair);
1519 final Condition c = lock.writeLock().newCondition();
1520 final CountDownLatch locked = new CountDownLatch(1);
1521 Thread t = newStartedThread(new CheckedRunnable() {
1522 public void realRun() throws InterruptedException {
1523 lock.writeLock().lock();
1524 assertHasNoWaiters(lock, c);
1525 assertFalse(lock.hasWaiters(c));
1526 locked.countDown();
1527 c.await();
1528 assertHasNoWaiters(lock, c);
1529 assertFalse(lock.hasWaiters(c));
1530 lock.writeLock().unlock();
1531 }});
1532
1533 await(locked);
1534 lock.writeLock().lock();
1535 assertHasWaiters(lock, c, t);
1536 assertTrue(lock.hasWaiters(c));
1537 c.signal();
1538 assertHasNoWaiters(lock, c);
1539 assertFalse(lock.hasWaiters(c));
1540 lock.writeLock().unlock();
1541 awaitTermination(t);
1542 assertHasNoWaiters(lock, c);
1543 }
1544
1545 /**
1546 * getWaitQueueLength returns number of waiting threads
1547 */
1548 public void testGetWaitQueueLength() { testGetWaitQueueLength(false); }
1549 public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
1550 public void testGetWaitQueueLength(boolean fair) {
1551 final PublicReentrantReadWriteLock lock =
1552 new PublicReentrantReadWriteLock(fair);
1553 final Condition c = lock.writeLock().newCondition();
1554 final CountDownLatch locked = new CountDownLatch(1);
1555 Thread t = newStartedThread(new CheckedRunnable() {
1556 public void realRun() throws InterruptedException {
1557 lock.writeLock().lock();
1558 assertEquals(0, lock.getWaitQueueLength(c));
1559 locked.countDown();
1560 c.await();
1561 lock.writeLock().unlock();
1562 }});
1563
1564 await(locked);
1565 lock.writeLock().lock();
1566 assertHasWaiters(lock, c, t);
1567 assertEquals(1, lock.getWaitQueueLength(c));
1568 c.signal();
1569 assertHasNoWaiters(lock, c);
1570 assertEquals(0, lock.getWaitQueueLength(c));
1571 lock.writeLock().unlock();
1572 awaitTermination(t);
1573 }
1574
1575 /**
1576 * getWaitingThreads returns only and all waiting threads
1577 */
1578 public void testGetWaitingThreads() { testGetWaitingThreads(false); }
1579 public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
1580 public void testGetWaitingThreads(boolean fair) {
1581 final PublicReentrantReadWriteLock lock =
1582 new PublicReentrantReadWriteLock(fair);
1583 final Condition c = lock.writeLock().newCondition();
1584 final CountDownLatch locked1 = new CountDownLatch(1);
1585 final CountDownLatch locked2 = new CountDownLatch(1);
1586 Thread t1 = new Thread(new CheckedRunnable() {
1587 public void realRun() throws InterruptedException {
1588 lock.writeLock().lock();
1589 assertTrue(lock.getWaitingThreads(c).isEmpty());
1590 locked1.countDown();
1591 c.await();
1592 lock.writeLock().unlock();
1593 }});
1594
1595 Thread t2 = new Thread(new CheckedRunnable() {
1596 public void realRun() throws InterruptedException {
1597 lock.writeLock().lock();
1598 assertFalse(lock.getWaitingThreads(c).isEmpty());
1599 locked2.countDown();
1600 c.await();
1601 lock.writeLock().unlock();
1602 }});
1603
1604 lock.writeLock().lock();
1605 assertTrue(lock.getWaitingThreads(c).isEmpty());
1606 lock.writeLock().unlock();
1607
1608 t1.start();
1609 await(locked1);
1610 t2.start();
1611 await(locked2);
1612
1613 lock.writeLock().lock();
1614 assertTrue(lock.hasWaiters(c));
1615 assertTrue(lock.getWaitingThreads(c).contains(t1));
1616 assertTrue(lock.getWaitingThreads(c).contains(t2));
1617 assertEquals(2, lock.getWaitingThreads(c).size());
1618 c.signalAll();
1619 assertHasNoWaiters(lock, c);
1620 lock.writeLock().unlock();
1621
1622 awaitTermination(t1);
1623 awaitTermination(t2);
1624
1625 assertHasNoWaiters(lock, c);
1626 }
1627
1628 /**
1629 * toString indicates current lock state
1630 */
1631 public void testToString() { testToString(false); }
1632 public void testToString_fair() { testToString(true); }
1633 public void testToString(boolean fair) {
1634 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1635 assertTrue(lock.toString().contains("Write locks = 0"));
1636 assertTrue(lock.toString().contains("Read locks = 0"));
1637 lock.writeLock().lock();
1638 assertTrue(lock.toString().contains("Write locks = 1"));
1639 assertTrue(lock.toString().contains("Read locks = 0"));
1640 lock.writeLock().unlock();
1641 lock.readLock().lock();
1642 lock.readLock().lock();
1643 assertTrue(lock.toString().contains("Write locks = 0"));
1644 assertTrue(lock.toString().contains("Read locks = 2"));
1645 }
1646
1647 /**
1648 * readLock.toString indicates current lock state
1649 */
1650 public void testReadLockToString() { testReadLockToString(false); }
1651 public void testReadLockToString_fair() { testReadLockToString(true); }
1652 public void testReadLockToString(boolean fair) {
1653 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1654 assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1655 lock.readLock().lock();
1656 lock.readLock().lock();
1657 assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1658 }
1659
1660 /**
1661 * writeLock.toString indicates current lock state
1662 */
1663 public void testWriteLockToString() { testWriteLockToString(false); }
1664 public void testWriteLockToString_fair() { testWriteLockToString(true); }
1665 public void testWriteLockToString(boolean fair) {
1666 ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1667 assertTrue(lock.writeLock().toString().contains("Unlocked"));
1668 lock.writeLock().lock();
1669 assertTrue(lock.writeLock().toString().contains("Locked"));
1670 lock.writeLock().unlock();
1671 assertTrue(lock.writeLock().toString().contains("Unlocked"));
1672 }
1673
1674 }