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

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.45 by jsr166, Sat May 7 19:03:26 2011 UTC vs.
Revision 1.46 by jsr166, Fri May 13 21:48:59 2011 UTC

# Line 65 | Line 65 | public class ReentrantLockTest extends J
65       * Releases write lock, checking that it had a hold count of 1.
66       */
67      void releaseLock(PublicReentrantLock lock) {
68 <        assertLockedBy(lock, Thread.currentThread());
68 >        assertLockedByMoi(lock);
69          lock.unlock();
70          assertFalse(lock.isHeldByCurrentThread());
71          assertNotLocked(lock);
# Line 108 | Line 108 | public class ReentrantLockTest extends J
108      }
109  
110      /**
111 +     * Checks that lock is locked by the current thread.
112 +     */
113 +    void assertLockedByMoi(PublicReentrantLock lock) {
114 +        assertLockedBy(lock, Thread.currentThread());
115 +    }
116 +
117 +    /**
118       * Checks that condition c has no waiters.
119       */
120      void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
# Line 129 | Line 136 | public class ReentrantLockTest extends J
136          lock.unlock();
137      }
138  
139 +    enum AwaitMethod { await, awaitNanos, awaitUntil };
140 +
141 +    /**
142 +     * Awaits condition using the specified AwaitMethod.
143 +     */
144 +    void await(Condition c, AwaitMethod awaitMethod)
145 +            throws InterruptedException {
146 +        switch (awaitMethod) {
147 +        case await:
148 +            c.await();
149 +            break;
150 +        case awaitNanos:
151 +            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
152 +            assertTrue(nanosRemaining > 0);
153 +            break;
154 +        case awaitUntil:
155 +            java.util.Date d = new java.util.Date();
156 +            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
157 +            break;
158 +        }
159 +    }
160 +
161      /**
162       * Constructor sets given fairness, and is in unlocked state
163       */
# Line 151 | Line 180 | public class ReentrantLockTest extends J
180      /**
181       * locking an unlocked lock succeeds
182       */
183 <    public void testLock() {
184 <        PublicReentrantLock lock = new PublicReentrantLock();
183 >    public void testLock()      { testLock(false); }
184 >    public void testLock_fair() { testLock(true); }
185 >    public void testLock(boolean fair) {
186 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
187          lock.lock();
188 <        assertLockedBy(lock, Thread.currentThread());
158 <        releaseLock(lock);
159 <    }
160 <
161 <    /**
162 <     * locking an unlocked fair lock succeeds
163 <     */
164 <    public void testFairLock() {
165 <        PublicReentrantLock lock = new PublicReentrantLock(true);
166 <        lock.lock();
167 <        assertLockedBy(lock, Thread.currentThread());
188 >        assertLockedByMoi(lock);
189          releaseLock(lock);
190      }
191  
192      /**
193       * Unlocking an unlocked lock throws IllegalMonitorStateException
194       */
195 <    public void testUnlock_IllegalMonitorStateException() {
196 <        ReentrantLock lock = new ReentrantLock();
195 >    public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
196 >    public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
197 >    public void testUnlock_IMSE(boolean fair) {
198 >        ReentrantLock lock = new ReentrantLock(fair);
199          try {
200              lock.unlock();
201              shouldThrow();
# Line 182 | Line 205 | public class ReentrantLockTest extends J
205      /**
206       * tryLock on an unlocked lock succeeds
207       */
208 <    public void testTryLock() {
209 <        PublicReentrantLock lock = new PublicReentrantLock();
208 >    public void testTryLock()      { testTryLock(false); }
209 >    public void testTryLock_fair() { testTryLock(true); }
210 >    public void testTryLock(boolean fair) {
211 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
212          assertTrue(lock.tryLock());
213 <        assertLockedBy(lock, Thread.currentThread());
213 >        assertLockedByMoi(lock);
214 >        assertTrue(lock.tryLock());
215 >        assertLockedByMoi(lock);
216 >        lock.unlock();
217          releaseLock(lock);
218      }
219  
220      /**
221       * hasQueuedThreads reports whether there are waiting threads
222       */
223 <    public void testHasQueuedThreads() throws InterruptedException {
224 <        final PublicReentrantLock lock = new PublicReentrantLock();
223 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
224 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
225 >    public void testHasQueuedThreads(boolean fair) {
226 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
227          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
228          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
229          assertFalse(lock.hasQueuedThreads());
# Line 216 | Line 246 | public class ReentrantLockTest extends J
246      /**
247       * getQueueLength reports number of waiting threads
248       */
249 <    public void testGetQueueLength() throws InterruptedException {
250 <        final PublicReentrantLock lock = new PublicReentrantLock();
251 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
252 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
223 <        assertEquals(0, lock.getQueueLength());
224 <        lock.lock();
225 <        t1.start();
226 <        waitForQueuedThread(lock, t1);
227 <        assertEquals(1, lock.getQueueLength());
228 <        t2.start();
229 <        waitForQueuedThread(lock, t2);
230 <        assertEquals(2, lock.getQueueLength());
231 <        t1.interrupt();
232 <        awaitTermination(t1);
233 <        assertEquals(1, lock.getQueueLength());
234 <        lock.unlock();
235 <        awaitTermination(t2);
236 <        assertEquals(0, lock.getQueueLength());
237 <    }
238 <
239 <    /**
240 <     * getQueueLength reports number of waiting threads
241 <     */
242 <    public void testGetQueueLength_fair() throws InterruptedException {
243 <        final PublicReentrantLock lock = new PublicReentrantLock(true);
249 >    public void testGetQueueLength()      { testGetQueueLength(false); }
250 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
251 >    public void testGetQueueLength(boolean fair) {
252 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
253          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
254          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
255          assertEquals(0, lock.getQueueLength());
# Line 262 | Line 271 | public class ReentrantLockTest extends J
271      /**
272       * hasQueuedThread(null) throws NPE
273       */
274 <    public void testHasQueuedThreadNPE() {
275 <        final ReentrantLock lock = new ReentrantLock();
274 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
275 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
276 >    public void testHasQueuedThreadNPE(boolean fair) {
277 >        final ReentrantLock lock = new ReentrantLock(fair);
278          try {
279              lock.hasQueuedThread(null);
280              shouldThrow();
# Line 273 | Line 284 | public class ReentrantLockTest extends J
284      /**
285       * hasQueuedThread reports whether a thread is queued.
286       */
287 <    public void testHasQueuedThread() throws InterruptedException {
288 <        final PublicReentrantLock lock = new PublicReentrantLock();
287 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
288 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
289 >    public void testHasQueuedThread(boolean fair) {
290 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
291          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
292          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
293          assertFalse(lock.hasQueuedThread(t1));
# Line 301 | Line 314 | public class ReentrantLockTest extends J
314      /**
315       * getQueuedThreads includes waiting threads
316       */
317 <    public void testGetQueuedThreads() throws InterruptedException {
318 <        final PublicReentrantLock lock = new PublicReentrantLock();
317 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
318 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
319 >    public void testGetQueuedThreads(boolean fair) {
320 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
321          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
322          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
323          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 330 | Line 345 | public class ReentrantLockTest extends J
345      /**
346       * timed tryLock is interruptible.
347       */
348 <    public void testTryLock_Interrupted() throws InterruptedException {
349 <        final PublicReentrantLock lock = new PublicReentrantLock();
348 >    public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
349 >    public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
350 >    public void testTryLock_Interruptible(boolean fair) {
351 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
352          lock.lock();
353          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
354              public void realRun() throws InterruptedException {
# Line 347 | Line 364 | public class ReentrantLockTest extends J
364      /**
365       * tryLock on a locked lock fails
366       */
367 <    public void testTryLockWhenLocked() throws InterruptedException {
368 <        final PublicReentrantLock lock = new PublicReentrantLock();
367 >    public void testTryLockWhenLocked()      { testTryLockWhenLocked(false); }
368 >    public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
369 >    public void testTryLockWhenLocked(boolean fair) {
370 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
371          lock.lock();
372          Thread t = newStartedThread(new CheckedRunnable() {
373              public void realRun() {
# Line 362 | Line 381 | public class ReentrantLockTest extends J
381      /**
382       * Timed tryLock on a locked lock times out
383       */
384 <    public void testTryLock_Timeout() throws InterruptedException {
385 <        final PublicReentrantLock lock = new PublicReentrantLock();
384 >    public void testTryLock_Timeout()      { testTryLock_Timeout(false); }
385 >    public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
386 >    public void testTryLock_Timeout(boolean fair) {
387 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
388          lock.lock();
389          Thread t = newStartedThread(new CheckedRunnable() {
390              public void realRun() throws InterruptedException {
# Line 380 | Line 401 | public class ReentrantLockTest extends J
401      /**
402       * getHoldCount returns number of recursive holds
403       */
404 <    public void testGetHoldCount() {
405 <        ReentrantLock lock = new ReentrantLock();
404 >    public void testGetHoldCount()      { testGetHoldCount(false); }
405 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
406 >    public void testGetHoldCount(boolean fair) {
407 >        ReentrantLock lock = new ReentrantLock(fair);
408          for (int i = 1; i <= SIZE; i++) {
409              lock.lock();
410              assertEquals(i, lock.getHoldCount());
# Line 395 | Line 418 | public class ReentrantLockTest extends J
418      /**
419       * isLocked is true when locked and false when not
420       */
421 <    public void testIsLocked() throws Exception {
422 <        final ReentrantLock lock = new ReentrantLock();
423 <        assertFalse(lock.isLocked());
424 <        lock.lock();
425 <        assertTrue(lock.isLocked());
426 <        lock.lock();
427 <        assertTrue(lock.isLocked());
428 <        lock.unlock();
429 <        assertTrue(lock.isLocked());
430 <        lock.unlock();
431 <        assertFalse(lock.isLocked());
432 <        final CyclicBarrier barrier = new CyclicBarrier(2);
433 <        Thread t = newStartedThread(new CheckedRunnable() {
434 <            public void realRun() throws Exception {
435 <                lock.lock();
436 <                assertTrue(lock.isLocked());
437 <                barrier.await();
438 <                barrier.await();
439 <                lock.unlock();
440 <            }});
441 <
442 <        barrier.await();
443 <        assertTrue(lock.isLocked());
444 <        barrier.await();
445 <        awaitTermination(t);
446 <        assertFalse(lock.isLocked());
447 <    }
448 <
449 <    /**
450 <     * lockInterruptibly is interruptible.
451 <     */
452 <    public void testLockInterruptibly_Interrupted() throws InterruptedException {
430 <        final PublicReentrantLock lock = new PublicReentrantLock();
431 <        lock.lock();
432 <        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
433 <        waitForQueuedThread(lock, t);
434 <        t.interrupt();
435 <        awaitTermination(t);
436 <        releaseLock(lock);
421 >    public void testIsLocked()      { testIsLocked(false); }
422 >    public void testIsLocked_fair() { testIsLocked(true); }
423 >    public void testIsLocked(boolean fair) {
424 >        try {
425 >            final ReentrantLock lock = new ReentrantLock(fair);
426 >            assertFalse(lock.isLocked());
427 >            lock.lock();
428 >            assertTrue(lock.isLocked());
429 >            lock.lock();
430 >            assertTrue(lock.isLocked());
431 >            lock.unlock();
432 >            assertTrue(lock.isLocked());
433 >            lock.unlock();
434 >            assertFalse(lock.isLocked());
435 >            final CyclicBarrier barrier = new CyclicBarrier(2);
436 >            Thread t = newStartedThread(new CheckedRunnable() {
437 >                    public void realRun() throws Exception {
438 >                        lock.lock();
439 >                        assertTrue(lock.isLocked());
440 >                        barrier.await();
441 >                        barrier.await();
442 >                        lock.unlock();
443 >                    }});
444 >
445 >            barrier.await();
446 >            assertTrue(lock.isLocked());
447 >            barrier.await();
448 >            awaitTermination(t);
449 >            assertFalse(lock.isLocked());
450 >        } catch (Exception e) {
451 >            threadUnexpectedException(e);
452 >        }
453      }
454  
455      /**
456       * lockInterruptibly succeeds when unlocked, else is interruptible
457       */
458 <    public void testLockInterruptibly_Interrupted2() throws InterruptedException {
459 <        final PublicReentrantLock lock = new PublicReentrantLock();
460 <        lock.lockInterruptibly();
458 >    public void testLockInterruptibly()      { testLockInterruptibly(false); }
459 >    public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
460 >    public void testLockInterruptibly(boolean fair) {
461 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
462 >        try {
463 >            lock.lockInterruptibly();
464 >        } catch (InterruptedException ie) {
465 >            threadUnexpectedException(ie);
466 >        }
467 >        assertLockedByMoi(lock);
468          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
469          waitForQueuedThread(lock, t);
470          t.interrupt();
# Line 454 | Line 477 | public class ReentrantLockTest extends J
477      /**
478       * Calling await without holding lock throws IllegalMonitorStateException
479       */
480 <    public void testAwait_IllegalMonitor() throws InterruptedException {
481 <        final ReentrantLock lock = new ReentrantLock();
480 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
481 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
482 >    public void testAwait_IMSE(boolean fair) {
483 >        final ReentrantLock lock = new ReentrantLock(fair);
484          final Condition c = lock.newCondition();
485 +        long startTime = System.nanoTime();
486          try {
487 <            c.await();
488 <            shouldThrow();
489 <        } catch (IllegalMonitorStateException success) {}
490 <        try {
491 <            c.await(LONG_DELAY_MS, MILLISECONDS);
492 <            shouldThrow();
493 <        } catch (IllegalMonitorStateException success) {}
494 <        try {
495 <            c.awaitNanos(100);
496 <            shouldThrow();
497 <        } catch (IllegalMonitorStateException success) {}
498 <        try {
499 <            c.awaitUninterruptibly();
500 <            shouldThrow();
501 <        } catch (IllegalMonitorStateException success) {}
487 >            try {
488 >                c.await();
489 >                shouldThrow();
490 >            } catch (IllegalMonitorStateException success) {}
491 >            try {
492 >                c.await(LONG_DELAY_MS, MILLISECONDS);
493 >                shouldThrow();
494 >            } catch (IllegalMonitorStateException success) {}
495 >            try {
496 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
497 >                shouldThrow();
498 >            } catch (IllegalMonitorStateException success) {}
499 >            try {
500 >                c.awaitUninterruptibly();
501 >                shouldThrow();
502 >            } catch (IllegalMonitorStateException success) {}
503 >        } catch (InterruptedException ie) {
504 >            threadUnexpectedException(ie);
505 >        }
506 >        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
507      }
508  
509      /**
510       * Calling signal without holding lock throws IllegalMonitorStateException
511       */
512 <    public void testSignal_IllegalMonitor() {
513 <        final ReentrantLock lock = new ReentrantLock();
512 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
513 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
514 >    public void testSignal_IMSE(boolean fair) {
515 >        final ReentrantLock lock = new ReentrantLock(fair);
516          final Condition c = lock.newCondition();
517          try {
518              c.signal();
# Line 490 | Line 523 | public class ReentrantLockTest extends J
523      /**
524       * awaitNanos without a signal times out
525       */
526 <    public void testAwaitNanos_Timeout() throws InterruptedException {
527 <        final ReentrantLock lock = new ReentrantLock();
528 <        final Condition c = lock.newCondition();
529 <        lock.lock();
530 <        long startTime = System.nanoTime();
531 <        long timeoutMillis = 10;
532 <        long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
533 <        long nanosRemaining = c.awaitNanos(timeoutNanos);
534 <        assertTrue(nanosRemaining <= 0);
535 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
536 <        lock.unlock();
526 >    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
527 >    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
528 >    public void testAwaitNanos_Timeout(boolean fair) {
529 >        try {
530 >            final ReentrantLock lock = new ReentrantLock(fair);
531 >            final Condition c = lock.newCondition();
532 >            lock.lock();
533 >            long startTime = System.nanoTime();
534 >            long timeoutMillis = 10;
535 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
536 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
537 >            assertTrue(nanosRemaining <= 0);
538 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
539 >            lock.unlock();
540 >        } catch (InterruptedException e) {
541 >            threadUnexpectedException(e);
542 >        }
543      }
544  
545      /**
546       * timed await without a signal times out
547       */
548 <    public void testAwait_Timeout() throws InterruptedException {
549 <        final ReentrantLock lock = new ReentrantLock();
550 <        final Condition c = lock.newCondition();
551 <        lock.lock();
552 <        long startTime = System.nanoTime();
553 <        long timeoutMillis = 10;
554 <        assertFalse(c.await(timeoutMillis, MILLISECONDS));
555 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
556 <        lock.unlock();
548 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
549 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
550 >    public void testAwait_Timeout(boolean fair) {
551 >        try {
552 >            final ReentrantLock lock = new ReentrantLock(fair);
553 >            final Condition c = lock.newCondition();
554 >            lock.lock();
555 >            long startTime = System.nanoTime();
556 >            long timeoutMillis = 10;
557 >            assertFalse(c.await(timeoutMillis, MILLISECONDS));
558 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
559 >            lock.unlock();
560 >        } catch (InterruptedException e) {
561 >            threadUnexpectedException(e);
562 >        }
563      }
564  
565      /**
566       * awaitUntil without a signal times out
567       */
568 <    public void testAwaitUntil_Timeout() throws InterruptedException {
569 <        final ReentrantLock lock = new ReentrantLock();
570 <        final Condition c = lock.newCondition();
571 <        lock.lock();
572 <        long startTime = System.nanoTime();
573 <        long timeoutMillis = 10;
574 <        java.util.Date d = new java.util.Date();
575 <        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
576 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
577 <        lock.unlock();
568 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
569 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
570 >    public void testAwaitUntil_Timeout(boolean fair) {
571 >        try {
572 >            final ReentrantLock lock = new ReentrantLock(fair);
573 >            final Condition c = lock.newCondition();
574 >            lock.lock();
575 >            long startTime = System.nanoTime();
576 >            long timeoutMillis = 10;
577 >            java.util.Date d = new java.util.Date();
578 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
579 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
580 >            lock.unlock();
581 >        } catch (InterruptedException e) {
582 >            threadUnexpectedException(e);
583 >        }
584      }
585  
586      /**
587       * await returns when signalled
588       */
589 <    public void testAwait() throws InterruptedException {
590 <        final PublicReentrantLock lock = new PublicReentrantLock();
589 >    public void testAwait()      { testAwait(false); }
590 >    public void testAwait_fair() { testAwait(true); }
591 >    public void testAwait(boolean fair) {
592 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
593          final Condition c = lock.newCondition();
594          final CountDownLatch locked = new CountDownLatch(1);
595          Thread t = newStartedThread(new CheckedRunnable() {
# Line 547 | Line 600 | public class ReentrantLockTest extends J
600                  lock.unlock();
601              }});
602  
603 <        locked.await();
603 >        await(locked);
604          lock.lock();
605          assertHasWaiters(lock, c, t);
606          c.signal();
# Line 560 | Line 613 | public class ReentrantLockTest extends J
613      /**
614       * hasWaiters throws NPE if null
615       */
616 <    public void testHasWaitersNPE() {
617 <        final ReentrantLock lock = new ReentrantLock();
616 >    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
617 >    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
618 >    public void testHasWaitersNPE(boolean fair) {
619 >        final ReentrantLock lock = new ReentrantLock(fair);
620          try {
621              lock.hasWaiters(null);
622              shouldThrow();
# Line 571 | Line 626 | public class ReentrantLockTest extends J
626      /**
627       * getWaitQueueLength throws NPE if null
628       */
629 <    public void testGetWaitQueueLengthNPE() {
630 <        final ReentrantLock lock = new ReentrantLock();
629 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
630 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
631 >    public void testGetWaitQueueLengthNPE(boolean fair) {
632 >        final ReentrantLock lock = new ReentrantLock(fair);
633          try {
634              lock.getWaitQueueLength(null);
635              shouldThrow();
# Line 582 | Line 639 | public class ReentrantLockTest extends J
639      /**
640       * getWaitingThreads throws NPE if null
641       */
642 <    public void testGetWaitingThreadsNPE() {
643 <        final PublicReentrantLock lock = new PublicReentrantLock();
642 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
643 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
644 >    public void testGetWaitingThreadsNPE(boolean fair) {
645 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
646          try {
647              lock.getWaitingThreads(null);
648              shouldThrow();
# Line 593 | Line 652 | public class ReentrantLockTest extends J
652      /**
653       * hasWaiters throws IllegalArgumentException if not owned
654       */
655 <    public void testHasWaitersIAE() {
656 <        final ReentrantLock lock = new ReentrantLock();
655 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
656 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
657 >    public void testHasWaitersIAE(boolean fair) {
658 >        final ReentrantLock lock = new ReentrantLock(fair);
659          final Condition c = lock.newCondition();
660 <        final ReentrantLock lock2 = new ReentrantLock();
660 >        final ReentrantLock lock2 = new ReentrantLock(fair);
661          try {
662              lock2.hasWaiters(c);
663              shouldThrow();
# Line 606 | Line 667 | public class ReentrantLockTest extends J
667      /**
668       * hasWaiters throws IllegalMonitorStateException if not locked
669       */
670 <    public void testHasWaitersIMSE() {
671 <        final ReentrantLock lock = new ReentrantLock();
670 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
671 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
672 >    public void testHasWaitersIMSE(boolean fair) {
673 >        final ReentrantLock lock = new ReentrantLock(fair);
674          final Condition c = lock.newCondition();
675          try {
676              lock.hasWaiters(c);
# Line 618 | Line 681 | public class ReentrantLockTest extends J
681      /**
682       * getWaitQueueLength throws IllegalArgumentException if not owned
683       */
684 <    public void testGetWaitQueueLengthIAE() {
685 <        final ReentrantLock lock = new ReentrantLock();
684 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
685 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
686 >    public void testGetWaitQueueLengthIAE(boolean fair) {
687 >        final ReentrantLock lock = new ReentrantLock(fair);
688          final Condition c = lock.newCondition();
689 <        final ReentrantLock lock2 = new ReentrantLock();
689 >        final ReentrantLock lock2 = new ReentrantLock(fair);
690          try {
691              lock2.getWaitQueueLength(c);
692              shouldThrow();
# Line 631 | Line 696 | public class ReentrantLockTest extends J
696      /**
697       * getWaitQueueLength throws IllegalMonitorStateException if not locked
698       */
699 <    public void testGetWaitQueueLengthIMSE() {
700 <        final ReentrantLock lock = new ReentrantLock();
699 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
700 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
701 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
702 >        final ReentrantLock lock = new ReentrantLock(fair);
703          final Condition c = lock.newCondition();
704          try {
705              lock.getWaitQueueLength(c);
# Line 643 | Line 710 | public class ReentrantLockTest extends J
710      /**
711       * getWaitingThreads throws IllegalArgumentException if not owned
712       */
713 <    public void testGetWaitingThreadsIAE() {
714 <        final PublicReentrantLock lock = new PublicReentrantLock();
713 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
714 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
715 >    public void testGetWaitingThreadsIAE(boolean fair) {
716 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
717          final Condition c = lock.newCondition();
718 <        final PublicReentrantLock lock2 = new PublicReentrantLock();
718 >        final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
719          try {
720              lock2.getWaitingThreads(c);
721              shouldThrow();
# Line 656 | Line 725 | public class ReentrantLockTest extends J
725      /**
726       * getWaitingThreads throws IllegalMonitorStateException if not locked
727       */
728 <    public void testGetWaitingThreadsIMSE() {
729 <        final PublicReentrantLock lock = new PublicReentrantLock();
728 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
729 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
730 >    public void testGetWaitingThreadsIMSE(boolean fair) {
731 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
732          final Condition c = lock.newCondition();
733          try {
734              lock.getWaitingThreads(c);
# Line 668 | Line 739 | public class ReentrantLockTest extends J
739      /**
740       * hasWaiters returns true when a thread is waiting, else false
741       */
742 <    public void testHasWaiters() throws InterruptedException {
743 <        final PublicReentrantLock lock = new PublicReentrantLock();
742 >    public void testHasWaiters()      { testHasWaiters(false); }
743 >    public void testHasWaiters_fair() { testHasWaiters(true); }
744 >    public void testHasWaiters(boolean fair) {
745 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
746          final Condition c = lock.newCondition();
747          final CountDownLatch locked = new CountDownLatch(1);
748          Thread t = newStartedThread(new CheckedRunnable() {
# Line 684 | Line 757 | public class ReentrantLockTest extends J
757                  lock.unlock();
758              }});
759  
760 <        locked.await();
760 >        await(locked);
761          lock.lock();
762          assertHasWaiters(lock, c, t);
763          assertTrue(lock.hasWaiters(c));
# Line 699 | Line 772 | public class ReentrantLockTest extends J
772      /**
773       * getWaitQueueLength returns number of waiting threads
774       */
775 <    public void testGetWaitQueueLength() throws InterruptedException {
776 <        final PublicReentrantLock lock = new PublicReentrantLock();
775 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
776 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
777 >    public void testGetWaitQueueLength(boolean fair) {
778 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
779          final Condition c = lock.newCondition();
780          final CountDownLatch locked1 = new CountDownLatch(1);
781          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 729 | Line 804 | public class ReentrantLockTest extends J
804          lock.unlock();
805  
806          t1.start();
807 <        locked1.await();
807 >        await(locked1);
808  
809          lock.lock();
810          assertHasWaiters(lock, c, t1);
# Line 737 | Line 812 | public class ReentrantLockTest extends J
812          lock.unlock();
813  
814          t2.start();
815 <        locked2.await();
815 >        await(locked2);
816  
817          lock.lock();
818          assertHasWaiters(lock, c, t1, t2);
# Line 755 | Line 830 | public class ReentrantLockTest extends J
830      /**
831       * getWaitingThreads returns only and all waiting threads
832       */
833 <    public void testGetWaitingThreads() throws InterruptedException {
834 <        final PublicReentrantLock lock = new PublicReentrantLock();
833 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
834 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
835 >    public void testGetWaitingThreads(boolean fair) {
836 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
837          final Condition c = lock.newCondition();
838          final CountDownLatch locked1 = new CountDownLatch(1);
839          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 783 | Line 860 | public class ReentrantLockTest extends J
860          lock.unlock();
861  
862          t1.start();
863 <        locked1.await();
863 >        await(locked1);
864  
865          lock.lock();
866          assertHasWaiters(lock, c, t1);
# Line 793 | Line 870 | public class ReentrantLockTest extends J
870          lock.unlock();
871  
872          t2.start();
873 <        locked2.await();
873 >        await(locked2);
874  
875          lock.lock();
876          assertHasWaiters(lock, c, t1, t2);
# Line 813 | Line 890 | public class ReentrantLockTest extends J
890      /**
891       * awaitUninterruptibly doesn't abort on interrupt
892       */
893 <    public void testAwaitUninterruptibly() throws InterruptedException {
894 <        final ReentrantLock lock = new ReentrantLock();
893 >    public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
894 >    public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
895 >    public void testAwaitUninterruptibly(boolean fair) {
896 >        final ReentrantLock lock = new ReentrantLock(fair);
897          final Condition c = lock.newCondition();
898          final CountDownLatch locked = new CountDownLatch(1);
899          Thread t = newStartedThread(new CheckedRunnable() {
# Line 826 | Line 905 | public class ReentrantLockTest extends J
905                  lock.unlock();
906              }});
907  
908 <        locked.await();
908 >        await(locked);
909          lock.lock();
910          lock.unlock();
911          t.interrupt();
912          long timeoutMillis = 10;
913 <        assertThreadJoinTimesOut(t, timeoutMillis);
913 >        assertThreadStaysAlive(t, timeoutMillis);
914          lock.lock();
915          c.signal();
916          lock.unlock();
# Line 839 | Line 918 | public class ReentrantLockTest extends J
918      }
919  
920      /**
921 <     * await is interruptible
921 >     * await/awaitNanos/awaitUntil is interruptible
922       */
923 <    public void testAwait_Interrupt() throws InterruptedException {
924 <        final PublicReentrantLock lock = new PublicReentrantLock();
923 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
924 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
925 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
926 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
927 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
928 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
929 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
930 >        final PublicReentrantLock lock =
931 >            new PublicReentrantLock(fair);
932          final Condition c = lock.newCondition();
933          final CountDownLatch locked = new CountDownLatch(1);
934          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
935              public void realRun() throws InterruptedException {
936                  lock.lock();
937 <                assertTrue(lock.isLocked());
852 <                assertTrue(lock.isHeldByCurrentThread());
937 >                assertLockedByMoi(lock);
938                  assertHasNoWaiters(lock, c);
939                  locked.countDown();
940                  try {
941 <                    c.await();
941 >                    await(c, awaitMethod);
942                  } finally {
943 <                    assertTrue(lock.isLocked());
859 <                    assertTrue(lock.isHeldByCurrentThread());
943 >                    assertLockedByMoi(lock);
944                      assertHasNoWaiters(lock, c);
945                      lock.unlock();
946                      assertFalse(Thread.interrupted());
947                  }
948              }});
949  
950 <        locked.await();
950 >        await(locked);
951          assertHasWaiters(lock, c, t);
952          t.interrupt();
953          awaitTermination(t);
954 <        assertFalse(lock.isLocked());
954 >        assertNotLocked(lock);
955      }
956  
957      /**
958 <     * awaitNanos is interruptible
958 >     * signalAll wakes up all threads
959       */
960 <    public void testAwaitNanos_Interrupt() throws InterruptedException {
961 <        final PublicReentrantLock lock = new PublicReentrantLock();
960 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
961 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
962 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
963 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
964 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
965 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
966 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
967 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
968          final Condition c = lock.newCondition();
969 <        final CountDownLatch locked = new CountDownLatch(1);
970 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
969 >        final CountDownLatch locked = new CountDownLatch(2);
970 >        class Awaiter extends CheckedRunnable {
971              public void realRun() throws InterruptedException {
972                  lock.lock();
883                assertTrue(lock.isLocked());
884                assertTrue(lock.isHeldByCurrentThread());
885                assertHasNoWaiters(lock, c);
973                  locked.countDown();
974 <                try {
975 <                    c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
976 <                } finally {
977 <                    assertTrue(lock.isLocked());
891 <                    assertTrue(lock.isHeldByCurrentThread());
892 <                    assertHasNoWaiters(lock, c);
893 <                    lock.unlock();
894 <                    assertFalse(Thread.interrupted());
895 <                }
896 <            }});
897 <
898 <        locked.await();
899 <        assertHasWaiters(lock, c, t);
900 <        t.interrupt();
901 <        awaitTermination(t);
902 <        assertFalse(lock.isLocked());
903 <    }
974 >                await(c, awaitMethod);
975 >                lock.unlock();
976 >            }
977 >        }
978  
979 <    /**
980 <     * awaitUntil is interruptible
907 <     */
908 <    public void testAwaitUntil_Interrupt() throws InterruptedException {
909 <        final PublicReentrantLock lock = new PublicReentrantLock();
910 <        final Condition c = lock.newCondition();
911 <        final CountDownLatch locked = new CountDownLatch(1);
912 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
913 <            public void realRun() throws InterruptedException {
914 <                lock.lock();
915 <                assertTrue(lock.isLocked());
916 <                assertTrue(lock.isHeldByCurrentThread());
917 <                assertHasNoWaiters(lock, c);
918 <                locked.countDown();
919 <                java.util.Date d = new java.util.Date();
920 <                try {
921 <                    c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS));
922 <                } finally {
923 <                    assertTrue(lock.isLocked());
924 <                    assertTrue(lock.isHeldByCurrentThread());
925 <                    assertHasNoWaiters(lock, c);
926 <                    lock.unlock();
927 <                    assertFalse(Thread.interrupted());
928 <                }
929 <            }});
979 >        Thread t1 = newStartedThread(new Awaiter());
980 >        Thread t2 = newStartedThread(new Awaiter());
981  
982 <        locked.await();
983 <        assertHasWaiters(lock, c, t);
984 <        t.interrupt();
985 <        awaitTermination(t);
986 <        assertFalse(lock.isLocked());
982 >        await(locked);
983 >        lock.lock();
984 >        assertHasWaiters(lock, c, t1, t2);
985 >        c.signalAll();
986 >        assertHasNoWaiters(lock, c);
987 >        lock.unlock();
988 >        awaitTermination(t1);
989 >        awaitTermination(t2);
990      }
991  
992      /**
993 <     * signalAll wakes up all threads
993 >     * signal wakes up waiting threads in FIFO order.
994       */
995 <    public void testSignalAll() throws InterruptedException {
996 <        final PublicReentrantLock lock = new PublicReentrantLock();
995 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
996 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
997 >    public void testSignalWakesFifo(boolean fair) {
998 >        final PublicReentrantLock lock =
999 >            new PublicReentrantLock(fair);
1000          final Condition c = lock.newCondition();
1001 <        final CountDownLatch locked = new CountDownLatch(2);
1001 >        final CountDownLatch locked1 = new CountDownLatch(1);
1002 >        final CountDownLatch locked2 = new CountDownLatch(1);
1003          Thread t1 = newStartedThread(new CheckedRunnable() {
1004              public void realRun() throws InterruptedException {
1005                  lock.lock();
1006 <                locked.countDown();
1006 >                locked1.countDown();
1007                  c.await();
1008                  lock.unlock();
1009              }});
1010  
1011 +        await(locked1);
1012 +
1013          Thread t2 = newStartedThread(new CheckedRunnable() {
1014              public void realRun() throws InterruptedException {
1015                  lock.lock();
1016 <                locked.countDown();
1016 >                locked2.countDown();
1017                  c.await();
1018                  lock.unlock();
1019              }});
1020  
1021 <        locked.await();
1021 >        await(locked2);
1022 >
1023          lock.lock();
1024          assertHasWaiters(lock, c, t1, t2);
1025 <        c.signalAll();
1025 >        assertFalse(lock.hasQueuedThreads());
1026 >        c.signal();
1027 >        assertHasWaiters(lock, c, t2);
1028 >        assertTrue(lock.hasQueuedThread(t1));
1029 >        assertFalse(lock.hasQueuedThread(t2));
1030 >        c.signal();
1031          assertHasNoWaiters(lock, c);
1032 +        assertTrue(lock.hasQueuedThread(t1));
1033 +        assertTrue(lock.hasQueuedThread(t2));
1034          lock.unlock();
1035          awaitTermination(t1);
1036          awaitTermination(t2);
# Line 971 | Line 1039 | public class ReentrantLockTest extends J
1039      /**
1040       * await after multiple reentrant locking preserves lock count
1041       */
1042 <    public void testAwaitLockCount() throws InterruptedException {
1043 <        final PublicReentrantLock lock = new PublicReentrantLock();
1042 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1043 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1044 >    public void testAwaitLockCount(boolean fair) {
1045 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
1046          final Condition c = lock.newCondition();
1047          final CountDownLatch locked = new CountDownLatch(2);
1048          Thread t1 = newStartedThread(new CheckedRunnable() {
1049              public void realRun() throws InterruptedException {
1050                  lock.lock();
1051 <                assertLockedBy(lock, Thread.currentThread());
1051 >                assertLockedByMoi(lock);
1052                  assertEquals(1, lock.getHoldCount());
1053                  locked.countDown();
1054                  c.await();
1055 <                assertLockedBy(lock, Thread.currentThread());
1055 >                assertLockedByMoi(lock);
1056                  assertEquals(1, lock.getHoldCount());
1057                  lock.unlock();
1058              }});
# Line 991 | Line 1061 | public class ReentrantLockTest extends J
1061              public void realRun() throws InterruptedException {
1062                  lock.lock();
1063                  lock.lock();
1064 <                assertLockedBy(lock, Thread.currentThread());
1064 >                assertLockedByMoi(lock);
1065                  assertEquals(2, lock.getHoldCount());
1066                  locked.countDown();
1067                  c.await();
1068 <                assertLockedBy(lock, Thread.currentThread());
1068 >                assertLockedByMoi(lock);
1069                  assertEquals(2, lock.getHoldCount());
1070                  lock.unlock();
1071                  lock.unlock();
1072              }});
1073  
1074 <        locked.await();
1074 >        await(locked);
1075          lock.lock();
1076          assertHasWaiters(lock, c, t1, t2);
1077          assertEquals(1, lock.getHoldCount());
# Line 1015 | Line 1085 | public class ReentrantLockTest extends J
1085      /**
1086       * A serialized lock deserializes as unlocked
1087       */
1088 <    public void testSerialization() throws Exception {
1089 <        ReentrantLock l = new ReentrantLock();
1090 <        l.lock();
1091 <        l.unlock();
1092 <
1093 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1094 <        ObjectOutputStream out =
1095 <            new ObjectOutputStream(new BufferedOutputStream(bout));
1096 <        out.writeObject(l);
1097 <        out.close();
1098 <
1099 <        ByteArrayInputStream bin =
1100 <            new ByteArrayInputStream(bout.toByteArray());
1101 <        ObjectInputStream in =
1102 <            new ObjectInputStream(new BufferedInputStream(bin));
1103 <        ReentrantLock r = (ReentrantLock) in.readObject();
1104 <        r.lock();
1105 <        r.unlock();
1088 >    public void testSerialization()      { testSerialization(false); }
1089 >    public void testSerialization_fair() { testSerialization(true); }
1090 >    public void testSerialization(boolean fair) {
1091 >        ReentrantLock lock = new ReentrantLock(fair);
1092 >        lock.lock();
1093 >
1094 >        ReentrantLock clone = serialClone(lock);
1095 >        assertEquals(lock.isFair(), clone.isFair());
1096 >        assertTrue(lock.isLocked());
1097 >        assertFalse(clone.isLocked());
1098 >        assertEquals(1, lock.getHoldCount());
1099 >        assertEquals(0, clone.getHoldCount());
1100 >        clone.lock();
1101 >        clone.lock();
1102 >        assertTrue(clone.isLocked());
1103 >        assertEquals(2, clone.getHoldCount());
1104 >        assertEquals(1, lock.getHoldCount());
1105 >        clone.unlock();
1106 >        clone.unlock();
1107 >        assertTrue(lock.isLocked());
1108 >        assertFalse(clone.isLocked());
1109      }
1110  
1111      /**
1112       * toString indicates current lock state
1113       */
1114 <    public void testToString() {
1115 <        ReentrantLock lock = new ReentrantLock();
1114 >    public void testToString()      { testToString(false); }
1115 >    public void testToString_fair() { testToString(true); }
1116 >    public void testToString(boolean fair) {
1117 >        ReentrantLock lock = new ReentrantLock(fair);
1118          String us = lock.toString();
1119          assertTrue(us.indexOf("Unlocked") >= 0);
1120          lock.lock();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines