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

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.14 by dl, Sat Jan 10 01:41:59 2004 UTC vs.
Revision 1.15 by dl, Sat Jan 10 20:37:20 2004 UTC

# Line 46 | Line 46 | public class AbstractQueuedSynchronizerT
46              if (getState() == 0) throw new IllegalMonitorStateException();
47          }
48          
49 <        public ConditionObject newCondition() { return new ConditionObject(); }
49 >        public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
50          
51          public void lock() {
52              acquireExclusiveUninterruptibly(1);
# Line 408 | Line 408 | public class AbstractQueuedSynchronizerT
408       */
409      public void testAwait_IllegalMonitor() {
410          final Mutex lock = new Mutex();
411 <        final Condition c = lock.newCondition();
411 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
412          try {
413              c.await();
414              shouldThrow();
# Line 425 | Line 425 | public class AbstractQueuedSynchronizerT
425       */
426      public void testSignal_IllegalMonitor() {
427          final Mutex lock = new Mutex();
428 <        final Condition c = lock.newCondition();
428 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
429          try {
430              c.signal();
431              shouldThrow();
# Line 442 | Line 442 | public class AbstractQueuedSynchronizerT
442       */
443      public void testAwaitNanos_Timeout() {
444          final Mutex lock = new Mutex();
445 <        final Condition c = lock.newCondition();
445 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
446          try {
447              lock.acquireExclusiveUninterruptibly(1);
448              long t = c.awaitNanos(100);
# Line 459 | Line 459 | public class AbstractQueuedSynchronizerT
459       */
460      public void testAwait_Timeout() {
461          final Mutex lock = new Mutex();
462 <        final Condition c = lock.newCondition();
462 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
463          try {
464              lock.acquireExclusiveUninterruptibly(1);
465              assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 475 | Line 475 | public class AbstractQueuedSynchronizerT
475       */
476      public void testAwaitUntil_Timeout() {
477          final Mutex lock = new Mutex();
478 <        final Condition c = lock.newCondition();
478 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
479          try {
480              lock.acquireExclusiveUninterruptibly(1);
481              java.util.Date d = new java.util.Date();
# Line 492 | Line 492 | public class AbstractQueuedSynchronizerT
492       */
493      public void testAwait() {
494          final Mutex lock = new Mutex();
495 <        final Condition c = lock.newCondition();
495 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
496 >        Thread t = new Thread(new Runnable() {
497 >                public void run() {
498 >                    try {
499 >                        lock.acquireExclusiveUninterruptibly(1);
500 >                        c.await();
501 >                        lock.releaseExclusive(1);
502 >                    }
503 >                    catch(InterruptedException e) {
504 >                        threadUnexpectedException();
505 >                    }
506 >                }
507 >            });
508 >
509 >        try {
510 >            t.start();
511 >            Thread.sleep(SHORT_DELAY_MS);
512 >            lock.acquireExclusiveUninterruptibly(1);
513 >            c.signal();
514 >            lock.releaseExclusive(1);
515 >            t.join(SHORT_DELAY_MS);
516 >            assertFalse(t.isAlive());
517 >        }
518 >        catch (Exception ex) {
519 >            unexpectedException();
520 >        }
521 >    }
522 >
523 >
524 >
525 >    /**
526 >     * hasWaiters throws NPE if null
527 >     */
528 >    public void testHasWaitersNPE() {
529 >        final Mutex lock = new Mutex();
530 >        try {
531 >            lock.hasWaiters(null);
532 >            shouldThrow();
533 >        } catch (NullPointerException success) {
534 >        } catch (Exception ex) {
535 >            unexpectedException();
536 >        }
537 >    }
538 >
539 >    /**
540 >     * getWaitQueueLength throws NPE if null
541 >     */
542 >    public void testGetWaitQueueLengthNPE() {
543 >        final Mutex lock = new Mutex();
544 >        try {
545 >            lock.getWaitQueueLength(null);
546 >            shouldThrow();
547 >        } catch (NullPointerException success) {
548 >        } catch (Exception ex) {
549 >            unexpectedException();
550 >        }
551 >    }
552 >
553 >
554 >    /**
555 >     * getWaitingThreads throws NPE if null
556 >     */
557 >    public void testGetWaitingThreadsNPE() {
558 >        final Mutex lock = new Mutex();
559 >        try {
560 >            lock.getWaitingThreads(null);
561 >            shouldThrow();
562 >        } catch (NullPointerException success) {
563 >        } catch (Exception ex) {
564 >            unexpectedException();
565 >        }
566 >    }
567 >
568 >
569 >    /**
570 >     * hasWaiters throws IAE if not owned
571 >     */
572 >    public void testHasWaitersIAE() {
573 >        final Mutex lock = new Mutex();
574 >        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
575 >        final Mutex lock2 = new Mutex();
576 >        try {
577 >            lock2.hasWaiters(c);
578 >            shouldThrow();
579 >        } catch (IllegalArgumentException success) {
580 >        } catch (Exception ex) {
581 >            unexpectedException();
582 >        }
583 >    }
584 >
585 >    /**
586 >     * hasWaiters throws IMSE if not locked
587 >     */
588 >    public void testHasWaitersIMSE() {
589 >        final Mutex lock = new Mutex();
590 >        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
591 >        try {
592 >            lock.hasWaiters(c);
593 >            shouldThrow();
594 >        } catch (IllegalMonitorStateException success) {
595 >        } catch (Exception ex) {
596 >            unexpectedException();
597 >        }
598 >    }
599 >
600 >
601 >    /**
602 >     * getWaitQueueLength throws IAE if not owned
603 >     */
604 >    public void testGetWaitQueueLengthIAE() {
605 >        final Mutex lock = new Mutex();
606 >        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
607 >        final Mutex lock2 = new Mutex();
608 >        try {
609 >            lock2.getWaitQueueLength(c);
610 >            shouldThrow();
611 >        } catch (IllegalArgumentException success) {
612 >        } catch (Exception ex) {
613 >            unexpectedException();
614 >        }
615 >    }
616 >
617 >    /**
618 >     * getWaitQueueLength throws IMSE if not locked
619 >     */
620 >    public void testGetWaitQueueLengthIMSE() {
621 >        final Mutex lock = new Mutex();
622 >        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
623 >        try {
624 >            lock.getWaitQueueLength(c);
625 >            shouldThrow();
626 >        } catch (IllegalMonitorStateException success) {
627 >        } catch (Exception ex) {
628 >            unexpectedException();
629 >        }
630 >    }
631 >
632 >
633 >    /**
634 >     * getWaitingThreads throws IAE if not owned
635 >     */
636 >    public void testGetWaitingThreadsIAE() {
637 >        final Mutex lock = new Mutex();
638 >        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
639 >        final Mutex lock2 = new Mutex();        
640 >        try {
641 >            lock2.getWaitingThreads(c);
642 >            shouldThrow();
643 >        } catch (IllegalArgumentException success) {
644 >        } catch (Exception ex) {
645 >            unexpectedException();
646 >        }
647 >    }
648 >
649 >    /**
650 >     * getWaitingThreads throws IMSE if not locked
651 >     */
652 >    public void testGetWaitingThreadsIMSE() {
653 >        final Mutex lock = new Mutex();
654 >        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
655 >        try {
656 >            lock.getWaitingThreads(c);
657 >            shouldThrow();
658 >        } catch (IllegalMonitorStateException success) {
659 >        } catch (Exception ex) {
660 >            unexpectedException();
661 >        }
662 >    }
663 >
664 >
665 >
666 >    /**
667 >     * hasWaiters returns true when a thread is waiting, else false
668 >     */
669 >    public void testHasWaiters() {
670 >        final Mutex lock = new Mutex();
671 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
672          Thread t = new Thread(new Runnable() {
673                  public void run() {
674                      try {
675                          lock.acquireExclusiveUninterruptibly(1);
676 +                        threadAssertFalse(lock.hasWaiters(c));
677 +                        threadAssertEquals(0, lock.getWaitQueueLength(c));
678 +                        c.await();
679 +                        lock.releaseExclusive(1);
680 +                    }
681 +                    catch(InterruptedException e) {
682 +                        threadUnexpectedException();
683 +                    }
684 +                }
685 +            });
686 +
687 +        try {
688 +            t.start();
689 +            Thread.sleep(SHORT_DELAY_MS);
690 +            lock.acquireExclusiveUninterruptibly(1);
691 +            assertTrue(lock.hasWaiters(c));
692 +            assertEquals(1, lock.getWaitQueueLength(c));
693 +            c.signal();
694 +            lock.releaseExclusive(1);
695 +            Thread.sleep(SHORT_DELAY_MS);
696 +            lock.acquireExclusiveUninterruptibly(1);
697 +            assertFalse(lock.hasWaiters(c));
698 +            assertEquals(0, lock.getWaitQueueLength(c));
699 +            lock.releaseExclusive(1);
700 +            t.join(SHORT_DELAY_MS);
701 +            assertFalse(t.isAlive());
702 +        }
703 +        catch (Exception ex) {
704 +            unexpectedException();
705 +        }
706 +    }
707 +
708 +    /**
709 +     * getWaitQueueLength returns number of waiting threads
710 +     */
711 +    public void testGetWaitQueueLength() {
712 +        final Mutex lock = new Mutex();
713 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
714 +        Thread t1 = new Thread(new Runnable() {
715 +                public void run() {
716 +                    try {
717 +                        lock.acquireExclusiveUninterruptibly(1);
718 +                        threadAssertFalse(lock.hasWaiters(c));
719 +                        threadAssertEquals(0, lock.getWaitQueueLength(c));
720 +                        c.await();
721 +                        lock.releaseExclusive(1);
722 +                    }
723 +                    catch(InterruptedException e) {
724 +                        threadUnexpectedException();
725 +                    }
726 +                }
727 +            });
728 +
729 +        Thread t2 = new Thread(new Runnable() {
730 +                public void run() {
731 +                    try {
732 +                        lock.acquireExclusiveUninterruptibly(1);
733 +                        threadAssertTrue(lock.hasWaiters(c));
734 +                        threadAssertEquals(1, lock.getWaitQueueLength(c));
735 +                        c.await();
736 +                        lock.releaseExclusive(1);
737 +                    }
738 +                    catch(InterruptedException e) {
739 +                        threadUnexpectedException();
740 +                    }
741 +                }
742 +            });
743 +
744 +        try {
745 +            t1.start();
746 +            Thread.sleep(SHORT_DELAY_MS);
747 +            t2.start();
748 +            Thread.sleep(SHORT_DELAY_MS);
749 +            lock.acquireExclusiveUninterruptibly(1);
750 +            assertTrue(lock.hasWaiters(c));
751 +            assertEquals(2, lock.getWaitQueueLength(c));
752 +            c.signalAll();
753 +            lock.releaseExclusive(1);
754 +            Thread.sleep(SHORT_DELAY_MS);
755 +            lock.acquireExclusiveUninterruptibly(1);
756 +            assertFalse(lock.hasWaiters(c));
757 +            assertEquals(0, lock.getWaitQueueLength(c));
758 +            lock.releaseExclusive(1);
759 +            t1.join(SHORT_DELAY_MS);
760 +            t2.join(SHORT_DELAY_MS);
761 +            assertFalse(t1.isAlive());
762 +            assertFalse(t2.isAlive());
763 +        }
764 +        catch (Exception ex) {
765 +            unexpectedException();
766 +        }
767 +    }
768 +
769 +    /**
770 +     * getWaitingThreads returns only and all waiting threads
771 +     */
772 +    public void testGetWaitingThreads() {
773 +        final Mutex lock = new Mutex();
774 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
775 +        Thread t1 = new Thread(new Runnable() {
776 +                public void run() {
777 +                    try {
778 +                        lock.acquireExclusiveUninterruptibly(1);
779 +                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
780 +                        c.await();
781 +                        lock.releaseExclusive(1);
782 +                    }
783 +                    catch(InterruptedException e) {
784 +                        threadUnexpectedException();
785 +                    }
786 +                }
787 +            });
788 +
789 +        Thread t2 = new Thread(new Runnable() {
790 +                public void run() {
791 +                    try {
792 +                        lock.acquireExclusiveUninterruptibly(1);
793 +                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
794                          c.await();
795                          lock.releaseExclusive(1);
796                      }
# Line 507 | Line 801 | public class AbstractQueuedSynchronizerT
801              });
802  
803          try {
804 +            lock.acquireExclusiveUninterruptibly(1);
805 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
806 +            lock.releaseExclusive(1);
807 +            t1.start();
808 +            Thread.sleep(SHORT_DELAY_MS);
809 +            t2.start();
810 +            Thread.sleep(SHORT_DELAY_MS);
811 +            lock.acquireExclusiveUninterruptibly(1);
812 +            assertTrue(lock.hasWaiters(c));
813 +            assertTrue(lock.getWaitingThreads(c).contains(t1));
814 +            assertTrue(lock.getWaitingThreads(c).contains(t2));
815 +            c.signalAll();
816 +            lock.releaseExclusive(1);
817 +            Thread.sleep(SHORT_DELAY_MS);
818 +            lock.acquireExclusiveUninterruptibly(1);
819 +            assertFalse(lock.hasWaiters(c));
820 +            assertTrue(lock.getWaitingThreads(c).isEmpty());
821 +            lock.releaseExclusive(1);
822 +            t1.join(SHORT_DELAY_MS);
823 +            t2.join(SHORT_DELAY_MS);
824 +            assertFalse(t1.isAlive());
825 +            assertFalse(t2.isAlive());
826 +        }
827 +        catch (Exception ex) {
828 +            unexpectedException();
829 +        }
830 +    }
831 +
832 +
833 +
834 +    /**
835 +     * awaitUninterruptibly doesn't abort on interrupt
836 +     */
837 +    public void testAwaitUninterruptibly() {
838 +        final Mutex lock = new Mutex();
839 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
840 +        Thread t = new Thread(new Runnable() {
841 +                public void run() {
842 +                    lock.acquireExclusiveUninterruptibly(1);
843 +                    c.awaitUninterruptibly();
844 +                    lock.releaseExclusive(1);
845 +                }
846 +            });
847 +
848 +        try {
849              t.start();
850              Thread.sleep(SHORT_DELAY_MS);
851 +            t.interrupt();
852              lock.acquireExclusiveUninterruptibly(1);
853              c.signal();
854              lock.releaseExclusive(1);
855 +            assert(t.isInterrupted());
856 +            t.join(SHORT_DELAY_MS);
857 +            assertFalse(t.isAlive());
858 +        }
859 +        catch (Exception ex) {
860 +            unexpectedException();
861 +        }
862 +    }
863 +
864 +    /**
865 +     * await is interruptible
866 +     */
867 +    public void testAwait_Interrupt() {
868 +        final Mutex lock = new Mutex();
869 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
870 +        Thread t = new Thread(new Runnable() {
871 +                public void run() {
872 +                    try {
873 +                        lock.acquireExclusiveUninterruptibly(1);
874 +                        c.await();
875 +                        lock.releaseExclusive(1);
876 +                        threadShouldThrow();
877 +                    }
878 +                    catch(InterruptedException success) {
879 +                    }
880 +                }
881 +            });
882 +
883 +        try {
884 +            t.start();
885 +            Thread.sleep(SHORT_DELAY_MS);
886 +            t.interrupt();
887 +            t.join(SHORT_DELAY_MS);
888 +            assertFalse(t.isAlive());
889 +        }
890 +        catch (Exception ex) {
891 +            unexpectedException();
892 +        }
893 +    }
894 +
895 +    /**
896 +     * awaitNanos is interruptible
897 +     */
898 +    public void testAwaitNanos_Interrupt() {
899 +        final Mutex lock = new Mutex();
900 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
901 +        Thread t = new Thread(new Runnable() {
902 +                public void run() {
903 +                    try {
904 +                        lock.acquireExclusiveUninterruptibly(1);
905 +                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
906 +                        lock.releaseExclusive(1);
907 +                        threadShouldThrow();
908 +                    }
909 +                    catch(InterruptedException success) {
910 +                    }
911 +                }
912 +            });
913 +
914 +        try {
915 +            t.start();
916 +            Thread.sleep(SHORT_DELAY_MS);
917 +            t.interrupt();
918 +            t.join(SHORT_DELAY_MS);
919 +            assertFalse(t.isAlive());
920 +        }
921 +        catch (Exception ex) {
922 +            unexpectedException();
923 +        }
924 +    }
925 +
926 +    /**
927 +     * awaitUntil is interruptible
928 +     */
929 +    public void testAwaitUntil_Interrupt() {
930 +        final Mutex lock = new Mutex();
931 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
932 +        Thread t = new Thread(new Runnable() {
933 +                public void run() {
934 +                    try {
935 +                        lock.acquireExclusiveUninterruptibly(1);
936 +                        java.util.Date d = new java.util.Date();
937 +                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
938 +                        lock.releaseExclusive(1);
939 +                        threadShouldThrow();
940 +                    }
941 +                    catch(InterruptedException success) {
942 +                    }
943 +                }
944 +            });
945 +
946 +        try {
947 +            t.start();
948 +            Thread.sleep(SHORT_DELAY_MS);
949 +            t.interrupt();
950              t.join(SHORT_DELAY_MS);
951              assertFalse(t.isAlive());
952          }
# Line 521 | Line 956 | public class AbstractQueuedSynchronizerT
956      }
957  
958      /**
959 +     * signalAll wakes up all threads
960 +     */
961 +    public void testSignalAll() {
962 +        final Mutex lock = new Mutex();
963 +        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
964 +        Thread t1 = new Thread(new Runnable() {
965 +                public void run() {
966 +                    try {
967 +                        lock.acquireExclusiveUninterruptibly(1);
968 +                        c.await();
969 +                        lock.releaseExclusive(1);
970 +                    }
971 +                    catch(InterruptedException e) {
972 +                        threadUnexpectedException();
973 +                    }
974 +                }
975 +            });
976 +
977 +        Thread t2 = new Thread(new Runnable() {
978 +                public void run() {
979 +                    try {
980 +                        lock.acquireExclusiveUninterruptibly(1);
981 +                        c.await();
982 +                        lock.releaseExclusive(1);
983 +                    }
984 +                    catch(InterruptedException e) {
985 +                        threadUnexpectedException();
986 +                    }
987 +                }
988 +            });
989 +
990 +        try {
991 +            t1.start();
992 +            t2.start();
993 +            Thread.sleep(SHORT_DELAY_MS);
994 +            lock.acquireExclusiveUninterruptibly(1);
995 +            c.signalAll();
996 +            lock.releaseExclusive(1);
997 +            t1.join(SHORT_DELAY_MS);
998 +            t2.join(SHORT_DELAY_MS);
999 +            assertFalse(t1.isAlive());
1000 +            assertFalse(t2.isAlive());
1001 +        }
1002 +        catch (Exception ex) {
1003 +            unexpectedException();
1004 +        }
1005 +    }
1006 +
1007 +
1008 +    /**
1009       * toString indicates current state
1010       */
1011      public void testToString() {
# Line 558 | Line 1043 | public class AbstractQueuedSynchronizerT
1043  
1044  
1045      /**
1046 <     * Latch isSignalled initially returns false, then true after release
1046 >     * tryReleaseShared setting state changes getState
1047       */
1048 <    public void testLatchIsSignalled() {
1048 >    public void testGetStateWithReleaseShared() {
1049          final BooleanLatch l = new BooleanLatch();
1050          assertFalse(l.isSignalled());
1051          l.releaseShared(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines