ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.60 by jsr166, Mon May 9 20:00:19 2011 UTC vs.
Revision 1.68 by jsr166, Wed Dec 31 05:04:04 2014 UTC

# Line 8 | Line 8
8  
9   import junit.framework.*;
10   import java.util.concurrent.atomic.AtomicBoolean;
11 < import java.util.concurrent.locks.*;
12 < import java.util.concurrent.*;
11 > import java.util.concurrent.locks.Condition;
12 > import java.util.concurrent.locks.Lock;
13 > import java.util.concurrent.locks.ReentrantReadWriteLock;
14 > import java.util.concurrent.CountDownLatch;
15   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 import java.io.*;
16   import java.util.*;
17  
18   public class ReentrantReadWriteLockTest extends JSR166TestCase {
# Line 67 | Line 68 | public class ReentrantReadWriteLockTest
68       */
69      void releaseWriteLock(PublicReentrantReadWriteLock lock) {
70          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
71 <        assertWriteLockedBy(lock, Thread.currentThread());
71 >        assertWriteLockedByMoi(lock);
72          assertEquals(1, lock.getWriteHoldCount());
73          writeLock.unlock();
74          assertNotWriteLocked(lock);
# Line 80 | Line 81 | public class ReentrantReadWriteLockTest
81          long startTime = System.nanoTime();
82          while (!lock.hasQueuedThread(t)) {
83              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
84 <                throw new AssertionError("timed out");
84 >                throw new AssertionFailedError("timed out");
85              Thread.yield();
86          }
87          assertTrue(t.isAlive());
88 <        assertTrue(lock.getOwner() != t);
88 >        assertNotSame(t, lock.getOwner());
89      }
90  
91      /**
# Line 94 | Line 95 | public class ReentrantReadWriteLockTest
95          assertFalse(lock.isWriteLocked());
96          assertFalse(lock.isWriteLockedByCurrentThread());
97          assertFalse(lock.writeLock().isHeldByCurrentThread());
97        assertNull(lock.getOwner());
98          assertEquals(0, lock.getWriteHoldCount());
99 +        assertEquals(0, lock.writeLock().getHoldCount());
100 +        assertNull(lock.getOwner());
101      }
102  
103      /**
# Line 110 | Line 112 | public class ReentrantReadWriteLockTest
112                       lock.writeLock().isHeldByCurrentThread());
113          assertEquals(t == Thread.currentThread(),
114                       lock.getWriteHoldCount() > 0);
115 +        assertEquals(t == Thread.currentThread(),
116 +                     lock.writeLock().getHoldCount() > 0);
117          assertEquals(0, lock.getReadLockCount());
118      }
119  
120      /**
121 +     * Checks that lock is write-locked by the current thread.
122 +     */
123 +    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
124 +        assertWriteLockedBy(lock, Thread.currentThread());
125 +    }
126 +
127 +    /**
128       * Checks that condition c has no waiters.
129       */
130      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 135 | Line 146 | public class ReentrantReadWriteLockTest
146          lock.writeLock().unlock();
147      }
148  
149 <    enum AwaitMethod { await, awaitNanos, awaitUntil };
149 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
150  
151      /**
152 <     * Awaits condition using the specified AwaitMethod
152 >     * Awaits condition using the specified AwaitMethod.
153       */
154      void await(Condition c, AwaitMethod awaitMethod)
155              throws InterruptedException {
# Line 146 | Line 157 | public class ReentrantReadWriteLockTest
157          case await:
158              c.await();
159              break;
160 +        case awaitTimed:
161 +            assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
162 +            break;
163          case awaitNanos:
164              long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
165              assertTrue(nanosRemaining > 0);
# Line 189 | Line 203 | public class ReentrantReadWriteLockTest
203              new PublicReentrantReadWriteLock(fair);
204          assertNotWriteLocked(lock);
205          lock.writeLock().lock();
206 <        assertWriteLockedBy(lock, Thread.currentThread());
206 >        assertWriteLockedByMoi(lock);
207          lock.writeLock().unlock();
208          assertNotWriteLocked(lock);
209          assertEquals(0, lock.getReadLockCount());
# Line 359 | Line 373 | public class ReentrantReadWriteLockTest
373      }
374  
375      /**
376 +     * write-tryLock on an unlocked lock succeeds
377 +     */
378 +    public void testWriteTryLock()      { testWriteTryLock(false); }
379 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
380 +    public void testWriteTryLock(boolean fair) {
381 +        final PublicReentrantReadWriteLock lock =
382 +            new PublicReentrantReadWriteLock(fair);
383 +        assertTrue(lock.writeLock().tryLock());
384 +        assertWriteLockedByMoi(lock);
385 +        assertTrue(lock.writeLock().tryLock());
386 +        assertWriteLockedByMoi(lock);
387 +        lock.writeLock().unlock();
388 +        releaseWriteLock(lock);
389 +    }
390 +
391 +    /**
392       * write-tryLock fails if locked
393       */
394      public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
# Line 478 | Line 508 | public class ReentrantReadWriteLockTest
508  
509      /**
510       * A thread that tries to acquire a fair read lock (non-reentrantly)
511 <     * will block if there is a waiting writer thread.
511 >     * will block if there is a waiting writer thread
512       */
513      public void testReaderWriterReaderFairFifo() {
514          final PublicReentrantReadWriteLock lock =
# Line 555 | Line 585 | public class ReentrantReadWriteLockTest
585      }
586  
587      /**
588 <     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
588 >     * Read trylock succeeds (barging) even in the presence of waiting
589 >     * readers and/or writers
590       */
591      public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
592      public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
# Line 622 | Line 653 | public class ReentrantReadWriteLockTest
653  
654          waitForQueuedThread(lock, t1);
655          waitForQueuedThread(lock, t2);
656 <        assertWriteLockedBy(lock, Thread.currentThread());
656 >        assertWriteLockedByMoi(lock);
657          lock.readLock().lock();
658          lock.readLock().unlock();
659          releaseWriteLock(lock);
# Line 656 | Line 687 | public class ReentrantReadWriteLockTest
687  
688          waitForQueuedThread(lock, t1);
689          waitForQueuedThread(lock, t2);
690 <        assertWriteLockedBy(lock, Thread.currentThread());
690 >        assertWriteLockedByMoi(lock);
691          lock.readLock().lock();
692          lock.readLock().unlock();
693 <        assertWriteLockedBy(lock, Thread.currentThread());
693 >        assertWriteLockedByMoi(lock);
694          lock.writeLock().unlock();
695          awaitTermination(t1);
696          awaitTermination(t2);
# Line 691 | Line 722 | public class ReentrantReadWriteLockTest
722  
723          waitForQueuedThread(lock, t1);
724          waitForQueuedThread(lock, t2);
725 <        assertWriteLockedBy(lock, Thread.currentThread());
725 >        assertWriteLockedByMoi(lock);
726          assertEquals(1, lock.getWriteHoldCount());
727          lock.writeLock().lock();
728 <        assertWriteLockedBy(lock, Thread.currentThread());
728 >        assertWriteLockedByMoi(lock);
729          assertEquals(2, lock.getWriteHoldCount());
730          lock.writeLock().unlock();
731 <        assertWriteLockedBy(lock, Thread.currentThread());
731 >        assertWriteLockedByMoi(lock);
732          assertEquals(1, lock.getWriteHoldCount());
733          lock.writeLock().unlock();
734          awaitTermination(t1);
# Line 745 | Line 776 | public class ReentrantReadWriteLockTest
776      public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
777      public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
778      public void testWriteTryLock_Timeout(boolean fair) {
779 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
779 >        final PublicReentrantReadWriteLock lock =
780 >            new PublicReentrantReadWriteLock(fair);
781          lock.writeLock().lock();
782          Thread t = newStartedThread(new CheckedRunnable() {
783              public void realRun() throws InterruptedException {
# Line 756 | Line 788 | public class ReentrantReadWriteLockTest
788              }});
789  
790          awaitTermination(t);
791 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
760 <        lock.writeLock().unlock();
791 >        releaseWriteLock(lock);
792      }
793  
794      /**
# Line 782 | Line 813 | public class ReentrantReadWriteLockTest
813      }
814  
815      /**
816 <     * write lockInterruptibly succeeds if lock free else is interruptible
816 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
817       */
818      public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
819      public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
# Line 791 | Line 822 | public class ReentrantReadWriteLockTest
822              new PublicReentrantReadWriteLock(fair);
823          try {
824              lock.writeLock().lockInterruptibly();
825 <        } catch (Throwable t) {
826 <            threadUnexpectedException(t);
825 >        } catch (InterruptedException ie) {
826 >            threadUnexpectedException(ie);
827          }
828          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
829              public void realRun() throws InterruptedException {
# Line 801 | Line 832 | public class ReentrantReadWriteLockTest
832  
833          waitForQueuedThread(lock, t);
834          t.interrupt();
835 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
836          awaitTermination(t);
837          releaseWriteLock(lock);
838      }
# Line 817 | Line 849 | public class ReentrantReadWriteLockTest
849              lock.readLock().lockInterruptibly();
850              lock.readLock().unlock();
851              lock.writeLock().lockInterruptibly();
852 <        } catch (Throwable t) {
853 <            threadUnexpectedException(t);
852 >        } catch (InterruptedException ie) {
853 >            threadUnexpectedException(ie);
854          }
855          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
856              public void realRun() throws InterruptedException {
# Line 839 | Line 871 | public class ReentrantReadWriteLockTest
871      public void testAwait_IMSE(boolean fair) {
872          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
873          final Condition c = lock.writeLock().newCondition();
874 <        long startTime = System.nanoTime();
875 <        try {
844 <            try {
845 <                c.await();
846 <                shouldThrow();
847 <            } catch (IllegalMonitorStateException success) {}
848 <            try {
849 <                c.await(LONG_DELAY_MS, MILLISECONDS);
850 <                shouldThrow();
851 <            } catch (IllegalMonitorStateException success) {}
852 <            try {
853 <                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
854 <                shouldThrow();
855 <            } catch (IllegalMonitorStateException success) {}
874 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
875 >            long startTime = System.nanoTime();
876              try {
877 <                c.awaitUninterruptibly();
877 >                await(c, awaitMethod);
878                  shouldThrow();
879 <            } catch (IllegalMonitorStateException success) {}
880 <        } catch (Throwable t) {
881 <            threadUnexpectedException(t);
879 >            } catch (IllegalMonitorStateException success) {
880 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
881 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
882          }
863        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
883      }
884  
885      /**
# Line 986 | Line 1005 | public class ReentrantReadWriteLockTest
1005      }
1006  
1007      /**
1008 <     * awaitUninterruptibly doesn't abort on interrupt
1008 >     * awaitUninterruptibly is uninterruptible
1009       */
1010      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1011      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1012      public void testAwaitUninterruptibly(boolean fair) {
1013          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1014          final Condition c = lock.writeLock().newCondition();
1015 <        final CountDownLatch locked = new CountDownLatch(1);
1016 <        Thread t = newStartedThread(new CheckedRunnable() {
1015 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1016 >
1017 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1018              public void realRun() {
1019 +                // Interrupt before awaitUninterruptibly
1020                  lock.writeLock().lock();
1021 <                locked.countDown();
1021 >                pleaseInterrupt.countDown();
1022 >                Thread.currentThread().interrupt();
1023                  c.awaitUninterruptibly();
1024                  assertTrue(Thread.interrupted());
1025                  lock.writeLock().unlock();
1026              }});
1027  
1028 <        await(locked);
1028 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1029 >            public void realRun() {
1030 >                // Interrupt during awaitUninterruptibly
1031 >                lock.writeLock().lock();
1032 >                pleaseInterrupt.countDown();
1033 >                c.awaitUninterruptibly();
1034 >                assertTrue(Thread.interrupted());
1035 >                lock.writeLock().unlock();
1036 >            }});
1037 >
1038 >        await(pleaseInterrupt);
1039          lock.writeLock().lock();
1040          lock.writeLock().unlock();
1041 <        t.interrupt();
1042 <        long timeoutMillis = 10;
1043 <        assertThreadJoinTimesOut(t, timeoutMillis);
1041 >        t2.interrupt();
1042 >
1043 >        assertThreadStaysAlive(t1);
1044 >        assertTrue(t2.isAlive());
1045 >
1046          lock.writeLock().lock();
1047 <        c.signal();
1047 >        c.signalAll();
1048          lock.writeLock().unlock();
1049 <        awaitTermination(t);
1049 >
1050 >        awaitTermination(t1);
1051 >        awaitTermination(t2);
1052      }
1053  
1054      /**
# Line 1020 | Line 1056 | public class ReentrantReadWriteLockTest
1056       */
1057      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1058      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1059 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1060 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1061      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1062      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1063      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 1032 | Line 1070 | public class ReentrantReadWriteLockTest
1070          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1071              public void realRun() throws InterruptedException {
1072                  lock.writeLock().lock();
1073 <                assertWriteLockedBy(lock, Thread.currentThread());
1073 >                assertWriteLockedByMoi(lock);
1074                  assertHasNoWaiters(lock, c);
1075                  locked.countDown();
1076                  try {
1077                      await(c, awaitMethod);
1078                  } finally {
1079 <                    assertWriteLockedBy(lock, Thread.currentThread());
1079 >                    assertWriteLockedByMoi(lock);
1080                      assertHasNoWaiters(lock, c);
1081                      lock.writeLock().unlock();
1082                      assertFalse(Thread.interrupted());
# Line 1057 | Line 1095 | public class ReentrantReadWriteLockTest
1095       */
1096      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1097      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1098 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1099 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1100      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1101      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1102      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 1090 | Line 1130 | public class ReentrantReadWriteLockTest
1130      }
1131  
1132      /**
1133 <     * signal wakes up waiting threads in FIFO order.
1133 >     * signal wakes up waiting threads in FIFO order
1134       */
1135      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1136      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1150 | Line 1190 | public class ReentrantReadWriteLockTest
1190          Thread t1 = newStartedThread(new CheckedRunnable() {
1191              public void realRun() throws InterruptedException {
1192                  lock.writeLock().lock();
1193 <                assertWriteLockedBy(lock, Thread.currentThread());
1193 >                assertWriteLockedByMoi(lock);
1194                  assertEquals(1, lock.writeLock().getHoldCount());
1195                  locked.countDown();
1196                  c.await();
1197 <                assertWriteLockedBy(lock, Thread.currentThread());
1197 >                assertWriteLockedByMoi(lock);
1198                  assertEquals(1, lock.writeLock().getHoldCount());
1199                  lock.writeLock().unlock();
1200              }});
# Line 1163 | Line 1203 | public class ReentrantReadWriteLockTest
1203              public void realRun() throws InterruptedException {
1204                  lock.writeLock().lock();
1205                  lock.writeLock().lock();
1206 <                assertWriteLockedBy(lock, Thread.currentThread());
1206 >                assertWriteLockedByMoi(lock);
1207                  assertEquals(2, lock.writeLock().getHoldCount());
1208                  locked.countDown();
1209                  c.await();
1210 <                assertWriteLockedBy(lock, Thread.currentThread());
1210 >                assertWriteLockedByMoi(lock);
1211                  assertEquals(2, lock.writeLock().getHoldCount());
1212                  lock.writeLock().unlock();
1213                  lock.writeLock().unlock();
# Line 1205 | Line 1245 | public class ReentrantReadWriteLockTest
1245          assertEquals(1, clone.getReadLockCount());
1246          clone.readLock().unlock();
1247          clone.writeLock().unlock();
1248 +        assertFalse(clone.isWriteLocked());
1249 +        assertEquals(1, lock.getReadLockCount());
1250 +        assertEquals(0, clone.getReadLockCount());
1251      }
1252  
1253      /**
# Line 1248 | Line 1291 | public class ReentrantReadWriteLockTest
1291      }
1292  
1293      /**
1294 <     * hasQueuedThread reports whether a thread is queued.
1294 >     * hasQueuedThread reports whether a thread is queued
1295       */
1296      public void testHasQueuedThread()      { testHasQueuedThread(false); }
1297      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 1589 | Line 1632 | public class ReentrantReadWriteLockTest
1632      public void testToString_fair() { testToString(true); }
1633      public void testToString(boolean fair) {
1634          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1635 <        String us = lock.toString();
1636 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1637 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1638 <        lock.writeLock().lock();
1639 <        String ws = lock.toString();
1597 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1598 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1635 >        assertTrue(lock.toString().contains("Write locks = 0"));
1636 >        assertTrue(lock.toString().contains("Read locks = 0"));
1637 >        lock.writeLock().lock();
1638 >        assertTrue(lock.toString().contains("Write locks = 1"));
1639 >        assertTrue(lock.toString().contains("Read locks = 0"));
1640          lock.writeLock().unlock();
1641          lock.readLock().lock();
1642          lock.readLock().lock();
1643 <        String rs = lock.toString();
1644 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1604 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1643 >        assertTrue(lock.toString().contains("Write locks = 0"));
1644 >        assertTrue(lock.toString().contains("Read locks = 2"));
1645      }
1646  
1647      /**
# Line 1611 | Line 1651 | public class ReentrantReadWriteLockTest
1651      public void testReadLockToString_fair() { testReadLockToString(true); }
1652      public void testReadLockToString(boolean fair) {
1653          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1654 <        String us = lock.readLock().toString();
1615 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1654 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1655          lock.readLock().lock();
1656          lock.readLock().lock();
1657 <        String rs = lock.readLock().toString();
1619 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1657 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1658      }
1659  
1660      /**
# Line 1626 | Line 1664 | public class ReentrantReadWriteLockTest
1664      public void testWriteLockToString_fair() { testWriteLockToString(true); }
1665      public void testWriteLockToString(boolean fair) {
1666          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1667 <        String us = lock.writeLock().toString();
1630 <        assertTrue(us.indexOf("Unlocked") >= 0);
1667 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1668          lock.writeLock().lock();
1669 <        String ls = lock.writeLock().toString();
1670 <        assertTrue(ls.indexOf("Locked") >= 0);
1669 >        assertTrue(lock.writeLock().toString().contains("Locked"));
1670 >        lock.writeLock().unlock();
1671 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1672      }
1673  
1674   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines