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.73 by jsr166, Sat Apr 25 04:55:31 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.
# 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 817 | Line 830 | public class ReentrantReadWriteLockTest
830              new PublicReentrantReadWriteLock(fair);
831          try {
832              lock.writeLock().lockInterruptibly();
833 <        } catch (InterruptedException ie) {
821 <            threadUnexpectedException(ie);
822 <        }
833 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
834          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
835              public void realRun() throws InterruptedException {
836                  lock.writeLock().lockInterruptibly();
# Line 844 | Line 855 | public class ReentrantReadWriteLockTest
855              lock.readLock().lockInterruptibly();
856              lock.readLock().unlock();
857              lock.writeLock().lockInterruptibly();
858 <        } catch (InterruptedException ie) {
848 <            threadUnexpectedException(ie);
849 <        }
858 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
859          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
860              public void realRun() throws InterruptedException {
861                  lock.readLock().lockInterruptibly();
# Line 866 | Line 875 | public class ReentrantReadWriteLockTest
875      public void testAwait_IMSE(boolean fair) {
876          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
877          final Condition c = lock.writeLock().newCondition();
878 <        long startTime = System.nanoTime();
879 <        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) {}
878 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
879 >            long startTime = System.nanoTime();
880              try {
881 <                c.awaitUninterruptibly();
881 >                await(c, awaitMethod);
882                  shouldThrow();
883 <            } catch (IllegalMonitorStateException success) {}
884 <        } catch (InterruptedException ie) {
885 <            threadUnexpectedException(ie);
883 >            } catch (IllegalMonitorStateException success) {
884 >            } catch (InterruptedException fail) {
885 >                threadUnexpectedException(fail);
886 >            }
887 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
888          }
890        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
889      }
890  
891      /**
# Line 936 | Line 934 | public class ReentrantReadWriteLockTest
934              assertTrue(nanosRemaining <= 0);
935              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
936              lock.writeLock().unlock();
937 <        } catch (InterruptedException e) {
940 <            threadUnexpectedException(e);
941 <        }
937 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
938      }
939  
940      /**
# Line 957 | Line 953 | public class ReentrantReadWriteLockTest
953              assertFalse(c.await(timeoutMillis, MILLISECONDS));
954              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
955              lock.writeLock().unlock();
956 <        } catch (InterruptedException e) {
961 <            threadUnexpectedException(e);
962 <        }
956 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
957      }
958  
959      /**
# Line 979 | Line 973 | public class ReentrantReadWriteLockTest
973              assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
974              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
975              lock.writeLock().unlock();
976 <        } catch (InterruptedException e) {
983 <            threadUnexpectedException(e);
984 <        }
976 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
977      }
978  
979      /**
# 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