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.48 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.54 by jsr166, Wed Dec 31 19:05:43 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 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, awaitTimed, awaitNanos, awaitUntil };
146 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
147  
148      /**
149       * Awaits condition using the specified AwaitMethod.
# Line 286 | Line 293 | public class ReentrantLockTest extends J
293      }
294  
295      /**
296 <     * hasQueuedThread reports whether a thread is queued.
296 >     * hasQueuedThread reports whether a thread is queued
297       */
298      public void testHasQueuedThread()      { testHasQueuedThread(false); }
299      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 347 | Line 354 | public class ReentrantLockTest extends J
354      }
355  
356      /**
357 <     * timed tryLock is interruptible.
357 >     * timed tryLock is interruptible
358       */
359      public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
360      public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
# Line 735 | Line 742 | public class ReentrantLockTest extends J
742      public void testHasWaiters(boolean fair) {
743          final PublicReentrantLock lock = new PublicReentrantLock(fair);
744          final Condition c = lock.newCondition();
745 <        final CountDownLatch locked = new CountDownLatch(1);
745 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
746          Thread t = newStartedThread(new CheckedRunnable() {
747              public void realRun() throws InterruptedException {
748                  lock.lock();
749                  assertHasNoWaiters(lock, c);
750                  assertFalse(lock.hasWaiters(c));
751 <                locked.countDown();
751 >                pleaseSignal.countDown();
752                  c.await();
753                  assertHasNoWaiters(lock, c);
754                  assertFalse(lock.hasWaiters(c));
755                  lock.unlock();
756              }});
757  
758 <        await(locked);
758 >        await(pleaseSignal);
759          lock.lock();
760          assertHasWaiters(lock, c, t);
761          assertTrue(lock.hasWaiters(c));
# Line 879 | Line 886 | public class ReentrantLockTest extends J
886      }
887  
888      /**
889 <     * awaitUninterruptibly doesn't abort on interrupt
889 >     * awaitUninterruptibly is uninterruptible
890       */
891      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
892      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
893      public void testAwaitUninterruptibly(boolean fair) {
894          final ReentrantLock lock = new ReentrantLock(fair);
895          final Condition c = lock.newCondition();
896 <        final CountDownLatch locked = new CountDownLatch(1);
897 <        Thread t = newStartedThread(new CheckedRunnable() {
896 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
897 >
898 >        Thread t1 = newStartedThread(new CheckedRunnable() {
899              public void realRun() {
900 +                // Interrupt before awaitUninterruptibly
901                  lock.lock();
902 <                locked.countDown();
902 >                pleaseInterrupt.countDown();
903 >                Thread.currentThread().interrupt();
904                  c.awaitUninterruptibly();
905                  assertTrue(Thread.interrupted());
906                  lock.unlock();
907              }});
908  
909 <        await(locked);
909 >        Thread t2 = newStartedThread(new CheckedRunnable() {
910 >            public void realRun() {
911 >                // Interrupt during awaitUninterruptibly
912 >                lock.lock();
913 >                pleaseInterrupt.countDown();
914 >                c.awaitUninterruptibly();
915 >                assertTrue(Thread.interrupted());
916 >                lock.unlock();
917 >            }});
918 >
919 >        await(pleaseInterrupt);
920          lock.lock();
921          lock.unlock();
922 <        t.interrupt();
923 <        long timeoutMillis = 10;
924 <        assertThreadStaysAlive(t, timeoutMillis);
922 >        t2.interrupt();
923 >
924 >        assertThreadStaysAlive(t1);
925 >        assertTrue(t2.isAlive());
926 >
927          lock.lock();
928 <        c.signal();
928 >        c.signalAll();
929          lock.unlock();
930 <        awaitTermination(t);
930 >
931 >        awaitTermination(t1);
932 >        awaitTermination(t2);
933      }
934  
935      /**
# Line 923 | Line 947 | public class ReentrantLockTest extends J
947          final PublicReentrantLock lock =
948              new PublicReentrantLock(fair);
949          final Condition c = lock.newCondition();
950 <        final CountDownLatch locked = new CountDownLatch(1);
950 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
951          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
952              public void realRun() throws InterruptedException {
953                  lock.lock();
954                  assertLockedByMoi(lock);
955                  assertHasNoWaiters(lock, c);
956 <                locked.countDown();
956 >                pleaseInterrupt.countDown();
957                  try {
958                      await(c, awaitMethod);
959                  } finally {
# Line 940 | Line 964 | public class ReentrantLockTest extends J
964                  }
965              }});
966  
967 <        await(locked);
967 >        await(pleaseInterrupt);
968          assertHasWaiters(lock, c, t);
969          t.interrupt();
970          awaitTermination(t);
# Line 961 | Line 985 | public class ReentrantLockTest extends J
985      public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
986          final PublicReentrantLock lock = new PublicReentrantLock(fair);
987          final Condition c = lock.newCondition();
988 <        final CountDownLatch locked = new CountDownLatch(2);
988 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
989          class Awaiter extends CheckedRunnable {
990              public void realRun() throws InterruptedException {
991                  lock.lock();
992 <                locked.countDown();
992 >                pleaseSignal.countDown();
993                  await(c, awaitMethod);
994                  lock.unlock();
995              }
# Line 974 | Line 998 | public class ReentrantLockTest extends J
998          Thread t1 = newStartedThread(new Awaiter());
999          Thread t2 = newStartedThread(new Awaiter());
1000  
1001 <        await(locked);
1001 >        await(pleaseSignal);
1002          lock.lock();
1003          assertHasWaiters(lock, c, t1, t2);
1004          c.signalAll();
# Line 985 | Line 1009 | public class ReentrantLockTest extends J
1009      }
1010  
1011      /**
1012 <     * signal wakes up waiting threads in FIFO order.
1012 >     * signal wakes up waiting threads in FIFO order
1013       */
1014      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1015      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1039 | Line 1063 | public class ReentrantLockTest extends J
1063      public void testAwaitLockCount(boolean fair) {
1064          final PublicReentrantLock lock = new PublicReentrantLock(fair);
1065          final Condition c = lock.newCondition();
1066 <        final CountDownLatch locked = new CountDownLatch(2);
1066 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1067          Thread t1 = newStartedThread(new CheckedRunnable() {
1068              public void realRun() throws InterruptedException {
1069                  lock.lock();
1070                  assertLockedByMoi(lock);
1071                  assertEquals(1, lock.getHoldCount());
1072 <                locked.countDown();
1072 >                pleaseSignal.countDown();
1073                  c.await();
1074                  assertLockedByMoi(lock);
1075                  assertEquals(1, lock.getHoldCount());
# Line 1058 | Line 1082 | public class ReentrantLockTest extends J
1082                  lock.lock();
1083                  assertLockedByMoi(lock);
1084                  assertEquals(2, lock.getHoldCount());
1085 <                locked.countDown();
1085 >                pleaseSignal.countDown();
1086                  c.await();
1087                  assertLockedByMoi(lock);
1088                  assertEquals(2, lock.getHoldCount());
# Line 1066 | Line 1090 | public class ReentrantLockTest extends J
1090                  lock.unlock();
1091              }});
1092  
1093 <        await(locked);
1093 >        await(pleaseSignal);
1094          lock.lock();
1095          assertHasWaiters(lock, c, t1, t2);
1096          assertEquals(1, lock.getHoldCount());
# Line 1110 | Line 1134 | public class ReentrantLockTest extends J
1134      public void testToString_fair() { testToString(true); }
1135      public void testToString(boolean fair) {
1136          ReentrantLock lock = new ReentrantLock(fair);
1137 <        String us = lock.toString();
1114 <        assertTrue(us.indexOf("Unlocked") >= 0);
1137 >        assertTrue(lock.toString().contains("Unlocked"));
1138          lock.lock();
1139 <        String ls = lock.toString();
1140 <        assertTrue(ls.indexOf("Locked") >= 0);
1139 >        assertTrue(lock.toString().contains("Locked"));
1140 >        lock.unlock();
1141 >        assertTrue(lock.toString().contains("Unlocked"));
1142      }
1143   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines