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.18 by dl, Sat Jan 10 01:41:59 2004 UTC vs.
Revision 1.24 by dl, Tue Aug 2 11:34:04 2005 UTC

# Line 126 | Line 126 | public class ReentrantReadWriteLockTest
126      /**
127       * getWriteHoldCount returns number of recursive holds
128       */
129 <    public void testGetHoldCount() {
129 >    public void testGetWriteHoldCount() {
130          ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
131          for(int i = 1; i <= SIZE; i++) {
132              lock.writeLock().lock();
# Line 137 | Line 137 | public class ReentrantReadWriteLockTest
137              assertEquals(i-1,lock.getWriteHoldCount());
138          }
139      }
140 +
141 +    /**
142 +     * getReadHoldCount returns number of recursive holds
143 +     */
144 +    public void testGetReadHoldCount() {
145 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
146 +        for(int i = 1; i <= SIZE; i++) {
147 +            lock.readLock().lock();
148 +            assertEquals(i,lock.getReadHoldCount());
149 +        }
150 +        for(int i = SIZE; i > 0; i--) {
151 +            lock.readLock().unlock();
152 +            assertEquals(i-1,lock.getReadHoldCount());
153 +        }
154 +    }
155      
156  
157      /**
# Line 376 | Line 391 | public class ReentrantReadWriteLockTest
391          }
392      }
393  
394 +    /**
395 +     * Read trylock succeeds if write locked by current thread
396 +     */
397 +    public void testReadHoldingWriteLock() {
398 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
399 +        lock.writeLock().lock();
400 +        assertTrue(lock.readLock().tryLock());
401 +        lock.readLock().unlock();
402 +        lock.writeLock().unlock();
403 +    }
404 +
405 +    /**
406 +     * Read lock succeeds if write locked by current thread even if
407 +     * other threads are waiting for readlock
408 +     */
409 +    public void testReadHoldingWriteLock2() {
410 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
411 +        lock.writeLock().lock();
412 +        Thread t1 = new Thread(new Runnable() {
413 +                public void run() {
414 +                    lock.readLock().lock();
415 +                    lock.readLock().unlock();
416 +                }
417 +            });
418 +        Thread t2 = new Thread(new Runnable() {
419 +                public void run() {
420 +                    lock.readLock().lock();
421 +                    lock.readLock().unlock();
422 +                }
423 +            });
424 +
425 +        try {
426 +            t1.start();
427 +            t2.start();
428 +            lock.readLock().lock();
429 +            lock.readLock().unlock();
430 +            Thread.sleep(SHORT_DELAY_MS);
431 +            lock.readLock().lock();
432 +            lock.readLock().unlock();
433 +            lock.writeLock().unlock();
434 +            t1.join(MEDIUM_DELAY_MS);
435 +            t2.join(MEDIUM_DELAY_MS);
436 +            assertTrue(!t1.isAlive());
437 +            assertTrue(!t2.isAlive());
438 +          
439 +        } catch(Exception e){
440 +            unexpectedException();
441 +        }
442 +    }
443 +
444 +    /**
445 +     *  Read lock succeeds if write locked by current thread even if
446 +     * other threads are waiting for writelock
447 +     */
448 +    public void testReadHoldingWriteLock3() {
449 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
450 +        lock.writeLock().lock();
451 +        Thread t1 = new Thread(new Runnable() {
452 +                public void run() {
453 +                    lock.writeLock().lock();
454 +                    lock.writeLock().unlock();
455 +                }
456 +            });
457 +        Thread t2 = new Thread(new Runnable() {
458 +                public void run() {
459 +                    lock.writeLock().lock();
460 +                    lock.writeLock().unlock();
461 +                }
462 +            });
463 +
464 +        try {
465 +            t1.start();
466 +            t2.start();
467 +            lock.readLock().lock();
468 +            lock.readLock().unlock();
469 +            Thread.sleep(SHORT_DELAY_MS);
470 +            lock.readLock().lock();
471 +            lock.readLock().unlock();
472 +            lock.writeLock().unlock();
473 +            t1.join(MEDIUM_DELAY_MS);
474 +            t2.join(MEDIUM_DELAY_MS);
475 +            assertTrue(!t1.isAlive());
476 +            assertTrue(!t2.isAlive());
477 +          
478 +        } catch(Exception e){
479 +            unexpectedException();
480 +        }
481 +    }
482 +
483 +
484 +    /**
485 +     *  Write lock succeeds if write locked by current thread even if
486 +     * other threads are waiting for writelock
487 +     */
488 +    public void testWriteHoldingWriteLock4() {
489 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
490 +        lock.writeLock().lock();
491 +        Thread t1 = new Thread(new Runnable() {
492 +                public void run() {
493 +                    lock.writeLock().lock();
494 +                    lock.writeLock().unlock();
495 +                }
496 +            });
497 +        Thread t2 = new Thread(new Runnable() {
498 +                public void run() {
499 +                    lock.writeLock().lock();
500 +                    lock.writeLock().unlock();
501 +                }
502 +            });
503 +
504 +        try {
505 +            t1.start();
506 +            t2.start();
507 +            lock.writeLock().lock();
508 +            lock.writeLock().unlock();
509 +            Thread.sleep(SHORT_DELAY_MS);
510 +            lock.writeLock().lock();
511 +            lock.writeLock().unlock();
512 +            lock.writeLock().unlock();
513 +            t1.join(MEDIUM_DELAY_MS);
514 +            t2.join(MEDIUM_DELAY_MS);
515 +            assertTrue(!t1.isAlive());
516 +            assertTrue(!t2.isAlive());
517 +          
518 +        } catch(Exception e){
519 +            unexpectedException();
520 +        }
521 +    }
522 +
523 +
524 +    /**
525 +     * Fair Read trylock succeeds if write locked by current thread
526 +     */
527 +    public void testReadHoldingWriteLockFair() {
528 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
529 +        lock.writeLock().lock();
530 +        assertTrue(lock.readLock().tryLock());
531 +        lock.readLock().unlock();
532 +        lock.writeLock().unlock();
533 +    }
534 +
535 +    /**
536 +     * Fair Read lock succeeds if write locked by current thread even if
537 +     * other threads are waiting for readlock
538 +     */
539 +    public void testReadHoldingWriteLockFair2() {
540 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
541 +        lock.writeLock().lock();
542 +        Thread t1 = new Thread(new Runnable() {
543 +                public void run() {
544 +                    lock.readLock().lock();
545 +                    lock.readLock().unlock();
546 +                }
547 +            });
548 +        Thread t2 = new Thread(new Runnable() {
549 +                public void run() {
550 +                    lock.readLock().lock();
551 +                    lock.readLock().unlock();
552 +                }
553 +            });
554 +
555 +        try {
556 +            t1.start();
557 +            t2.start();
558 +            lock.readLock().lock();
559 +            lock.readLock().unlock();
560 +            Thread.sleep(SHORT_DELAY_MS);
561 +            lock.readLock().lock();
562 +            lock.readLock().unlock();
563 +            lock.writeLock().unlock();
564 +            t1.join(MEDIUM_DELAY_MS);
565 +            t2.join(MEDIUM_DELAY_MS);
566 +            assertTrue(!t1.isAlive());
567 +            assertTrue(!t2.isAlive());
568 +          
569 +        } catch(Exception e){
570 +            unexpectedException();
571 +        }
572 +    }
573 +
574 +
575 +    /**
576 +     * Fair Read lock succeeds if write locked by current thread even if
577 +     * other threads are waiting for writelock
578 +     */
579 +    public void testReadHoldingWriteLockFair3() {
580 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
581 +        lock.writeLock().lock();
582 +        Thread t1 = new Thread(new Runnable() {
583 +                public void run() {
584 +                    lock.writeLock().lock();
585 +                    lock.writeLock().unlock();
586 +                }
587 +            });
588 +        Thread t2 = new Thread(new Runnable() {
589 +                public void run() {
590 +                    lock.writeLock().lock();
591 +                    lock.writeLock().unlock();
592 +                }
593 +            });
594 +
595 +        try {
596 +            t1.start();
597 +            t2.start();
598 +            lock.readLock().lock();
599 +            lock.readLock().unlock();
600 +            Thread.sleep(SHORT_DELAY_MS);
601 +            lock.readLock().lock();
602 +            lock.readLock().unlock();
603 +            lock.writeLock().unlock();
604 +            t1.join(MEDIUM_DELAY_MS);
605 +            t2.join(MEDIUM_DELAY_MS);
606 +            assertTrue(!t1.isAlive());
607 +            assertTrue(!t2.isAlive());
608 +          
609 +        } catch(Exception e){
610 +            unexpectedException();
611 +        }
612 +    }
613 +
614 +
615 +    /**
616 +     * Fair Write lock succeeds if write locked by current thread even if
617 +     * other threads are waiting for writelock
618 +     */
619 +    public void testWriteHoldingWriteLockFair4() {
620 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
621 +        lock.writeLock().lock();
622 +        Thread t1 = new Thread(new Runnable() {
623 +                public void run() {
624 +                    lock.writeLock().lock();
625 +                    lock.writeLock().unlock();
626 +                }
627 +            });
628 +        Thread t2 = new Thread(new Runnable() {
629 +                public void run() {
630 +                    lock.writeLock().lock();
631 +                    lock.writeLock().unlock();
632 +                }
633 +            });
634 +
635 +        try {
636 +            t1.start();
637 +            t2.start();
638 +            Thread.sleep(SHORT_DELAY_MS);
639 +            assertTrue(lock.isWriteLockedByCurrentThread());
640 +            assertTrue(lock.getWriteHoldCount() == 1);
641 +            lock.writeLock().lock();
642 +            assertTrue(lock.getWriteHoldCount() == 2);
643 +            lock.writeLock().unlock();
644 +            lock.writeLock().lock();
645 +            lock.writeLock().unlock();
646 +            lock.writeLock().unlock();
647 +            t1.join(MEDIUM_DELAY_MS);
648 +            t2.join(MEDIUM_DELAY_MS);
649 +            assertTrue(!t1.isAlive());
650 +            assertTrue(!t2.isAlive());
651 +          
652 +        } catch(Exception e){
653 +            unexpectedException();
654 +        }
655 +    }
656 +
657  
658      /**
659       * Read tryLock succeeds if readlocked but not writelocked
# Line 420 | Line 698 | public class ReentrantReadWriteLockTest
698          }
699      }
700  
701 +
702 +    /**
703 +     * Fair Read tryLock succeeds if readlocked but not writelocked
704 +     */
705 +    public void testTryLockWhenReadLockedFair() {
706 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
707 +        lock.readLock().lock();
708 +        Thread t = new Thread(new Runnable() {
709 +                public void run() {
710 +                    threadAssertTrue(lock.readLock().tryLock());
711 +                    lock.readLock().unlock();
712 +                }
713 +            });
714 +        try {
715 +            t.start();
716 +            t.join();
717 +            lock.readLock().unlock();
718 +        } catch(Exception e){
719 +            unexpectedException();
720 +        }
721 +    }
722 +
723 +    
724 +
725 +    /**
726 +     * Fair write tryLock fails when readlocked
727 +     */
728 +    public void testWriteTryLockWhenReadLockedFair() {
729 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
730 +        lock.readLock().lock();
731 +        Thread t = new Thread(new Runnable() {
732 +                public void run() {
733 +                    threadAssertFalse(lock.writeLock().tryLock());
734 +                }
735 +            });
736 +        try {
737 +            t.start();
738 +            t.join();
739 +            lock.readLock().unlock();
740 +        } catch(Exception e){
741 +            unexpectedException();
742 +        }
743 +    }
744 +
745      
746  
747      /**
# Line 591 | Line 913 | public class ReentrantReadWriteLockTest
913          final Condition c = lock.writeLock().newCondition();
914          try {
915              lock.writeLock().lock();
594            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
916              lock.writeLock().unlock();
917          }
918          catch (Exception ex) {
# Line 608 | Line 929 | public class ReentrantReadWriteLockTest
929          try {
930              lock.writeLock().lock();
931              java.util.Date d = new java.util.Date();
611            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
932              lock.writeLock().unlock();
933          }
934          catch (Exception ex) {
# Line 649 | Line 969 | public class ReentrantReadWriteLockTest
969          }
970      }
971  
972 +    /** A helper class for uninterruptible wait tests */
973 +    class UninterruptableThread extends Thread {
974 +        private Lock lock;
975 +        private Condition c;
976 +        
977 +        public volatile boolean canAwake = false;
978 +        public volatile boolean interrupted = false;
979 +        public volatile boolean lockStarted = false;
980 +        
981 +        public UninterruptableThread(Lock lock, Condition c) {
982 +            this.lock = lock;
983 +            this.c = c;
984 +        }
985 +        
986 +        public synchronized void run() {
987 +            lock.lock();
988 +            lockStarted = true;
989 +            
990 +            while (!canAwake) {
991 +                c.awaitUninterruptibly();
992 +            }
993 +            
994 +            interrupted = isInterrupted();
995 +            lock.unlock();
996 +        }
997 +    }
998 +
999      /**
1000       * awaitUninterruptibly doesn't abort on interrupt
1001       */
1002      public void testAwaitUninterruptibly() {
1003 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1003 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1004          final Condition c = lock.writeLock().newCondition();
1005 <        Thread t = new Thread(new Runnable() {
659 <                public void run() {
660 <                    lock.writeLock().lock();
661 <                    c.awaitUninterruptibly();
662 <                    lock.writeLock().unlock();
663 <                }
664 <            });
1005 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1006  
1007          try {
1008 <            t.start();
1009 <            Thread.sleep(SHORT_DELAY_MS);
1010 <            t.interrupt();
1008 >            thread.start();
1009 >
1010 >            while (!thread.lockStarted) {
1011 >                Thread.sleep(100);
1012 >            }
1013 >
1014              lock.writeLock().lock();
1015 <            c.signal();
1016 <            lock.writeLock().unlock();
1017 <            assert(t.isInterrupted());
1018 <            t.join(SHORT_DELAY_MS);
1019 <            assertFalse(t.isAlive());
1020 <        }
1021 <        catch (Exception ex) {
1015 >            try {
1016 >                thread.interrupt();
1017 >                thread.canAwake = true;
1018 >                c.signal();
1019 >            } finally {
1020 >                lock.writeLock().unlock();
1021 >            }
1022 >
1023 >            thread.join();
1024 >            assertTrue(thread.interrupted);
1025 >            assertFalse(thread.isAlive());
1026 >        } catch (Exception ex) {
1027              unexpectedException();
1028          }
1029      }
# Line 877 | Line 1226 | public class ReentrantReadWriteLockTest
1226      }
1227  
1228      /**
1229 +     * hasQueuedThread(null) throws NPE
1230 +     */
1231 +    public void testHasQueuedThreadNPE() {
1232 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1233 +        try {
1234 +            sync.hasQueuedThread(null);
1235 +            shouldThrow();
1236 +        } catch (NullPointerException success) {
1237 +        }
1238 +    }
1239 +
1240 +    /**
1241 +     * hasQueuedThread reports whether a thread is queued.
1242 +     */
1243 +    public void testHasQueuedThread() {
1244 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1245 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1246 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1247 +        try {
1248 +            assertFalse(sync.hasQueuedThread(t1));
1249 +            assertFalse(sync.hasQueuedThread(t2));
1250 +            sync.writeLock().lock();
1251 +            t1.start();
1252 +            Thread.sleep(SHORT_DELAY_MS);
1253 +            assertTrue(sync.hasQueuedThread(t1));
1254 +            t2.start();
1255 +            Thread.sleep(SHORT_DELAY_MS);
1256 +            assertTrue(sync.hasQueuedThread(t1));
1257 +            assertTrue(sync.hasQueuedThread(t2));
1258 +            t1.interrupt();
1259 +            Thread.sleep(SHORT_DELAY_MS);
1260 +            assertFalse(sync.hasQueuedThread(t1));
1261 +            assertTrue(sync.hasQueuedThread(t2));
1262 +            sync.writeLock().unlock();
1263 +            Thread.sleep(SHORT_DELAY_MS);
1264 +            assertFalse(sync.hasQueuedThread(t1));
1265 +            Thread.sleep(SHORT_DELAY_MS);
1266 +            assertFalse(sync.hasQueuedThread(t2));
1267 +            t1.join();
1268 +            t2.join();
1269 +        } catch(Exception e){
1270 +            unexpectedException();
1271 +        }
1272 +    }
1273 +
1274 +
1275 +    /**
1276       * getQueueLength reports number of waiting threads
1277       */
1278      public void testGetQueueLength() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines