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.17 by dl, Thu Jan 8 01:29:46 2004 UTC vs.
Revision 1.21 by dl, Thu Jan 20 00:39:13 2005 UTC

# Line 376 | Line 376 | public class ReentrantReadWriteLockTest
376          }
377      }
378  
379 +    /**
380 +     * Read trylock succeeds if write locked by current thread
381 +     */
382 +    public void testReadHoldingWriteLock() {
383 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 +        lock.writeLock().lock();
385 +        assertTrue(lock.readLock().tryLock());
386 +        lock.readLock().unlock();
387 +        lock.writeLock().unlock();
388 +    }
389 +
390 +    /**
391 +     * Read lock succeeds if write locked by current thread even if
392 +     * other threads are waiting
393 +     */
394 +    public void testReadHoldingWriteLock2() {
395 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
396 +        lock.writeLock().lock();
397 +        Thread t1 = new Thread(new Runnable() {
398 +                public void run() {
399 +                    lock.readLock().lock();
400 +                    lock.readLock().unlock();
401 +                }
402 +            });
403 +        Thread t2 = new Thread(new Runnable() {
404 +                public void run() {
405 +                    lock.readLock().lock();
406 +                    lock.readLock().unlock();
407 +                }
408 +            });
409 +
410 +        try {
411 +            t1.start();
412 +            t2.start();
413 +            lock.readLock().lock();
414 +            lock.readLock().unlock();
415 +            Thread.sleep(SHORT_DELAY_MS);
416 +            lock.readLock().lock();
417 +            lock.readLock().unlock();
418 +            lock.writeLock().unlock();
419 +            t1.join(MEDIUM_DELAY_MS);
420 +            t2.join(MEDIUM_DELAY_MS);
421 +            assertTrue(!t1.isAlive());
422 +            assertTrue(!t2.isAlive());
423 +          
424 +        } catch(Exception e){
425 +            unexpectedException();
426 +        }
427 +    }
428 +
429 +    /**
430 +     * Fair Read trylock succeeds if write locked by current thread
431 +     */
432 +    public void testReadHoldingWriteLockFair() {
433 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
434 +        lock.writeLock().lock();
435 +        assertTrue(lock.readLock().tryLock());
436 +        lock.readLock().unlock();
437 +        lock.writeLock().unlock();
438 +    }
439 +
440 +    /**
441 +     * Fair Read lock succeeds if write locked by current thread even if
442 +     * other threads are waiting
443 +     */
444 +    public void testReadHoldingWriteLockFair2() {
445 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
446 +        lock.writeLock().lock();
447 +        Thread t1 = new Thread(new Runnable() {
448 +                public void run() {
449 +                    lock.readLock().lock();
450 +                    lock.readLock().unlock();
451 +                }
452 +            });
453 +        Thread t2 = new Thread(new Runnable() {
454 +                public void run() {
455 +                    lock.readLock().lock();
456 +                    lock.readLock().unlock();
457 +                }
458 +            });
459 +
460 +        try {
461 +            t1.start();
462 +            t2.start();
463 +            lock.readLock().lock();
464 +            lock.readLock().unlock();
465 +            Thread.sleep(SHORT_DELAY_MS);
466 +            lock.readLock().lock();
467 +            lock.readLock().unlock();
468 +            lock.writeLock().unlock();
469 +            t1.join(MEDIUM_DELAY_MS);
470 +            t2.join(MEDIUM_DELAY_MS);
471 +            assertTrue(!t1.isAlive());
472 +            assertTrue(!t2.isAlive());
473 +          
474 +        } catch(Exception e){
475 +            unexpectedException();
476 +        }
477 +    }
478 +
479  
480      /**
481       * Read tryLock succeeds if readlocked but not writelocked
# Line 591 | Line 691 | public class ReentrantReadWriteLockTest
691          final Condition c = lock.writeLock().newCondition();
692          try {
693              lock.writeLock().lock();
594            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
694              lock.writeLock().unlock();
695          }
696          catch (Exception ex) {
# Line 608 | Line 707 | public class ReentrantReadWriteLockTest
707          try {
708              lock.writeLock().lock();
709              java.util.Date d = new java.util.Date();
611            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
710              lock.writeLock().unlock();
711          }
712          catch (Exception ex) {
# Line 877 | Line 975 | public class ReentrantReadWriteLockTest
975      }
976  
977      /**
978 +     * hasQueuedThread(null) throws NPE
979 +     */
980 +    public void testHasQueuedThreadNPE() {
981 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
982 +        try {
983 +            sync.hasQueuedThread(null);
984 +            shouldThrow();
985 +        } catch (NullPointerException success) {
986 +        }
987 +    }
988 +
989 +    /**
990 +     * hasQueuedThread reports whether a thread is queued.
991 +     */
992 +    public void testHasQueuedThread() {
993 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
994 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
995 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
996 +        try {
997 +            assertFalse(sync.hasQueuedThread(t1));
998 +            assertFalse(sync.hasQueuedThread(t2));
999 +            sync.writeLock().lock();
1000 +            t1.start();
1001 +            Thread.sleep(SHORT_DELAY_MS);
1002 +            assertTrue(sync.hasQueuedThread(t1));
1003 +            t2.start();
1004 +            Thread.sleep(SHORT_DELAY_MS);
1005 +            assertTrue(sync.hasQueuedThread(t1));
1006 +            assertTrue(sync.hasQueuedThread(t2));
1007 +            t1.interrupt();
1008 +            Thread.sleep(SHORT_DELAY_MS);
1009 +            assertFalse(sync.hasQueuedThread(t1));
1010 +            assertTrue(sync.hasQueuedThread(t2));
1011 +            sync.writeLock().unlock();
1012 +            Thread.sleep(SHORT_DELAY_MS);
1013 +            assertFalse(sync.hasQueuedThread(t1));
1014 +            Thread.sleep(SHORT_DELAY_MS);
1015 +            assertFalse(sync.hasQueuedThread(t2));
1016 +            t1.join();
1017 +            t2.join();
1018 +        } catch(Exception e){
1019 +            unexpectedException();
1020 +        }
1021 +    }
1022 +
1023 +
1024 +    /**
1025       * getQueueLength reports number of waiting threads
1026       */
1027      public void testGetQueueLength() {
# Line 1224 | Line 1369 | public class ReentrantReadWriteLockTest
1369          }
1370      }
1371  
1372 +    /**
1373 +     * toString indicates current lock state
1374 +     */
1375 +    public void testToString() {
1376 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1377 +        String us = lock.toString();
1378 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1379 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1380 +        lock.writeLock().lock();
1381 +        String ws = lock.toString();
1382 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1383 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1384 +        lock.writeLock().unlock();
1385 +        lock.readLock().lock();
1386 +        lock.readLock().lock();
1387 +        String rs = lock.toString();
1388 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1389 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1390 +    }
1391 +
1392 +    /**
1393 +     * readLock.toString indicates current lock state
1394 +     */
1395 +    public void testReadLockToString() {
1396 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1397 +        String us = lock.readLock().toString();
1398 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1399 +        lock.readLock().lock();
1400 +        lock.readLock().lock();
1401 +        String rs = lock.readLock().toString();
1402 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1403 +    }
1404 +
1405 +    /**
1406 +     * writeLock.toString indicates current lock state
1407 +     */
1408 +    public void testWriteLockToString() {
1409 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1410 +        String us = lock.writeLock().toString();
1411 +        assertTrue(us.indexOf("Unlocked") >= 0);
1412 +        lock.writeLock().lock();
1413 +        String ls = lock.writeLock().toString();
1414 +        assertTrue(ls.indexOf("Locked") >= 0);
1415 +    }
1416 +
1417   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines