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.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 84 | Line 85 | public class ReentrantReadWriteLockTest
85              Thread.yield();
86          }
87          assertTrue(t.isAlive());
88 <        assertTrue(lock.getOwner() != t);
88 >        assertNotSame(t, lock.getOwner());
89      }
90  
91      /**
# Line 145 | 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.
# Line 156 | 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 504 | 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 581 | 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 866 | 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 {
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) {}
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 (InterruptedException ie) {
881 <            threadUnexpectedException(ie);
879 >            } catch (IllegalMonitorStateException success) {
880 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
881 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
882          }
890        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
883      }
884  
885      /**
# Line 1013 | 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 <        assertThreadStaysAlive(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 1047 | 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 1084 | 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 1117 | 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 1278 | 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 1619 | 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();
1627 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1628 <        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);
1634 <        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 1641 | 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();
1645 <        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();
1649 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1657 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1658      }
1659  
1660      /**
# Line 1656 | 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();
1660 <        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