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.55 by jsr166, Wed Dec 31 20:34:16 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.concurrent.locks.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.*;
11 < import java.io.*;
10 >
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.HashSet;
14 > import java.util.concurrent.CountDownLatch;
15 > import java.util.concurrent.CyclicBarrier;
16 > import java.util.concurrent.locks.Condition;
17 > import java.util.concurrent.locks.ReentrantLock;
18 >
19 > import junit.framework.AssertionFailedError;
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ReentrantLockTest extends JSR166TestCase {
24      public static void main(String[] args) {
# Line 65 | Line 72 | public class ReentrantLockTest extends J
72       * Releases write lock, checking that it had a hold count of 1.
73       */
74      void releaseLock(PublicReentrantLock lock) {
75 <        assertLockedBy(lock, Thread.currentThread());
75 >        assertLockedByMoi(lock);
76          lock.unlock();
77          assertFalse(lock.isHeldByCurrentThread());
78          assertNotLocked(lock);
# Line 78 | Line 85 | public class ReentrantLockTest extends J
85          long startTime = System.nanoTime();
86          while (!lock.hasQueuedThread(t)) {
87              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
88 <                throw new AssertionError("timed out");
88 >                throw new AssertionFailedError("timed out");
89              Thread.yield();
90          }
91          assertTrue(t.isAlive());
92 <        assertTrue(lock.getOwner() != t);
92 >        assertNotSame(t, lock.getOwner());
93      }
94  
95      /**
# Line 108 | Line 115 | public class ReentrantLockTest extends J
115      }
116  
117      /**
118 +     * Checks that lock is locked by the current thread.
119 +     */
120 +    void assertLockedByMoi(PublicReentrantLock lock) {
121 +        assertLockedBy(lock, Thread.currentThread());
122 +    }
123 +
124 +    /**
125       * Checks that condition c has no waiters.
126       */
127      void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
# Line 129 | Line 143 | public class ReentrantLockTest extends J
143          lock.unlock();
144      }
145  
146 +    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
147 +
148 +    /**
149 +     * Awaits condition using the specified AwaitMethod.
150 +     */
151 +    void await(Condition c, AwaitMethod awaitMethod)
152 +            throws InterruptedException {
153 +        long timeoutMillis = 2 * LONG_DELAY_MS;
154 +        switch (awaitMethod) {
155 +        case await:
156 +            c.await();
157 +            break;
158 +        case awaitTimed:
159 +            assertTrue(c.await(timeoutMillis, MILLISECONDS));
160 +            break;
161 +        case awaitNanos:
162 +            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
163 +            long nanosRemaining = c.awaitNanos(nanosTimeout);
164 +            assertTrue(nanosRemaining > 0);
165 +            break;
166 +        case awaitUntil:
167 +            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
168 +            break;
169 +        default:
170 +            throw new AssertionError();
171 +        }
172 +    }
173 +
174      /**
175       * Constructor sets given fairness, and is in unlocked state
176       */
# Line 151 | Line 193 | public class ReentrantLockTest extends J
193      /**
194       * locking an unlocked lock succeeds
195       */
196 <    public void testLock() {
197 <        PublicReentrantLock lock = new PublicReentrantLock();
196 >    public void testLock()      { testLock(false); }
197 >    public void testLock_fair() { testLock(true); }
198 >    public void testLock(boolean fair) {
199 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
200          lock.lock();
201 <        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());
201 >        assertLockedByMoi(lock);
202          releaseLock(lock);
203      }
204  
205      /**
206       * Unlocking an unlocked lock throws IllegalMonitorStateException
207       */
208 <    public void testUnlock_IllegalMonitorStateException() {
209 <        ReentrantLock lock = new ReentrantLock();
208 >    public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
209 >    public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
210 >    public void testUnlock_IMSE(boolean fair) {
211 >        ReentrantLock lock = new ReentrantLock(fair);
212          try {
213              lock.unlock();
214              shouldThrow();
# Line 182 | Line 218 | public class ReentrantLockTest extends J
218      /**
219       * tryLock on an unlocked lock succeeds
220       */
221 <    public void testTryLock() {
222 <        PublicReentrantLock lock = new PublicReentrantLock();
221 >    public void testTryLock()      { testTryLock(false); }
222 >    public void testTryLock_fair() { testTryLock(true); }
223 >    public void testTryLock(boolean fair) {
224 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
225          assertTrue(lock.tryLock());
226 <        assertLockedBy(lock, Thread.currentThread());
226 >        assertLockedByMoi(lock);
227 >        assertTrue(lock.tryLock());
228 >        assertLockedByMoi(lock);
229 >        lock.unlock();
230          releaseLock(lock);
231      }
232  
233      /**
234       * hasQueuedThreads reports whether there are waiting threads
235       */
236 <    public void testHasQueuedThreads() throws InterruptedException {
237 <        final PublicReentrantLock lock = new PublicReentrantLock();
236 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
237 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
238 >    public void testHasQueuedThreads(boolean fair) {
239 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
240          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
241          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
242          assertFalse(lock.hasQueuedThreads());
# Line 216 | Line 259 | public class ReentrantLockTest extends J
259      /**
260       * getQueueLength reports number of waiting threads
261       */
262 <    public void testGetQueueLength() throws InterruptedException {
263 <        final PublicReentrantLock lock = new PublicReentrantLock();
264 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
265 <        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);
262 >    public void testGetQueueLength()      { testGetQueueLength(false); }
263 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
264 >    public void testGetQueueLength(boolean fair) {
265 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
266          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
267          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
268          assertEquals(0, lock.getQueueLength());
# Line 262 | Line 284 | public class ReentrantLockTest extends J
284      /**
285       * hasQueuedThread(null) throws NPE
286       */
287 <    public void testHasQueuedThreadNPE() {
288 <        final ReentrantLock lock = new ReentrantLock();
287 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
288 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
289 >    public void testHasQueuedThreadNPE(boolean fair) {
290 >        final ReentrantLock lock = new ReentrantLock(fair);
291          try {
292              lock.hasQueuedThread(null);
293              shouldThrow();
# Line 271 | Line 295 | public class ReentrantLockTest extends J
295      }
296  
297      /**
298 <     * hasQueuedThread reports whether a thread is queued.
298 >     * hasQueuedThread reports whether a thread is queued
299       */
300 <    public void testHasQueuedThread() throws InterruptedException {
301 <        final PublicReentrantLock lock = new PublicReentrantLock();
300 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
301 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
302 >    public void testHasQueuedThread(boolean fair) {
303 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
304          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
305          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
306          assertFalse(lock.hasQueuedThread(t1));
# Line 301 | Line 327 | public class ReentrantLockTest extends J
327      /**
328       * getQueuedThreads includes waiting threads
329       */
330 <    public void testGetQueuedThreads() throws InterruptedException {
331 <        final PublicReentrantLock lock = new PublicReentrantLock();
330 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
331 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
332 >    public void testGetQueuedThreads(boolean fair) {
333 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
334          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
335          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
336          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 328 | Line 356 | public class ReentrantLockTest extends J
356      }
357  
358      /**
359 <     * timed tryLock is interruptible.
359 >     * timed tryLock is interruptible
360       */
361 <    public void testTryLock_Interrupted() throws InterruptedException {
362 <        final PublicReentrantLock lock = new PublicReentrantLock();
361 >    public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
362 >    public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
363 >    public void testTryLock_Interruptible(boolean fair) {
364 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
365          lock.lock();
366          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
367              public void realRun() throws InterruptedException {
# Line 347 | Line 377 | public class ReentrantLockTest extends J
377      /**
378       * tryLock on a locked lock fails
379       */
380 <    public void testTryLockWhenLocked() throws InterruptedException {
381 <        final PublicReentrantLock lock = new PublicReentrantLock();
380 >    public void testTryLockWhenLocked()      { testTryLockWhenLocked(false); }
381 >    public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
382 >    public void testTryLockWhenLocked(boolean fair) {
383 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
384          lock.lock();
385          Thread t = newStartedThread(new CheckedRunnable() {
386              public void realRun() {
# Line 362 | Line 394 | public class ReentrantLockTest extends J
394      /**
395       * Timed tryLock on a locked lock times out
396       */
397 <    public void testTryLock_Timeout() throws InterruptedException {
398 <        final PublicReentrantLock lock = new PublicReentrantLock();
397 >    public void testTryLock_Timeout()      { testTryLock_Timeout(false); }
398 >    public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
399 >    public void testTryLock_Timeout(boolean fair) {
400 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
401          lock.lock();
402          Thread t = newStartedThread(new CheckedRunnable() {
403              public void realRun() throws InterruptedException {
# Line 380 | Line 414 | public class ReentrantLockTest extends J
414      /**
415       * getHoldCount returns number of recursive holds
416       */
417 <    public void testGetHoldCount() {
418 <        ReentrantLock lock = new ReentrantLock();
417 >    public void testGetHoldCount()      { testGetHoldCount(false); }
418 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
419 >    public void testGetHoldCount(boolean fair) {
420 >        ReentrantLock lock = new ReentrantLock(fair);
421          for (int i = 1; i <= SIZE; i++) {
422              lock.lock();
423              assertEquals(i, lock.getHoldCount());
# Line 395 | Line 431 | public class ReentrantLockTest extends J
431      /**
432       * isLocked is true when locked and false when not
433       */
434 <    public void testIsLocked() throws Exception {
435 <        final ReentrantLock lock = new ReentrantLock();
436 <        assertFalse(lock.isLocked());
437 <        lock.lock();
438 <        assertTrue(lock.isLocked());
439 <        lock.lock();
440 <        assertTrue(lock.isLocked());
441 <        lock.unlock();
442 <        assertTrue(lock.isLocked());
443 <        lock.unlock();
444 <        assertFalse(lock.isLocked());
445 <        final CyclicBarrier barrier = new CyclicBarrier(2);
446 <        Thread t = newStartedThread(new CheckedRunnable() {
447 <            public void realRun() throws Exception {
448 <                lock.lock();
449 <                assertTrue(lock.isLocked());
450 <                barrier.await();
451 <                barrier.await();
452 <                lock.unlock();
453 <            }});
454 <
455 <        barrier.await();
456 <        assertTrue(lock.isLocked());
457 <        barrier.await();
458 <        awaitTermination(t);
459 <        assertFalse(lock.isLocked());
460 <    }
461 <
462 <    /**
463 <     * lockInterruptibly is interruptible.
464 <     */
465 <    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);
434 >    public void testIsLocked()      { testIsLocked(false); }
435 >    public void testIsLocked_fair() { testIsLocked(true); }
436 >    public void testIsLocked(boolean fair) {
437 >        try {
438 >            final ReentrantLock lock = new ReentrantLock(fair);
439 >            assertFalse(lock.isLocked());
440 >            lock.lock();
441 >            assertTrue(lock.isLocked());
442 >            lock.lock();
443 >            assertTrue(lock.isLocked());
444 >            lock.unlock();
445 >            assertTrue(lock.isLocked());
446 >            lock.unlock();
447 >            assertFalse(lock.isLocked());
448 >            final CyclicBarrier barrier = new CyclicBarrier(2);
449 >            Thread t = newStartedThread(new CheckedRunnable() {
450 >                    public void realRun() throws Exception {
451 >                        lock.lock();
452 >                        assertTrue(lock.isLocked());
453 >                        barrier.await();
454 >                        barrier.await();
455 >                        lock.unlock();
456 >                    }});
457 >
458 >            barrier.await();
459 >            assertTrue(lock.isLocked());
460 >            barrier.await();
461 >            awaitTermination(t);
462 >            assertFalse(lock.isLocked());
463 >        } catch (Exception e) {
464 >            threadUnexpectedException(e);
465 >        }
466      }
467  
468      /**
469       * lockInterruptibly succeeds when unlocked, else is interruptible
470       */
471 <    public void testLockInterruptibly_Interrupted2() throws InterruptedException {
472 <        final PublicReentrantLock lock = new PublicReentrantLock();
473 <        lock.lockInterruptibly();
471 >    public void testLockInterruptibly()      { testLockInterruptibly(false); }
472 >    public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
473 >    public void testLockInterruptibly(boolean fair) {
474 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
475 >        try {
476 >            lock.lockInterruptibly();
477 >        } catch (InterruptedException ie) {
478 >            threadUnexpectedException(ie);
479 >        }
480 >        assertLockedByMoi(lock);
481          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
482          waitForQueuedThread(lock, t);
483          t.interrupt();
# Line 454 | Line 490 | public class ReentrantLockTest extends J
490      /**
491       * Calling await without holding lock throws IllegalMonitorStateException
492       */
493 <    public void testAwait_IllegalMonitor() throws InterruptedException {
494 <        final ReentrantLock lock = new ReentrantLock();
495 <        final Condition c = lock.newCondition();
496 <        try {
497 <            c.await();
498 <            shouldThrow();
499 <        } catch (IllegalMonitorStateException success) {}
500 <        try {
501 <            c.await(LONG_DELAY_MS, MILLISECONDS);
502 <            shouldThrow();
503 <        } catch (IllegalMonitorStateException success) {}
504 <        try {
505 <            c.awaitNanos(100);
506 <            shouldThrow();
471 <        } catch (IllegalMonitorStateException success) {}
472 <        try {
473 <            c.awaitUninterruptibly();
474 <            shouldThrow();
475 <        } catch (IllegalMonitorStateException success) {}
493 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
494 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
495 >    public void testAwait_IMSE(boolean fair) {
496 >        final ReentrantLock lock = new ReentrantLock(fair);
497 >        final Condition c = lock.newCondition();
498 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
499 >            long startTime = System.nanoTime();
500 >            try {
501 >                await(c, awaitMethod);
502 >                shouldThrow();
503 >            } catch (IllegalMonitorStateException success) {
504 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
505 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
506 >        }
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);
747 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
748          Thread t = newStartedThread(new CheckedRunnable() {
749              public void realRun() throws InterruptedException {
750                  lock.lock();
751                  assertHasNoWaiters(lock, c);
752                  assertFalse(lock.hasWaiters(c));
753 <                locked.countDown();
753 >                pleaseSignal.countDown();
754                  c.await();
755                  assertHasNoWaiters(lock, c);
756                  assertFalse(lock.hasWaiters(c));
757                  lock.unlock();
758              }});
759  
760 <        locked.await();
760 >        await(pleaseSignal);
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 811 | Line 888 | public class ReentrantLockTest extends J
888      }
889  
890      /**
891 <     * awaitUninterruptibly doesn't abort on interrupt
891 >     * awaitUninterruptibly is uninterruptible
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() {
898 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
899 >
900 >        Thread t1 = newStartedThread(new CheckedRunnable() {
901              public void realRun() {
902 +                // Interrupt before awaitUninterruptibly
903                  lock.lock();
904 <                locked.countDown();
904 >                pleaseInterrupt.countDown();
905 >                Thread.currentThread().interrupt();
906 >                c.awaitUninterruptibly();
907 >                assertTrue(Thread.interrupted());
908 >                lock.unlock();
909 >            }});
910 >
911 >        Thread t2 = newStartedThread(new CheckedRunnable() {
912 >            public void realRun() {
913 >                // Interrupt during awaitUninterruptibly
914 >                lock.lock();
915 >                pleaseInterrupt.countDown();
916                  c.awaitUninterruptibly();
917                  assertTrue(Thread.interrupted());
918                  lock.unlock();
919              }});
920  
921 <        locked.await();
921 >        await(pleaseInterrupt);
922          lock.lock();
923          lock.unlock();
924 <        t.interrupt();
925 <        long timeoutMillis = 10;
926 <        assertThreadJoinTimesOut(t, timeoutMillis);
924 >        t2.interrupt();
925 >
926 >        assertThreadStaysAlive(t1);
927 >        assertTrue(t2.isAlive());
928 >
929          lock.lock();
930 <        c.signal();
930 >        c.signalAll();
931          lock.unlock();
838        awaitTermination(t);
839    }
840
841    /**
842     * 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            }});
932  
933 <        locked.await();
934 <        assertHasWaiters(lock, c, t);
868 <        t.interrupt();
869 <        awaitTermination(t);
870 <        assertFalse(lock.isLocked());
933 >        awaitTermination(t1);
934 >        awaitTermination(t2);
935      }
936  
937      /**
938 <     * awaitNanos is interruptible
938 >     * await/awaitNanos/awaitUntil is interruptible
939       */
940 <    public void testAwaitNanos_Interrupt() throws InterruptedException {
941 <        final PublicReentrantLock lock = new PublicReentrantLock();
940 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
941 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
942 >    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
943 >    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
944 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
945 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
946 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
947 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
948 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
949 >        final PublicReentrantLock lock =
950 >            new PublicReentrantLock(fair);
951          final Condition c = lock.newCondition();
952 <        final CountDownLatch locked = new CountDownLatch(1);
952 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
953          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
954              public void realRun() throws InterruptedException {
955                  lock.lock();
956 <                assertTrue(lock.isLocked());
884 <                assertTrue(lock.isHeldByCurrentThread());
956 >                assertLockedByMoi(lock);
957                  assertHasNoWaiters(lock, c);
958 <                locked.countDown();
958 >                pleaseInterrupt.countDown();
959                  try {
960 <                    c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
960 >                    await(c, awaitMethod);
961                  } finally {
962 <                    assertTrue(lock.isLocked());
891 <                    assertTrue(lock.isHeldByCurrentThread());
962 >                    assertLockedByMoi(lock);
963                      assertHasNoWaiters(lock, c);
964                      lock.unlock();
965                      assertFalse(Thread.interrupted());
966                  }
967              }});
968  
969 <        locked.await();
969 >        await(pleaseInterrupt);
970          assertHasWaiters(lock, c, t);
971          t.interrupt();
972          awaitTermination(t);
973 <        assertFalse(lock.isLocked());
973 >        assertNotLocked(lock);
974      }
975  
976      /**
977 <     * awaitUntil is interruptible
977 >     * signalAll wakes up all threads
978       */
979 <    public void testAwaitUntil_Interrupt() throws InterruptedException {
980 <        final PublicReentrantLock lock = new PublicReentrantLock();
979 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
980 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
981 >    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
982 >    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
983 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
984 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
985 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
986 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
987 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
988 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
989          final Condition c = lock.newCondition();
990 <        final CountDownLatch locked = new CountDownLatch(1);
991 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
990 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
991 >        class Awaiter extends CheckedRunnable {
992              public void realRun() throws InterruptedException {
993                  lock.lock();
994 <                assertTrue(lock.isLocked());
995 <                assertTrue(lock.isHeldByCurrentThread());
996 <                assertHasNoWaiters(lock, c);
997 <                locked.countDown();
998 <                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 <            }});
994 >                pleaseSignal.countDown();
995 >                await(c, awaitMethod);
996 >                lock.unlock();
997 >            }
998 >        }
999  
1000 <        locked.await();
1001 <        assertHasWaiters(lock, c, t);
1002 <        t.interrupt();
1003 <        awaitTermination(t);
1004 <        assertFalse(lock.isLocked());
1000 >        Thread t1 = newStartedThread(new Awaiter());
1001 >        Thread t2 = newStartedThread(new Awaiter());
1002 >
1003 >        await(pleaseSignal);
1004 >        lock.lock();
1005 >        assertHasWaiters(lock, c, t1, t2);
1006 >        c.signalAll();
1007 >        assertHasNoWaiters(lock, c);
1008 >        lock.unlock();
1009 >        awaitTermination(t1);
1010 >        awaitTermination(t2);
1011      }
1012  
1013      /**
1014 <     * signalAll wakes up all threads
1014 >     * signal wakes up waiting threads in FIFO order
1015       */
1016 <    public void testSignalAll() throws InterruptedException {
1017 <        final PublicReentrantLock lock = new PublicReentrantLock();
1016 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1017 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1018 >    public void testSignalWakesFifo(boolean fair) {
1019 >        final PublicReentrantLock lock =
1020 >            new PublicReentrantLock(fair);
1021          final Condition c = lock.newCondition();
1022 <        final CountDownLatch locked = new CountDownLatch(2);
1022 >        final CountDownLatch locked1 = new CountDownLatch(1);
1023 >        final CountDownLatch locked2 = new CountDownLatch(1);
1024          Thread t1 = newStartedThread(new CheckedRunnable() {
1025              public void realRun() throws InterruptedException {
1026                  lock.lock();
1027 <                locked.countDown();
1027 >                locked1.countDown();
1028                  c.await();
1029                  lock.unlock();
1030              }});
1031  
1032 +        await(locked1);
1033 +
1034          Thread t2 = newStartedThread(new CheckedRunnable() {
1035              public void realRun() throws InterruptedException {
1036                  lock.lock();
1037 <                locked.countDown();
1037 >                locked2.countDown();
1038                  c.await();
1039                  lock.unlock();
1040              }});
1041  
1042 <        locked.await();
1042 >        await(locked2);
1043 >
1044          lock.lock();
1045          assertHasWaiters(lock, c, t1, t2);
1046 <        c.signalAll();
1046 >        assertFalse(lock.hasQueuedThreads());
1047 >        c.signal();
1048 >        assertHasWaiters(lock, c, t2);
1049 >        assertTrue(lock.hasQueuedThread(t1));
1050 >        assertFalse(lock.hasQueuedThread(t2));
1051 >        c.signal();
1052          assertHasNoWaiters(lock, c);
1053 +        assertTrue(lock.hasQueuedThread(t1));
1054 +        assertTrue(lock.hasQueuedThread(t2));
1055          lock.unlock();
1056          awaitTermination(t1);
1057          awaitTermination(t2);
# Line 971 | Line 1060 | public class ReentrantLockTest extends J
1060      /**
1061       * await after multiple reentrant locking preserves lock count
1062       */
1063 <    public void testAwaitLockCount() throws InterruptedException {
1064 <        final PublicReentrantLock lock = new PublicReentrantLock();
1063 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1064 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1065 >    public void testAwaitLockCount(boolean fair) {
1066 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
1067          final Condition c = lock.newCondition();
1068 <        final CountDownLatch locked = new CountDownLatch(2);
1068 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1069          Thread t1 = newStartedThread(new CheckedRunnable() {
1070              public void realRun() throws InterruptedException {
1071                  lock.lock();
1072 <                assertLockedBy(lock, Thread.currentThread());
1072 >                assertLockedByMoi(lock);
1073                  assertEquals(1, lock.getHoldCount());
1074 <                locked.countDown();
1074 >                pleaseSignal.countDown();
1075                  c.await();
1076 <                assertLockedBy(lock, Thread.currentThread());
1076 >                assertLockedByMoi(lock);
1077                  assertEquals(1, lock.getHoldCount());
1078                  lock.unlock();
1079              }});
# Line 991 | Line 1082 | public class ReentrantLockTest extends J
1082              public void realRun() throws InterruptedException {
1083                  lock.lock();
1084                  lock.lock();
1085 <                assertLockedBy(lock, Thread.currentThread());
1085 >                assertLockedByMoi(lock);
1086                  assertEquals(2, lock.getHoldCount());
1087 <                locked.countDown();
1087 >                pleaseSignal.countDown();
1088                  c.await();
1089 <                assertLockedBy(lock, Thread.currentThread());
1089 >                assertLockedByMoi(lock);
1090                  assertEquals(2, lock.getHoldCount());
1091                  lock.unlock();
1092                  lock.unlock();
1093              }});
1094  
1095 <        locked.await();
1095 >        await(pleaseSignal);
1096          lock.lock();
1097          assertHasWaiters(lock, c, t1, t2);
1098          assertEquals(1, lock.getHoldCount());
# Line 1015 | Line 1106 | public class ReentrantLockTest extends J
1106      /**
1107       * A serialized lock deserializes as unlocked
1108       */
1109 <    public void testSerialization() throws Exception {
1110 <        ReentrantLock l = new ReentrantLock();
1111 <        l.lock();
1112 <        l.unlock();
1113 <
1114 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1115 <        ObjectOutputStream out =
1116 <            new ObjectOutputStream(new BufferedOutputStream(bout));
1117 <        out.writeObject(l);
1118 <        out.close();
1119 <
1120 <        ByteArrayInputStream bin =
1121 <            new ByteArrayInputStream(bout.toByteArray());
1122 <        ObjectInputStream in =
1123 <            new ObjectInputStream(new BufferedInputStream(bin));
1124 <        ReentrantLock r = (ReentrantLock) in.readObject();
1125 <        r.lock();
1126 <        r.unlock();
1109 >    public void testSerialization()      { testSerialization(false); }
1110 >    public void testSerialization_fair() { testSerialization(true); }
1111 >    public void testSerialization(boolean fair) {
1112 >        ReentrantLock lock = new ReentrantLock(fair);
1113 >        lock.lock();
1114 >
1115 >        ReentrantLock clone = serialClone(lock);
1116 >        assertEquals(lock.isFair(), clone.isFair());
1117 >        assertTrue(lock.isLocked());
1118 >        assertFalse(clone.isLocked());
1119 >        assertEquals(1, lock.getHoldCount());
1120 >        assertEquals(0, clone.getHoldCount());
1121 >        clone.lock();
1122 >        clone.lock();
1123 >        assertTrue(clone.isLocked());
1124 >        assertEquals(2, clone.getHoldCount());
1125 >        assertEquals(1, lock.getHoldCount());
1126 >        clone.unlock();
1127 >        clone.unlock();
1128 >        assertTrue(lock.isLocked());
1129 >        assertFalse(clone.isLocked());
1130      }
1131  
1132      /**
1133       * toString indicates current lock state
1134       */
1135 <    public void testToString() {
1136 <        ReentrantLock lock = new ReentrantLock();
1137 <        String us = lock.toString();
1138 <        assertTrue(us.indexOf("Unlocked") >= 0);
1135 >    public void testToString()      { testToString(false); }
1136 >    public void testToString_fair() { testToString(true); }
1137 >    public void testToString(boolean fair) {
1138 >        ReentrantLock lock = new ReentrantLock(fair);
1139 >        assertTrue(lock.toString().contains("Unlocked"));
1140          lock.lock();
1141 <        String ls = lock.toString();
1142 <        assertTrue(ls.indexOf("Locked") >= 0);
1141 >        assertTrue(lock.toString().contains("Locked"));
1142 >        lock.unlock();
1143 >        assertTrue(lock.toString().contains("Unlocked"));
1144      }
1145   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines