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.52 by jsr166, Thu May 30 03:28:55 2013 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 < import java.util.concurrent.locks.*;
11 < import java.util.concurrent.*;
10 > import java.util.concurrent.locks.Condition;
11 > import java.util.concurrent.locks.ReentrantLock;
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.CyclicBarrier;
14   import static java.util.concurrent.TimeUnit.MILLISECONDS;
15   import java.util.*;
14 import java.io.*;
16  
17   public class ReentrantLockTest extends JSR166TestCase {
18      public static void main(String[] args) {
# Line 65 | Line 66 | public class ReentrantLockTest extends J
66       * Releases write lock, checking that it had a hold count of 1.
67       */
68      void releaseLock(PublicReentrantLock lock) {
69 <        assertLockedBy(lock, Thread.currentThread());
69 >        assertLockedByMoi(lock);
70          lock.unlock();
71          assertFalse(lock.isHeldByCurrentThread());
72          assertNotLocked(lock);
# Line 78 | Line 79 | public class ReentrantLockTest extends J
79          long startTime = System.nanoTime();
80          while (!lock.hasQueuedThread(t)) {
81              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
82 <                throw new AssertionError("timed out");
82 >                throw new AssertionFailedError("timed out");
83              Thread.yield();
84          }
85          assertTrue(t.isAlive());
86 <        assertTrue(lock.getOwner() != t);
86 >        assertNotSame(t, lock.getOwner());
87      }
88  
89      /**
# Line 108 | Line 109 | public class ReentrantLockTest extends J
109      }
110  
111      /**
112 +     * Checks that lock is locked by the current thread.
113 +     */
114 +    void assertLockedByMoi(PublicReentrantLock lock) {
115 +        assertLockedBy(lock, Thread.currentThread());
116 +    }
117 +
118 +    /**
119       * Checks that condition c has no waiters.
120       */
121      void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
# Line 129 | Line 137 | public class ReentrantLockTest extends J
137          lock.unlock();
138      }
139  
140 +    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
141 +
142 +    /**
143 +     * Awaits condition using the specified AwaitMethod.
144 +     */
145 +    void await(Condition c, AwaitMethod awaitMethod)
146 +            throws InterruptedException {
147 +        long timeoutMillis = 2 * LONG_DELAY_MS;
148 +        switch (awaitMethod) {
149 +        case await:
150 +            c.await();
151 +            break;
152 +        case awaitTimed:
153 +            assertTrue(c.await(timeoutMillis, MILLISECONDS));
154 +            break;
155 +        case awaitNanos:
156 +            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
157 +            long nanosRemaining = c.awaitNanos(nanosTimeout);
158 +            assertTrue(nanosRemaining > 0);
159 +            break;
160 +        case awaitUntil:
161 +            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
162 +            break;
163 +        }
164 +    }
165 +
166      /**
167       * Constructor sets given fairness, and is in unlocked state
168       */
# Line 151 | Line 185 | public class ReentrantLockTest extends J
185      /**
186       * locking an unlocked lock succeeds
187       */
188 <    public void testLock() {
189 <        PublicReentrantLock lock = new PublicReentrantLock();
190 <        lock.lock();
191 <        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);
188 >    public void testLock()      { testLock(false); }
189 >    public void testLock_fair() { testLock(true); }
190 >    public void testLock(boolean fair) {
191 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
192          lock.lock();
193 <        assertLockedBy(lock, Thread.currentThread());
193 >        assertLockedByMoi(lock);
194          releaseLock(lock);
195      }
196  
197      /**
198       * Unlocking an unlocked lock throws IllegalMonitorStateException
199       */
200 <    public void testUnlock_IllegalMonitorStateException() {
201 <        ReentrantLock lock = new ReentrantLock();
200 >    public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
201 >    public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
202 >    public void testUnlock_IMSE(boolean fair) {
203 >        ReentrantLock lock = new ReentrantLock(fair);
204          try {
205              lock.unlock();
206              shouldThrow();
# Line 182 | Line 210 | public class ReentrantLockTest extends J
210      /**
211       * tryLock on an unlocked lock succeeds
212       */
213 <    public void testTryLock() {
214 <        PublicReentrantLock lock = new PublicReentrantLock();
213 >    public void testTryLock()      { testTryLock(false); }
214 >    public void testTryLock_fair() { testTryLock(true); }
215 >    public void testTryLock(boolean fair) {
216 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
217          assertTrue(lock.tryLock());
218 <        assertLockedBy(lock, Thread.currentThread());
218 >        assertLockedByMoi(lock);
219 >        assertTrue(lock.tryLock());
220 >        assertLockedByMoi(lock);
221 >        lock.unlock();
222          releaseLock(lock);
223      }
224  
225      /**
226       * hasQueuedThreads reports whether there are waiting threads
227       */
228 <    public void testHasQueuedThreads() throws InterruptedException {
229 <        final PublicReentrantLock lock = new PublicReentrantLock();
228 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
229 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
230 >    public void testHasQueuedThreads(boolean fair) {
231 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
232          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
233          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
234          assertFalse(lock.hasQueuedThreads());
# Line 216 | Line 251 | public class ReentrantLockTest extends J
251      /**
252       * getQueueLength reports number of waiting threads
253       */
254 <    public void testGetQueueLength() throws InterruptedException {
255 <        final PublicReentrantLock lock = new PublicReentrantLock();
256 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
257 <        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);
254 >    public void testGetQueueLength()      { testGetQueueLength(false); }
255 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
256 >    public void testGetQueueLength(boolean fair) {
257 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
258          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
259          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
260          assertEquals(0, lock.getQueueLength());
# Line 262 | Line 276 | public class ReentrantLockTest extends J
276      /**
277       * hasQueuedThread(null) throws NPE
278       */
279 <    public void testHasQueuedThreadNPE() {
280 <        final ReentrantLock lock = new ReentrantLock();
279 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
280 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
281 >    public void testHasQueuedThreadNPE(boolean fair) {
282 >        final ReentrantLock lock = new ReentrantLock(fair);
283          try {
284              lock.hasQueuedThread(null);
285              shouldThrow();
# Line 271 | Line 287 | public class ReentrantLockTest extends J
287      }
288  
289      /**
290 <     * hasQueuedThread reports whether a thread is queued.
290 >     * hasQueuedThread reports whether a thread is queued
291       */
292 <    public void testHasQueuedThread() throws InterruptedException {
293 <        final PublicReentrantLock lock = new PublicReentrantLock();
292 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
293 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
294 >    public void testHasQueuedThread(boolean fair) {
295 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
296          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
297          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
298          assertFalse(lock.hasQueuedThread(t1));
# Line 301 | Line 319 | public class ReentrantLockTest extends J
319      /**
320       * getQueuedThreads includes waiting threads
321       */
322 <    public void testGetQueuedThreads() throws InterruptedException {
323 <        final PublicReentrantLock lock = new PublicReentrantLock();
322 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
323 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
324 >    public void testGetQueuedThreads(boolean fair) {
325 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
326          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
327          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
328          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 328 | Line 348 | public class ReentrantLockTest extends J
348      }
349  
350      /**
351 <     * timed tryLock is interruptible.
351 >     * timed tryLock is interruptible
352       */
353 <    public void testTryLock_Interrupted() throws InterruptedException {
354 <        final PublicReentrantLock lock = new PublicReentrantLock();
353 >    public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
354 >    public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
355 >    public void testTryLock_Interruptible(boolean fair) {
356 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
357          lock.lock();
358          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
359              public void realRun() throws InterruptedException {
# Line 347 | Line 369 | public class ReentrantLockTest extends J
369      /**
370       * tryLock on a locked lock fails
371       */
372 <    public void testTryLockWhenLocked() throws InterruptedException {
373 <        final PublicReentrantLock lock = new PublicReentrantLock();
372 >    public void testTryLockWhenLocked()      { testTryLockWhenLocked(false); }
373 >    public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
374 >    public void testTryLockWhenLocked(boolean fair) {
375 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
376          lock.lock();
377          Thread t = newStartedThread(new CheckedRunnable() {
378              public void realRun() {
# Line 362 | Line 386 | public class ReentrantLockTest extends J
386      /**
387       * Timed tryLock on a locked lock times out
388       */
389 <    public void testTryLock_Timeout() throws InterruptedException {
390 <        final PublicReentrantLock lock = new PublicReentrantLock();
389 >    public void testTryLock_Timeout()      { testTryLock_Timeout(false); }
390 >    public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
391 >    public void testTryLock_Timeout(boolean fair) {
392 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
393          lock.lock();
394          Thread t = newStartedThread(new CheckedRunnable() {
395              public void realRun() throws InterruptedException {
# Line 380 | Line 406 | public class ReentrantLockTest extends J
406      /**
407       * getHoldCount returns number of recursive holds
408       */
409 <    public void testGetHoldCount() {
410 <        ReentrantLock lock = new ReentrantLock();
409 >    public void testGetHoldCount()      { testGetHoldCount(false); }
410 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
411 >    public void testGetHoldCount(boolean fair) {
412 >        ReentrantLock lock = new ReentrantLock(fair);
413          for (int i = 1; i <= SIZE; i++) {
414              lock.lock();
415              assertEquals(i, lock.getHoldCount());
# Line 395 | Line 423 | public class ReentrantLockTest extends J
423      /**
424       * isLocked is true when locked and false when not
425       */
426 <    public void testIsLocked() throws Exception {
427 <        final ReentrantLock lock = new ReentrantLock();
428 <        assertFalse(lock.isLocked());
429 <        lock.lock();
430 <        assertTrue(lock.isLocked());
431 <        lock.lock();
432 <        assertTrue(lock.isLocked());
433 <        lock.unlock();
434 <        assertTrue(lock.isLocked());
435 <        lock.unlock();
436 <        assertFalse(lock.isLocked());
437 <        final CyclicBarrier barrier = new CyclicBarrier(2);
438 <        Thread t = newStartedThread(new CheckedRunnable() {
439 <            public void realRun() throws Exception {
440 <                lock.lock();
441 <                assertTrue(lock.isLocked());
442 <                barrier.await();
443 <                barrier.await();
444 <                lock.unlock();
445 <            }});
446 <
447 <        barrier.await();
448 <        assertTrue(lock.isLocked());
449 <        barrier.await();
450 <        awaitTermination(t);
451 <        assertFalse(lock.isLocked());
452 <    }
453 <
454 <    /**
455 <     * lockInterruptibly is interruptible.
456 <     */
457 <    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);
426 >    public void testIsLocked()      { testIsLocked(false); }
427 >    public void testIsLocked_fair() { testIsLocked(true); }
428 >    public void testIsLocked(boolean fair) {
429 >        try {
430 >            final ReentrantLock lock = new ReentrantLock(fair);
431 >            assertFalse(lock.isLocked());
432 >            lock.lock();
433 >            assertTrue(lock.isLocked());
434 >            lock.lock();
435 >            assertTrue(lock.isLocked());
436 >            lock.unlock();
437 >            assertTrue(lock.isLocked());
438 >            lock.unlock();
439 >            assertFalse(lock.isLocked());
440 >            final CyclicBarrier barrier = new CyclicBarrier(2);
441 >            Thread t = newStartedThread(new CheckedRunnable() {
442 >                    public void realRun() throws Exception {
443 >                        lock.lock();
444 >                        assertTrue(lock.isLocked());
445 >                        barrier.await();
446 >                        barrier.await();
447 >                        lock.unlock();
448 >                    }});
449 >
450 >            barrier.await();
451 >            assertTrue(lock.isLocked());
452 >            barrier.await();
453 >            awaitTermination(t);
454 >            assertFalse(lock.isLocked());
455 >        } catch (Exception e) {
456 >            threadUnexpectedException(e);
457 >        }
458      }
459  
460      /**
461       * lockInterruptibly succeeds when unlocked, else is interruptible
462       */
463 <    public void testLockInterruptibly_Interrupted2() throws InterruptedException {
464 <        final PublicReentrantLock lock = new PublicReentrantLock();
465 <        lock.lockInterruptibly();
463 >    public void testLockInterruptibly()      { testLockInterruptibly(false); }
464 >    public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
465 >    public void testLockInterruptibly(boolean fair) {
466 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
467 >        try {
468 >            lock.lockInterruptibly();
469 >        } catch (InterruptedException ie) {
470 >            threadUnexpectedException(ie);
471 >        }
472 >        assertLockedByMoi(lock);
473          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
474          waitForQueuedThread(lock, t);
475          t.interrupt();
# Line 454 | Line 482 | public class ReentrantLockTest extends J
482      /**
483       * Calling await without holding lock throws IllegalMonitorStateException
484       */
485 <    public void testAwait_IllegalMonitor() throws InterruptedException {
486 <        final ReentrantLock lock = new ReentrantLock();
487 <        final Condition c = lock.newCondition();
488 <        try {
489 <            c.await();
490 <            shouldThrow();
491 <        } catch (IllegalMonitorStateException success) {}
492 <        try {
493 <            c.await(LONG_DELAY_MS, MILLISECONDS);
494 <            shouldThrow();
495 <        } catch (IllegalMonitorStateException success) {}
496 <        try {
497 <            c.awaitNanos(100);
498 <            shouldThrow();
471 <        } catch (IllegalMonitorStateException success) {}
472 <        try {
473 <            c.awaitUninterruptibly();
474 <            shouldThrow();
475 <        } catch (IllegalMonitorStateException success) {}
485 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
486 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
487 >    public void testAwait_IMSE(boolean fair) {
488 >        final ReentrantLock lock = new ReentrantLock(fair);
489 >        final Condition c = lock.newCondition();
490 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
491 >            long startTime = System.nanoTime();
492 >            try {
493 >                await(c, awaitMethod);
494 >                shouldThrow();
495 >            } catch (IllegalMonitorStateException success) {
496 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
497 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
498 >        }
499      }
500  
501      /**
502       * Calling signal without holding lock throws IllegalMonitorStateException
503       */
504 <    public void testSignal_IllegalMonitor() {
505 <        final ReentrantLock lock = new ReentrantLock();
504 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
505 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
506 >    public void testSignal_IMSE(boolean fair) {
507 >        final ReentrantLock lock = new ReentrantLock(fair);
508          final Condition c = lock.newCondition();
509          try {
510              c.signal();
# Line 490 | Line 515 | public class ReentrantLockTest extends J
515      /**
516       * awaitNanos without a signal times out
517       */
518 <    public void testAwaitNanos_Timeout() throws InterruptedException {
519 <        final ReentrantLock lock = new ReentrantLock();
520 <        final Condition c = lock.newCondition();
521 <        lock.lock();
522 <        long startTime = System.nanoTime();
523 <        long timeoutMillis = 10;
524 <        long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
525 <        long nanosRemaining = c.awaitNanos(timeoutNanos);
526 <        assertTrue(nanosRemaining <= 0);
527 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
528 <        lock.unlock();
518 >    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
519 >    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
520 >    public void testAwaitNanos_Timeout(boolean fair) {
521 >        try {
522 >            final ReentrantLock lock = new ReentrantLock(fair);
523 >            final Condition c = lock.newCondition();
524 >            lock.lock();
525 >            long startTime = System.nanoTime();
526 >            long timeoutMillis = 10;
527 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
528 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
529 >            assertTrue(nanosRemaining <= 0);
530 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
531 >            lock.unlock();
532 >        } catch (InterruptedException e) {
533 >            threadUnexpectedException(e);
534 >        }
535      }
536  
537      /**
538       * timed await without a signal times out
539       */
540 <    public void testAwait_Timeout() throws InterruptedException {
541 <        final ReentrantLock lock = new ReentrantLock();
542 <        final Condition c = lock.newCondition();
543 <        lock.lock();
544 <        long startTime = System.nanoTime();
545 <        long timeoutMillis = 10;
546 <        assertFalse(c.await(timeoutMillis, MILLISECONDS));
547 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
548 <        lock.unlock();
540 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
541 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
542 >    public void testAwait_Timeout(boolean fair) {
543 >        try {
544 >            final ReentrantLock lock = new ReentrantLock(fair);
545 >            final Condition c = lock.newCondition();
546 >            lock.lock();
547 >            long startTime = System.nanoTime();
548 >            long timeoutMillis = 10;
549 >            assertFalse(c.await(timeoutMillis, MILLISECONDS));
550 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
551 >            lock.unlock();
552 >        } catch (InterruptedException e) {
553 >            threadUnexpectedException(e);
554 >        }
555      }
556  
557      /**
558       * awaitUntil without a signal times out
559       */
560 <    public void testAwaitUntil_Timeout() throws InterruptedException {
561 <        final ReentrantLock lock = new ReentrantLock();
562 <        final Condition c = lock.newCondition();
563 <        lock.lock();
564 <        long startTime = System.nanoTime();
565 <        long timeoutMillis = 10;
566 <        java.util.Date d = new java.util.Date();
567 <        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
568 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
569 <        lock.unlock();
560 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
561 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
562 >    public void testAwaitUntil_Timeout(boolean fair) {
563 >        try {
564 >            final ReentrantLock lock = new ReentrantLock(fair);
565 >            final Condition c = lock.newCondition();
566 >            lock.lock();
567 >            long startTime = System.nanoTime();
568 >            long timeoutMillis = 10;
569 >            java.util.Date d = new java.util.Date();
570 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
571 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
572 >            lock.unlock();
573 >        } catch (InterruptedException e) {
574 >            threadUnexpectedException(e);
575 >        }
576      }
577  
578      /**
579       * await returns when signalled
580       */
581 <    public void testAwait() throws InterruptedException {
582 <        final PublicReentrantLock lock = new PublicReentrantLock();
581 >    public void testAwait()      { testAwait(false); }
582 >    public void testAwait_fair() { testAwait(true); }
583 >    public void testAwait(boolean fair) {
584 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
585          final Condition c = lock.newCondition();
586          final CountDownLatch locked = new CountDownLatch(1);
587          Thread t = newStartedThread(new CheckedRunnable() {
# Line 547 | Line 592 | public class ReentrantLockTest extends J
592                  lock.unlock();
593              }});
594  
595 <        locked.await();
595 >        await(locked);
596          lock.lock();
597          assertHasWaiters(lock, c, t);
598          c.signal();
# Line 560 | Line 605 | public class ReentrantLockTest extends J
605      /**
606       * hasWaiters throws NPE if null
607       */
608 <    public void testHasWaitersNPE() {
609 <        final ReentrantLock lock = new ReentrantLock();
608 >    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
609 >    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
610 >    public void testHasWaitersNPE(boolean fair) {
611 >        final ReentrantLock lock = new ReentrantLock(fair);
612          try {
613              lock.hasWaiters(null);
614              shouldThrow();
# Line 571 | Line 618 | public class ReentrantLockTest extends J
618      /**
619       * getWaitQueueLength throws NPE if null
620       */
621 <    public void testGetWaitQueueLengthNPE() {
622 <        final ReentrantLock lock = new ReentrantLock();
621 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
622 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
623 >    public void testGetWaitQueueLengthNPE(boolean fair) {
624 >        final ReentrantLock lock = new ReentrantLock(fair);
625          try {
626              lock.getWaitQueueLength(null);
627              shouldThrow();
# Line 582 | Line 631 | public class ReentrantLockTest extends J
631      /**
632       * getWaitingThreads throws NPE if null
633       */
634 <    public void testGetWaitingThreadsNPE() {
635 <        final PublicReentrantLock lock = new PublicReentrantLock();
634 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
635 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
636 >    public void testGetWaitingThreadsNPE(boolean fair) {
637 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
638          try {
639              lock.getWaitingThreads(null);
640              shouldThrow();
# Line 593 | Line 644 | public class ReentrantLockTest extends J
644      /**
645       * hasWaiters throws IllegalArgumentException if not owned
646       */
647 <    public void testHasWaitersIAE() {
648 <        final ReentrantLock lock = new ReentrantLock();
647 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
648 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
649 >    public void testHasWaitersIAE(boolean fair) {
650 >        final ReentrantLock lock = new ReentrantLock(fair);
651          final Condition c = lock.newCondition();
652 <        final ReentrantLock lock2 = new ReentrantLock();
652 >        final ReentrantLock lock2 = new ReentrantLock(fair);
653          try {
654              lock2.hasWaiters(c);
655              shouldThrow();
# Line 606 | Line 659 | public class ReentrantLockTest extends J
659      /**
660       * hasWaiters throws IllegalMonitorStateException if not locked
661       */
662 <    public void testHasWaitersIMSE() {
663 <        final ReentrantLock lock = new ReentrantLock();
662 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
663 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
664 >    public void testHasWaitersIMSE(boolean fair) {
665 >        final ReentrantLock lock = new ReentrantLock(fair);
666          final Condition c = lock.newCondition();
667          try {
668              lock.hasWaiters(c);
# Line 618 | Line 673 | public class ReentrantLockTest extends J
673      /**
674       * getWaitQueueLength throws IllegalArgumentException if not owned
675       */
676 <    public void testGetWaitQueueLengthIAE() {
677 <        final ReentrantLock lock = new ReentrantLock();
676 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
677 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
678 >    public void testGetWaitQueueLengthIAE(boolean fair) {
679 >        final ReentrantLock lock = new ReentrantLock(fair);
680          final Condition c = lock.newCondition();
681 <        final ReentrantLock lock2 = new ReentrantLock();
681 >        final ReentrantLock lock2 = new ReentrantLock(fair);
682          try {
683              lock2.getWaitQueueLength(c);
684              shouldThrow();
# Line 631 | Line 688 | public class ReentrantLockTest extends J
688      /**
689       * getWaitQueueLength throws IllegalMonitorStateException if not locked
690       */
691 <    public void testGetWaitQueueLengthIMSE() {
692 <        final ReentrantLock lock = new ReentrantLock();
691 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
692 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
693 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
694 >        final ReentrantLock lock = new ReentrantLock(fair);
695          final Condition c = lock.newCondition();
696          try {
697              lock.getWaitQueueLength(c);
# Line 643 | Line 702 | public class ReentrantLockTest extends J
702      /**
703       * getWaitingThreads throws IllegalArgumentException if not owned
704       */
705 <    public void testGetWaitingThreadsIAE() {
706 <        final PublicReentrantLock lock = new PublicReentrantLock();
705 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
706 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
707 >    public void testGetWaitingThreadsIAE(boolean fair) {
708 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
709          final Condition c = lock.newCondition();
710 <        final PublicReentrantLock lock2 = new PublicReentrantLock();
710 >        final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
711          try {
712              lock2.getWaitingThreads(c);
713              shouldThrow();
# Line 656 | Line 717 | public class ReentrantLockTest extends J
717      /**
718       * getWaitingThreads throws IllegalMonitorStateException if not locked
719       */
720 <    public void testGetWaitingThreadsIMSE() {
721 <        final PublicReentrantLock lock = new PublicReentrantLock();
720 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
721 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
722 >    public void testGetWaitingThreadsIMSE(boolean fair) {
723 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
724          final Condition c = lock.newCondition();
725          try {
726              lock.getWaitingThreads(c);
# Line 668 | Line 731 | public class ReentrantLockTest extends J
731      /**
732       * hasWaiters returns true when a thread is waiting, else false
733       */
734 <    public void testHasWaiters() throws InterruptedException {
735 <        final PublicReentrantLock lock = new PublicReentrantLock();
734 >    public void testHasWaiters()      { testHasWaiters(false); }
735 >    public void testHasWaiters_fair() { testHasWaiters(true); }
736 >    public void testHasWaiters(boolean fair) {
737 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
738          final Condition c = lock.newCondition();
739 <        final CountDownLatch locked = new CountDownLatch(1);
739 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
740          Thread t = newStartedThread(new CheckedRunnable() {
741              public void realRun() throws InterruptedException {
742                  lock.lock();
743                  assertHasNoWaiters(lock, c);
744                  assertFalse(lock.hasWaiters(c));
745 <                locked.countDown();
745 >                pleaseSignal.countDown();
746                  c.await();
747                  assertHasNoWaiters(lock, c);
748                  assertFalse(lock.hasWaiters(c));
749                  lock.unlock();
750              }});
751  
752 <        locked.await();
752 >        await(pleaseSignal);
753          lock.lock();
754          assertHasWaiters(lock, c, t);
755          assertTrue(lock.hasWaiters(c));
# Line 699 | Line 764 | public class ReentrantLockTest extends J
764      /**
765       * getWaitQueueLength returns number of waiting threads
766       */
767 <    public void testGetWaitQueueLength() throws InterruptedException {
768 <        final PublicReentrantLock lock = new PublicReentrantLock();
767 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
768 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
769 >    public void testGetWaitQueueLength(boolean fair) {
770 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
771          final Condition c = lock.newCondition();
772          final CountDownLatch locked1 = new CountDownLatch(1);
773          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 729 | Line 796 | public class ReentrantLockTest extends J
796          lock.unlock();
797  
798          t1.start();
799 <        locked1.await();
799 >        await(locked1);
800  
801          lock.lock();
802          assertHasWaiters(lock, c, t1);
# Line 737 | Line 804 | public class ReentrantLockTest extends J
804          lock.unlock();
805  
806          t2.start();
807 <        locked2.await();
807 >        await(locked2);
808  
809          lock.lock();
810          assertHasWaiters(lock, c, t1, t2);
# Line 755 | Line 822 | public class ReentrantLockTest extends J
822      /**
823       * getWaitingThreads returns only and all waiting threads
824       */
825 <    public void testGetWaitingThreads() throws InterruptedException {
826 <        final PublicReentrantLock lock = new PublicReentrantLock();
825 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
826 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
827 >    public void testGetWaitingThreads(boolean fair) {
828 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
829          final Condition c = lock.newCondition();
830          final CountDownLatch locked1 = new CountDownLatch(1);
831          final CountDownLatch locked2 = new CountDownLatch(1);
# Line 783 | Line 852 | public class ReentrantLockTest extends J
852          lock.unlock();
853  
854          t1.start();
855 <        locked1.await();
855 >        await(locked1);
856  
857          lock.lock();
858          assertHasWaiters(lock, c, t1);
# Line 793 | Line 862 | public class ReentrantLockTest extends J
862          lock.unlock();
863  
864          t2.start();
865 <        locked2.await();
865 >        await(locked2);
866  
867          lock.lock();
868          assertHasWaiters(lock, c, t1, t2);
# Line 811 | Line 880 | public class ReentrantLockTest extends J
880      }
881  
882      /**
883 <     * awaitUninterruptibly doesn't abort on interrupt
883 >     * awaitUninterruptibly is uninterruptible
884       */
885 <    public void testAwaitUninterruptibly() throws InterruptedException {
886 <        final ReentrantLock lock = new ReentrantLock();
885 >    public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
886 >    public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
887 >    public void testAwaitUninterruptibly(boolean fair) {
888 >        final ReentrantLock lock = new ReentrantLock(fair);
889          final Condition c = lock.newCondition();
890 <        final CountDownLatch locked = new CountDownLatch(1);
891 <        Thread t = newStartedThread(new CheckedRunnable() {
890 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
891 >
892 >        Thread t1 = newStartedThread(new CheckedRunnable() {
893              public void realRun() {
894 +                // Interrupt before awaitUninterruptibly
895                  lock.lock();
896 <                locked.countDown();
896 >                pleaseInterrupt.countDown();
897 >                Thread.currentThread().interrupt();
898 >                c.awaitUninterruptibly();
899 >                assertTrue(Thread.interrupted());
900 >                lock.unlock();
901 >            }});
902 >
903 >        Thread t2 = newStartedThread(new CheckedRunnable() {
904 >            public void realRun() {
905 >                // Interrupt during awaitUninterruptibly
906 >                lock.lock();
907 >                pleaseInterrupt.countDown();
908                  c.awaitUninterruptibly();
909                  assertTrue(Thread.interrupted());
910                  lock.unlock();
911              }});
912  
913 <        locked.await();
913 >        await(pleaseInterrupt);
914          lock.lock();
915          lock.unlock();
916 <        t.interrupt();
917 <        long timeoutMillis = 10;
918 <        assertThreadJoinTimesOut(t, timeoutMillis);
916 >        t2.interrupt();
917 >
918 >        assertThreadStaysAlive(t1);
919 >        assertTrue(t2.isAlive());
920 >
921          lock.lock();
922 <        c.signal();
922 >        c.signalAll();
923          lock.unlock();
838        awaitTermination(t);
839    }
924  
925 <    /**
926 <     * await is interruptible
843 <     */
844 <    public void testAwait_Interrupt() throws InterruptedException {
845 <        final PublicReentrantLock lock = new PublicReentrantLock();
846 <        final Condition c = lock.newCondition();
847 <        final CountDownLatch locked = new CountDownLatch(1);
848 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
849 <            public void realRun() throws InterruptedException {
850 <                lock.lock();
851 <                assertTrue(lock.isLocked());
852 <                assertTrue(lock.isHeldByCurrentThread());
853 <                assertHasNoWaiters(lock, c);
854 <                locked.countDown();
855 <                try {
856 <                    c.await();
857 <                } finally {
858 <                    assertTrue(lock.isLocked());
859 <                    assertTrue(lock.isHeldByCurrentThread());
860 <                    assertHasNoWaiters(lock, c);
861 <                    lock.unlock();
862 <                    assertFalse(Thread.interrupted());
863 <                }
864 <            }});
865 <
866 <        locked.await();
867 <        assertHasWaiters(lock, c, t);
868 <        t.interrupt();
869 <        awaitTermination(t);
870 <        assertFalse(lock.isLocked());
925 >        awaitTermination(t1);
926 >        awaitTermination(t2);
927      }
928  
929      /**
930 <     * awaitNanos is interruptible
930 >     * await/awaitNanos/awaitUntil is interruptible
931       */
932 <    public void testAwaitNanos_Interrupt() throws InterruptedException {
933 <        final PublicReentrantLock lock = new PublicReentrantLock();
932 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
933 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
934 >    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
935 >    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
936 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
937 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
938 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
939 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
940 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
941 >        final PublicReentrantLock lock =
942 >            new PublicReentrantLock(fair);
943          final Condition c = lock.newCondition();
944 <        final CountDownLatch locked = new CountDownLatch(1);
944 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
945          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
946              public void realRun() throws InterruptedException {
947                  lock.lock();
948 <                assertTrue(lock.isLocked());
884 <                assertTrue(lock.isHeldByCurrentThread());
948 >                assertLockedByMoi(lock);
949                  assertHasNoWaiters(lock, c);
950 <                locked.countDown();
950 >                pleaseInterrupt.countDown();
951                  try {
952 <                    c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
952 >                    await(c, awaitMethod);
953                  } finally {
954 <                    assertTrue(lock.isLocked());
891 <                    assertTrue(lock.isHeldByCurrentThread());
954 >                    assertLockedByMoi(lock);
955                      assertHasNoWaiters(lock, c);
956                      lock.unlock();
957                      assertFalse(Thread.interrupted());
958                  }
959              }});
960  
961 <        locked.await();
961 >        await(pleaseInterrupt);
962          assertHasWaiters(lock, c, t);
963          t.interrupt();
964          awaitTermination(t);
965 <        assertFalse(lock.isLocked());
965 >        assertNotLocked(lock);
966      }
967  
968      /**
969 <     * awaitUntil is interruptible
969 >     * signalAll wakes up all threads
970       */
971 <    public void testAwaitUntil_Interrupt() throws InterruptedException {
972 <        final PublicReentrantLock lock = new PublicReentrantLock();
971 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
972 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
973 >    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
974 >    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
975 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
976 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
977 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
978 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
979 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
980 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
981          final Condition c = lock.newCondition();
982 <        final CountDownLatch locked = new CountDownLatch(1);
983 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
982 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
983 >        class Awaiter extends CheckedRunnable {
984              public void realRun() throws InterruptedException {
985                  lock.lock();
986 <                assertTrue(lock.isLocked());
987 <                assertTrue(lock.isHeldByCurrentThread());
988 <                assertHasNoWaiters(lock, c);
989 <                locked.countDown();
990 <                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 <            }});
986 >                pleaseSignal.countDown();
987 >                await(c, awaitMethod);
988 >                lock.unlock();
989 >            }
990 >        }
991  
992 <        locked.await();
993 <        assertHasWaiters(lock, c, t);
994 <        t.interrupt();
995 <        awaitTermination(t);
996 <        assertFalse(lock.isLocked());
992 >        Thread t1 = newStartedThread(new Awaiter());
993 >        Thread t2 = newStartedThread(new Awaiter());
994 >
995 >        await(pleaseSignal);
996 >        lock.lock();
997 >        assertHasWaiters(lock, c, t1, t2);
998 >        c.signalAll();
999 >        assertHasNoWaiters(lock, c);
1000 >        lock.unlock();
1001 >        awaitTermination(t1);
1002 >        awaitTermination(t2);
1003      }
1004  
1005      /**
1006 <     * signalAll wakes up all threads
1006 >     * signal wakes up waiting threads in FIFO order
1007       */
1008 <    public void testSignalAll() throws InterruptedException {
1009 <        final PublicReentrantLock lock = new PublicReentrantLock();
1008 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1009 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1010 >    public void testSignalWakesFifo(boolean fair) {
1011 >        final PublicReentrantLock lock =
1012 >            new PublicReentrantLock(fair);
1013          final Condition c = lock.newCondition();
1014 <        final CountDownLatch locked = new CountDownLatch(2);
1014 >        final CountDownLatch locked1 = new CountDownLatch(1);
1015 >        final CountDownLatch locked2 = new CountDownLatch(1);
1016          Thread t1 = newStartedThread(new CheckedRunnable() {
1017              public void realRun() throws InterruptedException {
1018                  lock.lock();
1019 <                locked.countDown();
1019 >                locked1.countDown();
1020                  c.await();
1021                  lock.unlock();
1022              }});
1023  
1024 +        await(locked1);
1025 +
1026          Thread t2 = newStartedThread(new CheckedRunnable() {
1027              public void realRun() throws InterruptedException {
1028                  lock.lock();
1029 <                locked.countDown();
1029 >                locked2.countDown();
1030                  c.await();
1031                  lock.unlock();
1032              }});
1033  
1034 <        locked.await();
1034 >        await(locked2);
1035 >
1036          lock.lock();
1037          assertHasWaiters(lock, c, t1, t2);
1038 <        c.signalAll();
1038 >        assertFalse(lock.hasQueuedThreads());
1039 >        c.signal();
1040 >        assertHasWaiters(lock, c, t2);
1041 >        assertTrue(lock.hasQueuedThread(t1));
1042 >        assertFalse(lock.hasQueuedThread(t2));
1043 >        c.signal();
1044          assertHasNoWaiters(lock, c);
1045 +        assertTrue(lock.hasQueuedThread(t1));
1046 +        assertTrue(lock.hasQueuedThread(t2));
1047          lock.unlock();
1048          awaitTermination(t1);
1049          awaitTermination(t2);
# Line 971 | Line 1052 | public class ReentrantLockTest extends J
1052      /**
1053       * await after multiple reentrant locking preserves lock count
1054       */
1055 <    public void testAwaitLockCount() throws InterruptedException {
1056 <        final PublicReentrantLock lock = new PublicReentrantLock();
1055 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1056 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1057 >    public void testAwaitLockCount(boolean fair) {
1058 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
1059          final Condition c = lock.newCondition();
1060 <        final CountDownLatch locked = new CountDownLatch(2);
1060 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1061          Thread t1 = newStartedThread(new CheckedRunnable() {
1062              public void realRun() throws InterruptedException {
1063                  lock.lock();
1064 <                assertLockedBy(lock, Thread.currentThread());
1064 >                assertLockedByMoi(lock);
1065                  assertEquals(1, lock.getHoldCount());
1066 <                locked.countDown();
1066 >                pleaseSignal.countDown();
1067                  c.await();
1068 <                assertLockedBy(lock, Thread.currentThread());
1068 >                assertLockedByMoi(lock);
1069                  assertEquals(1, lock.getHoldCount());
1070                  lock.unlock();
1071              }});
# Line 991 | Line 1074 | public class ReentrantLockTest extends J
1074              public void realRun() throws InterruptedException {
1075                  lock.lock();
1076                  lock.lock();
1077 <                assertLockedBy(lock, Thread.currentThread());
1077 >                assertLockedByMoi(lock);
1078                  assertEquals(2, lock.getHoldCount());
1079 <                locked.countDown();
1079 >                pleaseSignal.countDown();
1080                  c.await();
1081 <                assertLockedBy(lock, Thread.currentThread());
1081 >                assertLockedByMoi(lock);
1082                  assertEquals(2, lock.getHoldCount());
1083                  lock.unlock();
1084                  lock.unlock();
1085              }});
1086  
1087 <        locked.await();
1087 >        await(pleaseSignal);
1088          lock.lock();
1089          assertHasWaiters(lock, c, t1, t2);
1090          assertEquals(1, lock.getHoldCount());
# Line 1015 | Line 1098 | public class ReentrantLockTest extends J
1098      /**
1099       * A serialized lock deserializes as unlocked
1100       */
1101 <    public void testSerialization() throws Exception {
1102 <        ReentrantLock l = new ReentrantLock();
1103 <        l.lock();
1104 <        l.unlock();
1105 <
1106 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1107 <        ObjectOutputStream out =
1108 <            new ObjectOutputStream(new BufferedOutputStream(bout));
1109 <        out.writeObject(l);
1110 <        out.close();
1111 <
1112 <        ByteArrayInputStream bin =
1113 <            new ByteArrayInputStream(bout.toByteArray());
1114 <        ObjectInputStream in =
1115 <            new ObjectInputStream(new BufferedInputStream(bin));
1116 <        ReentrantLock r = (ReentrantLock) in.readObject();
1117 <        r.lock();
1118 <        r.unlock();
1101 >    public void testSerialization()      { testSerialization(false); }
1102 >    public void testSerialization_fair() { testSerialization(true); }
1103 >    public void testSerialization(boolean fair) {
1104 >        ReentrantLock lock = new ReentrantLock(fair);
1105 >        lock.lock();
1106 >
1107 >        ReentrantLock clone = serialClone(lock);
1108 >        assertEquals(lock.isFair(), clone.isFair());
1109 >        assertTrue(lock.isLocked());
1110 >        assertFalse(clone.isLocked());
1111 >        assertEquals(1, lock.getHoldCount());
1112 >        assertEquals(0, clone.getHoldCount());
1113 >        clone.lock();
1114 >        clone.lock();
1115 >        assertTrue(clone.isLocked());
1116 >        assertEquals(2, clone.getHoldCount());
1117 >        assertEquals(1, lock.getHoldCount());
1118 >        clone.unlock();
1119 >        clone.unlock();
1120 >        assertTrue(lock.isLocked());
1121 >        assertFalse(clone.isLocked());
1122      }
1123  
1124      /**
1125       * toString indicates current lock state
1126       */
1127 <    public void testToString() {
1128 <        ReentrantLock lock = new ReentrantLock();
1129 <        String us = lock.toString();
1130 <        assertTrue(us.indexOf("Unlocked") >= 0);
1127 >    public void testToString()      { testToString(false); }
1128 >    public void testToString_fair() { testToString(true); }
1129 >    public void testToString(boolean fair) {
1130 >        ReentrantLock lock = new ReentrantLock(fair);
1131 >        assertTrue(lock.toString().contains("Unlocked"));
1132          lock.lock();
1133 <        String ls = lock.toString();
1134 <        assertTrue(ls.indexOf("Locked") >= 0);
1133 >        assertTrue(lock.toString().contains("Locked"));
1134 >        lock.unlock();
1135 >        assertTrue(lock.toString().contains("Unlocked"));
1136      }
1137   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines