ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.11 by dl, Sat Dec 27 18:27:02 2003 UTC vs.
Revision 1.15 by dl, Mon Dec 29 19:05:40 2003 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 56 | Line 57 | public class ReentrantLockTest extends J
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
60 +        public Collection<Thread> getWaitingThreads(Condition c) {
61 +            return super.getWaitingThreads(c);
62 +        }
63 +
64  
65      }
66  
# Line 102 | Line 107 | public class ReentrantLockTest extends J
107      }
108  
109      /**
110 <     * trylock on an unlocked lock succeeds
110 >     * tryLock on an unlocked lock succeeds
111       */
112      public void testTryLock() {
113          ReentrantLock rl = new ReentrantLock();
# Line 113 | Line 118 | public class ReentrantLockTest extends J
118  
119  
120      /**
121 +     * hasQueuedThreads reports whether there are waiting threads
122 +     */
123 +    public void testhasQueuedThreads() {
124 +        final ReentrantLock lock = new ReentrantLock();
125 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
126 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
127 +        try {
128 +            assertFalse(lock.hasQueuedThreads());
129 +            lock.lock();
130 +            t1.start();
131 +            Thread.sleep(SHORT_DELAY_MS);
132 +            assertTrue(lock.hasQueuedThreads());
133 +            t2.start();
134 +            Thread.sleep(SHORT_DELAY_MS);
135 +            assertTrue(lock.hasQueuedThreads());
136 +            t1.interrupt();
137 +            Thread.sleep(SHORT_DELAY_MS);
138 +            assertTrue(lock.hasQueuedThreads());
139 +            lock.unlock();
140 +            Thread.sleep(SHORT_DELAY_MS);
141 +            assertFalse(lock.hasQueuedThreads());
142 +            t1.join();
143 +            t2.join();
144 +        } catch(Exception e){
145 +            unexpectedException();
146 +        }
147 +    }
148 +
149 +    /**
150       * getQueueLength reports number of waiting threads
151       */
152      public void testGetQueueLength() {
# Line 175 | Line 209 | public class ReentrantLockTest extends J
209  
210  
211      /**
212 <     * timed trylock is interruptible.
212 >     * timed tryLock is interruptible.
213       */
214      public void testInterruptedException2() {
215          final ReentrantLock lock = new ReentrantLock();
# Line 198 | Line 232 | public class ReentrantLockTest extends J
232  
233  
234      /**
235 <     * Trylock on a locked lock fails
235 >     * TryLock on a locked lock fails
236       */
237      public void testTryLockWhenLocked() {
238          final ReentrantLock lock = new ReentrantLock();
# Line 218 | Line 252 | public class ReentrantLockTest extends J
252      }
253  
254      /**
255 <     * Timed trylock on a locked lock times out
255 >     * Timed tryLock on a locked lock times out
256       */
257      public void testTryLock_Timeout() {
258          final ReentrantLock lock = new ReentrantLock();
# Line 418 | Line 452 | public class ReentrantLockTest extends J
452       */
453      public void testAwait() {
454          final ReentrantLock lock = new ReentrantLock();
455 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
455 >        final Condition c = lock.newCondition();
456          Thread t = new Thread(new Runnable() {
457                  public void run() {
458                      try {
# Line 447 | Line 481 | public class ReentrantLockTest extends J
481      }
482  
483      /**
484 +     * hasWaiters throws NPE if null
485 +     */
486 +    public void testHasWaitersNPE() {
487 +        final ReentrantLock lock = new ReentrantLock();
488 +        try {
489 +            lock.hasWaiters(null);
490 +            shouldThrow();
491 +        } catch (NullPointerException success) {
492 +        } catch (Exception ex) {
493 +            unexpectedException();
494 +        }
495 +    }
496 +
497 +    /**
498 +     * getWaitQueueLength throws NPE if null
499 +     */
500 +    public void testGetWaitQueueLengthNPE() {
501 +        final ReentrantLock lock = new ReentrantLock();
502 +        try {
503 +            lock.getWaitQueueLength(null);
504 +            shouldThrow();
505 +        } catch (NullPointerException success) {
506 +        } catch (Exception ex) {
507 +            unexpectedException();
508 +        }
509 +    }
510 +
511 +
512 +    /**
513 +     * getWaitingThreads throws NPE if null
514 +     */
515 +    public void testGetWaitingThreadsNPE() {
516 +        final PublicReentrantLock lock = new PublicReentrantLock();
517 +        try {
518 +            lock.getWaitingThreads(null);
519 +            shouldThrow();
520 +        } catch (NullPointerException success) {
521 +        } catch (Exception ex) {
522 +            unexpectedException();
523 +        }
524 +    }
525 +
526 +
527 +    /**
528 +     * hasWaiters throws IAE if not owned
529 +     */
530 +    public void testHasWaitersIAE() {
531 +        final ReentrantLock lock = new ReentrantLock();
532 +        final Condition c = (lock.newCondition());
533 +        final ReentrantLock lock2 = new ReentrantLock();
534 +        try {
535 +            lock2.hasWaiters(c);
536 +            shouldThrow();
537 +        } catch (IllegalArgumentException success) {
538 +        } catch (Exception ex) {
539 +            unexpectedException();
540 +        }
541 +    }
542 +
543 +    /**
544 +     * hasWaiters throws IMSE if not locked
545 +     */
546 +    public void testHasWaitersIMSE() {
547 +        final ReentrantLock lock = new ReentrantLock();
548 +        final Condition c = (lock.newCondition());
549 +        try {
550 +            lock.hasWaiters(c);
551 +            shouldThrow();
552 +        } catch (IllegalMonitorStateException success) {
553 +        } catch (Exception ex) {
554 +            unexpectedException();
555 +        }
556 +    }
557 +
558 +
559 +    /**
560 +     * getWaitQueueLength throws IAE if not owned
561 +     */
562 +    public void testGetWaitQueueLengthIAE() {
563 +        final ReentrantLock lock = new ReentrantLock();
564 +        final Condition c = (lock.newCondition());
565 +        final ReentrantLock lock2 = new ReentrantLock();
566 +        try {
567 +            lock2.getWaitQueueLength(c);
568 +            shouldThrow();
569 +        } catch (IllegalArgumentException success) {
570 +        } catch (Exception ex) {
571 +            unexpectedException();
572 +        }
573 +    }
574 +
575 +    /**
576 +     * getWaitQueueLength throws IMSE if not locked
577 +     */
578 +    public void testGetWaitQueueLengthIMSE() {
579 +        final ReentrantLock lock = new ReentrantLock();
580 +        final Condition c = (lock.newCondition());
581 +        try {
582 +            lock.getWaitQueueLength(c);
583 +            shouldThrow();
584 +        } catch (IllegalMonitorStateException success) {
585 +        } catch (Exception ex) {
586 +            unexpectedException();
587 +        }
588 +    }
589 +
590 +
591 +    /**
592 +     * getWaitingThreads throws IAE if not owned
593 +     */
594 +    public void testGetWaitingThreadsIAE() {
595 +        final PublicReentrantLock lock = new PublicReentrantLock();    
596 +        final Condition c = (lock.newCondition());
597 +        final PublicReentrantLock lock2 = new PublicReentrantLock();    
598 +        try {
599 +            lock2.getWaitingThreads(c);
600 +            shouldThrow();
601 +        } catch (IllegalArgumentException success) {
602 +        } catch (Exception ex) {
603 +            unexpectedException();
604 +        }
605 +    }
606 +
607 +    /**
608 +     * getWaitingThreads throws IMSE if not locked
609 +     */
610 +    public void testGetWaitingThreadsIMSE() {
611 +        final PublicReentrantLock lock = new PublicReentrantLock();    
612 +        final Condition c = (lock.newCondition());
613 +        try {
614 +            lock.getWaitingThreads(c);
615 +            shouldThrow();
616 +        } catch (IllegalMonitorStateException success) {
617 +        } catch (Exception ex) {
618 +            unexpectedException();
619 +        }
620 +    }
621 +
622 +
623 +
624 +    /**
625       * hasWaiters returns true when a thread is waiting, else false
626       */
627      public void testHasWaiters() {
628          final ReentrantLock lock = new ReentrantLock();
629 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
629 >        final Condition c = lock.newCondition();
630          Thread t = new Thread(new Runnable() {
631                  public void run() {
632                      try {
633                          lock.lock();
634 <                        threadAssertFalse(c.hasWaiters());
635 <                        threadAssertEquals(0, c.getWaitQueueLength());
634 >                        threadAssertFalse(lock.hasWaiters(c));
635 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
636                          c.await();
637                          lock.unlock();
638                      }
# Line 471 | Line 646 | public class ReentrantLockTest extends J
646              t.start();
647              Thread.sleep(SHORT_DELAY_MS);
648              lock.lock();
649 <            assertTrue(c.hasWaiters());
650 <            assertEquals(1, c.getWaitQueueLength());
649 >            assertTrue(lock.hasWaiters(c));
650 >            assertEquals(1, lock.getWaitQueueLength(c));
651              c.signal();
652              lock.unlock();
653              Thread.sleep(SHORT_DELAY_MS);
654              lock.lock();
655 <            assertFalse(c.hasWaiters());
656 <            assertEquals(0, c.getWaitQueueLength());
655 >            assertFalse(lock.hasWaiters(c));
656 >            assertEquals(0, lock.getWaitQueueLength(c));
657              lock.unlock();
658              t.join(SHORT_DELAY_MS);
659              assertFalse(t.isAlive());
# Line 493 | Line 668 | public class ReentrantLockTest extends J
668       */
669      public void testGetWaitQueueLength() {
670          final ReentrantLock lock = new ReentrantLock();
671 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
671 >        final Condition c = lock.newCondition();
672          Thread t1 = new Thread(new Runnable() {
673                  public void run() {
674                      try {
675                          lock.lock();
676 <                        threadAssertFalse(c.hasWaiters());
677 <                        threadAssertEquals(0, c.getWaitQueueLength());
676 >                        threadAssertFalse(lock.hasWaiters(c));
677 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
678                          c.await();
679                          lock.unlock();
680                      }
# Line 513 | Line 688 | public class ReentrantLockTest extends J
688                  public void run() {
689                      try {
690                          lock.lock();
691 <                        threadAssertTrue(c.hasWaiters());
692 <                        threadAssertEquals(1, c.getWaitQueueLength());
691 >                        threadAssertTrue(lock.hasWaiters(c));
692 >                        threadAssertEquals(1, lock.getWaitQueueLength(c));
693                          c.await();
694                          lock.unlock();
695                      }
# Line 530 | Line 705 | public class ReentrantLockTest extends J
705              t2.start();
706              Thread.sleep(SHORT_DELAY_MS);
707              lock.lock();
708 <            assertTrue(c.hasWaiters());
709 <            assertEquals(2, c.getWaitQueueLength());
708 >            assertTrue(lock.hasWaiters(c));
709 >            assertEquals(2, lock.getWaitQueueLength(c));
710              c.signalAll();
711              lock.unlock();
712              Thread.sleep(SHORT_DELAY_MS);
713              lock.lock();
714 <            assertFalse(c.hasWaiters());
715 <            assertEquals(0, c.getWaitQueueLength());
714 >            assertFalse(lock.hasWaiters(c));
715 >            assertEquals(0, lock.getWaitQueueLength(c));
716              lock.unlock();
717              t1.join(SHORT_DELAY_MS);
718              t2.join(SHORT_DELAY_MS);
# Line 550 | Line 725 | public class ReentrantLockTest extends J
725      }
726  
727      /**
728 +     * getWaitingThreads returns only and all waiting threads
729 +     */
730 +    public void testGetWaitingThreads() {
731 +        final PublicReentrantLock lock = new PublicReentrantLock();    
732 +        final Condition c = lock.newCondition();
733 +        Thread t1 = new Thread(new Runnable() {
734 +                public void run() {
735 +                    try {
736 +                        lock.lock();
737 +                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
738 +                        c.await();
739 +                        lock.unlock();
740 +                    }
741 +                    catch(InterruptedException e) {
742 +                        threadUnexpectedException();
743 +                    }
744 +                }
745 +            });
746 +
747 +        Thread t2 = new Thread(new Runnable() {
748 +                public void run() {
749 +                    try {
750 +                        lock.lock();
751 +                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
752 +                        c.await();
753 +                        lock.unlock();
754 +                    }
755 +                    catch(InterruptedException e) {
756 +                        threadUnexpectedException();
757 +                    }
758 +                }
759 +            });
760 +
761 +        try {
762 +            lock.lock();
763 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
764 +            lock.unlock();
765 +            t1.start();
766 +            Thread.sleep(SHORT_DELAY_MS);
767 +            t2.start();
768 +            Thread.sleep(SHORT_DELAY_MS);
769 +            lock.lock();
770 +            assertTrue(lock.hasWaiters(c));
771 +            assertTrue(lock.getWaitingThreads(c).contains(t1));
772 +            assertTrue(lock.getWaitingThreads(c).contains(t2));
773 +            c.signalAll();
774 +            lock.unlock();
775 +            Thread.sleep(SHORT_DELAY_MS);
776 +            lock.lock();
777 +            assertFalse(lock.hasWaiters(c));
778 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
779 +            lock.unlock();
780 +            t1.join(SHORT_DELAY_MS);
781 +            t2.join(SHORT_DELAY_MS);
782 +            assertFalse(t1.isAlive());
783 +            assertFalse(t2.isAlive());
784 +        }
785 +        catch (Exception ex) {
786 +            unexpectedException();
787 +        }
788 +    }
789 +
790 +
791 +
792 +    /**
793       * awaitUninterruptibly doesn't abort on interrupt
794       */
795      public void testAwaitUninterruptibly() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines