ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.58 by jsr166, Sat May 7 14:43:13 2011 UTC vs.
Revision 1.63 by jsr166, Sat May 21 06:24:33 2011 UTC

# Line 67 | Line 67 | public class ReentrantReadWriteLockTest
67       */
68      void releaseWriteLock(PublicReentrantReadWriteLock lock) {
69          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
70 <        assertWriteLockedBy(lock, Thread.currentThread());
70 >        assertWriteLockedByMoi(lock);
71          assertEquals(1, lock.getWriteHoldCount());
72          writeLock.unlock();
73          assertNotWriteLocked(lock);
# Line 80 | Line 80 | public class ReentrantReadWriteLockTest
80          long startTime = System.nanoTime();
81          while (!lock.hasQueuedThread(t)) {
82              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
83 <                throw new AssertionError("timed out");
83 >                throw new AssertionFailedError("timed out");
84              Thread.yield();
85          }
86          assertTrue(t.isAlive());
# Line 94 | Line 94 | public class ReentrantReadWriteLockTest
94          assertFalse(lock.isWriteLocked());
95          assertFalse(lock.isWriteLockedByCurrentThread());
96          assertFalse(lock.writeLock().isHeldByCurrentThread());
97        assertNull(lock.getOwner());
97          assertEquals(0, lock.getWriteHoldCount());
98 +        assertEquals(0, lock.writeLock().getHoldCount());
99 +        assertNull(lock.getOwner());
100      }
101  
102      /**
# Line 110 | Line 111 | public class ReentrantReadWriteLockTest
111                       lock.writeLock().isHeldByCurrentThread());
112          assertEquals(t == Thread.currentThread(),
113                       lock.getWriteHoldCount() > 0);
114 +        assertEquals(t == Thread.currentThread(),
115 +                     lock.writeLock().getHoldCount() > 0);
116          assertEquals(0, lock.getReadLockCount());
117      }
118  
119      /**
120 +     * Checks that lock is write-locked by the current thread.
121 +     */
122 +    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
123 +        assertWriteLockedBy(lock, Thread.currentThread());
124 +    }
125 +
126 +    /**
127       * Checks that condition c has no waiters.
128       */
129      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 135 | Line 145 | public class ReentrantReadWriteLockTest
145          lock.writeLock().unlock();
146      }
147  
148 +    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
149 +
150 +    /**
151 +     * Awaits condition using the specified AwaitMethod.
152 +     */
153 +    void await(Condition c, AwaitMethod awaitMethod)
154 +            throws InterruptedException {
155 +        switch (awaitMethod) {
156 +        case await:
157 +            c.await();
158 +            break;
159 +        case awaitTimed:
160 +            assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
161 +            break;
162 +        case awaitNanos:
163 +            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
164 +            assertTrue(nanosRemaining > 0);
165 +            break;
166 +        case awaitUntil:
167 +            java.util.Date d = new java.util.Date();
168 +            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
169 +            break;
170 +        }
171 +    }
172 +
173      /**
174       * Constructor sets given fairness, and is in unlocked state
175       */
# Line 160 | Line 195 | public class ReentrantReadWriteLockTest
195      /**
196       * write-locking and read-locking an unlocked lock succeed
197       */
198 <    public void testLock() {
199 <        PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
198 >    public void testLock()      { testLock(false); }
199 >    public void testLock_fair() { testLock(true); }
200 >    public void testLock(boolean fair) {
201 >        PublicReentrantReadWriteLock lock =
202 >            new PublicReentrantReadWriteLock(fair);
203          assertNotWriteLocked(lock);
204          lock.writeLock().lock();
205 <        assertWriteLockedBy(lock, Thread.currentThread());
168 <        lock.writeLock().unlock();
169 <        assertNotWriteLocked(lock);
170 <        assertEquals(0, lock.getReadLockCount());
171 <        lock.readLock().lock();
172 <        assertNotWriteLocked(lock);
173 <        assertEquals(1, lock.getReadLockCount());
174 <        lock.readLock().unlock();
175 <        assertNotWriteLocked(lock);
176 <        assertEquals(0, lock.getReadLockCount());
177 <    }
178 <
179 <    /**
180 <     * locking an unlocked fair lock succeeds
181 <     */
182 <    public void testFairLock() {
183 <        PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
184 <        assertNotWriteLocked(lock);
185 <        lock.writeLock().lock();
186 <        assertWriteLockedBy(lock, Thread.currentThread());
205 >        assertWriteLockedByMoi(lock);
206          lock.writeLock().unlock();
207          assertNotWriteLocked(lock);
208          assertEquals(0, lock.getReadLockCount());
# Line 198 | Line 217 | public class ReentrantReadWriteLockTest
217      /**
218       * getWriteHoldCount returns number of recursive holds
219       */
220 <    public void testGetWriteHoldCount() {
221 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
220 >    public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
221 >    public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
222 >    public void testGetWriteHoldCount(boolean fair) {
223 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
224          for (int i = 1; i <= SIZE; i++) {
225              lock.writeLock().lock();
226              assertEquals(i,lock.getWriteHoldCount());
# Line 211 | Line 232 | public class ReentrantReadWriteLockTest
232      }
233  
234      /**
235 <     * WriteLock.getHoldCount returns number of recursive holds
235 >     * writelock.getHoldCount returns number of recursive holds
236       */
237 <    public void testGetHoldCount() {
238 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
237 >    public void testGetHoldCount()      { testGetHoldCount(false); }
238 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
239 >    public void testGetHoldCount(boolean fair) {
240 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
241          for (int i = 1; i <= SIZE; i++) {
242              lock.writeLock().lock();
243              assertEquals(i,lock.writeLock().getHoldCount());
# Line 228 | Line 251 | public class ReentrantReadWriteLockTest
251      /**
252       * getReadHoldCount returns number of recursive holds
253       */
254 <    public void testGetReadHoldCount() {
255 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
254 >    public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
255 >    public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
256 >    public void testGetReadHoldCount(boolean fair) {
257 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
258          for (int i = 1; i <= SIZE; i++) {
259              lock.readLock().lock();
260              assertEquals(i,lock.getReadHoldCount());
# Line 243 | Line 268 | public class ReentrantReadWriteLockTest
268      /**
269       * write-unlocking an unlocked lock throws IllegalMonitorStateException
270       */
271 <    public void testWriteUnlock_IllegalMonitorStateException() {
272 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
271 >    public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
272 >    public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
273 >    public void testWriteUnlock_IMSE(boolean fair) {
274 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
275          try {
276              lock.writeLock().unlock();
277              shouldThrow();
# Line 254 | Line 281 | public class ReentrantReadWriteLockTest
281      /**
282       * read-unlocking an unlocked lock throws IllegalMonitorStateException
283       */
284 <    public void testReadUnlock_IllegalMonitorStateException() {
285 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
284 >    public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
285 >    public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
286 >    public void testReadUnlock_IMSE(boolean fair) {
287 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
288          try {
289              lock.readLock().unlock();
290              shouldThrow();
# Line 265 | Line 294 | public class ReentrantReadWriteLockTest
294      /**
295       * write-lockInterruptibly is interruptible
296       */
297 <    public void testWriteLockInterruptibly_Interrupted() throws Exception {
298 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
297 >    public void testWriteLockInterruptibly_Interruptible()      { testWriteLockInterruptibly_Interruptible(false); }
298 >    public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
299 >    public void testWriteLockInterruptibly_Interruptible(boolean fair) {
300 >        final PublicReentrantReadWriteLock lock =
301 >            new PublicReentrantReadWriteLock(fair);
302          lock.writeLock().lock();
303          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
304              public void realRun() throws InterruptedException {
# Line 282 | Line 314 | public class ReentrantReadWriteLockTest
314      /**
315       * timed write-tryLock is interruptible
316       */
317 <    public void testWriteTryLock_Interrupted() throws InterruptedException {
318 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
317 >    public void testWriteTryLock_Interruptible()      { testWriteTryLock_Interruptible(false); }
318 >    public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
319 >    public void testWriteTryLock_Interruptible(boolean fair) {
320 >        final PublicReentrantReadWriteLock lock =
321 >            new PublicReentrantReadWriteLock(fair);
322          lock.writeLock().lock();
323          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
324              public void realRun() throws InterruptedException {
325 <                lock.writeLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
325 >                lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
326              }});
327  
328          waitForQueuedThread(lock, t);
# Line 299 | Line 334 | public class ReentrantReadWriteLockTest
334      /**
335       * read-lockInterruptibly is interruptible
336       */
337 <    public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
338 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
337 >    public void testReadLockInterruptibly_Interruptible()      { testReadLockInterruptibly_Interruptible(false); }
338 >    public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
339 >    public void testReadLockInterruptibly_Interruptible(boolean fair) {
340 >        final PublicReentrantReadWriteLock lock =
341 >            new PublicReentrantReadWriteLock(fair);
342          lock.writeLock().lock();
343          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
344              public void realRun() throws InterruptedException {
# Line 316 | Line 354 | public class ReentrantReadWriteLockTest
354      /**
355       * timed read-tryLock is interruptible
356       */
357 <    public void testReadTryLock_Interrupted() throws InterruptedException {
358 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
357 >    public void testReadTryLock_Interruptible()      { testReadTryLock_Interruptible(false); }
358 >    public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
359 >    public void testReadTryLock_Interruptible(boolean fair) {
360 >        final PublicReentrantReadWriteLock lock =
361 >            new PublicReentrantReadWriteLock(fair);
362          lock.writeLock().lock();
363          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
364              public void realRun() throws InterruptedException {
365 <                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
365 >                lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
366              }});
367  
368          waitForQueuedThread(lock, t);
# Line 331 | Line 372 | public class ReentrantReadWriteLockTest
372      }
373  
374      /**
375 +     * write-tryLock on an unlocked lock succeeds
376 +     */
377 +    public void testWriteTryLock()      { testWriteTryLock(false); }
378 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
379 +    public void testWriteTryLock(boolean fair) {
380 +        final PublicReentrantReadWriteLock lock =
381 +            new PublicReentrantReadWriteLock(fair);
382 +        assertTrue(lock.writeLock().tryLock());
383 +        assertWriteLockedByMoi(lock);
384 +        assertTrue(lock.writeLock().tryLock());
385 +        assertWriteLockedByMoi(lock);
386 +        lock.writeLock().unlock();
387 +        releaseWriteLock(lock);
388 +    }
389 +
390 +    /**
391       * write-tryLock fails if locked
392       */
393 <    public void testWriteTryLockWhenLocked() throws InterruptedException {
394 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
393 >    public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
394 >    public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
395 >    public void testWriteTryLockWhenLocked(boolean fair) {
396 >        final PublicReentrantReadWriteLock lock =
397 >            new PublicReentrantReadWriteLock(fair);
398          lock.writeLock().lock();
399          Thread t = newStartedThread(new CheckedRunnable() {
400              public void realRun() {
# Line 348 | Line 408 | public class ReentrantReadWriteLockTest
408      /**
409       * read-tryLock fails if locked
410       */
411 <    public void testReadTryLockWhenLocked() throws InterruptedException {
412 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
411 >    public void testReadTryLockWhenLocked()      { testReadTryLockWhenLocked(false); }
412 >    public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
413 >    public void testReadTryLockWhenLocked(boolean fair) {
414 >        final PublicReentrantReadWriteLock lock =
415 >            new PublicReentrantReadWriteLock(fair);
416          lock.writeLock().lock();
417          Thread t = newStartedThread(new CheckedRunnable() {
418              public void realRun() {
# Line 363 | Line 426 | public class ReentrantReadWriteLockTest
426      /**
427       * Multiple threads can hold a read lock when not write-locked
428       */
429 <    public void testMultipleReadLocks() throws InterruptedException {
430 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
429 >    public void testMultipleReadLocks()      { testMultipleReadLocks(false); }
430 >    public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
431 >    public void testMultipleReadLocks(boolean fair) {
432 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
433          lock.readLock().lock();
434          Thread t = newStartedThread(new CheckedRunnable() {
435 <            public void realRun() {
435 >            public void realRun() throws InterruptedException {
436                  assertTrue(lock.readLock().tryLock());
437                  lock.readLock().unlock();
438 +                assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
439 +                lock.readLock().unlock();
440 +                lock.readLock().lock();
441 +                lock.readLock().unlock();
442              }});
443  
444          awaitTermination(t);
# Line 379 | Line 448 | public class ReentrantReadWriteLockTest
448      /**
449       * A writelock succeeds only after a reading thread unlocks
450       */
451 <    public void testWriteAfterReadLock() throws InterruptedException {
452 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
451 >    public void testWriteAfterReadLock()      { testWriteAfterReadLock(false); }
452 >    public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
453 >    public void testWriteAfterReadLock(boolean fair) {
454 >        final PublicReentrantReadWriteLock lock =
455 >            new PublicReentrantReadWriteLock(fair);
456          lock.readLock().lock();
385
457          Thread t = newStartedThread(new CheckedRunnable() {
458              public void realRun() {
459                  assertEquals(1, lock.getReadLockCount());
# Line 402 | Line 473 | public class ReentrantReadWriteLockTest
473      /**
474       * A writelock succeeds only after reading threads unlock
475       */
476 <    public void testWriteAfterMultipleReadLocks() throws InterruptedException {
477 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
476 >    public void testWriteAfterMultipleReadLocks()      { testWriteAfterMultipleReadLocks(false); }
477 >    public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
478 >    public void testWriteAfterMultipleReadLocks(boolean fair) {
479 >        final PublicReentrantReadWriteLock lock =
480 >            new PublicReentrantReadWriteLock(fair);
481          lock.readLock().lock();
482          lock.readLock().lock();
483          Thread t1 = newStartedThread(new CheckedRunnable() {
# Line 435 | Line 509 | public class ReentrantReadWriteLockTest
509       * A thread that tries to acquire a fair read lock (non-reentrantly)
510       * will block if there is a waiting writer thread.
511       */
512 <    public void testReaderWriterReaderFairFifo() throws InterruptedException {
513 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
512 >    public void testReaderWriterReaderFairFifo() {
513 >        final PublicReentrantReadWriteLock lock =
514 >            new PublicReentrantReadWriteLock(true);
515          final AtomicBoolean t1GotLock = new AtomicBoolean(false);
516  
517          lock.readLock().lock();
# Line 471 | Line 546 | public class ReentrantReadWriteLockTest
546      /**
547       * Readlocks succeed only after a writing thread unlocks
548       */
549 <    public void testReadAfterWriteLock() throws InterruptedException {
550 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
549 >    public void testReadAfterWriteLock()      { testReadAfterWriteLock(false); }
550 >    public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
551 >    public void testReadAfterWriteLock(boolean fair) {
552 >        final PublicReentrantReadWriteLock lock =
553 >            new PublicReentrantReadWriteLock(fair);
554          lock.writeLock().lock();
555          Thread t1 = newStartedThread(new CheckedRunnable() {
556              public void realRun() {
# Line 495 | Line 573 | public class ReentrantReadWriteLockTest
573      /**
574       * Read trylock succeeds if write locked by current thread
575       */
576 <    public void testReadHoldingWriteLock() {
577 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
576 >    public void testReadHoldingWriteLock()      { testReadHoldingWriteLock(false); }
577 >    public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
578 >    public void testReadHoldingWriteLock(boolean fair) {
579 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
580          lock.writeLock().lock();
581          assertTrue(lock.readLock().tryLock());
582          lock.readLock().unlock();
# Line 504 | Line 584 | public class ReentrantReadWriteLockTest
584      }
585  
586      /**
587 <     * Read lock succeeds if write locked by current thread even if
508 <     * other threads are waiting for readlock
587 >     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
588       */
589 <    public void testReadHoldingWriteLock2() throws InterruptedException {
590 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
591 <        lock.writeLock().lock();
589 >    public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
590 >    public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
591 >    public void testReadTryLockBarging(boolean fair) {
592 >        final PublicReentrantReadWriteLock lock =
593 >            new PublicReentrantReadWriteLock(fair);
594          lock.readLock().lock();
514        lock.readLock().unlock();
595  
596          Thread t1 = newStartedThread(new CheckedRunnable() {
597              public void realRun() {
598 <                lock.readLock().lock();
599 <                lock.readLock().unlock();
520 <            }});
521 <        Thread t2 = newStartedThread(new CheckedRunnable() {
522 <            public void realRun() {
523 <                lock.readLock().lock();
524 <                lock.readLock().unlock();
598 >                lock.writeLock().lock();
599 >                lock.writeLock().unlock();
600              }});
601  
602          waitForQueuedThread(lock, t1);
528        waitForQueuedThread(lock, t2);
529        assertWriteLockedBy(lock, Thread.currentThread());
530        lock.readLock().lock();
531        lock.readLock().unlock();
532        releaseWriteLock(lock);
533        awaitTermination(t1);
534        awaitTermination(t2);
535    }
536
537    /**
538     * Read lock succeeds if write locked by current thread even if
539     * other threads are waiting for writelock
540     */
541    public void testReadHoldingWriteLock3() throws InterruptedException {
542        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
543        lock.writeLock().lock();
544        lock.readLock().lock();
545        lock.readLock().unlock();
603  
547        Thread t1 = newStartedThread(new CheckedRunnable() {
548            public void realRun() {
549                lock.writeLock().lock();
550                lock.writeLock().unlock();
551            }});
604          Thread t2 = newStartedThread(new CheckedRunnable() {
605              public void realRun() {
606 <                lock.writeLock().lock();
607 <                lock.writeLock().unlock();
606 >                lock.readLock().lock();
607 >                lock.readLock().unlock();
608              }});
609  
610 <        waitForQueuedThread(lock, t1);
611 <        waitForQueuedThread(lock, t2);
560 <        assertWriteLockedBy(lock, Thread.currentThread());
561 <        lock.readLock().lock();
562 <        lock.readLock().unlock();
563 <        releaseWriteLock(lock);
564 <        awaitTermination(t1);
565 <        awaitTermination(t2);
566 <    }
610 >        if (fair)
611 >            waitForQueuedThread(lock, t2);
612  
613 <    /**
569 <     * Write lock succeeds if write locked by current thread even if
570 <     * other threads are waiting for writelock
571 <     */
572 <    public void testWriteHoldingWriteLock4() throws InterruptedException {
573 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
574 <        lock.writeLock().lock();
575 <        lock.writeLock().lock();
576 <        lock.writeLock().unlock();
577 <
578 <        Thread t1 = newStartedThread(new CheckedRunnable() {
613 >        Thread t3 = newStartedThread(new CheckedRunnable() {
614              public void realRun() {
615 <                lock.writeLock().lock();
616 <                lock.writeLock().unlock();
582 <            }});
583 <        Thread t2 = newStartedThread(new CheckedRunnable() {
584 <            public void realRun() {
585 <                lock.writeLock().lock();
586 <                lock.writeLock().unlock();
615 >                lock.readLock().tryLock();
616 >                lock.readLock().unlock();
617              }});
618  
619 <        waitForQueuedThread(lock, t1);
620 <        waitForQueuedThread(lock, t2);
621 <        assertWriteLockedBy(lock, Thread.currentThread());
622 <        assertEquals(1, lock.getWriteHoldCount());
623 <        lock.writeLock().lock();
594 <        assertWriteLockedBy(lock, Thread.currentThread());
595 <        assertEquals(2, lock.getWriteHoldCount());
596 <        lock.writeLock().unlock();
597 <        releaseWriteLock(lock);
619 >        assertTrue(lock.getReadLockCount() > 0);
620 >        awaitTermination(t3);
621 >        assertTrue(t1.isAlive());
622 >        if (fair) assertTrue(t2.isAlive());
623 >        lock.readLock().unlock();
624          awaitTermination(t1);
625          awaitTermination(t2);
626      }
627  
628      /**
629 <     * Fair Read trylock succeeds if write locked by current thread
604 <     */
605 <    public void testReadHoldingWriteLockFair() {
606 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
607 <        lock.writeLock().lock();
608 <        assertTrue(lock.readLock().tryLock());
609 <        lock.readLock().unlock();
610 <        lock.writeLock().unlock();
611 <    }
612 <
613 <    /**
614 <     * Fair Read lock succeeds if write locked by current thread even if
629 >     * Read lock succeeds if write locked by current thread even if
630       * other threads are waiting for readlock
631       */
632 <    public void testReadHoldingWriteLockFair2() throws InterruptedException {
633 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
632 >    public void testReadHoldingWriteLock2()      { testReadHoldingWriteLock2(false); }
633 >    public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
634 >    public void testReadHoldingWriteLock2(boolean fair) {
635 >        final PublicReentrantReadWriteLock lock =
636 >            new PublicReentrantReadWriteLock(fair);
637          lock.writeLock().lock();
638          lock.readLock().lock();
639          lock.readLock().unlock();
# Line 633 | Line 651 | public class ReentrantReadWriteLockTest
651  
652          waitForQueuedThread(lock, t1);
653          waitForQueuedThread(lock, t2);
654 <        assertWriteLockedBy(lock, Thread.currentThread());
654 >        assertWriteLockedByMoi(lock);
655          lock.readLock().lock();
656          lock.readLock().unlock();
657          releaseWriteLock(lock);
# Line 642 | Line 660 | public class ReentrantReadWriteLockTest
660      }
661  
662      /**
663 <     * Fair Read lock succeeds if write locked by current thread even if
663 >     * Read lock succeeds if write locked by current thread even if
664       * other threads are waiting for writelock
665       */
666 <    public void testReadHoldingWriteLockFair3() throws InterruptedException {
667 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
666 >    public void testReadHoldingWriteLock3()      { testReadHoldingWriteLock3(false); }
667 >    public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
668 >    public void testReadHoldingWriteLock3(boolean fair) {
669 >        final PublicReentrantReadWriteLock lock =
670 >            new PublicReentrantReadWriteLock(fair);
671          lock.writeLock().lock();
672          lock.readLock().lock();
673          lock.readLock().unlock();
# Line 664 | Line 685 | public class ReentrantReadWriteLockTest
685  
686          waitForQueuedThread(lock, t1);
687          waitForQueuedThread(lock, t2);
688 <        assertWriteLockedBy(lock, Thread.currentThread());
688 >        assertWriteLockedByMoi(lock);
689          lock.readLock().lock();
690          lock.readLock().unlock();
691 <        releaseWriteLock(lock);
691 >        assertWriteLockedByMoi(lock);
692 >        lock.writeLock().unlock();
693          awaitTermination(t1);
694          awaitTermination(t2);
695      }
696  
697      /**
698 <     * Fair Write lock succeeds if write locked by current thread even if
698 >     * Write lock succeeds if write locked by current thread even if
699       * other threads are waiting for writelock
700       */
701 <    public void testWriteHoldingWriteLockFair4() throws InterruptedException {
702 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(true);
701 >    public void testWriteHoldingWriteLock4()      { testWriteHoldingWriteLock4(false); }
702 >    public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
703 >    public void testWriteHoldingWriteLock4(boolean fair) {
704 >        final PublicReentrantReadWriteLock lock =
705 >            new PublicReentrantReadWriteLock(fair);
706 >        lock.writeLock().lock();
707          lock.writeLock().lock();
708 +        lock.writeLock().unlock();
709 +
710          Thread t1 = newStartedThread(new CheckedRunnable() {
711              public void realRun() {
712                  lock.writeLock().lock();
# Line 692 | Line 720 | public class ReentrantReadWriteLockTest
720  
721          waitForQueuedThread(lock, t1);
722          waitForQueuedThread(lock, t2);
723 <        assertWriteLockedBy(lock, Thread.currentThread());
723 >        assertWriteLockedByMoi(lock);
724          assertEquals(1, lock.getWriteHoldCount());
725          lock.writeLock().lock();
726 +        assertWriteLockedByMoi(lock);
727          assertEquals(2, lock.getWriteHoldCount());
728          lock.writeLock().unlock();
729 <        lock.writeLock().lock();
729 >        assertWriteLockedByMoi(lock);
730 >        assertEquals(1, lock.getWriteHoldCount());
731          lock.writeLock().unlock();
702        releaseWriteLock(lock);
732          awaitTermination(t1);
733          awaitTermination(t2);
734      }
# Line 707 | Line 736 | public class ReentrantReadWriteLockTest
736      /**
737       * Read tryLock succeeds if readlocked but not writelocked
738       */
739 <    public void testTryLockWhenReadLocked() throws InterruptedException {
740 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
739 >    public void testTryLockWhenReadLocked()      { testTryLockWhenReadLocked(false); }
740 >    public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
741 >    public void testTryLockWhenReadLocked(boolean fair) {
742 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
743          lock.readLock().lock();
744          Thread t = newStartedThread(new CheckedRunnable() {
745              public void realRun() {
# Line 723 | Line 754 | public class ReentrantReadWriteLockTest
754      /**
755       * write tryLock fails when readlocked
756       */
757 <    public void testWriteTryLockWhenReadLocked() throws InterruptedException {
758 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
759 <        lock.readLock().lock();
760 <        Thread t = newStartedThread(new CheckedRunnable() {
730 <            public void realRun() {
731 <                assertFalse(lock.writeLock().tryLock());
732 <            }});
733 <
734 <        awaitTermination(t);
735 <        lock.readLock().unlock();
736 <    }
737 <
738 <    /**
739 <     * Fair Read tryLock succeeds if readlocked but not writelocked
740 <     */
741 <    public void testTryLockWhenReadLockedFair() throws InterruptedException {
742 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
743 <        lock.readLock().lock();
744 <        Thread t = newStartedThread(new CheckedRunnable() {
745 <            public void realRun() {
746 <                assertTrue(lock.readLock().tryLock());
747 <                lock.readLock().unlock();
748 <            }});
749 <
750 <        awaitTermination(t);
751 <        lock.readLock().unlock();
752 <    }
753 <
754 <    /**
755 <     * Fair write tryLock fails when readlocked
756 <     */
757 <    public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
758 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
757 >    public void testWriteTryLockWhenReadLocked()      { testWriteTryLockWhenReadLocked(false); }
758 >    public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
759 >    public void testWriteTryLockWhenReadLocked(boolean fair) {
760 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
761          lock.readLock().lock();
762          Thread t = newStartedThread(new CheckedRunnable() {
763              public void realRun() {
# Line 769 | Line 771 | public class ReentrantReadWriteLockTest
771      /**
772       * write timed tryLock times out if locked
773       */
774 <    public void testWriteTryLock_Timeout() throws InterruptedException {
775 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
774 >    public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
775 >    public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
776 >    public void testWriteTryLock_Timeout(boolean fair) {
777 >        final PublicReentrantReadWriteLock lock =
778 >            new PublicReentrantReadWriteLock(fair);
779          lock.writeLock().lock();
780          Thread t = newStartedThread(new CheckedRunnable() {
781              public void realRun() throws InterruptedException {
782 <                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
782 >                long startTime = System.nanoTime();
783 >                long timeoutMillis = 10;
784 >                assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
785 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
786              }});
787  
788          awaitTermination(t);
789 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
782 <        lock.writeLock().unlock();
789 >        releaseWriteLock(lock);
790      }
791  
792      /**
793       * read timed tryLock times out if write-locked
794       */
795 <    public void testReadTryLock_Timeout() throws InterruptedException {
796 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
795 >    public void testReadTryLock_Timeout()      { testReadTryLock_Timeout(false); }
796 >    public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
797 >    public void testReadTryLock_Timeout(boolean fair) {
798 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
799          lock.writeLock().lock();
800          Thread t = newStartedThread(new CheckedRunnable() {
801              public void realRun() throws InterruptedException {
802 <                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
802 >                long startTime = System.nanoTime();
803 >                long timeoutMillis = 10;
804 >                assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
805 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
806              }});
807  
808          awaitTermination(t);
# Line 799 | Line 811 | public class ReentrantReadWriteLockTest
811      }
812  
813      /**
814 <     * write lockInterruptibly succeeds if lock free else is interruptible
814 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
815       */
816 <    public void testWriteLockInterruptibly() throws InterruptedException {
817 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
818 <        lock.writeLock().lockInterruptibly();
816 >    public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
817 >    public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
818 >    public void testWriteLockInterruptibly(boolean fair) {
819 >        final PublicReentrantReadWriteLock lock =
820 >            new PublicReentrantReadWriteLock(fair);
821 >        try {
822 >            lock.writeLock().lockInterruptibly();
823 >        } catch (InterruptedException ie) {
824 >            threadUnexpectedException(ie);
825 >        }
826          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
827              public void realRun() throws InterruptedException {
828                  lock.writeLock().lockInterruptibly();
# Line 811 | Line 830 | public class ReentrantReadWriteLockTest
830  
831          waitForQueuedThread(lock, t);
832          t.interrupt();
833 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
834          awaitTermination(t);
835          releaseWriteLock(lock);
836      }
# Line 818 | Line 838 | public class ReentrantReadWriteLockTest
838      /**
839       * read lockInterruptibly succeeds if lock free else is interruptible
840       */
841 <    public void testReadLockInterruptibly() throws InterruptedException {
842 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
843 <        lock.writeLock().lockInterruptibly();
841 >    public void testReadLockInterruptibly()      { testReadLockInterruptibly(false); }
842 >    public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
843 >    public void testReadLockInterruptibly(boolean fair) {
844 >        final PublicReentrantReadWriteLock lock =
845 >            new PublicReentrantReadWriteLock(fair);
846 >        try {
847 >            lock.readLock().lockInterruptibly();
848 >            lock.readLock().unlock();
849 >            lock.writeLock().lockInterruptibly();
850 >        } catch (InterruptedException ie) {
851 >            threadUnexpectedException(ie);
852 >        }
853          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
854              public void realRun() throws InterruptedException {
855                  lock.readLock().lockInterruptibly();
# Line 835 | Line 864 | public class ReentrantReadWriteLockTest
864      /**
865       * Calling await without holding lock throws IllegalMonitorStateException
866       */
867 <    public void testAwait_IllegalMonitor() throws InterruptedException {
868 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
869 <        final Condition c = lock.writeLock().newCondition();
870 <        try {
871 <            c.await();
872 <            shouldThrow();
873 <        } catch (IllegalMonitorStateException success) {}
874 <        try {
875 <            c.await(LONG_DELAY_MS, MILLISECONDS);
876 <            shouldThrow();
877 <        } catch (IllegalMonitorStateException success) {}
878 <        try {
879 <            c.awaitNanos(100);
880 <            shouldThrow();
852 <        } catch (IllegalMonitorStateException success) {}
853 <        try {
854 <            c.awaitUninterruptibly();
855 <            shouldThrow();
856 <        } catch (IllegalMonitorStateException success) {}
867 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
868 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
869 >    public void testAwait_IMSE(boolean fair) {
870 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
871 >        final Condition c = lock.writeLock().newCondition();
872 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
873 >            long startTime = System.nanoTime();
874 >            try {
875 >                await(c, awaitMethod);
876 >                shouldThrow();
877 >            } catch (IllegalMonitorStateException success) {
878 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
879 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
880 >        }
881      }
882  
883      /**
884       * Calling signal without holding lock throws IllegalMonitorStateException
885       */
886 <    public void testSignal_IllegalMonitor() {
887 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
886 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
887 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
888 >    public void testSignal_IMSE(boolean fair) {
889 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
890          final Condition c = lock.writeLock().newCondition();
891          try {
892              c.signal();
# Line 871 | Line 897 | public class ReentrantReadWriteLockTest
897      /**
898       * Calling signalAll without holding lock throws IllegalMonitorStateException
899       */
900 <    public void testSignalAll_IllegalMonitor() {
901 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
900 >    public void testSignalAll_IMSE()      { testSignalAll_IMSE(false); }
901 >    public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
902 >    public void testSignalAll_IMSE(boolean fair) {
903 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
904          final Condition c = lock.writeLock().newCondition();
905          try {
906              c.signalAll();
# Line 883 | Line 911 | public class ReentrantReadWriteLockTest
911      /**
912       * awaitNanos without a signal times out
913       */
914 <    public void testAwaitNanos_Timeout() throws InterruptedException {
915 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
916 <        final Condition c = lock.writeLock().newCondition();
917 <        lock.writeLock().lock();
918 <        long startTime = System.nanoTime();
919 <        long timeoutMillis = 10;
920 <        long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
921 <        long nanosRemaining = c.awaitNanos(timeoutNanos);
922 <        assertTrue(nanosRemaining <= 0);
923 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
924 <        lock.writeLock().unlock();
914 >    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
915 >    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
916 >    public void testAwaitNanos_Timeout(boolean fair) {
917 >        try {
918 >            final ReentrantReadWriteLock lock =
919 >                new ReentrantReadWriteLock(fair);
920 >            final Condition c = lock.writeLock().newCondition();
921 >            lock.writeLock().lock();
922 >            long startTime = System.nanoTime();
923 >            long timeoutMillis = 10;
924 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
925 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
926 >            assertTrue(nanosRemaining <= 0);
927 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
928 >            lock.writeLock().unlock();
929 >        } catch (InterruptedException e) {
930 >            threadUnexpectedException(e);
931 >        }
932      }
933  
934      /**
935       * timed await without a signal times out
936       */
937 <    public void testAwait_Timeout() throws InterruptedException {
938 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
939 <        final Condition c = lock.writeLock().newCondition();
940 <        lock.writeLock().lock();
941 <        long startTime = System.nanoTime();
942 <        long timeoutMillis = 10;
943 <        assertFalse(c.await(timeoutMillis, MILLISECONDS));
944 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
945 <        lock.writeLock().unlock();
937 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
938 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
939 >    public void testAwait_Timeout(boolean fair) {
940 >        try {
941 >            final ReentrantReadWriteLock lock =
942 >                new ReentrantReadWriteLock(fair);
943 >            final Condition c = lock.writeLock().newCondition();
944 >            lock.writeLock().lock();
945 >            long startTime = System.nanoTime();
946 >            long timeoutMillis = 10;
947 >            assertFalse(c.await(timeoutMillis, MILLISECONDS));
948 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
949 >            lock.writeLock().unlock();
950 >        } catch (InterruptedException e) {
951 >            threadUnexpectedException(e);
952 >        }
953      }
954  
955      /**
956       * awaitUntil without a signal times out
957       */
958 <    public void testAwaitUntil_Timeout() throws InterruptedException {
959 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
960 <        final Condition c = lock.writeLock().newCondition();
961 <        lock.writeLock().lock();
962 <        java.util.Date d = new java.util.Date();
963 <        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
964 <        lock.writeLock().unlock();
958 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
959 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
960 >    public void testAwaitUntil_Timeout(boolean fair) {
961 >        try {
962 >            final ReentrantReadWriteLock lock =
963 >                new ReentrantReadWriteLock(fair);
964 >            final Condition c = lock.writeLock().newCondition();
965 >            lock.writeLock().lock();
966 >            long startTime = System.nanoTime();
967 >            long timeoutMillis = 10;
968 >            java.util.Date d = new java.util.Date();
969 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
970 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
971 >            lock.writeLock().unlock();
972 >        } catch (InterruptedException e) {
973 >            threadUnexpectedException(e);
974 >        }
975      }
976  
977      /**
978       * await returns when signalled
979       */
980 <    public void testAwait() throws InterruptedException {
981 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
980 >    public void testAwait()      { testAwait(false); }
981 >    public void testAwait_fair() { testAwait(true); }
982 >    public void testAwait(boolean fair) {
983 >        final PublicReentrantReadWriteLock lock =
984 >            new PublicReentrantReadWriteLock(fair);
985          final Condition c = lock.writeLock().newCondition();
986          final CountDownLatch locked = new CountDownLatch(1);
987          Thread t = newStartedThread(new CheckedRunnable() {
# Line 937 | Line 992 | public class ReentrantReadWriteLockTest
992                  lock.writeLock().unlock();
993              }});
994  
995 <        locked.await();
995 >        await(locked);
996          lock.writeLock().lock();
997 +        assertHasWaiters(lock, c, t);
998          c.signal();
999 +        assertHasNoWaiters(lock, c);
1000          assertTrue(t.isAlive());
1001          lock.writeLock().unlock();
1002          awaitTermination(t);
# Line 948 | Line 1005 | public class ReentrantReadWriteLockTest
1005      /**
1006       * awaitUninterruptibly doesn't abort on interrupt
1007       */
1008 <    public void testAwaitUninterruptibly() throws InterruptedException {
1009 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1008 >    public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1009 >    public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1010 >    public void testAwaitUninterruptibly(boolean fair) {
1011 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1012          final Condition c = lock.writeLock().newCondition();
1013          final CountDownLatch locked = new CountDownLatch(1);
955        final AtomicBoolean canAwake = new AtomicBoolean(false);
1014          Thread t = newStartedThread(new CheckedRunnable() {
1015              public void realRun() {
1016                  lock.writeLock().lock();
# Line 962 | Line 1020 | public class ReentrantReadWriteLockTest
1020                  lock.writeLock().unlock();
1021              }});
1022  
1023 <        locked.await();
1023 >        await(locked);
1024          lock.writeLock().lock();
1025          lock.writeLock().unlock();
1026          t.interrupt();
1027 <        t.join(10);
1028 <        assertTrue(t.isAlive());
1027 >        long timeoutMillis = 10;
1028 >        assertThreadStaysAlive(t, timeoutMillis);
1029          lock.writeLock().lock();
1030          c.signal();
1031          lock.writeLock().unlock();
# Line 975 | Line 1033 | public class ReentrantReadWriteLockTest
1033      }
1034  
1035      /**
1036 <     * await is interruptible
979 <     */
980 <    public void testAwait_Interrupt() throws InterruptedException {
981 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
982 <        final Condition c = lock.writeLock().newCondition();
983 <        final CountDownLatch locked = new CountDownLatch(1);
984 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
985 <            public void realRun() throws InterruptedException {
986 <                lock.writeLock().lock();
987 <                assertWriteLockedBy(lock, Thread.currentThread());
988 <                assertHasNoWaiters(lock, c);
989 <                locked.countDown();
990 <                try {
991 <                    c.await();
992 <                } finally {
993 <                    assertWriteLockedBy(lock, Thread.currentThread());
994 <                    assertHasNoWaiters(lock, c);
995 <                    lock.writeLock().unlock();
996 <                    assertFalse(Thread.interrupted());
997 <                }
998 <            }});
999 <
1000 <        locked.await();
1001 <        assertHasWaiters(lock, c, t);
1002 <        t.interrupt();
1003 <        awaitTermination(t);
1004 <        assertNotWriteLocked(lock);
1005 <    }
1006 <
1007 <    /**
1008 <     * awaitNanos is interruptible
1009 <     */
1010 <    public void testAwaitNanos_Interrupt() throws InterruptedException {
1011 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1012 <        final Condition c = lock.writeLock().newCondition();
1013 <        final CountDownLatch locked = new CountDownLatch(1);
1014 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1015 <            public void realRun() throws InterruptedException {
1016 <                lock.writeLock().lock();
1017 <                assertWriteLockedBy(lock, Thread.currentThread());
1018 <                assertHasNoWaiters(lock, c);
1019 <                locked.countDown();
1020 <                try {
1021 <                    c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
1022 <                } finally {
1023 <                    assertWriteLockedBy(lock, Thread.currentThread());
1024 <                    assertHasNoWaiters(lock, c);
1025 <                    lock.writeLock().unlock();
1026 <                    assertFalse(Thread.interrupted());
1027 <                }
1028 <            }});
1029 <
1030 <        locked.await();
1031 <        assertHasWaiters(lock, c, t);
1032 <        t.interrupt();
1033 <        awaitTermination(t);
1034 <        assertNotWriteLocked(lock);
1035 <    }
1036 <
1037 <    /**
1038 <     * awaitUntil is interruptible
1036 >     * await/awaitNanos/awaitUntil is interruptible
1037       */
1038 <    public void testAwaitUntil_Interrupt() throws InterruptedException {
1039 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1038 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1039 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1040 >    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1041 >    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1042 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1043 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1044 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
1045 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
1046 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1047 >        final PublicReentrantReadWriteLock lock =
1048 >            new PublicReentrantReadWriteLock(fair);
1049          final Condition c = lock.writeLock().newCondition();
1050          final CountDownLatch locked = new CountDownLatch(1);
1051          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1052              public void realRun() throws InterruptedException {
1053                  lock.writeLock().lock();
1054 <                assertWriteLockedBy(lock, Thread.currentThread());
1054 >                assertWriteLockedByMoi(lock);
1055                  assertHasNoWaiters(lock, c);
1056                  locked.countDown();
1050                java.util.Date d = new java.util.Date();
1057                  try {
1058 <                    c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1058 >                    await(c, awaitMethod);
1059                  } finally {
1060 <                    assertWriteLockedBy(lock, Thread.currentThread());
1060 >                    assertWriteLockedByMoi(lock);
1061                      assertHasNoWaiters(lock, c);
1062                      lock.writeLock().unlock();
1063                      assertFalse(Thread.interrupted());
1064                  }
1065              }});
1066  
1067 <        locked.await();
1067 >        await(locked);
1068          assertHasWaiters(lock, c, t);
1069          t.interrupt();
1070          awaitTermination(t);
# Line 1068 | Line 1074 | public class ReentrantReadWriteLockTest
1074      /**
1075       * signalAll wakes up all threads
1076       */
1077 <    public void testSignalAll() throws InterruptedException {
1078 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1077 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1078 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1079 >    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1080 >    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1081 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1082 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1083 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
1084 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
1085 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1086 >        final PublicReentrantReadWriteLock lock =
1087 >            new PublicReentrantReadWriteLock(fair);
1088          final Condition c = lock.writeLock().newCondition();
1089          final CountDownLatch locked = new CountDownLatch(2);
1090          final Lock writeLock = lock.writeLock();
1091 <        Thread t1 = newStartedThread(new CheckedRunnable() {
1091 >        class Awaiter extends CheckedRunnable {
1092              public void realRun() throws InterruptedException {
1093                  writeLock.lock();
1094                  locked.countDown();
1095 <                c.await();
1095 >                await(c, awaitMethod);
1096                  writeLock.unlock();
1097 <            }});
1097 >            }
1098 >        }
1099  
1100 <        Thread t2 = newStartedThread(new CheckedRunnable() {
1101 <            public void realRun() throws InterruptedException {
1086 <                writeLock.lock();
1087 <                locked.countDown();
1088 <                c.await();
1089 <                writeLock.unlock();
1090 <            }});
1100 >        Thread t1 = newStartedThread(new Awaiter());
1101 >        Thread t2 = newStartedThread(new Awaiter());
1102  
1103 <        locked.await();
1103 >        await(locked);
1104          writeLock.lock();
1105          assertHasWaiters(lock, c, t1, t2);
1106          c.signalAll();
# Line 1102 | Line 1113 | public class ReentrantReadWriteLockTest
1113      /**
1114       * signal wakes up waiting threads in FIFO order.
1115       */
1116 <    public void testSignalWakesFifo() throws InterruptedException {
1117 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1116 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1117 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1118 >    public void testSignalWakesFifo(boolean fair) {
1119 >        final PublicReentrantReadWriteLock lock =
1120 >            new PublicReentrantReadWriteLock(fair);
1121          final Condition c = lock.writeLock().newCondition();
1122          final CountDownLatch locked1 = new CountDownLatch(1);
1123          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 1116 | Line 1130 | public class ReentrantReadWriteLockTest
1130                  writeLock.unlock();
1131              }});
1132  
1133 <        locked1.await();
1133 >        await(locked1);
1134  
1135          Thread t2 = newStartedThread(new CheckedRunnable() {
1136              public void realRun() throws InterruptedException {
# Line 1126 | Line 1140 | public class ReentrantReadWriteLockTest
1140                  writeLock.unlock();
1141              }});
1142  
1143 <        locked2.await();
1143 >        await(locked2);
1144  
1145          writeLock.lock();
1146          assertHasWaiters(lock, c, t1, t2);
# Line 1145 | Line 1159 | public class ReentrantReadWriteLockTest
1159      }
1160  
1161      /**
1162 +     * await after multiple reentrant locking preserves lock count
1163 +     */
1164 +    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1165 +    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1166 +    public void testAwaitLockCount(boolean fair) {
1167 +        final PublicReentrantReadWriteLock lock =
1168 +            new PublicReentrantReadWriteLock(fair);
1169 +        final Condition c = lock.writeLock().newCondition();
1170 +        final CountDownLatch locked = new CountDownLatch(2);
1171 +        Thread t1 = newStartedThread(new CheckedRunnable() {
1172 +            public void realRun() throws InterruptedException {
1173 +                lock.writeLock().lock();
1174 +                assertWriteLockedByMoi(lock);
1175 +                assertEquals(1, lock.writeLock().getHoldCount());
1176 +                locked.countDown();
1177 +                c.await();
1178 +                assertWriteLockedByMoi(lock);
1179 +                assertEquals(1, lock.writeLock().getHoldCount());
1180 +                lock.writeLock().unlock();
1181 +            }});
1182 +
1183 +        Thread t2 = newStartedThread(new CheckedRunnable() {
1184 +            public void realRun() throws InterruptedException {
1185 +                lock.writeLock().lock();
1186 +                lock.writeLock().lock();
1187 +                assertWriteLockedByMoi(lock);
1188 +                assertEquals(2, lock.writeLock().getHoldCount());
1189 +                locked.countDown();
1190 +                c.await();
1191 +                assertWriteLockedByMoi(lock);
1192 +                assertEquals(2, lock.writeLock().getHoldCount());
1193 +                lock.writeLock().unlock();
1194 +                lock.writeLock().unlock();
1195 +            }});
1196 +
1197 +        await(locked);
1198 +        lock.writeLock().lock();
1199 +        assertHasWaiters(lock, c, t1, t2);
1200 +        c.signalAll();
1201 +        assertHasNoWaiters(lock, c);
1202 +        lock.writeLock().unlock();
1203 +        awaitTermination(t1);
1204 +        awaitTermination(t2);
1205 +    }
1206 +
1207 +    /**
1208       * A serialized lock deserializes as unlocked
1209       */
1210 <    public void testSerialization() throws Exception {
1211 <        ReentrantReadWriteLock l = new ReentrantReadWriteLock();
1212 <        l.readLock().lock();
1213 <        l.readLock().unlock();
1214 <
1215 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1216 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1217 <        out.writeObject(l);
1218 <        out.close();
1219 <
1220 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1221 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1222 <        ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
1223 <        r.readLock().lock();
1224 <        r.readLock().unlock();
1210 >    public void testSerialization()      { testSerialization(false); }
1211 >    public void testSerialization_fair() { testSerialization(true); }
1212 >    public void testSerialization(boolean fair) {
1213 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1214 >        lock.writeLock().lock();
1215 >        lock.readLock().lock();
1216 >
1217 >        ReentrantReadWriteLock clone = serialClone(lock);
1218 >        assertEquals(lock.isFair(), clone.isFair());
1219 >        assertTrue(lock.isWriteLocked());
1220 >        assertFalse(clone.isWriteLocked());
1221 >        assertEquals(1, lock.getReadLockCount());
1222 >        assertEquals(0, clone.getReadLockCount());
1223 >        clone.writeLock().lock();
1224 >        clone.readLock().lock();
1225 >        assertTrue(clone.isWriteLocked());
1226 >        assertEquals(1, clone.getReadLockCount());
1227 >        clone.readLock().unlock();
1228 >        clone.writeLock().unlock();
1229 >        assertFalse(clone.isWriteLocked());
1230 >        assertEquals(1, lock.getReadLockCount());
1231 >        assertEquals(0, clone.getReadLockCount());
1232      }
1233  
1234      /**
1235       * hasQueuedThreads reports whether there are waiting threads
1236       */
1237 <    public void testhasQueuedThreads() throws InterruptedException {
1238 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1237 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
1238 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
1239 >    public void testHasQueuedThreads(boolean fair) {
1240 >        final PublicReentrantReadWriteLock lock =
1241 >            new PublicReentrantReadWriteLock(fair);
1242          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1243          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1244          assertFalse(lock.hasQueuedThreads());
1245          lock.writeLock().lock();
1246          assertFalse(lock.hasQueuedThreads());
1177        long startTime = System.nanoTime();
1247          t1.start();
1248          waitForQueuedThread(lock, t1);
1249 +        assertTrue(lock.hasQueuedThreads());
1250          t2.start();
1251          waitForQueuedThread(lock, t2);
1252          assertTrue(lock.hasQueuedThreads());
# Line 1191 | Line 1261 | public class ReentrantReadWriteLockTest
1261      /**
1262       * hasQueuedThread(null) throws NPE
1263       */
1264 <    public void testHasQueuedThreadNPE() {
1265 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1264 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
1265 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
1266 >    public void testHasQueuedThreadNPE(boolean fair) {
1267 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1268          try {
1269 <            sync.hasQueuedThread(null);
1269 >            lock.hasQueuedThread(null);
1270              shouldThrow();
1271          } catch (NullPointerException success) {}
1272      }
# Line 1202 | Line 1274 | public class ReentrantReadWriteLockTest
1274      /**
1275       * hasQueuedThread reports whether a thread is queued.
1276       */
1277 <    public void testHasQueuedThread() throws InterruptedException {
1278 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1277 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
1278 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
1279 >    public void testHasQueuedThread(boolean fair) {
1280 >        final PublicReentrantReadWriteLock lock =
1281 >            new PublicReentrantReadWriteLock(fair);
1282          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1283          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1284          assertFalse(lock.hasQueuedThread(t1));
1285          assertFalse(lock.hasQueuedThread(t2));
1286          lock.writeLock().lock();
1212        long startTime = System.nanoTime();
1287          t1.start();
1288          waitForQueuedThread(lock, t1);
1289          assertTrue(lock.hasQueuedThread(t1));
# Line 1231 | Line 1305 | public class ReentrantReadWriteLockTest
1305      /**
1306       * getQueueLength reports number of waiting threads
1307       */
1308 <    public void testGetQueueLength() throws InterruptedException {
1309 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1308 >    public void testGetQueueLength()      { testGetQueueLength(false); }
1309 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
1310 >    public void testGetQueueLength(boolean fair) {
1311 >        final PublicReentrantReadWriteLock lock =
1312 >            new PublicReentrantReadWriteLock(fair);
1313          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1314          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1315          assertEquals(0, lock.getQueueLength());
1316          lock.writeLock().lock();
1240        long startTime = System.nanoTime();
1317          t1.start();
1318          waitForQueuedThread(lock, t1);
1319          assertEquals(1, lock.getQueueLength());
# Line 1255 | Line 1331 | public class ReentrantReadWriteLockTest
1331      /**
1332       * getQueuedThreads includes waiting threads
1333       */
1334 <    public void testGetQueuedThreads() throws InterruptedException {
1335 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1334 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
1335 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
1336 >    public void testGetQueuedThreads(boolean fair) {
1337 >        final PublicReentrantReadWriteLock lock =
1338 >            new PublicReentrantReadWriteLock(fair);
1339          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1340          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1341          assertTrue(lock.getQueuedThreads().isEmpty());
1342          lock.writeLock().lock();
1264        long startTime = System.nanoTime();
1343          assertTrue(lock.getQueuedThreads().isEmpty());
1344          t1.start();
1345          waitForQueuedThread(lock, t1);
# Line 1285 | Line 1363 | public class ReentrantReadWriteLockTest
1363      /**
1364       * hasWaiters throws NPE if null
1365       */
1366 <    public void testHasWaitersNPE() {
1367 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1366 >    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
1367 >    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
1368 >    public void testHasWaitersNPE(boolean fair) {
1369 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1370          try {
1371              lock.hasWaiters(null);
1372              shouldThrow();
# Line 1296 | Line 1376 | public class ReentrantReadWriteLockTest
1376      /**
1377       * getWaitQueueLength throws NPE if null
1378       */
1379 <    public void testGetWaitQueueLengthNPE() {
1380 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1379 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
1380 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
1381 >    public void testGetWaitQueueLengthNPE(boolean fair) {
1382 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1383          try {
1384              lock.getWaitQueueLength(null);
1385              shouldThrow();
# Line 1307 | Line 1389 | public class ReentrantReadWriteLockTest
1389      /**
1390       * getWaitingThreads throws NPE if null
1391       */
1392 <    public void testGetWaitingThreadsNPE() {
1393 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1392 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
1393 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
1394 >    public void testGetWaitingThreadsNPE(boolean fair) {
1395 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair);
1396          try {
1397              lock.getWaitingThreads(null);
1398              shouldThrow();
# Line 1316 | Line 1400 | public class ReentrantReadWriteLockTest
1400      }
1401  
1402      /**
1403 <     * hasWaiters throws IAE if not owned
1403 >     * hasWaiters throws IllegalArgumentException if not owned
1404       */
1405 <    public void testHasWaitersIAE() {
1406 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1405 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
1406 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
1407 >    public void testHasWaitersIAE(boolean fair) {
1408 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1409          final Condition c = lock.writeLock().newCondition();
1410 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1410 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1411          try {
1412              lock2.hasWaiters(c);
1413              shouldThrow();
# Line 1329 | Line 1415 | public class ReentrantReadWriteLockTest
1415      }
1416  
1417      /**
1418 <     * hasWaiters throws IMSE if not locked
1418 >     * hasWaiters throws IllegalMonitorStateException if not locked
1419       */
1420 <    public void testHasWaitersIMSE() {
1421 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1420 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
1421 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
1422 >    public void testHasWaitersIMSE(boolean fair) {
1423 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1424          final Condition c = lock.writeLock().newCondition();
1425          try {
1426              lock.hasWaiters(c);
# Line 1341 | Line 1429 | public class ReentrantReadWriteLockTest
1429      }
1430  
1431      /**
1432 <     * getWaitQueueLength throws IAE if not owned
1432 >     * getWaitQueueLength throws IllegalArgumentException if not owned
1433       */
1434 <    public void testGetWaitQueueLengthIAE() {
1435 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1434 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
1435 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
1436 >    public void testGetWaitQueueLengthIAE(boolean fair) {
1437 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1438          final Condition c = lock.writeLock().newCondition();
1439 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1439 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1440          try {
1441              lock2.getWaitQueueLength(c);
1442              shouldThrow();
# Line 1354 | Line 1444 | public class ReentrantReadWriteLockTest
1444      }
1445  
1446      /**
1447 <     * getWaitQueueLength throws IMSE if not locked
1447 >     * getWaitQueueLength throws IllegalMonitorStateException if not locked
1448       */
1449 <    public void testGetWaitQueueLengthIMSE() {
1450 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1449 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
1450 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
1451 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
1452 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1453          final Condition c = lock.writeLock().newCondition();
1454          try {
1455              lock.getWaitQueueLength(c);
# Line 1366 | Line 1458 | public class ReentrantReadWriteLockTest
1458      }
1459  
1460      /**
1461 <     * getWaitingThreads throws IAE if not owned
1461 >     * getWaitingThreads throws IllegalArgumentException if not owned
1462       */
1463 <    public void testGetWaitingThreadsIAE() {
1464 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1463 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
1464 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
1465 >    public void testGetWaitingThreadsIAE(boolean fair) {
1466 >        final PublicReentrantReadWriteLock lock =
1467 >            new PublicReentrantReadWriteLock(fair);
1468          final Condition c = lock.writeLock().newCondition();
1469 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1469 >        final PublicReentrantReadWriteLock lock2 =
1470 >            new PublicReentrantReadWriteLock(fair);
1471          try {
1472              lock2.getWaitingThreads(c);
1473              shouldThrow();
# Line 1379 | Line 1475 | public class ReentrantReadWriteLockTest
1475      }
1476  
1477      /**
1478 <     * getWaitingThreads throws IMSE if not locked
1478 >     * getWaitingThreads throws IllegalMonitorStateException if not locked
1479       */
1480 <    public void testGetWaitingThreadsIMSE() {
1481 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1480 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
1481 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
1482 >    public void testGetWaitingThreadsIMSE(boolean fair) {
1483 >        final PublicReentrantReadWriteLock lock =
1484 >            new PublicReentrantReadWriteLock(fair);
1485          final Condition c = lock.writeLock().newCondition();
1486          try {
1487              lock.getWaitingThreads(c);
# Line 1393 | Line 1492 | public class ReentrantReadWriteLockTest
1492      /**
1493       * hasWaiters returns true when a thread is waiting, else false
1494       */
1495 <    public void testHasWaiters() throws InterruptedException {
1496 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1495 >    public void testHasWaiters()      { testHasWaiters(false); }
1496 >    public void testHasWaiters_fair() { testHasWaiters(true); }
1497 >    public void testHasWaiters(boolean fair) {
1498 >        final PublicReentrantReadWriteLock lock =
1499 >            new PublicReentrantReadWriteLock(fair);
1500          final Condition c = lock.writeLock().newCondition();
1501          final CountDownLatch locked = new CountDownLatch(1);
1502          Thread t = newStartedThread(new CheckedRunnable() {
1503              public void realRun() throws InterruptedException {
1504                  lock.writeLock().lock();
1505 +                assertHasNoWaiters(lock, c);
1506                  assertFalse(lock.hasWaiters(c));
1507                  locked.countDown();
1405                assertEquals(0, lock.getWaitQueueLength(c));
1508                  c.await();
1509 +                assertHasNoWaiters(lock, c);
1510 +                assertFalse(lock.hasWaiters(c));
1511                  lock.writeLock().unlock();
1512              }});
1513  
1514 <        locked.await();
1514 >        await(locked);
1515          lock.writeLock().lock();
1516 +        assertHasWaiters(lock, c, t);
1517          assertTrue(lock.hasWaiters(c));
1413        assertEquals(1, lock.getWaitQueueLength(c));
1518          c.signal();
1519          assertHasNoWaiters(lock, c);
1520 +        assertFalse(lock.hasWaiters(c));
1521          lock.writeLock().unlock();
1522          awaitTermination(t);
1523          assertHasNoWaiters(lock, c);
# Line 1421 | Line 1526 | public class ReentrantReadWriteLockTest
1526      /**
1527       * getWaitQueueLength returns number of waiting threads
1528       */
1529 <    public void testGetWaitQueueLength() throws InterruptedException {
1530 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1529 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
1530 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
1531 >    public void testGetWaitQueueLength(boolean fair) {
1532 >        final PublicReentrantReadWriteLock lock =
1533 >            new PublicReentrantReadWriteLock(fair);
1534          final Condition c = lock.writeLock().newCondition();
1535          final CountDownLatch locked = new CountDownLatch(1);
1536          Thread t = newStartedThread(new CheckedRunnable() {
# Line 1434 | Line 1542 | public class ReentrantReadWriteLockTest
1542                  lock.writeLock().unlock();
1543              }});
1544  
1545 <        locked.await();
1545 >        await(locked);
1546          lock.writeLock().lock();
1547          assertHasWaiters(lock, c, t);
1548          assertEquals(1, lock.getWaitQueueLength(c));
# Line 1448 | Line 1556 | public class ReentrantReadWriteLockTest
1556      /**
1557       * getWaitingThreads returns only and all waiting threads
1558       */
1559 <    public void testGetWaitingThreads() throws InterruptedException {
1560 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1559 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
1560 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
1561 >    public void testGetWaitingThreads(boolean fair) {
1562 >        final PublicReentrantReadWriteLock lock =
1563 >            new PublicReentrantReadWriteLock(fair);
1564          final Condition c = lock.writeLock().newCondition();
1565          final CountDownLatch locked1 = new CountDownLatch(1);
1566          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 1476 | Line 1587 | public class ReentrantReadWriteLockTest
1587          lock.writeLock().unlock();
1588  
1589          t1.start();
1590 <        locked1.await();
1590 >        await(locked1);
1591          t2.start();
1592 <        locked2.await();
1592 >        await(locked2);
1593  
1594          lock.writeLock().lock();
1595          assertTrue(lock.hasWaiters(c));
# Line 1498 | Line 1609 | public class ReentrantReadWriteLockTest
1609      /**
1610       * toString indicates current lock state
1611       */
1612 <    public void testToString() {
1613 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1612 >    public void testToString()      { testToString(false); }
1613 >    public void testToString_fair() { testToString(true); }
1614 >    public void testToString(boolean fair) {
1615 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1616          String us = lock.toString();
1617          assertTrue(us.indexOf("Write locks = 0") >= 0);
1618          assertTrue(us.indexOf("Read locks = 0") >= 0);
# Line 1518 | Line 1631 | public class ReentrantReadWriteLockTest
1631      /**
1632       * readLock.toString indicates current lock state
1633       */
1634 <    public void testReadLockToString() {
1635 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1634 >    public void testReadLockToString()      { testReadLockToString(false); }
1635 >    public void testReadLockToString_fair() { testReadLockToString(true); }
1636 >    public void testReadLockToString(boolean fair) {
1637 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1638          String us = lock.readLock().toString();
1639          assertTrue(us.indexOf("Read locks = 0") >= 0);
1640          lock.readLock().lock();
# Line 1531 | Line 1646 | public class ReentrantReadWriteLockTest
1646      /**
1647       * writeLock.toString indicates current lock state
1648       */
1649 <    public void testWriteLockToString() {
1650 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1649 >    public void testWriteLockToString()      { testWriteLockToString(false); }
1650 >    public void testWriteLockToString_fair() { testWriteLockToString(true); }
1651 >    public void testWriteLockToString(boolean fair) {
1652 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1653          String us = lock.writeLock().toString();
1654          assertTrue(us.indexOf("Unlocked") >= 0);
1655          lock.writeLock().lock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines