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.70 by jsr166, Wed Dec 31 20:34:16 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.concurrent.atomic.AtomicBoolean;
11 import java.util.concurrent.locks.*;
12 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.io.*;
11 < import java.util.*;
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.atomic.AtomicBoolean;
16 > import java.util.concurrent.locks.Condition;
17 > import java.util.concurrent.locks.Lock;
18 > import java.util.concurrent.locks.ReentrantReadWriteLock;
19 >
20 > import junit.framework.AssertionFailedError;
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
23  
24   public class ReentrantReadWriteLockTest extends JSR166TestCase {
25      public static void main(String[] args) {
# Line 67 | Line 74 | public class ReentrantReadWriteLockTest
74       */
75      void releaseWriteLock(PublicReentrantReadWriteLock lock) {
76          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
77 <        assertWriteLockedBy(lock, Thread.currentThread());
77 >        assertWriteLockedByMoi(lock);
78          assertEquals(1, lock.getWriteHoldCount());
79          writeLock.unlock();
80          assertNotWriteLocked(lock);
# Line 80 | Line 87 | public class ReentrantReadWriteLockTest
87          long startTime = System.nanoTime();
88          while (!lock.hasQueuedThread(t)) {
89              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
90 <                throw new AssertionError("timed out");
90 >                throw new AssertionFailedError("timed out");
91              Thread.yield();
92          }
93          assertTrue(t.isAlive());
94 <        assertTrue(lock.getOwner() != t);
94 >        assertNotSame(t, lock.getOwner());
95      }
96  
97      /**
# Line 94 | Line 101 | public class ReentrantReadWriteLockTest
101          assertFalse(lock.isWriteLocked());
102          assertFalse(lock.isWriteLockedByCurrentThread());
103          assertFalse(lock.writeLock().isHeldByCurrentThread());
97        assertNull(lock.getOwner());
104          assertEquals(0, lock.getWriteHoldCount());
105 +        assertEquals(0, lock.writeLock().getHoldCount());
106 +        assertNull(lock.getOwner());
107      }
108  
109      /**
# Line 110 | Line 118 | public class ReentrantReadWriteLockTest
118                       lock.writeLock().isHeldByCurrentThread());
119          assertEquals(t == Thread.currentThread(),
120                       lock.getWriteHoldCount() > 0);
121 +        assertEquals(t == Thread.currentThread(),
122 +                     lock.writeLock().getHoldCount() > 0);
123          assertEquals(0, lock.getReadLockCount());
124      }
125  
126      /**
127 +     * Checks that lock is write-locked by the current thread.
128 +     */
129 +    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
130 +        assertWriteLockedBy(lock, Thread.currentThread());
131 +    }
132 +
133 +    /**
134       * Checks that condition c has no waiters.
135       */
136      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 135 | Line 152 | public class ReentrantReadWriteLockTest
152          lock.writeLock().unlock();
153      }
154  
155 <    enum AwaitMethod { await, awaitNanos, awaitUntil };
155 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
156  
157      /**
158 <     * Awaits condition using the specified AwaitMethod
158 >     * Awaits condition using the specified AwaitMethod.
159       */
160      void await(Condition c, AwaitMethod awaitMethod)
161              throws InterruptedException {
# Line 146 | Line 163 | public class ReentrantReadWriteLockTest
163          case await:
164              c.await();
165              break;
166 +        case awaitTimed:
167 +            assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
168 +            break;
169          case awaitNanos:
170              long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
171              assertTrue(nanosRemaining > 0);
# Line 154 | Line 174 | public class ReentrantReadWriteLockTest
174              java.util.Date d = new java.util.Date();
175              assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
176              break;
177 +        default:
178 +            throw new AssertionError();
179          }
180      }
181  
# Line 189 | Line 211 | public class ReentrantReadWriteLockTest
211              new PublicReentrantReadWriteLock(fair);
212          assertNotWriteLocked(lock);
213          lock.writeLock().lock();
214 <        assertWriteLockedBy(lock, Thread.currentThread());
214 >        assertWriteLockedByMoi(lock);
215          lock.writeLock().unlock();
216          assertNotWriteLocked(lock);
217          assertEquals(0, lock.getReadLockCount());
# Line 359 | Line 381 | public class ReentrantReadWriteLockTest
381      }
382  
383      /**
384 +     * write-tryLock on an unlocked lock succeeds
385 +     */
386 +    public void testWriteTryLock()      { testWriteTryLock(false); }
387 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
388 +    public void testWriteTryLock(boolean fair) {
389 +        final PublicReentrantReadWriteLock lock =
390 +            new PublicReentrantReadWriteLock(fair);
391 +        assertTrue(lock.writeLock().tryLock());
392 +        assertWriteLockedByMoi(lock);
393 +        assertTrue(lock.writeLock().tryLock());
394 +        assertWriteLockedByMoi(lock);
395 +        lock.writeLock().unlock();
396 +        releaseWriteLock(lock);
397 +    }
398 +
399 +    /**
400       * write-tryLock fails if locked
401       */
402      public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
# Line 478 | Line 516 | public class ReentrantReadWriteLockTest
516  
517      /**
518       * A thread that tries to acquire a fair read lock (non-reentrantly)
519 <     * will block if there is a waiting writer thread.
519 >     * will block if there is a waiting writer thread
520       */
521      public void testReaderWriterReaderFairFifo() {
522          final PublicReentrantReadWriteLock lock =
# Line 555 | Line 593 | public class ReentrantReadWriteLockTest
593      }
594  
595      /**
596 <     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
596 >     * Read trylock succeeds (barging) even in the presence of waiting
597 >     * readers and/or writers
598       */
599      public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
600      public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
# Line 622 | Line 661 | public class ReentrantReadWriteLockTest
661  
662          waitForQueuedThread(lock, t1);
663          waitForQueuedThread(lock, t2);
664 <        assertWriteLockedBy(lock, Thread.currentThread());
664 >        assertWriteLockedByMoi(lock);
665          lock.readLock().lock();
666          lock.readLock().unlock();
667          releaseWriteLock(lock);
# Line 656 | Line 695 | public class ReentrantReadWriteLockTest
695  
696          waitForQueuedThread(lock, t1);
697          waitForQueuedThread(lock, t2);
698 <        assertWriteLockedBy(lock, Thread.currentThread());
698 >        assertWriteLockedByMoi(lock);
699          lock.readLock().lock();
700          lock.readLock().unlock();
701 <        assertWriteLockedBy(lock, Thread.currentThread());
701 >        assertWriteLockedByMoi(lock);
702          lock.writeLock().unlock();
703          awaitTermination(t1);
704          awaitTermination(t2);
# Line 691 | Line 730 | public class ReentrantReadWriteLockTest
730  
731          waitForQueuedThread(lock, t1);
732          waitForQueuedThread(lock, t2);
733 <        assertWriteLockedBy(lock, Thread.currentThread());
733 >        assertWriteLockedByMoi(lock);
734          assertEquals(1, lock.getWriteHoldCount());
735          lock.writeLock().lock();
736 <        assertWriteLockedBy(lock, Thread.currentThread());
736 >        assertWriteLockedByMoi(lock);
737          assertEquals(2, lock.getWriteHoldCount());
738          lock.writeLock().unlock();
739 <        assertWriteLockedBy(lock, Thread.currentThread());
739 >        assertWriteLockedByMoi(lock);
740          assertEquals(1, lock.getWriteHoldCount());
741          lock.writeLock().unlock();
742          awaitTermination(t1);
# Line 745 | Line 784 | public class ReentrantReadWriteLockTest
784      public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
785      public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
786      public void testWriteTryLock_Timeout(boolean fair) {
787 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
787 >        final PublicReentrantReadWriteLock lock =
788 >            new PublicReentrantReadWriteLock(fair);
789          lock.writeLock().lock();
790          Thread t = newStartedThread(new CheckedRunnable() {
791              public void realRun() throws InterruptedException {
# Line 756 | Line 796 | public class ReentrantReadWriteLockTest
796              }});
797  
798          awaitTermination(t);
799 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
760 <        lock.writeLock().unlock();
799 >        releaseWriteLock(lock);
800      }
801  
802      /**
# Line 782 | Line 821 | public class ReentrantReadWriteLockTest
821      }
822  
823      /**
824 <     * write lockInterruptibly succeeds if lock free else is interruptible
824 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
825       */
826      public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
827      public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
# Line 791 | Line 830 | public class ReentrantReadWriteLockTest
830              new PublicReentrantReadWriteLock(fair);
831          try {
832              lock.writeLock().lockInterruptibly();
833 <        } catch (Throwable t) {
834 <            threadUnexpectedException(t);
833 >        } catch (InterruptedException ie) {
834 >            threadUnexpectedException(ie);
835          }
836          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
837              public void realRun() throws InterruptedException {
# Line 801 | Line 840 | public class ReentrantReadWriteLockTest
840  
841          waitForQueuedThread(lock, t);
842          t.interrupt();
843 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
844          awaitTermination(t);
845          releaseWriteLock(lock);
846      }
# Line 817 | Line 857 | public class ReentrantReadWriteLockTest
857              lock.readLock().lockInterruptibly();
858              lock.readLock().unlock();
859              lock.writeLock().lockInterruptibly();
860 <        } catch (Throwable t) {
861 <            threadUnexpectedException(t);
860 >        } catch (InterruptedException ie) {
861 >            threadUnexpectedException(ie);
862          }
863          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
864              public void realRun() throws InterruptedException {
# Line 839 | Line 879 | public class ReentrantReadWriteLockTest
879      public void testAwait_IMSE(boolean fair) {
880          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
881          final Condition c = lock.writeLock().newCondition();
882 <        long startTime = System.nanoTime();
883 <        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) {}
882 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
883 >            long startTime = System.nanoTime();
884              try {
885 <                c.awaitUninterruptibly();
885 >                await(c, awaitMethod);
886                  shouldThrow();
887 <            } catch (IllegalMonitorStateException success) {}
888 <        } catch (Throwable t) {
889 <            threadUnexpectedException(t);
887 >            } catch (IllegalMonitorStateException success) {
888 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
889 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
890          }
863        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
891      }
892  
893      /**
# Line 986 | Line 1013 | public class ReentrantReadWriteLockTest
1013      }
1014  
1015      /**
1016 <     * awaitUninterruptibly doesn't abort on interrupt
1016 >     * awaitUninterruptibly is uninterruptible
1017       */
1018      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1019      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1020      public void testAwaitUninterruptibly(boolean fair) {
1021          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1022          final Condition c = lock.writeLock().newCondition();
1023 <        final CountDownLatch locked = new CountDownLatch(1);
1024 <        Thread t = newStartedThread(new CheckedRunnable() {
1023 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1024 >
1025 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1026              public void realRun() {
1027 +                // Interrupt before awaitUninterruptibly
1028                  lock.writeLock().lock();
1029 <                locked.countDown();
1029 >                pleaseInterrupt.countDown();
1030 >                Thread.currentThread().interrupt();
1031                  c.awaitUninterruptibly();
1032                  assertTrue(Thread.interrupted());
1033                  lock.writeLock().unlock();
1034              }});
1035  
1036 <        await(locked);
1036 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1037 >            public void realRun() {
1038 >                // Interrupt during awaitUninterruptibly
1039 >                lock.writeLock().lock();
1040 >                pleaseInterrupt.countDown();
1041 >                c.awaitUninterruptibly();
1042 >                assertTrue(Thread.interrupted());
1043 >                lock.writeLock().unlock();
1044 >            }});
1045 >
1046 >        await(pleaseInterrupt);
1047          lock.writeLock().lock();
1048          lock.writeLock().unlock();
1049 <        t.interrupt();
1050 <        long timeoutMillis = 10;
1051 <        assertThreadJoinTimesOut(t, timeoutMillis);
1049 >        t2.interrupt();
1050 >
1051 >        assertThreadStaysAlive(t1);
1052 >        assertTrue(t2.isAlive());
1053 >
1054          lock.writeLock().lock();
1055 <        c.signal();
1055 >        c.signalAll();
1056          lock.writeLock().unlock();
1057 <        awaitTermination(t);
1057 >
1058 >        awaitTermination(t1);
1059 >        awaitTermination(t2);
1060      }
1061  
1062      /**
# Line 1020 | Line 1064 | public class ReentrantReadWriteLockTest
1064       */
1065      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1066      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1067 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1068 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1069      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1070      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1071      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 1032 | Line 1078 | public class ReentrantReadWriteLockTest
1078          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1079              public void realRun() throws InterruptedException {
1080                  lock.writeLock().lock();
1081 <                assertWriteLockedBy(lock, Thread.currentThread());
1081 >                assertWriteLockedByMoi(lock);
1082                  assertHasNoWaiters(lock, c);
1083                  locked.countDown();
1084                  try {
1085                      await(c, awaitMethod);
1086                  } finally {
1087 <                    assertWriteLockedBy(lock, Thread.currentThread());
1087 >                    assertWriteLockedByMoi(lock);
1088                      assertHasNoWaiters(lock, c);
1089                      lock.writeLock().unlock();
1090                      assertFalse(Thread.interrupted());
# Line 1057 | Line 1103 | public class ReentrantReadWriteLockTest
1103       */
1104      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1105      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1106 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1107 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1108      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1109      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1110      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 1090 | Line 1138 | public class ReentrantReadWriteLockTest
1138      }
1139  
1140      /**
1141 <     * signal wakes up waiting threads in FIFO order.
1141 >     * signal wakes up waiting threads in FIFO order
1142       */
1143      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1144      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1150 | Line 1198 | public class ReentrantReadWriteLockTest
1198          Thread t1 = newStartedThread(new CheckedRunnable() {
1199              public void realRun() throws InterruptedException {
1200                  lock.writeLock().lock();
1201 <                assertWriteLockedBy(lock, Thread.currentThread());
1201 >                assertWriteLockedByMoi(lock);
1202                  assertEquals(1, lock.writeLock().getHoldCount());
1203                  locked.countDown();
1204                  c.await();
1205 <                assertWriteLockedBy(lock, Thread.currentThread());
1205 >                assertWriteLockedByMoi(lock);
1206                  assertEquals(1, lock.writeLock().getHoldCount());
1207                  lock.writeLock().unlock();
1208              }});
# Line 1163 | Line 1211 | public class ReentrantReadWriteLockTest
1211              public void realRun() throws InterruptedException {
1212                  lock.writeLock().lock();
1213                  lock.writeLock().lock();
1214 <                assertWriteLockedBy(lock, Thread.currentThread());
1214 >                assertWriteLockedByMoi(lock);
1215                  assertEquals(2, lock.writeLock().getHoldCount());
1216                  locked.countDown();
1217                  c.await();
1218 <                assertWriteLockedBy(lock, Thread.currentThread());
1218 >                assertWriteLockedByMoi(lock);
1219                  assertEquals(2, lock.writeLock().getHoldCount());
1220                  lock.writeLock().unlock();
1221                  lock.writeLock().unlock();
# Line 1205 | Line 1253 | public class ReentrantReadWriteLockTest
1253          assertEquals(1, clone.getReadLockCount());
1254          clone.readLock().unlock();
1255          clone.writeLock().unlock();
1256 +        assertFalse(clone.isWriteLocked());
1257 +        assertEquals(1, lock.getReadLockCount());
1258 +        assertEquals(0, clone.getReadLockCount());
1259      }
1260  
1261      /**
# Line 1248 | Line 1299 | public class ReentrantReadWriteLockTest
1299      }
1300  
1301      /**
1302 <     * hasQueuedThread reports whether a thread is queued.
1302 >     * hasQueuedThread reports whether a thread is queued
1303       */
1304      public void testHasQueuedThread()      { testHasQueuedThread(false); }
1305      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 1589 | Line 1640 | public class ReentrantReadWriteLockTest
1640      public void testToString_fair() { testToString(true); }
1641      public void testToString(boolean fair) {
1642          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1643 <        String us = lock.toString();
1644 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1645 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1646 <        lock.writeLock().lock();
1647 <        String ws = lock.toString();
1597 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1598 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1643 >        assertTrue(lock.toString().contains("Write locks = 0"));
1644 >        assertTrue(lock.toString().contains("Read locks = 0"));
1645 >        lock.writeLock().lock();
1646 >        assertTrue(lock.toString().contains("Write locks = 1"));
1647 >        assertTrue(lock.toString().contains("Read locks = 0"));
1648          lock.writeLock().unlock();
1649          lock.readLock().lock();
1650          lock.readLock().lock();
1651 <        String rs = lock.toString();
1652 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1604 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1651 >        assertTrue(lock.toString().contains("Write locks = 0"));
1652 >        assertTrue(lock.toString().contains("Read locks = 2"));
1653      }
1654  
1655      /**
# Line 1611 | Line 1659 | public class ReentrantReadWriteLockTest
1659      public void testReadLockToString_fair() { testReadLockToString(true); }
1660      public void testReadLockToString(boolean fair) {
1661          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1662 <        String us = lock.readLock().toString();
1615 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1662 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1663          lock.readLock().lock();
1664          lock.readLock().lock();
1665 <        String rs = lock.readLock().toString();
1619 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1665 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1666      }
1667  
1668      /**
# Line 1626 | Line 1672 | public class ReentrantReadWriteLockTest
1672      public void testWriteLockToString_fair() { testWriteLockToString(true); }
1673      public void testWriteLockToString(boolean fair) {
1674          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1675 <        String us = lock.writeLock().toString();
1630 <        assertTrue(us.indexOf("Unlocked") >= 0);
1675 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1676          lock.writeLock().lock();
1677 <        String ls = lock.writeLock().toString();
1678 <        assertTrue(ls.indexOf("Locked") >= 0);
1677 >        assertTrue(lock.writeLock().toString().contains("Locked"));
1678 >        lock.writeLock().unlock();
1679 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1680      }
1681  
1682   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines