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.47 by jsr166, Sun May 15 17:30:21 2011 UTC vs.
Revision 1.60 by jsr166, Fri Jul 3 00:23:43 2015 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) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28          return new TestSuite(ReentrantLockTest.class);
29      }
30  
31      /**
32 <     * A runnable calling lockInterruptibly
32 >     * A checked runnable calling lockInterruptibly
33       */
34      class InterruptibleLockRunnable extends CheckedRunnable {
35          final ReentrantLock lock;
36 <        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
36 >        InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
37          public void realRun() throws InterruptedException {
38              lock.lockInterruptibly();
39          }
40      }
41  
42      /**
43 <     * A runnable calling lockInterruptibly that expects to be
43 >     * A checked runnable calling lockInterruptibly that expects to be
44       * interrupted
45       */
46      class InterruptedLockRunnable extends CheckedInterruptedRunnable {
47          final ReentrantLock lock;
48 <        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
48 >        InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
49          public void realRun() throws InterruptedException {
50              lock.lockInterruptibly();
51          }
# Line 82 | Line 89 | public class ReentrantLockTest extends J
89              Thread.yield();
90          }
91          assertTrue(t.isAlive());
92 <        assertTrue(lock.getOwner() != t);
92 >        assertNotSame(t, lock.getOwner());
93      }
94  
95      /**
# Line 136 | Line 143 | public class ReentrantLockTest extends J
143          lock.unlock();
144      }
145  
146 <    enum AwaitMethod { await, awaitNanos, awaitUntil };
146 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
147  
148      /**
149 <     * Awaits condition using the specified AwaitMethod.
149 >     * Awaits condition "indefinitely" 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 nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
162 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
163 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
164              assertTrue(nanosRemaining > 0);
165 +            assertTrue(nanosRemaining <= timeoutNanos);
166              break;
167          case awaitUntil:
168 <            java.util.Date d = new java.util.Date();
156 <            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
168 >            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
169              break;
170 +        default:
171 +            throw new AssertionError();
172          }
173      }
174  
# Line 282 | Line 296 | public class ReentrantLockTest extends J
296      }
297  
298      /**
299 <     * hasQueuedThread reports whether a thread is queued.
299 >     * hasQueuedThread reports whether a thread is queued
300       */
301      public void testHasQueuedThread()      { testHasQueuedThread(false); }
302      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 343 | Line 357 | public class ReentrantLockTest extends J
357      }
358  
359      /**
360 <     * timed tryLock is interruptible.
360 >     * timed tryLock is interruptible
361       */
362      public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
363      public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
# Line 411 | Line 425 | public class ReentrantLockTest extends J
425          }
426          for (int i = SIZE; i > 0; i--) {
427              lock.unlock();
428 <            assertEquals(i-1, lock.getHoldCount());
428 >            assertEquals(i - 1, lock.getHoldCount());
429          }
430      }
431  
# Line 447 | Line 461 | public class ReentrantLockTest extends J
461              barrier.await();
462              awaitTermination(t);
463              assertFalse(lock.isLocked());
464 <        } catch (Exception e) {
451 <            threadUnexpectedException(e);
452 <        }
464 >        } catch (Exception fail) { threadUnexpectedException(fail); }
465      }
466  
467      /**
# Line 461 | Line 473 | public class ReentrantLockTest extends J
473          final PublicReentrantLock lock = new PublicReentrantLock(fair);
474          try {
475              lock.lockInterruptibly();
476 <        } catch (InterruptedException ie) {
465 <            threadUnexpectedException(ie);
466 <        }
476 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
477          assertLockedByMoi(lock);
478          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
479          waitForQueuedThread(lock, t);
# Line 482 | Line 492 | public class ReentrantLockTest extends J
492      public void testAwait_IMSE(boolean fair) {
493          final ReentrantLock lock = new ReentrantLock(fair);
494          final Condition c = lock.newCondition();
495 <        long startTime = System.nanoTime();
496 <        try {
487 <            try {
488 <                c.await();
489 <                shouldThrow();
490 <            } catch (IllegalMonitorStateException success) {}
491 <            try {
492 <                c.await(LONG_DELAY_MS, MILLISECONDS);
493 <                shouldThrow();
494 <            } catch (IllegalMonitorStateException success) {}
495 <            try {
496 <                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
497 <                shouldThrow();
498 <            } catch (IllegalMonitorStateException success) {}
495 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
496 >            long startTime = System.nanoTime();
497              try {
498 <                c.awaitUninterruptibly();
498 >                await(c, awaitMethod);
499                  shouldThrow();
500 <            } catch (IllegalMonitorStateException success) {}
501 <        } catch (InterruptedException ie) {
502 <            threadUnexpectedException(ie);
500 >            } catch (IllegalMonitorStateException success) {
501 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
502 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
503          }
506        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
504      }
505  
506      /**
# Line 537 | Line 534 | public class ReentrantLockTest extends J
534              assertTrue(nanosRemaining <= 0);
535              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
536              lock.unlock();
537 <        } catch (InterruptedException e) {
541 <            threadUnexpectedException(e);
542 <        }
537 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
538      }
539  
540      /**
# Line 557 | Line 552 | public class ReentrantLockTest extends J
552              assertFalse(c.await(timeoutMillis, MILLISECONDS));
553              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
554              lock.unlock();
555 <        } catch (InterruptedException e) {
561 <            threadUnexpectedException(e);
562 <        }
555 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
556      }
557  
558      /**
# Line 578 | Line 571 | public class ReentrantLockTest extends J
571              assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
572              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
573              lock.unlock();
574 <        } catch (InterruptedException e) {
582 <            threadUnexpectedException(e);
583 <        }
574 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
575      }
576  
577      /**
# Line 744 | Line 735 | public class ReentrantLockTest extends J
735      public void testHasWaiters(boolean fair) {
736          final PublicReentrantLock lock = new PublicReentrantLock(fair);
737          final Condition c = lock.newCondition();
738 <        final CountDownLatch locked = new CountDownLatch(1);
738 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
739          Thread t = newStartedThread(new CheckedRunnable() {
740              public void realRun() throws InterruptedException {
741                  lock.lock();
742                  assertHasNoWaiters(lock, c);
743                  assertFalse(lock.hasWaiters(c));
744 <                locked.countDown();
744 >                pleaseSignal.countDown();
745                  c.await();
746                  assertHasNoWaiters(lock, c);
747                  assertFalse(lock.hasWaiters(c));
748                  lock.unlock();
749              }});
750  
751 <        await(locked);
751 >        await(pleaseSignal);
752          lock.lock();
753          assertHasWaiters(lock, c, t);
754          assertTrue(lock.hasWaiters(c));
# Line 888 | Line 879 | public class ReentrantLockTest extends J
879      }
880  
881      /**
882 <     * awaitUninterruptibly doesn't abort on interrupt
882 >     * awaitUninterruptibly is uninterruptible
883       */
884      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
885      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
886      public void testAwaitUninterruptibly(boolean fair) {
887          final ReentrantLock lock = new ReentrantLock(fair);
888          final Condition c = lock.newCondition();
889 <        final CountDownLatch locked = new CountDownLatch(1);
890 <        Thread t = newStartedThread(new CheckedRunnable() {
889 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
890 >
891 >        Thread t1 = newStartedThread(new CheckedRunnable() {
892              public void realRun() {
893 +                // Interrupt before awaitUninterruptibly
894                  lock.lock();
895 <                locked.countDown();
895 >                pleaseInterrupt.countDown();
896 >                Thread.currentThread().interrupt();
897                  c.awaitUninterruptibly();
898                  assertTrue(Thread.interrupted());
899                  lock.unlock();
900              }});
901  
902 <        await(locked);
902 >        Thread t2 = newStartedThread(new CheckedRunnable() {
903 >            public void realRun() {
904 >                // Interrupt during awaitUninterruptibly
905 >                lock.lock();
906 >                pleaseInterrupt.countDown();
907 >                c.awaitUninterruptibly();
908 >                assertTrue(Thread.interrupted());
909 >                lock.unlock();
910 >            }});
911 >
912 >        await(pleaseInterrupt);
913          lock.lock();
914          lock.unlock();
915 <        t.interrupt();
916 <        long timeoutMillis = 10;
917 <        assertThreadStaysAlive(t, timeoutMillis);
915 >        t2.interrupt();
916 >
917 >        assertThreadStaysAlive(t1);
918 >        assertTrue(t2.isAlive());
919 >
920          lock.lock();
921 <        c.signal();
921 >        c.signalAll();
922          lock.unlock();
923 <        awaitTermination(t);
923 >
924 >        awaitTermination(t1);
925 >        awaitTermination(t2);
926      }
927  
928      /**
# Line 922 | Line 930 | public class ReentrantLockTest extends J
930       */
931      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
932      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
933 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
934 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
935      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
936      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
937      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 930 | Line 940 | public class ReentrantLockTest extends J
940          final PublicReentrantLock lock =
941              new PublicReentrantLock(fair);
942          final Condition c = lock.newCondition();
943 <        final CountDownLatch locked = new CountDownLatch(1);
943 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
944          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
945              public void realRun() throws InterruptedException {
946                  lock.lock();
947                  assertLockedByMoi(lock);
948                  assertHasNoWaiters(lock, c);
949 <                locked.countDown();
949 >                pleaseInterrupt.countDown();
950                  try {
951                      await(c, awaitMethod);
952                  } finally {
# Line 947 | Line 957 | public class ReentrantLockTest extends J
957                  }
958              }});
959  
960 <        await(locked);
960 >        await(pleaseInterrupt);
961          assertHasWaiters(lock, c, t);
962          t.interrupt();
963          awaitTermination(t);
# Line 959 | Line 969 | public class ReentrantLockTest extends J
969       */
970      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
971      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
972 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
973 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
974      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
975      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
976      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 966 | Line 978 | public class ReentrantLockTest extends J
978      public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
979          final PublicReentrantLock lock = new PublicReentrantLock(fair);
980          final Condition c = lock.newCondition();
981 <        final CountDownLatch locked = new CountDownLatch(2);
981 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
982          class Awaiter extends CheckedRunnable {
983              public void realRun() throws InterruptedException {
984                  lock.lock();
985 <                locked.countDown();
985 >                pleaseSignal.countDown();
986                  await(c, awaitMethod);
987                  lock.unlock();
988              }
# Line 979 | Line 991 | public class ReentrantLockTest extends J
991          Thread t1 = newStartedThread(new Awaiter());
992          Thread t2 = newStartedThread(new Awaiter());
993  
994 <        await(locked);
994 >        await(pleaseSignal);
995          lock.lock();
996          assertHasWaiters(lock, c, t1, t2);
997          c.signalAll();
# Line 990 | Line 1002 | public class ReentrantLockTest extends J
1002      }
1003  
1004      /**
1005 <     * signal wakes up waiting threads in FIFO order.
1005 >     * signal wakes up waiting threads in FIFO order
1006       */
1007      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1008      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1044 | Line 1056 | public class ReentrantLockTest extends J
1056      public void testAwaitLockCount(boolean fair) {
1057          final PublicReentrantLock lock = new PublicReentrantLock(fair);
1058          final Condition c = lock.newCondition();
1059 <        final CountDownLatch locked = new CountDownLatch(2);
1059 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1060          Thread t1 = newStartedThread(new CheckedRunnable() {
1061              public void realRun() throws InterruptedException {
1062                  lock.lock();
1063                  assertLockedByMoi(lock);
1064                  assertEquals(1, lock.getHoldCount());
1065 <                locked.countDown();
1065 >                pleaseSignal.countDown();
1066                  c.await();
1067                  assertLockedByMoi(lock);
1068                  assertEquals(1, lock.getHoldCount());
# Line 1063 | Line 1075 | public class ReentrantLockTest extends J
1075                  lock.lock();
1076                  assertLockedByMoi(lock);
1077                  assertEquals(2, lock.getHoldCount());
1078 <                locked.countDown();
1078 >                pleaseSignal.countDown();
1079                  c.await();
1080                  assertLockedByMoi(lock);
1081                  assertEquals(2, lock.getHoldCount());
# Line 1071 | Line 1083 | public class ReentrantLockTest extends J
1083                  lock.unlock();
1084              }});
1085  
1086 <        await(locked);
1086 >        await(pleaseSignal);
1087          lock.lock();
1088          assertHasWaiters(lock, c, t1, t2);
1089          assertEquals(1, lock.getHoldCount());
# Line 1115 | Line 1127 | public class ReentrantLockTest extends J
1127      public void testToString_fair() { testToString(true); }
1128      public void testToString(boolean fair) {
1129          ReentrantLock lock = new ReentrantLock(fair);
1130 <        String us = lock.toString();
1119 <        assertTrue(us.indexOf("Unlocked") >= 0);
1130 >        assertTrue(lock.toString().contains("Unlocked"));
1131          lock.lock();
1132 <        String ls = lock.toString();
1133 <        assertTrue(ls.indexOf("Locked") >= 0);
1132 >        assertTrue(lock.toString().contains("Locked"));
1133 >        lock.unlock();
1134 >        assertTrue(lock.toString().contains("Unlocked"));
1135      }
1136   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines