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.62 by jsr166, Sun May 15 17:30:21 2011 UTC vs.
Revision 1.76 by jsr166, Fri Jul 3 00:25:35 2015 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) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28      public static Test suite() {
29          return new TestSuite(ReentrantReadWriteLockTest.class);
# Line 84 | Line 91 | public class ReentrantReadWriteLockTest
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.
158 >     * Awaits condition "indefinitely" using the specified AwaitMethod.
159       */
160      void await(Condition c, AwaitMethod awaitMethod)
161              throws InterruptedException {
162 +        long timeoutMillis = 2 * LONG_DELAY_MS;
163          switch (awaitMethod) {
164          case await:
165              c.await();
166              break;
167 +        case awaitTimed:
168 +            assertTrue(c.await(timeoutMillis, MILLISECONDS));
169 +            break;
170          case awaitNanos:
171 <            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
172 <            assertTrue(nanosRemaining > 0);
171 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
172 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
173 >            assertTrue(nanosRemaining > timeoutNanos / 2);
174 >            assertTrue(nanosRemaining <= timeoutNanos);
175              break;
176          case awaitUntil:
177 <            java.util.Date d = new java.util.Date();
165 <            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
177 >            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
178              break;
179 +        default:
180 +            throw new AssertionError();
181          }
182      }
183  
# Line 224 | Line 238 | public class ReentrantReadWriteLockTest
238          }
239          for (int i = SIZE; i > 0; i--) {
240              lock.writeLock().unlock();
241 <            assertEquals(i-1,lock.getWriteHoldCount());
241 >            assertEquals(i - 1,lock.getWriteHoldCount());
242          }
243      }
244  
# Line 241 | Line 255 | public class ReentrantReadWriteLockTest
255          }
256          for (int i = SIZE; i > 0; i--) {
257              lock.writeLock().unlock();
258 <            assertEquals(i-1,lock.writeLock().getHoldCount());
258 >            assertEquals(i - 1,lock.writeLock().getHoldCount());
259          }
260      }
261  
# Line 258 | Line 272 | public class ReentrantReadWriteLockTest
272          }
273          for (int i = SIZE; i > 0; i--) {
274              lock.readLock().unlock();
275 <            assertEquals(i-1,lock.getReadHoldCount());
275 >            assertEquals(i - 1,lock.getReadHoldCount());
276          }
277      }
278  
# Line 504 | Line 518 | public class ReentrantReadWriteLockTest
518  
519      /**
520       * A thread that tries to acquire a fair read lock (non-reentrantly)
521 <     * will block if there is a waiting writer thread.
521 >     * will block if there is a waiting writer thread
522       */
523      public void testReaderWriterReaderFairFifo() {
524          final PublicReentrantReadWriteLock lock =
# Line 581 | Line 595 | public class ReentrantReadWriteLockTest
595      }
596  
597      /**
598 <     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
598 >     * Read trylock succeeds (barging) even in the presence of waiting
599 >     * readers and/or writers
600       */
601      public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
602      public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
# Line 817 | Line 832 | public class ReentrantReadWriteLockTest
832              new PublicReentrantReadWriteLock(fair);
833          try {
834              lock.writeLock().lockInterruptibly();
835 <        } catch (InterruptedException ie) {
821 <            threadUnexpectedException(ie);
822 <        }
835 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
836          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
837              public void realRun() throws InterruptedException {
838                  lock.writeLock().lockInterruptibly();
# Line 844 | Line 857 | public class ReentrantReadWriteLockTest
857              lock.readLock().lockInterruptibly();
858              lock.readLock().unlock();
859              lock.writeLock().lockInterruptibly();
860 <        } catch (InterruptedException ie) {
848 <            threadUnexpectedException(ie);
849 <        }
860 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
861          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
862              public void realRun() throws InterruptedException {
863                  lock.readLock().lockInterruptibly();
# Line 866 | 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 {
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) {}
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 (InterruptedException ie) {
887 <            threadUnexpectedException(ie);
885 >            } catch (IllegalMonitorStateException success) {
886 >            } catch (InterruptedException fail) {
887 >                threadUnexpectedException(fail);
888 >            }
889 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
890          }
890        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
891      }
892  
893      /**
# Line 936 | Line 936 | public class ReentrantReadWriteLockTest
936              assertTrue(nanosRemaining <= 0);
937              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
938              lock.writeLock().unlock();
939 <        } catch (InterruptedException e) {
940 <            threadUnexpectedException(e);
941 <        }
939 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
940      }
941  
942      /**
# Line 957 | Line 955 | public class ReentrantReadWriteLockTest
955              assertFalse(c.await(timeoutMillis, MILLISECONDS));
956              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
957              lock.writeLock().unlock();
958 <        } catch (InterruptedException e) {
961 <            threadUnexpectedException(e);
962 <        }
958 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
959      }
960  
961      /**
# Line 979 | Line 975 | public class ReentrantReadWriteLockTest
975              assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
976              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
977              lock.writeLock().unlock();
978 <        } catch (InterruptedException e) {
983 <            threadUnexpectedException(e);
984 <        }
978 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
979      }
980  
981      /**
# Line 1013 | Line 1007 | public class ReentrantReadWriteLockTest
1007      }
1008  
1009      /**
1010 <     * awaitUninterruptibly doesn't abort on interrupt
1010 >     * awaitUninterruptibly is uninterruptible
1011       */
1012      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1013      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1014      public void testAwaitUninterruptibly(boolean fair) {
1015          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1016          final Condition c = lock.writeLock().newCondition();
1017 <        final CountDownLatch locked = new CountDownLatch(1);
1018 <        Thread t = newStartedThread(new CheckedRunnable() {
1017 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1018 >
1019 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1020              public void realRun() {
1021 +                // Interrupt before awaitUninterruptibly
1022                  lock.writeLock().lock();
1023 <                locked.countDown();
1023 >                pleaseInterrupt.countDown();
1024 >                Thread.currentThread().interrupt();
1025                  c.awaitUninterruptibly();
1026                  assertTrue(Thread.interrupted());
1027                  lock.writeLock().unlock();
1028              }});
1029  
1030 <        await(locked);
1030 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1031 >            public void realRun() {
1032 >                // Interrupt during awaitUninterruptibly
1033 >                lock.writeLock().lock();
1034 >                pleaseInterrupt.countDown();
1035 >                c.awaitUninterruptibly();
1036 >                assertTrue(Thread.interrupted());
1037 >                lock.writeLock().unlock();
1038 >            }});
1039 >
1040 >        await(pleaseInterrupt);
1041          lock.writeLock().lock();
1042          lock.writeLock().unlock();
1043 <        t.interrupt();
1044 <        long timeoutMillis = 10;
1045 <        assertThreadStaysAlive(t, timeoutMillis);
1043 >        t2.interrupt();
1044 >
1045 >        assertThreadStaysAlive(t1);
1046 >        assertTrue(t2.isAlive());
1047 >
1048          lock.writeLock().lock();
1049 <        c.signal();
1049 >        c.signalAll();
1050          lock.writeLock().unlock();
1051 <        awaitTermination(t);
1051 >
1052 >        awaitTermination(t1);
1053 >        awaitTermination(t2);
1054      }
1055  
1056      /**
# Line 1047 | Line 1058 | public class ReentrantReadWriteLockTest
1058       */
1059      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1060      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1061 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1062 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1063      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1064      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1065      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 1084 | Line 1097 | public class ReentrantReadWriteLockTest
1097       */
1098      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1099      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1100 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1101 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1102      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1103      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1104      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 1117 | Line 1132 | public class ReentrantReadWriteLockTest
1132      }
1133  
1134      /**
1135 <     * signal wakes up waiting threads in FIFO order.
1135 >     * signal wakes up waiting threads in FIFO order
1136       */
1137      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1138      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1278 | Line 1293 | public class ReentrantReadWriteLockTest
1293      }
1294  
1295      /**
1296 <     * hasQueuedThread reports whether a thread is queued.
1296 >     * hasQueuedThread reports whether a thread is queued
1297       */
1298      public void testHasQueuedThread()      { testHasQueuedThread(false); }
1299      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 1619 | Line 1634 | public class ReentrantReadWriteLockTest
1634      public void testToString_fair() { testToString(true); }
1635      public void testToString(boolean fair) {
1636          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1637 <        String us = lock.toString();
1638 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1639 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1640 <        lock.writeLock().lock();
1641 <        String ws = lock.toString();
1627 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1628 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1637 >        assertTrue(lock.toString().contains("Write locks = 0"));
1638 >        assertTrue(lock.toString().contains("Read locks = 0"));
1639 >        lock.writeLock().lock();
1640 >        assertTrue(lock.toString().contains("Write locks = 1"));
1641 >        assertTrue(lock.toString().contains("Read locks = 0"));
1642          lock.writeLock().unlock();
1643          lock.readLock().lock();
1644          lock.readLock().lock();
1645 <        String rs = lock.toString();
1646 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1634 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1645 >        assertTrue(lock.toString().contains("Write locks = 0"));
1646 >        assertTrue(lock.toString().contains("Read locks = 2"));
1647      }
1648  
1649      /**
# Line 1641 | Line 1653 | public class ReentrantReadWriteLockTest
1653      public void testReadLockToString_fair() { testReadLockToString(true); }
1654      public void testReadLockToString(boolean fair) {
1655          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1656 <        String us = lock.readLock().toString();
1645 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1656 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1657          lock.readLock().lock();
1658          lock.readLock().lock();
1659 <        String rs = lock.readLock().toString();
1649 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1659 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1660      }
1661  
1662      /**
# Line 1656 | Line 1666 | public class ReentrantReadWriteLockTest
1666      public void testWriteLockToString_fair() { testWriteLockToString(true); }
1667      public void testWriteLockToString(boolean fair) {
1668          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1669 <        String us = lock.writeLock().toString();
1660 <        assertTrue(us.indexOf("Unlocked") >= 0);
1669 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1670          lock.writeLock().lock();
1671 <        String ls = lock.writeLock().toString();
1672 <        assertTrue(ls.indexOf("Locked") >= 0);
1671 >        assertTrue(lock.writeLock().toString().contains("Locked"));
1672 >        lock.writeLock().unlock();
1673 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1674      }
1675  
1676   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines