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.63 by jsr166, Sat May 21 06:24:33 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 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, awaitTimed, awaitNanos, awaitUntil };
155 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
156  
157      /**
158       * Awaits condition using the specified AwaitMethod.
# Line 507 | 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 584 | 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 1003 | 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 <        assertThreadStaysAlive(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 1111 | 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 1272 | 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 1613 | 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();
1621 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1622 <        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);
1628 <        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 1635 | 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();
1639 <        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();
1643 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1663 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1664      }
1665  
1666      /**
# Line 1650 | 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();
1654 <        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