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.46 by jsr166, Fri May 13 21:48:59 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 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 136 | Line 137 | public class ReentrantLockTest extends J
137          lock.unlock();
138      }
139  
140 <    enum AwaitMethod { await, awaitNanos, awaitUntil };
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 nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
156 >            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
157 >            long nanosRemaining = c.awaitNanos(nanosTimeout);
158              assertTrue(nanosRemaining > 0);
159              break;
160          case awaitUntil:
161 <            java.util.Date d = new java.util.Date();
156 <            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
161 >            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
162              break;
163          }
164      }
# Line 282 | 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()      { testHasQueuedThread(false); }
293      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 343 | 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_Interruptible()      { testTryLock_Interruptible(false); }
354      public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
# Line 482 | Line 487 | public class ReentrantLockTest extends J
487      public void testAwait_IMSE(boolean fair) {
488          final ReentrantLock lock = new ReentrantLock(fair);
489          final Condition c = lock.newCondition();
490 <        long startTime = System.nanoTime();
491 <        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) {}
490 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
491 >            long startTime = System.nanoTime();
492              try {
493 <                c.awaitUninterruptibly();
493 >                await(c, awaitMethod);
494                  shouldThrow();
495 <            } catch (IllegalMonitorStateException success) {}
496 <        } catch (InterruptedException ie) {
497 <            threadUnexpectedException(ie);
495 >            } catch (IllegalMonitorStateException success) {
496 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
497 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
498          }
506        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
499      }
500  
501      /**
# Line 744 | Line 736 | public class ReentrantLockTest extends J
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 <        await(locked);
752 >        await(pleaseSignal);
753          lock.lock();
754          assertHasWaiters(lock, c, t);
755          assertTrue(lock.hasWaiters(c));
# Line 888 | 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()      { 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 <        await(locked);
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 >        await(pleaseInterrupt);
914          lock.lock();
915          lock.unlock();
916 <        t.interrupt();
917 <        long timeoutMillis = 10;
918 <        assertThreadStaysAlive(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();
924 <        awaitTermination(t);
924 >
925 >        awaitTermination(t1);
926 >        awaitTermination(t2);
927      }
928  
929      /**
# Line 922 | Line 931 | public class ReentrantLockTest extends J
931       */
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); }
# Line 930 | Line 941 | public class ReentrantLockTest extends J
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                  assertLockedByMoi(lock);
949                  assertHasNoWaiters(lock, c);
950 <                locked.countDown();
950 >                pleaseInterrupt.countDown();
951                  try {
952                      await(c, awaitMethod);
953                  } finally {
# Line 947 | Line 958 | public class ReentrantLockTest extends J
958                  }
959              }});
960  
961 <        await(locked);
961 >        await(pleaseInterrupt);
962          assertHasWaiters(lock, c, t);
963          t.interrupt();
964          awaitTermination(t);
# Line 959 | Line 970 | public class ReentrantLockTest extends J
970       */
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); }
# Line 966 | Line 979 | public class ReentrantLockTest extends J
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(2);
982 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
983          class Awaiter extends CheckedRunnable {
984              public void realRun() throws InterruptedException {
985                  lock.lock();
986 <                locked.countDown();
986 >                pleaseSignal.countDown();
987                  await(c, awaitMethod);
988                  lock.unlock();
989              }
# Line 979 | Line 992 | public class ReentrantLockTest extends J
992          Thread t1 = newStartedThread(new Awaiter());
993          Thread t2 = newStartedThread(new Awaiter());
994  
995 <        await(locked);
995 >        await(pleaseSignal);
996          lock.lock();
997          assertHasWaiters(lock, c, t1, t2);
998          c.signalAll();
# Line 990 | Line 1003 | public class ReentrantLockTest extends J
1003      }
1004  
1005      /**
1006 <     * signal wakes up waiting threads in FIFO order.
1006 >     * signal wakes up waiting threads in FIFO order
1007       */
1008      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1009      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1044 | Line 1057 | public class ReentrantLockTest extends J
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                  assertLockedByMoi(lock);
1065                  assertEquals(1, lock.getHoldCount());
1066 <                locked.countDown();
1066 >                pleaseSignal.countDown();
1067                  c.await();
1068                  assertLockedByMoi(lock);
1069                  assertEquals(1, lock.getHoldCount());
# Line 1063 | Line 1076 | public class ReentrantLockTest extends J
1076                  lock.lock();
1077                  assertLockedByMoi(lock);
1078                  assertEquals(2, lock.getHoldCount());
1079 <                locked.countDown();
1079 >                pleaseSignal.countDown();
1080                  c.await();
1081                  assertLockedByMoi(lock);
1082                  assertEquals(2, lock.getHoldCount());
# Line 1071 | Line 1084 | public class ReentrantLockTest extends J
1084                  lock.unlock();
1085              }});
1086  
1087 <        await(locked);
1087 >        await(pleaseSignal);
1088          lock.lock();
1089          assertHasWaiters(lock, c, t1, t2);
1090          assertEquals(1, lock.getHoldCount());
# Line 1115 | Line 1128 | public class ReentrantLockTest extends J
1128      public void testToString_fair() { testToString(true); }
1129      public void testToString(boolean fair) {
1130          ReentrantLock lock = new ReentrantLock(fair);
1131 <        String us = lock.toString();
1119 <        assertTrue(us.indexOf("Unlocked") >= 0);
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