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.61 by jsr166, Fri May 13 21:48:59 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 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 145 | 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.
# Line 156 | 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 164 | 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 504 | 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 581 | 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 866 | 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 {
871 <            try {
872 <                c.await();
873 <                shouldThrow();
874 <            } catch (IllegalMonitorStateException success) {}
875 <            try {
876 <                c.await(LONG_DELAY_MS, MILLISECONDS);
877 <                shouldThrow();
878 <            } catch (IllegalMonitorStateException success) {}
879 <            try {
880 <                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
881 <                shouldThrow();
882 <            } 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 (InterruptedException ie) {
889 <            threadUnexpectedException(ie);
887 >            } catch (IllegalMonitorStateException success) {
888 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
889 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
890          }
890        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
891      }
892  
893      /**
# Line 1013 | 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 <        assertThreadStaysAlive(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 1047 | 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 1084 | 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 1117 | 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 1278 | 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 1619 | 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();
1627 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1628 <        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);
1634 <        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 1641 | 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();
1645 <        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();
1649 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1665 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1666      }
1667  
1668      /**
# Line 1656 | 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();
1660 <        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