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.69 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.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 189 | Line 209 | public class ReentrantReadWriteLockTest
209              new PublicReentrantReadWriteLock(fair);
210          assertNotWriteLocked(lock);
211          lock.writeLock().lock();
212 <        assertWriteLockedBy(lock, Thread.currentThread());
212 >        assertWriteLockedByMoi(lock);
213          lock.writeLock().unlock();
214          assertNotWriteLocked(lock);
215          assertEquals(0, lock.getReadLockCount());
# Line 359 | Line 379 | public class ReentrantReadWriteLockTest
379      }
380  
381      /**
382 +     * write-tryLock on an unlocked lock succeeds
383 +     */
384 +    public void testWriteTryLock()      { testWriteTryLock(false); }
385 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
386 +    public void testWriteTryLock(boolean fair) {
387 +        final PublicReentrantReadWriteLock lock =
388 +            new PublicReentrantReadWriteLock(fair);
389 +        assertTrue(lock.writeLock().tryLock());
390 +        assertWriteLockedByMoi(lock);
391 +        assertTrue(lock.writeLock().tryLock());
392 +        assertWriteLockedByMoi(lock);
393 +        lock.writeLock().unlock();
394 +        releaseWriteLock(lock);
395 +    }
396 +
397 +    /**
398       * write-tryLock fails if locked
399       */
400      public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
# Line 478 | Line 514 | public class ReentrantReadWriteLockTest
514  
515      /**
516       * A thread that tries to acquire a fair read lock (non-reentrantly)
517 <     * will block if there is a waiting writer thread.
517 >     * will block if there is a waiting writer thread
518       */
519      public void testReaderWriterReaderFairFifo() {
520          final PublicReentrantReadWriteLock lock =
# Line 555 | Line 591 | public class ReentrantReadWriteLockTest
591      }
592  
593      /**
594 <     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
594 >     * Read trylock succeeds (barging) even in the presence of waiting
595 >     * readers and/or writers
596       */
597      public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
598      public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
# Line 622 | Line 659 | public class ReentrantReadWriteLockTest
659  
660          waitForQueuedThread(lock, t1);
661          waitForQueuedThread(lock, t2);
662 <        assertWriteLockedBy(lock, Thread.currentThread());
662 >        assertWriteLockedByMoi(lock);
663          lock.readLock().lock();
664          lock.readLock().unlock();
665          releaseWriteLock(lock);
# Line 656 | Line 693 | public class ReentrantReadWriteLockTest
693  
694          waitForQueuedThread(lock, t1);
695          waitForQueuedThread(lock, t2);
696 <        assertWriteLockedBy(lock, Thread.currentThread());
696 >        assertWriteLockedByMoi(lock);
697          lock.readLock().lock();
698          lock.readLock().unlock();
699 <        assertWriteLockedBy(lock, Thread.currentThread());
699 >        assertWriteLockedByMoi(lock);
700          lock.writeLock().unlock();
701          awaitTermination(t1);
702          awaitTermination(t2);
# Line 691 | Line 728 | public class ReentrantReadWriteLockTest
728  
729          waitForQueuedThread(lock, t1);
730          waitForQueuedThread(lock, t2);
731 <        assertWriteLockedBy(lock, Thread.currentThread());
731 >        assertWriteLockedByMoi(lock);
732          assertEquals(1, lock.getWriteHoldCount());
733          lock.writeLock().lock();
734 <        assertWriteLockedBy(lock, Thread.currentThread());
734 >        assertWriteLockedByMoi(lock);
735          assertEquals(2, lock.getWriteHoldCount());
736          lock.writeLock().unlock();
737 <        assertWriteLockedBy(lock, Thread.currentThread());
737 >        assertWriteLockedByMoi(lock);
738          assertEquals(1, lock.getWriteHoldCount());
739          lock.writeLock().unlock();
740          awaitTermination(t1);
# Line 745 | Line 782 | public class ReentrantReadWriteLockTest
782      public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
783      public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
784      public void testWriteTryLock_Timeout(boolean fair) {
785 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
785 >        final PublicReentrantReadWriteLock lock =
786 >            new PublicReentrantReadWriteLock(fair);
787          lock.writeLock().lock();
788          Thread t = newStartedThread(new CheckedRunnable() {
789              public void realRun() throws InterruptedException {
# Line 756 | Line 794 | public class ReentrantReadWriteLockTest
794              }});
795  
796          awaitTermination(t);
797 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
760 <        lock.writeLock().unlock();
797 >        releaseWriteLock(lock);
798      }
799  
800      /**
# Line 782 | Line 819 | public class ReentrantReadWriteLockTest
819      }
820  
821      /**
822 <     * write lockInterruptibly succeeds if lock free else is interruptible
822 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
823       */
824      public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
825      public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
# Line 791 | Line 828 | public class ReentrantReadWriteLockTest
828              new PublicReentrantReadWriteLock(fair);
829          try {
830              lock.writeLock().lockInterruptibly();
831 <        } catch (Throwable t) {
832 <            threadUnexpectedException(t);
831 >        } catch (InterruptedException ie) {
832 >            threadUnexpectedException(ie);
833          }
834          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
835              public void realRun() throws InterruptedException {
# Line 801 | Line 838 | public class ReentrantReadWriteLockTest
838  
839          waitForQueuedThread(lock, t);
840          t.interrupt();
841 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
842          awaitTermination(t);
843          releaseWriteLock(lock);
844      }
# Line 817 | Line 855 | public class ReentrantReadWriteLockTest
855              lock.readLock().lockInterruptibly();
856              lock.readLock().unlock();
857              lock.writeLock().lockInterruptibly();
858 <        } catch (Throwable t) {
859 <            threadUnexpectedException(t);
858 >        } catch (InterruptedException ie) {
859 >            threadUnexpectedException(ie);
860          }
861          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
862              public void realRun() throws InterruptedException {
# Line 839 | Line 877 | public class ReentrantReadWriteLockTest
877      public void testAwait_IMSE(boolean fair) {
878          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
879          final Condition c = lock.writeLock().newCondition();
880 <        long startTime = System.nanoTime();
881 <        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) {}
880 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
881 >            long startTime = System.nanoTime();
882              try {
883 <                c.awaitUninterruptibly();
883 >                await(c, awaitMethod);
884                  shouldThrow();
885 <            } catch (IllegalMonitorStateException success) {}
886 <        } catch (Throwable t) {
887 <            threadUnexpectedException(t);
885 >            } catch (IllegalMonitorStateException success) {
886 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
887 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
888          }
863        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
889      }
890  
891      /**
# Line 986 | Line 1011 | public class ReentrantReadWriteLockTest
1011      }
1012  
1013      /**
1014 <     * awaitUninterruptibly doesn't abort on interrupt
1014 >     * awaitUninterruptibly is uninterruptible
1015       */
1016      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1017      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1018      public void testAwaitUninterruptibly(boolean fair) {
1019          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1020          final Condition c = lock.writeLock().newCondition();
1021 <        final CountDownLatch locked = new CountDownLatch(1);
1022 <        Thread t = newStartedThread(new CheckedRunnable() {
1021 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1022 >
1023 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1024              public void realRun() {
1025 +                // Interrupt before awaitUninterruptibly
1026                  lock.writeLock().lock();
1027 <                locked.countDown();
1027 >                pleaseInterrupt.countDown();
1028 >                Thread.currentThread().interrupt();
1029                  c.awaitUninterruptibly();
1030                  assertTrue(Thread.interrupted());
1031                  lock.writeLock().unlock();
1032              }});
1033  
1034 <        await(locked);
1034 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1035 >            public void realRun() {
1036 >                // Interrupt during awaitUninterruptibly
1037 >                lock.writeLock().lock();
1038 >                pleaseInterrupt.countDown();
1039 >                c.awaitUninterruptibly();
1040 >                assertTrue(Thread.interrupted());
1041 >                lock.writeLock().unlock();
1042 >            }});
1043 >
1044 >        await(pleaseInterrupt);
1045          lock.writeLock().lock();
1046          lock.writeLock().unlock();
1047 <        t.interrupt();
1048 <        long timeoutMillis = 10;
1049 <        assertThreadJoinTimesOut(t, timeoutMillis);
1047 >        t2.interrupt();
1048 >
1049 >        assertThreadStaysAlive(t1);
1050 >        assertTrue(t2.isAlive());
1051 >
1052          lock.writeLock().lock();
1053 <        c.signal();
1053 >        c.signalAll();
1054          lock.writeLock().unlock();
1055 <        awaitTermination(t);
1055 >
1056 >        awaitTermination(t1);
1057 >        awaitTermination(t2);
1058      }
1059  
1060      /**
# Line 1020 | Line 1062 | public class ReentrantReadWriteLockTest
1062       */
1063      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1064      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1065 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1066 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1067      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1068      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1069      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 1032 | Line 1076 | public class ReentrantReadWriteLockTest
1076          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1077              public void realRun() throws InterruptedException {
1078                  lock.writeLock().lock();
1079 <                assertWriteLockedBy(lock, Thread.currentThread());
1079 >                assertWriteLockedByMoi(lock);
1080                  assertHasNoWaiters(lock, c);
1081                  locked.countDown();
1082                  try {
1083                      await(c, awaitMethod);
1084                  } finally {
1085 <                    assertWriteLockedBy(lock, Thread.currentThread());
1085 >                    assertWriteLockedByMoi(lock);
1086                      assertHasNoWaiters(lock, c);
1087                      lock.writeLock().unlock();
1088                      assertFalse(Thread.interrupted());
# Line 1057 | Line 1101 | public class ReentrantReadWriteLockTest
1101       */
1102      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1103      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1104 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1105 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1106      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1107      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1108      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 1090 | Line 1136 | public class ReentrantReadWriteLockTest
1136      }
1137  
1138      /**
1139 <     * signal wakes up waiting threads in FIFO order.
1139 >     * signal wakes up waiting threads in FIFO order
1140       */
1141      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1142      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1150 | Line 1196 | public class ReentrantReadWriteLockTest
1196          Thread t1 = newStartedThread(new CheckedRunnable() {
1197              public void realRun() throws InterruptedException {
1198                  lock.writeLock().lock();
1199 <                assertWriteLockedBy(lock, Thread.currentThread());
1199 >                assertWriteLockedByMoi(lock);
1200                  assertEquals(1, lock.writeLock().getHoldCount());
1201                  locked.countDown();
1202                  c.await();
1203 <                assertWriteLockedBy(lock, Thread.currentThread());
1203 >                assertWriteLockedByMoi(lock);
1204                  assertEquals(1, lock.writeLock().getHoldCount());
1205                  lock.writeLock().unlock();
1206              }});
# Line 1163 | Line 1209 | public class ReentrantReadWriteLockTest
1209              public void realRun() throws InterruptedException {
1210                  lock.writeLock().lock();
1211                  lock.writeLock().lock();
1212 <                assertWriteLockedBy(lock, Thread.currentThread());
1212 >                assertWriteLockedByMoi(lock);
1213                  assertEquals(2, lock.writeLock().getHoldCount());
1214                  locked.countDown();
1215                  c.await();
1216 <                assertWriteLockedBy(lock, Thread.currentThread());
1216 >                assertWriteLockedByMoi(lock);
1217                  assertEquals(2, lock.writeLock().getHoldCount());
1218                  lock.writeLock().unlock();
1219                  lock.writeLock().unlock();
# Line 1205 | Line 1251 | public class ReentrantReadWriteLockTest
1251          assertEquals(1, clone.getReadLockCount());
1252          clone.readLock().unlock();
1253          clone.writeLock().unlock();
1254 +        assertFalse(clone.isWriteLocked());
1255 +        assertEquals(1, lock.getReadLockCount());
1256 +        assertEquals(0, clone.getReadLockCount());
1257      }
1258  
1259      /**
# Line 1248 | Line 1297 | public class ReentrantReadWriteLockTest
1297      }
1298  
1299      /**
1300 <     * hasQueuedThread reports whether a thread is queued.
1300 >     * hasQueuedThread reports whether a thread is queued
1301       */
1302      public void testHasQueuedThread()      { testHasQueuedThread(false); }
1303      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 1589 | Line 1638 | public class ReentrantReadWriteLockTest
1638      public void testToString_fair() { testToString(true); }
1639      public void testToString(boolean fair) {
1640          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1641 <        String us = lock.toString();
1642 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1643 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1644 <        lock.writeLock().lock();
1645 <        String ws = lock.toString();
1597 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1598 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1641 >        assertTrue(lock.toString().contains("Write locks = 0"));
1642 >        assertTrue(lock.toString().contains("Read locks = 0"));
1643 >        lock.writeLock().lock();
1644 >        assertTrue(lock.toString().contains("Write locks = 1"));
1645 >        assertTrue(lock.toString().contains("Read locks = 0"));
1646          lock.writeLock().unlock();
1647          lock.readLock().lock();
1648          lock.readLock().lock();
1649 <        String rs = lock.toString();
1650 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1604 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1649 >        assertTrue(lock.toString().contains("Write locks = 0"));
1650 >        assertTrue(lock.toString().contains("Read locks = 2"));
1651      }
1652  
1653      /**
# Line 1611 | Line 1657 | public class ReentrantReadWriteLockTest
1657      public void testReadLockToString_fair() { testReadLockToString(true); }
1658      public void testReadLockToString(boolean fair) {
1659          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1660 <        String us = lock.readLock().toString();
1615 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1660 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1661          lock.readLock().lock();
1662          lock.readLock().lock();
1663 <        String rs = lock.readLock().toString();
1619 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1663 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1664      }
1665  
1666      /**
# Line 1626 | Line 1670 | public class ReentrantReadWriteLockTest
1670      public void testWriteLockToString_fair() { testWriteLockToString(true); }
1671      public void testWriteLockToString(boolean fair) {
1672          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1673 <        String us = lock.writeLock().toString();
1630 <        assertTrue(us.indexOf("Unlocked") >= 0);
1673 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1674          lock.writeLock().lock();
1675 <        String ls = lock.writeLock().toString();
1676 <        assertTrue(ls.indexOf("Locked") >= 0);
1675 >        assertTrue(lock.writeLock().toString().contains("Locked"));
1676 >        lock.writeLock().unlock();
1677 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1678      }
1679  
1680   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines