606 |
|
} |
607 |
|
} |
608 |
|
|
609 |
+ |
/** |
610 |
+ |
* completed submit of callable returns result |
611 |
+ |
*/ |
612 |
+ |
public void testSubmitCallable() { |
613 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
614 |
+ |
try { |
615 |
+ |
Future<String> future = e.submit(new StringTask()); |
616 |
+ |
String result = future.get(); |
617 |
+ |
assertSame(TEST_STRING, result); |
618 |
+ |
} |
619 |
+ |
catch (ExecutionException ex) { |
620 |
+ |
unexpectedException(); |
621 |
+ |
} |
622 |
+ |
catch (InterruptedException ex) { |
623 |
+ |
unexpectedException(); |
624 |
+ |
} finally { |
625 |
+ |
joinPool(e); |
626 |
+ |
} |
627 |
+ |
} |
628 |
+ |
|
629 |
+ |
/** |
630 |
+ |
* completed submit of runnable returns successfully |
631 |
+ |
*/ |
632 |
+ |
public void testSubmitRunnable() { |
633 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
634 |
+ |
try { |
635 |
+ |
Future<?> future = e.submit(new NoOpRunnable()); |
636 |
+ |
future.get(); |
637 |
+ |
assertTrue(future.isDone()); |
638 |
+ |
} |
639 |
+ |
catch (ExecutionException ex) { |
640 |
+ |
unexpectedException(); |
641 |
+ |
} |
642 |
+ |
catch (InterruptedException ex) { |
643 |
+ |
unexpectedException(); |
644 |
+ |
} finally { |
645 |
+ |
joinPool(e); |
646 |
+ |
} |
647 |
+ |
} |
648 |
+ |
|
649 |
+ |
/** |
650 |
+ |
* completed submit of (runnable, result) returns result |
651 |
+ |
*/ |
652 |
+ |
public void testSubmitRunnable2() { |
653 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
654 |
+ |
try { |
655 |
+ |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
656 |
+ |
String result = future.get(); |
657 |
+ |
assertSame(TEST_STRING, result); |
658 |
+ |
} |
659 |
+ |
catch (ExecutionException ex) { |
660 |
+ |
unexpectedException(); |
661 |
+ |
} |
662 |
+ |
catch (InterruptedException ex) { |
663 |
+ |
unexpectedException(); |
664 |
+ |
} finally { |
665 |
+ |
joinPool(e); |
666 |
+ |
} |
667 |
+ |
} |
668 |
+ |
|
669 |
+ |
|
670 |
+ |
|
671 |
+ |
|
672 |
+ |
|
673 |
+ |
/** |
674 |
+ |
* invokeAny(null) throws NPE |
675 |
+ |
*/ |
676 |
+ |
public void testInvokeAny1() { |
677 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
678 |
+ |
try { |
679 |
+ |
e.invokeAny(null); |
680 |
+ |
} catch (NullPointerException success) { |
681 |
+ |
} catch(Exception ex) { |
682 |
+ |
unexpectedException(); |
683 |
+ |
} finally { |
684 |
+ |
joinPool(e); |
685 |
+ |
} |
686 |
+ |
} |
687 |
+ |
|
688 |
+ |
/** |
689 |
+ |
* invokeAny(empty collection) throws IAE |
690 |
+ |
*/ |
691 |
+ |
public void testInvokeAny2() { |
692 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
693 |
+ |
try { |
694 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
695 |
+ |
} catch (IllegalArgumentException success) { |
696 |
+ |
} catch(Exception ex) { |
697 |
+ |
unexpectedException(); |
698 |
+ |
} finally { |
699 |
+ |
joinPool(e); |
700 |
+ |
} |
701 |
+ |
} |
702 |
+ |
|
703 |
+ |
/** |
704 |
+ |
* invokeAny(c) throws NPE if c has null elements |
705 |
+ |
*/ |
706 |
+ |
public void testInvokeAny3() { |
707 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
708 |
+ |
try { |
709 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
710 |
+ |
l.add(new StringTask()); |
711 |
+ |
l.add(null); |
712 |
+ |
e.invokeAny(l); |
713 |
+ |
} catch (NullPointerException success) { |
714 |
+ |
} catch(Exception ex) { |
715 |
+ |
unexpectedException(); |
716 |
+ |
} finally { |
717 |
+ |
joinPool(e); |
718 |
+ |
} |
719 |
+ |
} |
720 |
+ |
|
721 |
+ |
/** |
722 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
723 |
+ |
*/ |
724 |
+ |
public void testInvokeAny4() { |
725 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
726 |
+ |
try { |
727 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
728 |
+ |
l.add(new NPETask()); |
729 |
+ |
e.invokeAny(l); |
730 |
+ |
} catch (ExecutionException success) { |
731 |
+ |
} catch(Exception ex) { |
732 |
+ |
unexpectedException(); |
733 |
+ |
} finally { |
734 |
+ |
joinPool(e); |
735 |
+ |
} |
736 |
+ |
} |
737 |
+ |
|
738 |
+ |
/** |
739 |
+ |
* invokeAny(c) returns result of some task |
740 |
+ |
*/ |
741 |
+ |
public void testInvokeAny5() { |
742 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
743 |
+ |
try { |
744 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
745 |
+ |
l.add(new StringTask()); |
746 |
+ |
l.add(new StringTask()); |
747 |
+ |
String result = e.invokeAny(l); |
748 |
+ |
assertSame(TEST_STRING, result); |
749 |
+ |
} catch (ExecutionException success) { |
750 |
+ |
} catch(Exception ex) { |
751 |
+ |
unexpectedException(); |
752 |
+ |
} finally { |
753 |
+ |
joinPool(e); |
754 |
+ |
} |
755 |
+ |
} |
756 |
+ |
|
757 |
+ |
/** |
758 |
+ |
* invokeAll(null) throws NPE |
759 |
+ |
*/ |
760 |
+ |
public void testInvokeAll1() { |
761 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
762 |
+ |
try { |
763 |
+ |
e.invokeAll(null); |
764 |
+ |
} catch (NullPointerException success) { |
765 |
+ |
} catch(Exception ex) { |
766 |
+ |
unexpectedException(); |
767 |
+ |
} finally { |
768 |
+ |
joinPool(e); |
769 |
+ |
} |
770 |
+ |
} |
771 |
+ |
|
772 |
+ |
/** |
773 |
+ |
* invokeAll(empty collection) returns empty collection |
774 |
+ |
*/ |
775 |
+ |
public void testInvokeAll2() { |
776 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
777 |
+ |
try { |
778 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
779 |
+ |
assertTrue(r.isEmpty()); |
780 |
+ |
} catch(Exception ex) { |
781 |
+ |
unexpectedException(); |
782 |
+ |
} finally { |
783 |
+ |
joinPool(e); |
784 |
+ |
} |
785 |
+ |
} |
786 |
+ |
|
787 |
+ |
/** |
788 |
+ |
* invokeAll(c) throws NPE if c has null elements |
789 |
+ |
*/ |
790 |
+ |
public void testInvokeAll3() { |
791 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
792 |
+ |
try { |
793 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
794 |
+ |
l.add(new StringTask()); |
795 |
+ |
l.add(null); |
796 |
+ |
e.invokeAll(l); |
797 |
+ |
} catch (NullPointerException success) { |
798 |
+ |
} catch(Exception ex) { |
799 |
+ |
unexpectedException(); |
800 |
+ |
} finally { |
801 |
+ |
joinPool(e); |
802 |
+ |
} |
803 |
+ |
} |
804 |
+ |
|
805 |
+ |
/** |
806 |
+ |
* get of invokeAll(c) throws exception on failed task |
807 |
+ |
*/ |
808 |
+ |
public void testInvokeAll4() { |
809 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
810 |
+ |
try { |
811 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
812 |
+ |
l.add(new NPETask()); |
813 |
+ |
List<Future<String>> result = e.invokeAll(l); |
814 |
+ |
assertEquals(1, result.size()); |
815 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
816 |
+ |
it.next().get(); |
817 |
+ |
} catch(ExecutionException success) { |
818 |
+ |
} catch(Exception ex) { |
819 |
+ |
unexpectedException(); |
820 |
+ |
} finally { |
821 |
+ |
joinPool(e); |
822 |
+ |
} |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
/** |
826 |
+ |
* invokeAll(c) returns results of all completed tasks |
827 |
+ |
*/ |
828 |
+ |
public void testInvokeAll5() { |
829 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
830 |
+ |
try { |
831 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
832 |
+ |
l.add(new StringTask()); |
833 |
+ |
l.add(new StringTask()); |
834 |
+ |
List<Future<String>> result = e.invokeAll(l); |
835 |
+ |
assertEquals(2, result.size()); |
836 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
837 |
+ |
assertSame(TEST_STRING, it.next().get()); |
838 |
+ |
} catch (ExecutionException success) { |
839 |
+ |
} catch(Exception ex) { |
840 |
+ |
unexpectedException(); |
841 |
+ |
} finally { |
842 |
+ |
joinPool(e); |
843 |
+ |
} |
844 |
+ |
} |
845 |
+ |
|
846 |
+ |
/** |
847 |
+ |
* timed invokeAny(null) throws NPE |
848 |
+ |
*/ |
849 |
+ |
public void testTimedInvokeAny1() { |
850 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
851 |
+ |
try { |
852 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
853 |
+ |
} catch (NullPointerException success) { |
854 |
+ |
} catch(Exception ex) { |
855 |
+ |
unexpectedException(); |
856 |
+ |
} finally { |
857 |
+ |
joinPool(e); |
858 |
+ |
} |
859 |
+ |
} |
860 |
+ |
|
861 |
+ |
/** |
862 |
+ |
* timed invokeAny(,,null) throws NPE |
863 |
+ |
*/ |
864 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
865 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
866 |
+ |
try { |
867 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
+ |
l.add(new StringTask()); |
869 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
870 |
+ |
} catch (NullPointerException success) { |
871 |
+ |
} catch(Exception ex) { |
872 |
+ |
unexpectedException(); |
873 |
+ |
} finally { |
874 |
+ |
joinPool(e); |
875 |
+ |
} |
876 |
+ |
} |
877 |
+ |
|
878 |
+ |
/** |
879 |
+ |
* timed invokeAny(empty collection) throws IAE |
880 |
+ |
*/ |
881 |
+ |
public void testTimedInvokeAny2() { |
882 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
883 |
+ |
try { |
884 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
885 |
+ |
} catch (IllegalArgumentException success) { |
886 |
+ |
} catch(Exception ex) { |
887 |
+ |
unexpectedException(); |
888 |
+ |
} finally { |
889 |
+ |
joinPool(e); |
890 |
+ |
} |
891 |
+ |
} |
892 |
+ |
|
893 |
+ |
/** |
894 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
895 |
+ |
*/ |
896 |
+ |
public void testTimedInvokeAny3() { |
897 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
898 |
+ |
try { |
899 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
900 |
+ |
l.add(new StringTask()); |
901 |
+ |
l.add(null); |
902 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
903 |
+ |
} catch (NullPointerException success) { |
904 |
+ |
} catch(Exception ex) { |
905 |
+ |
ex.printStackTrace(); |
906 |
+ |
unexpectedException(); |
907 |
+ |
} finally { |
908 |
+ |
joinPool(e); |
909 |
+ |
} |
910 |
+ |
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
914 |
+ |
*/ |
915 |
+ |
public void testTimedInvokeAny4() { |
916 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
917 |
+ |
try { |
918 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
919 |
+ |
l.add(new NPETask()); |
920 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
921 |
+ |
} catch(ExecutionException success) { |
922 |
+ |
} catch(Exception ex) { |
923 |
+ |
unexpectedException(); |
924 |
+ |
} finally { |
925 |
+ |
joinPool(e); |
926 |
+ |
} |
927 |
+ |
} |
928 |
+ |
|
929 |
+ |
/** |
930 |
+ |
* timed invokeAny(c) returns result of some task |
931 |
+ |
*/ |
932 |
+ |
public void testTimedInvokeAny5() { |
933 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
934 |
+ |
try { |
935 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
936 |
+ |
l.add(new StringTask()); |
937 |
+ |
l.add(new StringTask()); |
938 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
939 |
+ |
assertSame(TEST_STRING, result); |
940 |
+ |
} catch (ExecutionException success) { |
941 |
+ |
} catch(Exception ex) { |
942 |
+ |
unexpectedException(); |
943 |
+ |
} finally { |
944 |
+ |
joinPool(e); |
945 |
+ |
} |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
/** |
949 |
+ |
* timed invokeAll(null) throws NPE |
950 |
+ |
*/ |
951 |
+ |
public void testTimedInvokeAll1() { |
952 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
953 |
+ |
try { |
954 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
955 |
+ |
} catch (NullPointerException success) { |
956 |
+ |
} catch(Exception ex) { |
957 |
+ |
unexpectedException(); |
958 |
+ |
} finally { |
959 |
+ |
joinPool(e); |
960 |
+ |
} |
961 |
+ |
} |
962 |
+ |
|
963 |
+ |
/** |
964 |
+ |
* timed invokeAll(,,null) throws NPE |
965 |
+ |
*/ |
966 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
967 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
968 |
+ |
try { |
969 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
970 |
+ |
l.add(new StringTask()); |
971 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
972 |
+ |
} catch (NullPointerException success) { |
973 |
+ |
} catch(Exception ex) { |
974 |
+ |
unexpectedException(); |
975 |
+ |
} finally { |
976 |
+ |
joinPool(e); |
977 |
+ |
} |
978 |
+ |
} |
979 |
+ |
|
980 |
+ |
/** |
981 |
+ |
* timed invokeAll(empty collection) returns empty collection |
982 |
+ |
*/ |
983 |
+ |
public void testTimedInvokeAll2() { |
984 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
985 |
+ |
try { |
986 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
987 |
+ |
assertTrue(r.isEmpty()); |
988 |
+ |
} catch(Exception ex) { |
989 |
+ |
unexpectedException(); |
990 |
+ |
} finally { |
991 |
+ |
joinPool(e); |
992 |
+ |
} |
993 |
+ |
} |
994 |
+ |
|
995 |
+ |
/** |
996 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
997 |
+ |
*/ |
998 |
+ |
public void testTimedInvokeAll3() { |
999 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1000 |
+ |
try { |
1001 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1002 |
+ |
l.add(new StringTask()); |
1003 |
+ |
l.add(null); |
1004 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1005 |
+ |
} catch (NullPointerException success) { |
1006 |
+ |
} catch(Exception ex) { |
1007 |
+ |
unexpectedException(); |
1008 |
+ |
} finally { |
1009 |
+ |
joinPool(e); |
1010 |
+ |
} |
1011 |
+ |
} |
1012 |
+ |
|
1013 |
+ |
/** |
1014 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1015 |
+ |
*/ |
1016 |
+ |
public void testTimedInvokeAll4() { |
1017 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1018 |
+ |
try { |
1019 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1020 |
+ |
l.add(new NPETask()); |
1021 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1022 |
+ |
assertEquals(1, result.size()); |
1023 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1024 |
+ |
it.next().get(); |
1025 |
+ |
} catch(ExecutionException success) { |
1026 |
+ |
} catch(Exception ex) { |
1027 |
+ |
unexpectedException(); |
1028 |
+ |
} finally { |
1029 |
+ |
joinPool(e); |
1030 |
+ |
} |
1031 |
+ |
} |
1032 |
+ |
|
1033 |
+ |
/** |
1034 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1035 |
+ |
*/ |
1036 |
+ |
public void testTimedInvokeAll5() { |
1037 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1038 |
+ |
try { |
1039 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1040 |
+ |
l.add(new StringTask()); |
1041 |
+ |
l.add(new StringTask()); |
1042 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1043 |
+ |
assertEquals(2, result.size()); |
1044 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1045 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1046 |
+ |
} catch (ExecutionException success) { |
1047 |
+ |
} catch(Exception ex) { |
1048 |
+ |
unexpectedException(); |
1049 |
+ |
} finally { |
1050 |
+ |
joinPool(e); |
1051 |
+ |
} |
1052 |
+ |
} |
1053 |
+ |
|
1054 |
+ |
/** |
1055 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1056 |
+ |
*/ |
1057 |
+ |
public void testTimedInvokeAll6() { |
1058 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1059 |
+ |
try { |
1060 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1061 |
+ |
l.add(new StringTask()); |
1062 |
+ |
l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING)); |
1063 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1064 |
+ |
assertEquals(2, result.size()); |
1065 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1066 |
+ |
Future<String> f1 = it.next(); |
1067 |
+ |
Future<String> f2 = it.next(); |
1068 |
+ |
assertTrue(f1.isDone()); |
1069 |
+ |
assertFalse(f1.isCancelled()); |
1070 |
+ |
assertTrue(f2.isDone()); |
1071 |
+ |
assertTrue(f2.isCancelled()); |
1072 |
+ |
} catch(Exception ex) { |
1073 |
+ |
unexpectedException(); |
1074 |
+ |
} finally { |
1075 |
+ |
joinPool(e); |
1076 |
+ |
} |
1077 |
+ |
} |
1078 |
+ |
|
1079 |
+ |
|
1080 |
|
} |