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.61 by jsr166, Fri Jul 3 00:25:35 2015 UTC vs.
Revision 1.67 by jsr166, Fri Sep 29 22:31:55 2017 UTC

# Line 8 | Line 8
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 + import java.util.ArrayList;
12   import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.HashSet;
15   import java.util.concurrent.CountDownLatch;
16   import java.util.concurrent.CyclicBarrier;
17 + import java.util.concurrent.ThreadLocalRandom;
18   import java.util.concurrent.locks.Condition;
19   import java.util.concurrent.locks.ReentrantLock;
20  
# Line 20 | Line 22 | import junit.framework.AssertionFailedEr
22   import junit.framework.Test;
23   import junit.framework.TestSuite;
24  
25 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
26   public class ReentrantLockTest extends JSR166TestCase {
27      public static void main(String[] args) {
28          main(suite(), args);
# Line 145 | Line 148 | public class ReentrantLockTest extends J
148  
149      enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
150  
151 +    static AwaitMethod randomAwaitMethod() {
152 +        AwaitMethod[] awaitMethods = AwaitMethod.values();
153 +        return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)];
154 +    }
155 +
156      /**
157       * Awaits condition "indefinitely" using the specified AwaitMethod.
158       */
# Line 209 | Line 217 | public class ReentrantLockTest extends J
217      public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
218      public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
219      public void testUnlock_IMSE(boolean fair) {
220 <        ReentrantLock lock = new ReentrantLock(fair);
220 >        final ReentrantLock lock = new ReentrantLock(fair);
221          try {
222              lock.unlock();
223              shouldThrow();
# Line 399 | Line 407 | public class ReentrantLockTest extends J
407      public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
408      public void testTryLock_Timeout(boolean fair) {
409          final PublicReentrantLock lock = new PublicReentrantLock(fair);
410 +        final long timeoutMillis = timeoutMillis();
411          lock.lock();
412          Thread t = newStartedThread(new CheckedRunnable() {
413              public void realRun() throws InterruptedException {
414                  long startTime = System.nanoTime();
406                long timeoutMillis = 10;
415                  assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
416                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
417              }});
# Line 418 | Line 426 | public class ReentrantLockTest extends J
426      public void testGetHoldCount()      { testGetHoldCount(false); }
427      public void testGetHoldCount_fair() { testGetHoldCount(true); }
428      public void testGetHoldCount(boolean fair) {
429 <        ReentrantLock lock = new ReentrantLock(fair);
429 >        final ReentrantLock lock = new ReentrantLock(fair);
430          for (int i = 1; i <= SIZE; i++) {
431              lock.lock();
432              assertEquals(i, lock.getHoldCount());
# Line 435 | Line 443 | public class ReentrantLockTest extends J
443      public void testIsLocked()      { testIsLocked(false); }
444      public void testIsLocked_fair() { testIsLocked(true); }
445      public void testIsLocked(boolean fair) {
446 +        final ReentrantLock lock = new ReentrantLock(fair);
447          try {
439            final ReentrantLock lock = new ReentrantLock(fair);
448              assertFalse(lock.isLocked());
449              lock.lock();
450              assertTrue(lock.isLocked());
# Line 523 | Line 531 | public class ReentrantLockTest extends J
531      public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
532      public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
533      public void testAwaitNanos_Timeout(boolean fair) {
534 +        final ReentrantLock lock = new ReentrantLock(fair);
535 +        final Condition c = lock.newCondition();
536 +        final long timeoutMillis = timeoutMillis();
537 +        final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
538 +        lock.lock();
539 +        final long startTime = System.nanoTime();
540          try {
527            final ReentrantLock lock = new ReentrantLock(fair);
528            final Condition c = lock.newCondition();
529            lock.lock();
530            long startTime = System.nanoTime();
531            long timeoutMillis = 10;
532            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
541              long nanosRemaining = c.awaitNanos(timeoutNanos);
542              assertTrue(nanosRemaining <= 0);
535            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
536            lock.unlock();
543          } catch (InterruptedException fail) { threadUnexpectedException(fail); }
544 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
545 +        lock.unlock();
546      }
547  
548      /**
# Line 543 | Line 551 | public class ReentrantLockTest extends J
551      public void testAwait_Timeout()      { testAwait_Timeout(false); }
552      public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
553      public void testAwait_Timeout(boolean fair) {
554 +        final ReentrantLock lock = new ReentrantLock(fair);
555 +        final Condition c = lock.newCondition();
556 +        final long timeoutMillis = timeoutMillis();
557 +        lock.lock();
558 +        final long startTime = System.nanoTime();
559          try {
547            final ReentrantLock lock = new ReentrantLock(fair);
548            final Condition c = lock.newCondition();
549            lock.lock();
550            long startTime = System.nanoTime();
551            long timeoutMillis = 10;
560              assertFalse(c.await(timeoutMillis, MILLISECONDS));
553            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
554            lock.unlock();
561          } catch (InterruptedException fail) { threadUnexpectedException(fail); }
562 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
563 +        lock.unlock();
564      }
565  
566      /**
# Line 561 | Line 569 | public class ReentrantLockTest extends J
569      public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
570      public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
571      public void testAwaitUntil_Timeout(boolean fair) {
572 +        final ReentrantLock lock = new ReentrantLock(fair);
573 +        final Condition c = lock.newCondition();
574 +        lock.lock();
575 +        // We shouldn't assume that nanoTime and currentTimeMillis
576 +        // use the same time source, so don't use nanoTime here.
577 +        final java.util.Date delayedDate = delayedDate(timeoutMillis());
578          try {
579 <            final ReentrantLock lock = new ReentrantLock(fair);
566 <            final Condition c = lock.newCondition();
567 <            lock.lock();
568 <            long startTime = System.nanoTime();
569 <            long timeoutMillis = 10;
570 <            java.util.Date d = new java.util.Date();
571 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
572 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
573 <            lock.unlock();
579 >            assertFalse(c.awaitUntil(delayedDate));
580          } catch (InterruptedException fail) { threadUnexpectedException(fail); }
581 +        assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
582 +        lock.unlock();
583      }
584  
585      /**
# Line 885 | Line 893 | public class ReentrantLockTest extends J
893      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
894      public void testAwaitUninterruptibly(boolean fair) {
895          final ReentrantLock lock = new ReentrantLock(fair);
896 <        final Condition c = lock.newCondition();
896 >        final Condition condition = lock.newCondition();
897          final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
898  
899          Thread t1 = newStartedThread(new CheckedRunnable() {
# Line 894 | Line 902 | public class ReentrantLockTest extends J
902                  lock.lock();
903                  pleaseInterrupt.countDown();
904                  Thread.currentThread().interrupt();
905 <                c.awaitUninterruptibly();
905 >                condition.awaitUninterruptibly();
906                  assertTrue(Thread.interrupted());
907                  lock.unlock();
908              }});
# Line 904 | Line 912 | public class ReentrantLockTest extends J
912                  // Interrupt during awaitUninterruptibly
913                  lock.lock();
914                  pleaseInterrupt.countDown();
915 <                c.awaitUninterruptibly();
915 >                condition.awaitUninterruptibly();
916                  assertTrue(Thread.interrupted());
917                  lock.unlock();
918              }});
919  
920          await(pleaseInterrupt);
921 +        t2.interrupt();
922          lock.lock();
923          lock.unlock();
924 <        t2.interrupt();
925 <
917 <        assertThreadStaysAlive(t1);
918 <        assertTrue(t2.isAlive());
924 >        assertThreadBlocks(t1, Thread.State.WAITING);
925 >        assertThreadBlocks(t2, Thread.State.WAITING);
926  
927          lock.lock();
928 <        c.signalAll();
928 >        condition.signalAll();
929          lock.unlock();
930  
931          awaitTermination(t1);
# Line 1100 | Line 1107 | public class ReentrantLockTest extends J
1107      public void testSerialization()      { testSerialization(false); }
1108      public void testSerialization_fair() { testSerialization(true); }
1109      public void testSerialization(boolean fair) {
1110 <        ReentrantLock lock = new ReentrantLock(fair);
1110 >        final ReentrantLock lock = new ReentrantLock(fair);
1111          lock.lock();
1112  
1113          ReentrantLock clone = serialClone(lock);
# Line 1126 | Line 1133 | public class ReentrantLockTest extends J
1133      public void testToString()      { testToString(false); }
1134      public void testToString_fair() { testToString(true); }
1135      public void testToString(boolean fair) {
1136 <        ReentrantLock lock = new ReentrantLock(fair);
1136 >        final ReentrantLock lock = new ReentrantLock(fair);
1137          assertTrue(lock.toString().contains("Unlocked"));
1138          lock.lock();
1139 <        assertTrue(lock.toString().contains("Locked"));
1139 >        assertTrue(lock.toString().contains("Locked by"));
1140          lock.unlock();
1141          assertTrue(lock.toString().contains("Unlocked"));
1142      }
1143 +
1144 +    /**
1145 +     * Tests scenario for JDK-8187408
1146 +     * AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
1147 +     */
1148 +    public void testBug8187408() throws InterruptedException {
1149 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
1150 +        final AwaitMethod awaitMethod = randomAwaitMethod();
1151 +        final int nThreads = rnd.nextInt(2, 10);
1152 +        final ReentrantLock lock = new ReentrantLock();
1153 +        final Condition cond = lock.newCondition();
1154 +        final CountDownLatch done = new CountDownLatch(nThreads);
1155 +        final ArrayList<Thread> threads = new ArrayList<>();
1156 +
1157 +        Runnable rogue = () -> {
1158 +            while (done.getCount() > 0) {
1159 +                try {
1160 +                    // call await without holding lock?!
1161 +                    await(cond, awaitMethod);
1162 +                    throw new AssertionError("should throw");
1163 +                }
1164 +                catch (IllegalMonitorStateException expected) {}
1165 +                catch (Throwable fail) { threadUnexpectedException(fail); }}};
1166 +        Thread rogueThread = new Thread(rogue, "rogue");
1167 +        threads.add(rogueThread);
1168 +        rogueThread.start();
1169 +
1170 +        Runnable waiter = () -> {
1171 +            lock.lock();
1172 +            try {
1173 +                done.countDown();
1174 +                cond.await();
1175 +            } catch (Throwable fail) {
1176 +                threadUnexpectedException(fail);
1177 +            } finally {
1178 +                lock.unlock();
1179 +            }};
1180 +        for (int i = 0; i < nThreads; i++) {
1181 +            Thread thread = new Thread(waiter, "waiter");
1182 +            threads.add(thread);
1183 +            thread.start();
1184 +        }
1185 +
1186 +        assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1187 +        lock.lock();
1188 +        try {
1189 +            assertEquals(nThreads, lock.getWaitQueueLength(cond));
1190 +        } finally {
1191 +            cond.signalAll();
1192 +            lock.unlock();
1193 +        }
1194 +        for (Thread thread : threads) {
1195 +            thread.join(LONG_DELAY_MS);
1196 +            assertFalse(thread.isAlive());
1197 +        }
1198 +    }
1199   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines