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.60 by jsr166, Mon May 9 20:00:19 2011 UTC vs.
Revision 1.64 by jsr166, Tue May 24 23:40:14 2011 UTC

# Line 67 | Line 67 | public class ReentrantReadWriteLockTest
67       */
68      void releaseWriteLock(PublicReentrantReadWriteLock lock) {
69          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
70 <        assertWriteLockedBy(lock, Thread.currentThread());
70 >        assertWriteLockedByMoi(lock);
71          assertEquals(1, lock.getWriteHoldCount());
72          writeLock.unlock();
73          assertNotWriteLocked(lock);
# Line 80 | Line 80 | public class ReentrantReadWriteLockTest
80          long startTime = System.nanoTime();
81          while (!lock.hasQueuedThread(t)) {
82              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
83 <                throw new AssertionError("timed out");
83 >                throw new AssertionFailedError("timed out");
84              Thread.yield();
85          }
86          assertTrue(t.isAlive());
# Line 94 | Line 94 | public class ReentrantReadWriteLockTest
94          assertFalse(lock.isWriteLocked());
95          assertFalse(lock.isWriteLockedByCurrentThread());
96          assertFalse(lock.writeLock().isHeldByCurrentThread());
97        assertNull(lock.getOwner());
97          assertEquals(0, lock.getWriteHoldCount());
98 +        assertEquals(0, lock.writeLock().getHoldCount());
99 +        assertNull(lock.getOwner());
100      }
101  
102      /**
# Line 110 | Line 111 | public class ReentrantReadWriteLockTest
111                       lock.writeLock().isHeldByCurrentThread());
112          assertEquals(t == Thread.currentThread(),
113                       lock.getWriteHoldCount() > 0);
114 +        assertEquals(t == Thread.currentThread(),
115 +                     lock.writeLock().getHoldCount() > 0);
116          assertEquals(0, lock.getReadLockCount());
117      }
118  
119      /**
120 +     * Checks that lock is write-locked by the current thread.
121 +     */
122 +    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
123 +        assertWriteLockedBy(lock, Thread.currentThread());
124 +    }
125 +
126 +    /**
127       * Checks that condition c has no waiters.
128       */
129      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 135 | Line 145 | public class ReentrantReadWriteLockTest
145          lock.writeLock().unlock();
146      }
147  
148 <    enum AwaitMethod { await, awaitNanos, awaitUntil };
148 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
149  
150      /**
151 <     * Awaits condition using the specified AwaitMethod
151 >     * Awaits condition using the specified AwaitMethod.
152       */
153      void await(Condition c, AwaitMethod awaitMethod)
154              throws InterruptedException {
# Line 146 | Line 156 | public class ReentrantReadWriteLockTest
156          case await:
157              c.await();
158              break;
159 +        case awaitTimed:
160 +            assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
161 +            break;
162          case awaitNanos:
163              long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
164              assertTrue(nanosRemaining > 0);
# Line 189 | Line 202 | public class ReentrantReadWriteLockTest
202              new PublicReentrantReadWriteLock(fair);
203          assertNotWriteLocked(lock);
204          lock.writeLock().lock();
205 <        assertWriteLockedBy(lock, Thread.currentThread());
205 >        assertWriteLockedByMoi(lock);
206          lock.writeLock().unlock();
207          assertNotWriteLocked(lock);
208          assertEquals(0, lock.getReadLockCount());
# Line 359 | Line 372 | public class ReentrantReadWriteLockTest
372      }
373  
374      /**
375 +     * write-tryLock on an unlocked lock succeeds
376 +     */
377 +    public void testWriteTryLock()      { testWriteTryLock(false); }
378 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
379 +    public void testWriteTryLock(boolean fair) {
380 +        final PublicReentrantReadWriteLock lock =
381 +            new PublicReentrantReadWriteLock(fair);
382 +        assertTrue(lock.writeLock().tryLock());
383 +        assertWriteLockedByMoi(lock);
384 +        assertTrue(lock.writeLock().tryLock());
385 +        assertWriteLockedByMoi(lock);
386 +        lock.writeLock().unlock();
387 +        releaseWriteLock(lock);
388 +    }
389 +
390 +    /**
391       * write-tryLock fails if locked
392       */
393      public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
# Line 478 | Line 507 | public class ReentrantReadWriteLockTest
507  
508      /**
509       * A thread that tries to acquire a fair read lock (non-reentrantly)
510 <     * will block if there is a waiting writer thread.
510 >     * will block if there is a waiting writer thread
511       */
512      public void testReaderWriterReaderFairFifo() {
513          final PublicReentrantReadWriteLock lock =
# Line 555 | Line 584 | public class ReentrantReadWriteLockTest
584      }
585  
586      /**
587 <     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
587 >     * Read trylock succeeds (barging) even in the presence of waiting
588 >     * readers and/or writers
589       */
590      public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
591      public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
# Line 622 | Line 652 | public class ReentrantReadWriteLockTest
652  
653          waitForQueuedThread(lock, t1);
654          waitForQueuedThread(lock, t2);
655 <        assertWriteLockedBy(lock, Thread.currentThread());
655 >        assertWriteLockedByMoi(lock);
656          lock.readLock().lock();
657          lock.readLock().unlock();
658          releaseWriteLock(lock);
# Line 656 | Line 686 | public class ReentrantReadWriteLockTest
686  
687          waitForQueuedThread(lock, t1);
688          waitForQueuedThread(lock, t2);
689 <        assertWriteLockedBy(lock, Thread.currentThread());
689 >        assertWriteLockedByMoi(lock);
690          lock.readLock().lock();
691          lock.readLock().unlock();
692 <        assertWriteLockedBy(lock, Thread.currentThread());
692 >        assertWriteLockedByMoi(lock);
693          lock.writeLock().unlock();
694          awaitTermination(t1);
695          awaitTermination(t2);
# Line 691 | Line 721 | public class ReentrantReadWriteLockTest
721  
722          waitForQueuedThread(lock, t1);
723          waitForQueuedThread(lock, t2);
724 <        assertWriteLockedBy(lock, Thread.currentThread());
724 >        assertWriteLockedByMoi(lock);
725          assertEquals(1, lock.getWriteHoldCount());
726          lock.writeLock().lock();
727 <        assertWriteLockedBy(lock, Thread.currentThread());
727 >        assertWriteLockedByMoi(lock);
728          assertEquals(2, lock.getWriteHoldCount());
729          lock.writeLock().unlock();
730 <        assertWriteLockedBy(lock, Thread.currentThread());
730 >        assertWriteLockedByMoi(lock);
731          assertEquals(1, lock.getWriteHoldCount());
732          lock.writeLock().unlock();
733          awaitTermination(t1);
# Line 745 | Line 775 | public class ReentrantReadWriteLockTest
775      public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
776      public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
777      public void testWriteTryLock_Timeout(boolean fair) {
778 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
778 >        final PublicReentrantReadWriteLock lock =
779 >            new PublicReentrantReadWriteLock(fair);
780          lock.writeLock().lock();
781          Thread t = newStartedThread(new CheckedRunnable() {
782              public void realRun() throws InterruptedException {
# Line 756 | Line 787 | public class ReentrantReadWriteLockTest
787              }});
788  
789          awaitTermination(t);
790 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
760 <        lock.writeLock().unlock();
790 >        releaseWriteLock(lock);
791      }
792  
793      /**
# Line 782 | Line 812 | public class ReentrantReadWriteLockTest
812      }
813  
814      /**
815 <     * write lockInterruptibly succeeds if lock free else is interruptible
815 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
816       */
817      public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
818      public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
# Line 791 | Line 821 | public class ReentrantReadWriteLockTest
821              new PublicReentrantReadWriteLock(fair);
822          try {
823              lock.writeLock().lockInterruptibly();
824 <        } catch (Throwable t) {
825 <            threadUnexpectedException(t);
824 >        } catch (InterruptedException ie) {
825 >            threadUnexpectedException(ie);
826          }
827          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
828              public void realRun() throws InterruptedException {
# Line 801 | Line 831 | public class ReentrantReadWriteLockTest
831  
832          waitForQueuedThread(lock, t);
833          t.interrupt();
834 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
835          awaitTermination(t);
836          releaseWriteLock(lock);
837      }
# Line 817 | Line 848 | public class ReentrantReadWriteLockTest
848              lock.readLock().lockInterruptibly();
849              lock.readLock().unlock();
850              lock.writeLock().lockInterruptibly();
851 <        } catch (Throwable t) {
852 <            threadUnexpectedException(t);
851 >        } catch (InterruptedException ie) {
852 >            threadUnexpectedException(ie);
853          }
854          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
855              public void realRun() throws InterruptedException {
# Line 839 | Line 870 | public class ReentrantReadWriteLockTest
870      public void testAwait_IMSE(boolean fair) {
871          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
872          final Condition c = lock.writeLock().newCondition();
873 <        long startTime = System.nanoTime();
874 <        try {
844 <            try {
845 <                c.await();
846 <                shouldThrow();
847 <            } catch (IllegalMonitorStateException success) {}
848 <            try {
849 <                c.await(LONG_DELAY_MS, MILLISECONDS);
850 <                shouldThrow();
851 <            } catch (IllegalMonitorStateException success) {}
852 <            try {
853 <                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
854 <                shouldThrow();
855 <            } catch (IllegalMonitorStateException success) {}
873 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
874 >            long startTime = System.nanoTime();
875              try {
876 <                c.awaitUninterruptibly();
876 >                await(c, awaitMethod);
877                  shouldThrow();
878 <            } catch (IllegalMonitorStateException success) {}
879 <        } catch (Throwable t) {
880 <            threadUnexpectedException(t);
878 >            } catch (IllegalMonitorStateException success) {
879 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
880 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
881          }
863        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
882      }
883  
884      /**
# Line 1008 | Line 1026 | public class ReentrantReadWriteLockTest
1026          lock.writeLock().unlock();
1027          t.interrupt();
1028          long timeoutMillis = 10;
1029 <        assertThreadJoinTimesOut(t, timeoutMillis);
1029 >        assertThreadStaysAlive(t, timeoutMillis);
1030          lock.writeLock().lock();
1031          c.signal();
1032          lock.writeLock().unlock();
# Line 1020 | Line 1038 | public class ReentrantReadWriteLockTest
1038       */
1039      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1040      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1041 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1042 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1043      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1044      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1045      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 1032 | Line 1052 | public class ReentrantReadWriteLockTest
1052          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1053              public void realRun() throws InterruptedException {
1054                  lock.writeLock().lock();
1055 <                assertWriteLockedBy(lock, Thread.currentThread());
1055 >                assertWriteLockedByMoi(lock);
1056                  assertHasNoWaiters(lock, c);
1057                  locked.countDown();
1058                  try {
1059                      await(c, awaitMethod);
1060                  } finally {
1061 <                    assertWriteLockedBy(lock, Thread.currentThread());
1061 >                    assertWriteLockedByMoi(lock);
1062                      assertHasNoWaiters(lock, c);
1063                      lock.writeLock().unlock();
1064                      assertFalse(Thread.interrupted());
# Line 1057 | Line 1077 | public class ReentrantReadWriteLockTest
1077       */
1078      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1079      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1080 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1081 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1082      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1083      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1084      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 1090 | Line 1112 | public class ReentrantReadWriteLockTest
1112      }
1113  
1114      /**
1115 <     * signal wakes up waiting threads in FIFO order.
1115 >     * signal wakes up waiting threads in FIFO order
1116       */
1117      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1118      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1150 | Line 1172 | public class ReentrantReadWriteLockTest
1172          Thread t1 = newStartedThread(new CheckedRunnable() {
1173              public void realRun() throws InterruptedException {
1174                  lock.writeLock().lock();
1175 <                assertWriteLockedBy(lock, Thread.currentThread());
1175 >                assertWriteLockedByMoi(lock);
1176                  assertEquals(1, lock.writeLock().getHoldCount());
1177                  locked.countDown();
1178                  c.await();
1179 <                assertWriteLockedBy(lock, Thread.currentThread());
1179 >                assertWriteLockedByMoi(lock);
1180                  assertEquals(1, lock.writeLock().getHoldCount());
1181                  lock.writeLock().unlock();
1182              }});
# Line 1163 | Line 1185 | public class ReentrantReadWriteLockTest
1185              public void realRun() throws InterruptedException {
1186                  lock.writeLock().lock();
1187                  lock.writeLock().lock();
1188 <                assertWriteLockedBy(lock, Thread.currentThread());
1188 >                assertWriteLockedByMoi(lock);
1189                  assertEquals(2, lock.writeLock().getHoldCount());
1190                  locked.countDown();
1191                  c.await();
1192 <                assertWriteLockedBy(lock, Thread.currentThread());
1192 >                assertWriteLockedByMoi(lock);
1193                  assertEquals(2, lock.writeLock().getHoldCount());
1194                  lock.writeLock().unlock();
1195                  lock.writeLock().unlock();
# Line 1205 | Line 1227 | public class ReentrantReadWriteLockTest
1227          assertEquals(1, clone.getReadLockCount());
1228          clone.readLock().unlock();
1229          clone.writeLock().unlock();
1230 +        assertFalse(clone.isWriteLocked());
1231 +        assertEquals(1, lock.getReadLockCount());
1232 +        assertEquals(0, clone.getReadLockCount());
1233      }
1234  
1235      /**
# Line 1248 | Line 1273 | public class ReentrantReadWriteLockTest
1273      }
1274  
1275      /**
1276 <     * hasQueuedThread reports whether a thread is queued.
1276 >     * hasQueuedThread reports whether a thread is queued
1277       */
1278      public void testHasQueuedThread()      { testHasQueuedThread(false); }
1279      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 1589 | Line 1614 | public class ReentrantReadWriteLockTest
1614      public void testToString_fair() { testToString(true); }
1615      public void testToString(boolean fair) {
1616          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1617 <        String us = lock.toString();
1618 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1619 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1620 <        lock.writeLock().lock();
1621 <        String ws = lock.toString();
1597 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1598 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1617 >        assertTrue(lock.toString().contains("Write locks = 0"));
1618 >        assertTrue(lock.toString().contains("Read locks = 0"));
1619 >        lock.writeLock().lock();
1620 >        assertTrue(lock.toString().contains("Write locks = 1"));
1621 >        assertTrue(lock.toString().contains("Read locks = 0"));
1622          lock.writeLock().unlock();
1623          lock.readLock().lock();
1624          lock.readLock().lock();
1625 <        String rs = lock.toString();
1626 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1604 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1625 >        assertTrue(lock.toString().contains("Write locks = 0"));
1626 >        assertTrue(lock.toString().contains("Read locks = 2"));
1627      }
1628  
1629      /**
# Line 1611 | Line 1633 | public class ReentrantReadWriteLockTest
1633      public void testReadLockToString_fair() { testReadLockToString(true); }
1634      public void testReadLockToString(boolean fair) {
1635          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1636 <        String us = lock.readLock().toString();
1615 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1636 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1637          lock.readLock().lock();
1638          lock.readLock().lock();
1639 <        String rs = lock.readLock().toString();
1619 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1639 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1640      }
1641  
1642      /**
# Line 1626 | Line 1646 | public class ReentrantReadWriteLockTest
1646      public void testWriteLockToString_fair() { testWriteLockToString(true); }
1647      public void testWriteLockToString(boolean fair) {
1648          ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1649 <        String us = lock.writeLock().toString();
1630 <        assertTrue(us.indexOf("Unlocked") >= 0);
1649 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1650          lock.writeLock().lock();
1651 <        String ls = lock.writeLock().toString();
1652 <        assertTrue(ls.indexOf("Locked") >= 0);
1651 >        assertTrue(lock.writeLock().toString().contains("Locked"));
1652 >        lock.writeLock().unlock();
1653 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1654      }
1655  
1656   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines