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.74 by jsr166, Sun May 24 01:42:14 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, awaitTimed, awaitNanos, awaitUntil };
155 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
156  
157      /**
158       * Awaits condition using the specified AwaitMethod.
# Line 167 | 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 227 | Line 236 | public class ReentrantReadWriteLockTest
236          }
237          for (int i = SIZE; i > 0; i--) {
238              lock.writeLock().unlock();
239 <            assertEquals(i-1,lock.getWriteHoldCount());
239 >            assertEquals(i - 1,lock.getWriteHoldCount());
240          }
241      }
242  
# Line 244 | Line 253 | public class ReentrantReadWriteLockTest
253          }
254          for (int i = SIZE; i > 0; i--) {
255              lock.writeLock().unlock();
256 <            assertEquals(i-1,lock.writeLock().getHoldCount());
256 >            assertEquals(i - 1,lock.writeLock().getHoldCount());
257          }
258      }
259  
# Line 261 | Line 270 | public class ReentrantReadWriteLockTest
270          }
271          for (int i = SIZE; i > 0; i--) {
272              lock.readLock().unlock();
273 <            assertEquals(i-1,lock.getReadHoldCount());
273 >            assertEquals(i - 1,lock.getReadHoldCount());
274          }
275      }
276  
# Line 507 | 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 584 | 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 820 | Line 830 | public class ReentrantReadWriteLockTest
830              new PublicReentrantReadWriteLock(fair);
831          try {
832              lock.writeLock().lockInterruptibly();
833 <        } catch (InterruptedException ie) {
824 <            threadUnexpectedException(ie);
825 <        }
833 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
834          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
835              public void realRun() throws InterruptedException {
836                  lock.writeLock().lockInterruptibly();
# Line 847 | Line 855 | public class ReentrantReadWriteLockTest
855              lock.readLock().lockInterruptibly();
856              lock.readLock().unlock();
857              lock.writeLock().lockInterruptibly();
858 <        } catch (InterruptedException ie) {
851 <            threadUnexpectedException(ie);
852 <        }
858 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
859          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
860              public void realRun() throws InterruptedException {
861                  lock.readLock().lockInterruptibly();
# Line 875 | Line 881 | public class ReentrantReadWriteLockTest
881                  await(c, awaitMethod);
882                  shouldThrow();
883              } catch (IllegalMonitorStateException success) {
884 <            } catch (InterruptedException e) { threadUnexpectedException(e); }
884 >            } catch (InterruptedException fail) {
885 >                threadUnexpectedException(fail);
886 >            }
887              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
888          }
889      }
# Line 926 | Line 934 | public class ReentrantReadWriteLockTest
934              assertTrue(nanosRemaining <= 0);
935              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
936              lock.writeLock().unlock();
937 <        } catch (InterruptedException e) {
930 <            threadUnexpectedException(e);
931 <        }
937 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
938      }
939  
940      /**
# Line 947 | Line 953 | public class ReentrantReadWriteLockTest
953              assertFalse(c.await(timeoutMillis, MILLISECONDS));
954              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
955              lock.writeLock().unlock();
956 <        } catch (InterruptedException e) {
951 <            threadUnexpectedException(e);
952 <        }
956 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
957      }
958  
959      /**
# Line 969 | 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) {
973 <            threadUnexpectedException(e);
974 <        }
976 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
977      }
978  
979      /**
# Line 1003 | 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 1111 | 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 1272 | 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 1613 | 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();
1621 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1622 <        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);
1628 <        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 1635 | 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();
1639 <        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();
1643 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1657 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1658      }
1659  
1660      /**
# Line 1650 | 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();
1654 <        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