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.56 by jsr166, Mon Feb 2 04:50:55 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) {
# Line 22 | Line 29 | public class ReentrantLockTest extends J
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.
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 nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
163 >            long nanosRemaining = c.awaitNanos(nanosTimeout);
164              assertTrue(nanosRemaining > 0);
165              break;
166          case awaitUntil:
167 <            java.util.Date d = new java.util.Date();
156 <            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
167 >            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
168              break;
169 +        default:
170 +            throw new AssertionError();
171          }
172      }
173  
# Line 282 | 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()      { testHasQueuedThread(false); }
301      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 343 | 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_Interruptible()      { testTryLock_Interruptible(false); }
362      public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
# Line 482 | Line 495 | public class ReentrantLockTest extends J
495      public void testAwait_IMSE(boolean fair) {
496          final ReentrantLock lock = new ReentrantLock(fair);
497          final Condition c = lock.newCondition();
498 <        long startTime = System.nanoTime();
499 <        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) {}
498 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
499 >            long startTime = System.nanoTime();
500              try {
501 <                c.awaitUninterruptibly();
501 >                await(c, awaitMethod);
502                  shouldThrow();
503 <            } catch (IllegalMonitorStateException success) {}
504 <        } catch (InterruptedException ie) {
505 <            threadUnexpectedException(ie);
503 >            } catch (IllegalMonitorStateException success) {
504 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
505 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
506          }
506        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
507      }
508  
509      /**
# Line 744 | Line 744 | public class ReentrantLockTest extends J
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 <        await(locked);
760 >        await(pleaseSignal);
761          lock.lock();
762          assertHasWaiters(lock, c, t);
763          assertTrue(lock.hasWaiters(c));
# Line 888 | 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()      { 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 <        await(locked);
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 >        await(pleaseInterrupt);
922          lock.lock();
923          lock.unlock();
924 <        t.interrupt();
925 <        long timeoutMillis = 10;
926 <        assertThreadStaysAlive(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();
932 <        awaitTermination(t);
932 >
933 >        awaitTermination(t1);
934 >        awaitTermination(t2);
935      }
936  
937      /**
# Line 922 | Line 939 | public class ReentrantLockTest extends J
939       */
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); }
# Line 930 | Line 949 | public class ReentrantLockTest extends J
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                  assertLockedByMoi(lock);
957                  assertHasNoWaiters(lock, c);
958 <                locked.countDown();
958 >                pleaseInterrupt.countDown();
959                  try {
960                      await(c, awaitMethod);
961                  } finally {
# Line 947 | Line 966 | public class ReentrantLockTest extends J
966                  }
967              }});
968  
969 <        await(locked);
969 >        await(pleaseInterrupt);
970          assertHasWaiters(lock, c, t);
971          t.interrupt();
972          awaitTermination(t);
# Line 959 | Line 978 | public class ReentrantLockTest extends J
978       */
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); }
# Line 966 | Line 987 | public class ReentrantLockTest extends J
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(2);
990 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
991          class Awaiter extends CheckedRunnable {
992              public void realRun() throws InterruptedException {
993                  lock.lock();
994 <                locked.countDown();
994 >                pleaseSignal.countDown();
995                  await(c, awaitMethod);
996                  lock.unlock();
997              }
# Line 979 | Line 1000 | public class ReentrantLockTest extends J
1000          Thread t1 = newStartedThread(new Awaiter());
1001          Thread t2 = newStartedThread(new Awaiter());
1002  
1003 <        await(locked);
1003 >        await(pleaseSignal);
1004          lock.lock();
1005          assertHasWaiters(lock, c, t1, t2);
1006          c.signalAll();
# Line 990 | Line 1011 | public class ReentrantLockTest extends J
1011      }
1012  
1013      /**
1014 <     * signal wakes up waiting threads in FIFO order.
1014 >     * signal wakes up waiting threads in FIFO order
1015       */
1016      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1017      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1044 | Line 1065 | public class ReentrantLockTest extends J
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                  assertLockedByMoi(lock);
1073                  assertEquals(1, lock.getHoldCount());
1074 <                locked.countDown();
1074 >                pleaseSignal.countDown();
1075                  c.await();
1076                  assertLockedByMoi(lock);
1077                  assertEquals(1, lock.getHoldCount());
# Line 1063 | Line 1084 | public class ReentrantLockTest extends J
1084                  lock.lock();
1085                  assertLockedByMoi(lock);
1086                  assertEquals(2, lock.getHoldCount());
1087 <                locked.countDown();
1087 >                pleaseSignal.countDown();
1088                  c.await();
1089                  assertLockedByMoi(lock);
1090                  assertEquals(2, lock.getHoldCount());
# Line 1071 | Line 1092 | public class ReentrantLockTest extends J
1092                  lock.unlock();
1093              }});
1094  
1095 <        await(locked);
1095 >        await(pleaseSignal);
1096          lock.lock();
1097          assertHasWaiters(lock, c, t1, t2);
1098          assertEquals(1, lock.getHoldCount());
# Line 1115 | Line 1136 | public class ReentrantLockTest extends J
1136      public void testToString_fair() { testToString(true); }
1137      public void testToString(boolean fair) {
1138          ReentrantLock lock = new ReentrantLock(fair);
1139 <        String us = lock.toString();
1119 <        assertTrue(us.indexOf("Unlocked") >= 0);
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