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.58 by jsr166, Sat May 7 14:43:13 2011 UTC vs.
Revision 1.59 by jsr166, Sat May 7 19:03:26 2011 UTC

# Line 287 | Line 287 | public class ReentrantReadWriteLockTest
287          lock.writeLock().lock();
288          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
289              public void realRun() throws InterruptedException {
290 <                lock.writeLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
290 >                lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
291              }});
292  
293          waitForQueuedThread(lock, t);
# Line 321 | Line 321 | public class ReentrantReadWriteLockTest
321          lock.writeLock().lock();
322          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
323              public void realRun() throws InterruptedException {
324 <                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
324 >                lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
325              }});
326  
327          waitForQueuedThread(lock, t);
# Line 774 | Line 774 | public class ReentrantReadWriteLockTest
774          lock.writeLock().lock();
775          Thread t = newStartedThread(new CheckedRunnable() {
776              public void realRun() throws InterruptedException {
777 <                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
777 >                long startTime = System.nanoTime();
778 >                long timeoutMillis = 10;
779 >                assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
780 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
781              }});
782  
783          awaitTermination(t);
# Line 790 | Line 793 | public class ReentrantReadWriteLockTest
793          lock.writeLock().lock();
794          Thread t = newStartedThread(new CheckedRunnable() {
795              public void realRun() throws InterruptedException {
796 <                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
796 >                long startTime = System.nanoTime();
797 >                long timeoutMillis = 10;
798 >                assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
799 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
800              }});
801  
802          awaitTermination(t);
# Line 917 | Line 923 | public class ReentrantReadWriteLockTest
923          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
924          final Condition c = lock.writeLock().newCondition();
925          lock.writeLock().lock();
926 +        long startTime = System.nanoTime();
927 +        long timeoutMillis = 10;
928          java.util.Date d = new java.util.Date();
929 <        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
929 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
930 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
931          lock.writeLock().unlock();
932      }
933  
# Line 926 | Line 935 | public class ReentrantReadWriteLockTest
935       * await returns when signalled
936       */
937      public void testAwait() throws InterruptedException {
938 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
939          final Condition c = lock.writeLock().newCondition();
940          final CountDownLatch locked = new CountDownLatch(1);
941          Thread t = newStartedThread(new CheckedRunnable() {
# Line 939 | Line 948 | public class ReentrantReadWriteLockTest
948  
949          locked.await();
950          lock.writeLock().lock();
951 +        assertHasWaiters(lock, c, t);
952          c.signal();
953 +        assertHasNoWaiters(lock, c);
954          assertTrue(t.isAlive());
955          lock.writeLock().unlock();
956          awaitTermination(t);
# Line 952 | Line 963 | public class ReentrantReadWriteLockTest
963          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
964          final Condition c = lock.writeLock().newCondition();
965          final CountDownLatch locked = new CountDownLatch(1);
955        final AtomicBoolean canAwake = new AtomicBoolean(false);
966          Thread t = newStartedThread(new CheckedRunnable() {
967              public void realRun() {
968                  lock.writeLock().lock();
# Line 966 | Line 976 | public class ReentrantReadWriteLockTest
976          lock.writeLock().lock();
977          lock.writeLock().unlock();
978          t.interrupt();
979 <        t.join(10);
980 <        assertTrue(t.isAlive());
979 >        long timeoutMillis = 10;
980 >        assertThreadJoinTimesOut(t, timeoutMillis);
981          lock.writeLock().lock();
982          c.signal();
983          lock.writeLock().unlock();
# Line 1018 | Line 1028 | public class ReentrantReadWriteLockTest
1028                  assertHasNoWaiters(lock, c);
1029                  locked.countDown();
1030                  try {
1031 <                    c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
1031 >                    c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
1032                  } finally {
1033                      assertWriteLockedBy(lock, Thread.currentThread());
1034                      assertHasNoWaiters(lock, c);
# Line 1049 | Line 1059 | public class ReentrantReadWriteLockTest
1059                  locked.countDown();
1060                  java.util.Date d = new java.util.Date();
1061                  try {
1062 <                    c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1062 >                    c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS));
1063                  } finally {
1064                      assertWriteLockedBy(lock, Thread.currentThread());
1065                      assertHasNoWaiters(lock, c);
# Line 1145 | Line 1155 | public class ReentrantReadWriteLockTest
1155      }
1156  
1157      /**
1158 +     * await after multiple reentrant locking preserves lock count
1159 +     */
1160 +    public void testAwaitLockCount() throws InterruptedException {
1161 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1162 +        final Condition c = lock.writeLock().newCondition();
1163 +        final CountDownLatch locked = new CountDownLatch(2);
1164 +        Thread t1 = newStartedThread(new CheckedRunnable() {
1165 +            public void realRun() throws InterruptedException {
1166 +                lock.writeLock().lock();
1167 +                assertWriteLockedBy(lock, Thread.currentThread());
1168 +                assertEquals(1, lock.writeLock().getHoldCount());
1169 +                locked.countDown();
1170 +                c.await();
1171 +                assertWriteLockedBy(lock, Thread.currentThread());
1172 +                assertEquals(1, lock.writeLock().getHoldCount());
1173 +                lock.writeLock().unlock();
1174 +            }});
1175 +
1176 +        Thread t2 = newStartedThread(new CheckedRunnable() {
1177 +            public void realRun() throws InterruptedException {
1178 +                lock.writeLock().lock();
1179 +                lock.writeLock().lock();
1180 +                assertWriteLockedBy(lock, Thread.currentThread());
1181 +                assertEquals(2, lock.writeLock().getHoldCount());
1182 +                locked.countDown();
1183 +                c.await();
1184 +                assertWriteLockedBy(lock, Thread.currentThread());
1185 +                assertEquals(2, lock.writeLock().getHoldCount());
1186 +                lock.writeLock().unlock();
1187 +                lock.writeLock().unlock();
1188 +            }});
1189 +
1190 +        locked.await();
1191 +        lock.writeLock().lock();
1192 +        assertHasWaiters(lock, c, t1, t2);
1193 +        c.signalAll();
1194 +        assertHasNoWaiters(lock, c);
1195 +        lock.writeLock().unlock();
1196 +        awaitTermination(t1);
1197 +        awaitTermination(t2);
1198 +    }
1199 +
1200 +    /**
1201       * A serialized lock deserializes as unlocked
1202       */
1203      public void testSerialization() throws Exception {
# Line 1167 | Line 1220 | public class ReentrantReadWriteLockTest
1220      /**
1221       * hasQueuedThreads reports whether there are waiting threads
1222       */
1223 <    public void testhasQueuedThreads() throws InterruptedException {
1223 >    public void testHasQueuedThreads() throws InterruptedException {
1224          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1225          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1226          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1227          assertFalse(lock.hasQueuedThreads());
1228          lock.writeLock().lock();
1229          assertFalse(lock.hasQueuedThreads());
1177        long startTime = System.nanoTime();
1230          t1.start();
1231          waitForQueuedThread(lock, t1);
1232 +        assertTrue(lock.hasQueuedThreads());
1233          t2.start();
1234          waitForQueuedThread(lock, t2);
1235          assertTrue(lock.hasQueuedThreads());
# Line 1192 | Line 1245 | public class ReentrantReadWriteLockTest
1245       * hasQueuedThread(null) throws NPE
1246       */
1247      public void testHasQueuedThreadNPE() {
1248 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1248 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1249          try {
1250 <            sync.hasQueuedThread(null);
1250 >            lock.hasQueuedThread(null);
1251              shouldThrow();
1252          } catch (NullPointerException success) {}
1253      }
# Line 1209 | Line 1262 | public class ReentrantReadWriteLockTest
1262          assertFalse(lock.hasQueuedThread(t1));
1263          assertFalse(lock.hasQueuedThread(t2));
1264          lock.writeLock().lock();
1212        long startTime = System.nanoTime();
1265          t1.start();
1266          waitForQueuedThread(lock, t1);
1267          assertTrue(lock.hasQueuedThread(t1));
# Line 1237 | Line 1289 | public class ReentrantReadWriteLockTest
1289          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1290          assertEquals(0, lock.getQueueLength());
1291          lock.writeLock().lock();
1240        long startTime = System.nanoTime();
1292          t1.start();
1293          waitForQueuedThread(lock, t1);
1294          assertEquals(1, lock.getQueueLength());
# Line 1261 | Line 1312 | public class ReentrantReadWriteLockTest
1312          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1313          assertTrue(lock.getQueuedThreads().isEmpty());
1314          lock.writeLock().lock();
1264        long startTime = System.nanoTime();
1315          assertTrue(lock.getQueuedThreads().isEmpty());
1316          t1.start();
1317          waitForQueuedThread(lock, t1);
# Line 1316 | Line 1366 | public class ReentrantReadWriteLockTest
1366      }
1367  
1368      /**
1369 <     * hasWaiters throws IAE if not owned
1369 >     * hasWaiters throws IllegalArgumentException if not owned
1370       */
1371      public void testHasWaitersIAE() {
1372          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1329 | Line 1379 | public class ReentrantReadWriteLockTest
1379      }
1380  
1381      /**
1382 <     * hasWaiters throws IMSE if not locked
1382 >     * hasWaiters throws IllegalMonitorStateException if not locked
1383       */
1384      public void testHasWaitersIMSE() {
1385          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1341 | Line 1391 | public class ReentrantReadWriteLockTest
1391      }
1392  
1393      /**
1394 <     * getWaitQueueLength throws IAE if not owned
1394 >     * getWaitQueueLength throws IllegalArgumentException if not owned
1395       */
1396      public void testGetWaitQueueLengthIAE() {
1397          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1354 | Line 1404 | public class ReentrantReadWriteLockTest
1404      }
1405  
1406      /**
1407 <     * getWaitQueueLength throws IMSE if not locked
1407 >     * getWaitQueueLength throws IllegalMonitorStateException if not locked
1408       */
1409      public void testGetWaitQueueLengthIMSE() {
1410          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 1366 | Line 1416 | public class ReentrantReadWriteLockTest
1416      }
1417  
1418      /**
1419 <     * getWaitingThreads throws IAE if not owned
1419 >     * getWaitingThreads throws IllegalArgumentException if not owned
1420       */
1421      public void testGetWaitingThreadsIAE() {
1422          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
# Line 1379 | Line 1429 | public class ReentrantReadWriteLockTest
1429      }
1430  
1431      /**
1432 <     * getWaitingThreads throws IMSE if not locked
1432 >     * getWaitingThreads throws IllegalMonitorStateException if not locked
1433       */
1434      public void testGetWaitingThreadsIMSE() {
1435          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
# Line 1400 | Line 1450 | public class ReentrantReadWriteLockTest
1450          Thread t = newStartedThread(new CheckedRunnable() {
1451              public void realRun() throws InterruptedException {
1452                  lock.writeLock().lock();
1453 +                assertHasNoWaiters(lock, c);
1454                  assertFalse(lock.hasWaiters(c));
1455                  locked.countDown();
1405                assertEquals(0, lock.getWaitQueueLength(c));
1456                  c.await();
1457 +                assertHasNoWaiters(lock, c);
1458 +                assertFalse(lock.hasWaiters(c));
1459                  lock.writeLock().unlock();
1460              }});
1461  
1462          locked.await();
1463          lock.writeLock().lock();
1464 +        assertHasWaiters(lock, c, t);
1465          assertTrue(lock.hasWaiters(c));
1413        assertEquals(1, lock.getWaitQueueLength(c));
1466          c.signal();
1467          assertHasNoWaiters(lock, c);
1468 +        assertFalse(lock.hasWaiters(c));
1469          lock.writeLock().unlock();
1470          awaitTermination(t);
1471          assertHasNoWaiters(lock, c);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines