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.50 by jsr166, Tue May 31 16:16:24 2011 UTC vs.
Revision 1.62 by jsr166, Fri Jul 3 01:25:15 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.concurrent.locks.Condition;
11 < import java.util.concurrent.locks.ReentrantLock;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
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 static java.util.concurrent.TimeUnit.MILLISECONDS;
17 < import java.util.*;
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 83 | 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 137 | 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.
149 >     * Awaits condition "indefinitely" using the specified AwaitMethod.
150       */
151      void await(Condition c, AwaitMethod awaitMethod)
152              throws InterruptedException {
# Line 153 | Line 159 | public class ReentrantLockTest extends J
159              assertTrue(c.await(timeoutMillis, MILLISECONDS));
160              break;
161          case awaitNanos:
162 <            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
163 <            long nanosRemaining = c.awaitNanos(nanosTimeout);
164 <            assertTrue(nanosRemaining > 0);
162 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
163 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
164 >            assertTrue(nanosRemaining > timeoutNanos / 2);
165 >            assertTrue(nanosRemaining <= timeoutNanos);
166              break;
167          case awaitUntil:
168              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
169              break;
170 +        default:
171 +            throw new AssertionError();
172          }
173      }
174  
# Line 416 | 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 452 | Line 461 | public class ReentrantLockTest extends J
461              barrier.await();
462              awaitTermination(t);
463              assertFalse(lock.isLocked());
464 <        } catch (Exception e) {
456 <            threadUnexpectedException(e);
457 <        }
464 >        } catch (Exception fail) { threadUnexpectedException(fail); }
465      }
466  
467      /**
# Line 466 | Line 473 | public class ReentrantLockTest extends J
473          final PublicReentrantLock lock = new PublicReentrantLock(fair);
474          try {
475              lock.lockInterruptibly();
476 <        } catch (InterruptedException ie) {
470 <            threadUnexpectedException(ie);
471 <        }
476 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
477          assertLockedByMoi(lock);
478          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
479          waitForQueuedThread(lock, t);
# Line 529 | Line 534 | public class ReentrantLockTest extends J
534              assertTrue(nanosRemaining <= 0);
535              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
536              lock.unlock();
537 <        } catch (InterruptedException e) {
533 <            threadUnexpectedException(e);
534 <        }
537 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
538      }
539  
540      /**
# Line 549 | 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) {
553 <            threadUnexpectedException(e);
554 <        }
555 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
556      }
557  
558      /**
# Line 565 | Line 566 | public class ReentrantLockTest extends J
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();
570 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
571 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
569 >            assertFalse(c.awaitUntil(delayedDate(timeoutMillis())));
570 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
571              lock.unlock();
572 <        } catch (InterruptedException e) {
574 <            threadUnexpectedException(e);
575 <        }
572 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
573      }
574  
575      /**
# Line 736 | Line 733 | public class ReentrantLockTest extends J
733      public void testHasWaiters(boolean fair) {
734          final PublicReentrantLock lock = new PublicReentrantLock(fair);
735          final Condition c = lock.newCondition();
736 <        final CountDownLatch locked = new CountDownLatch(1);
736 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
737          Thread t = newStartedThread(new CheckedRunnable() {
738              public void realRun() throws InterruptedException {
739                  lock.lock();
740                  assertHasNoWaiters(lock, c);
741                  assertFalse(lock.hasWaiters(c));
742 <                locked.countDown();
742 >                pleaseSignal.countDown();
743                  c.await();
744                  assertHasNoWaiters(lock, c);
745                  assertFalse(lock.hasWaiters(c));
746                  lock.unlock();
747              }});
748  
749 <        await(locked);
749 >        await(pleaseSignal);
750          lock.lock();
751          assertHasWaiters(lock, c, t);
752          assertTrue(lock.hasWaiters(c));
# Line 880 | Line 877 | public class ReentrantLockTest extends J
877      }
878  
879      /**
880 <     * awaitUninterruptibly doesn't abort on interrupt
880 >     * awaitUninterruptibly is uninterruptible
881       */
882      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
883      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
884      public void testAwaitUninterruptibly(boolean fair) {
885          final ReentrantLock lock = new ReentrantLock(fair);
886          final Condition c = lock.newCondition();
887 <        final CountDownLatch locked = new CountDownLatch(1);
888 <        Thread t = newStartedThread(new CheckedRunnable() {
887 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
888 >
889 >        Thread t1 = newStartedThread(new CheckedRunnable() {
890              public void realRun() {
891 +                // Interrupt before awaitUninterruptibly
892                  lock.lock();
893 <                locked.countDown();
893 >                pleaseInterrupt.countDown();
894 >                Thread.currentThread().interrupt();
895                  c.awaitUninterruptibly();
896                  assertTrue(Thread.interrupted());
897                  lock.unlock();
898              }});
899  
900 <        await(locked);
900 >        Thread t2 = newStartedThread(new CheckedRunnable() {
901 >            public void realRun() {
902 >                // Interrupt during awaitUninterruptibly
903 >                lock.lock();
904 >                pleaseInterrupt.countDown();
905 >                c.awaitUninterruptibly();
906 >                assertTrue(Thread.interrupted());
907 >                lock.unlock();
908 >            }});
909 >
910 >        await(pleaseInterrupt);
911          lock.lock();
912          lock.unlock();
913 <        t.interrupt();
914 <        long timeoutMillis = 10;
915 <        assertThreadStaysAlive(t, timeoutMillis);
913 >        t2.interrupt();
914 >
915 >        assertThreadStaysAlive(t1);
916 >        assertTrue(t2.isAlive());
917 >
918          lock.lock();
919 <        c.signal();
919 >        c.signalAll();
920          lock.unlock();
921 <        awaitTermination(t);
921 >
922 >        awaitTermination(t1);
923 >        awaitTermination(t2);
924      }
925  
926      /**
# Line 924 | Line 938 | public class ReentrantLockTest extends J
938          final PublicReentrantLock lock =
939              new PublicReentrantLock(fair);
940          final Condition c = lock.newCondition();
941 <        final CountDownLatch locked = new CountDownLatch(1);
941 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
942          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
943              public void realRun() throws InterruptedException {
944                  lock.lock();
945                  assertLockedByMoi(lock);
946                  assertHasNoWaiters(lock, c);
947 <                locked.countDown();
947 >                pleaseInterrupt.countDown();
948                  try {
949                      await(c, awaitMethod);
950                  } finally {
# Line 941 | Line 955 | public class ReentrantLockTest extends J
955                  }
956              }});
957  
958 <        await(locked);
958 >        await(pleaseInterrupt);
959          assertHasWaiters(lock, c, t);
960          t.interrupt();
961          awaitTermination(t);
# Line 962 | Line 976 | public class ReentrantLockTest extends J
976      public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
977          final PublicReentrantLock lock = new PublicReentrantLock(fair);
978          final Condition c = lock.newCondition();
979 <        final CountDownLatch locked = new CountDownLatch(2);
979 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
980          class Awaiter extends CheckedRunnable {
981              public void realRun() throws InterruptedException {
982                  lock.lock();
983 <                locked.countDown();
983 >                pleaseSignal.countDown();
984                  await(c, awaitMethod);
985                  lock.unlock();
986              }
# Line 975 | Line 989 | public class ReentrantLockTest extends J
989          Thread t1 = newStartedThread(new Awaiter());
990          Thread t2 = newStartedThread(new Awaiter());
991  
992 <        await(locked);
992 >        await(pleaseSignal);
993          lock.lock();
994          assertHasWaiters(lock, c, t1, t2);
995          c.signalAll();
# Line 1040 | Line 1054 | public class ReentrantLockTest extends J
1054      public void testAwaitLockCount(boolean fair) {
1055          final PublicReentrantLock lock = new PublicReentrantLock(fair);
1056          final Condition c = lock.newCondition();
1057 <        final CountDownLatch locked = new CountDownLatch(2);
1057 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1058          Thread t1 = newStartedThread(new CheckedRunnable() {
1059              public void realRun() throws InterruptedException {
1060                  lock.lock();
1061                  assertLockedByMoi(lock);
1062                  assertEquals(1, lock.getHoldCount());
1063 <                locked.countDown();
1063 >                pleaseSignal.countDown();
1064                  c.await();
1065                  assertLockedByMoi(lock);
1066                  assertEquals(1, lock.getHoldCount());
# Line 1059 | Line 1073 | public class ReentrantLockTest extends J
1073                  lock.lock();
1074                  assertLockedByMoi(lock);
1075                  assertEquals(2, lock.getHoldCount());
1076 <                locked.countDown();
1076 >                pleaseSignal.countDown();
1077                  c.await();
1078                  assertLockedByMoi(lock);
1079                  assertEquals(2, lock.getHoldCount());
# Line 1067 | Line 1081 | public class ReentrantLockTest extends J
1081                  lock.unlock();
1082              }});
1083  
1084 <        await(locked);
1084 >        await(pleaseSignal);
1085          lock.lock();
1086          assertHasWaiters(lock, c, t1, t2);
1087          assertEquals(1, lock.getHoldCount());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines