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.*; |
595 |
|
Thread.sleep(SHORT_DELAY_MS); |
596 |
|
assertFalse(task.isCancelled()); |
597 |
|
task.cancel(true); |
598 |
< |
assertTrue(task.isCancelled()); |
598 |
> |
assertTrue(task.isDone()); |
599 |
|
Thread.sleep(SHORT_DELAY_MS); |
600 |
|
assertTrue(p1.isTerminated()); |
601 |
|
} |
840 |
|
} catch(Exception ex) { |
841 |
|
unexpectedException(); |
842 |
|
} finally { |
843 |
+ |
joinPool(e); |
844 |
+ |
} |
845 |
+ |
} |
846 |
+ |
|
847 |
+ |
/** |
848 |
+ |
* timed invokeAny(null) throws NPE |
849 |
+ |
*/ |
850 |
+ |
public void testTimedInvokeAny1() { |
851 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
852 |
+ |
try { |
853 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
854 |
+ |
} catch (NullPointerException success) { |
855 |
+ |
} catch(Exception ex) { |
856 |
+ |
unexpectedException(); |
857 |
+ |
} finally { |
858 |
+ |
joinPool(e); |
859 |
+ |
} |
860 |
+ |
} |
861 |
+ |
|
862 |
+ |
/** |
863 |
+ |
* timed invokeAny(,,null) throws NPE |
864 |
+ |
*/ |
865 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
866 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
867 |
+ |
try { |
868 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
869 |
+ |
l.add(new StringTask()); |
870 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
871 |
+ |
} catch (NullPointerException success) { |
872 |
+ |
} catch(Exception ex) { |
873 |
+ |
unexpectedException(); |
874 |
+ |
} finally { |
875 |
+ |
joinPool(e); |
876 |
+ |
} |
877 |
+ |
} |
878 |
+ |
|
879 |
+ |
/** |
880 |
+ |
* timed invokeAny(empty collection) throws IAE |
881 |
+ |
*/ |
882 |
+ |
public void testTimedInvokeAny2() { |
883 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
884 |
+ |
try { |
885 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
886 |
+ |
} catch (IllegalArgumentException success) { |
887 |
+ |
} catch(Exception ex) { |
888 |
+ |
unexpectedException(); |
889 |
+ |
} finally { |
890 |
+ |
joinPool(e); |
891 |
+ |
} |
892 |
+ |
} |
893 |
+ |
|
894 |
+ |
/** |
895 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
896 |
+ |
*/ |
897 |
+ |
public void testTimedInvokeAny3() { |
898 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
899 |
+ |
try { |
900 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
901 |
+ |
l.add(new StringTask()); |
902 |
+ |
l.add(null); |
903 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
904 |
+ |
} catch (NullPointerException success) { |
905 |
+ |
} catch(Exception ex) { |
906 |
+ |
ex.printStackTrace(); |
907 |
+ |
unexpectedException(); |
908 |
+ |
} finally { |
909 |
+ |
joinPool(e); |
910 |
+ |
} |
911 |
+ |
} |
912 |
+ |
|
913 |
+ |
/** |
914 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
915 |
+ |
*/ |
916 |
+ |
public void testTimedInvokeAny4() { |
917 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
918 |
+ |
try { |
919 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
920 |
+ |
l.add(new NPETask()); |
921 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
922 |
+ |
} catch(ExecutionException success) { |
923 |
+ |
} catch(Exception ex) { |
924 |
+ |
unexpectedException(); |
925 |
+ |
} finally { |
926 |
+ |
joinPool(e); |
927 |
+ |
} |
928 |
+ |
} |
929 |
+ |
|
930 |
+ |
/** |
931 |
+ |
* timed invokeAny(c) returns result of some task |
932 |
+ |
*/ |
933 |
+ |
public void testTimedInvokeAny5() { |
934 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
935 |
+ |
try { |
936 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
937 |
+ |
l.add(new StringTask()); |
938 |
+ |
l.add(new StringTask()); |
939 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
940 |
+ |
assertSame(TEST_STRING, result); |
941 |
+ |
} catch (ExecutionException success) { |
942 |
+ |
} catch(Exception ex) { |
943 |
+ |
unexpectedException(); |
944 |
+ |
} finally { |
945 |
+ |
joinPool(e); |
946 |
+ |
} |
947 |
+ |
} |
948 |
+ |
|
949 |
+ |
/** |
950 |
+ |
* timed invokeAll(null) throws NPE |
951 |
+ |
*/ |
952 |
+ |
public void testTimedInvokeAll1() { |
953 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
954 |
+ |
try { |
955 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
956 |
+ |
} catch (NullPointerException success) { |
957 |
+ |
} catch(Exception ex) { |
958 |
+ |
unexpectedException(); |
959 |
+ |
} finally { |
960 |
+ |
joinPool(e); |
961 |
+ |
} |
962 |
+ |
} |
963 |
+ |
|
964 |
+ |
/** |
965 |
+ |
* timed invokeAll(,,null) throws NPE |
966 |
+ |
*/ |
967 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
968 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
969 |
+ |
try { |
970 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
971 |
+ |
l.add(new StringTask()); |
972 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
973 |
+ |
} catch (NullPointerException success) { |
974 |
+ |
} catch(Exception ex) { |
975 |
+ |
unexpectedException(); |
976 |
+ |
} finally { |
977 |
+ |
joinPool(e); |
978 |
+ |
} |
979 |
+ |
} |
980 |
+ |
|
981 |
+ |
/** |
982 |
+ |
* timed invokeAll(empty collection) returns empty collection |
983 |
+ |
*/ |
984 |
+ |
public void testTimedInvokeAll2() { |
985 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
986 |
+ |
try { |
987 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
988 |
+ |
assertTrue(r.isEmpty()); |
989 |
+ |
} catch(Exception ex) { |
990 |
+ |
unexpectedException(); |
991 |
+ |
} finally { |
992 |
+ |
joinPool(e); |
993 |
+ |
} |
994 |
+ |
} |
995 |
+ |
|
996 |
+ |
/** |
997 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
998 |
+ |
*/ |
999 |
+ |
public void testTimedInvokeAll3() { |
1000 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1001 |
+ |
try { |
1002 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1003 |
+ |
l.add(new StringTask()); |
1004 |
+ |
l.add(null); |
1005 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1006 |
+ |
} catch (NullPointerException success) { |
1007 |
+ |
} catch(Exception ex) { |
1008 |
+ |
unexpectedException(); |
1009 |
+ |
} finally { |
1010 |
+ |
joinPool(e); |
1011 |
+ |
} |
1012 |
+ |
} |
1013 |
+ |
|
1014 |
+ |
/** |
1015 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1016 |
+ |
*/ |
1017 |
+ |
public void testTimedInvokeAll4() { |
1018 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1019 |
+ |
try { |
1020 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1021 |
+ |
l.add(new NPETask()); |
1022 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1023 |
+ |
assertEquals(1, result.size()); |
1024 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1025 |
+ |
it.next().get(); |
1026 |
+ |
} catch(ExecutionException success) { |
1027 |
+ |
} catch(Exception ex) { |
1028 |
+ |
unexpectedException(); |
1029 |
+ |
} finally { |
1030 |
+ |
joinPool(e); |
1031 |
+ |
} |
1032 |
+ |
} |
1033 |
+ |
|
1034 |
+ |
/** |
1035 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1036 |
+ |
*/ |
1037 |
+ |
public void testTimedInvokeAll5() { |
1038 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1039 |
+ |
try { |
1040 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1041 |
+ |
l.add(new StringTask()); |
1042 |
+ |
l.add(new StringTask()); |
1043 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1044 |
+ |
assertEquals(2, result.size()); |
1045 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1046 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1047 |
+ |
} catch (ExecutionException success) { |
1048 |
+ |
} catch(Exception ex) { |
1049 |
+ |
unexpectedException(); |
1050 |
+ |
} finally { |
1051 |
+ |
joinPool(e); |
1052 |
+ |
} |
1053 |
+ |
} |
1054 |
+ |
|
1055 |
+ |
/** |
1056 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1057 |
+ |
*/ |
1058 |
+ |
public void testTimedInvokeAll6() { |
1059 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1060 |
+ |
try { |
1061 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1062 |
+ |
l.add(new StringTask()); |
1063 |
+ |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1064 |
+ |
l.add(new StringTask()); |
1065 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1066 |
+ |
assertEquals(3, result.size()); |
1067 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1068 |
+ |
Future<String> f1 = it.next(); |
1069 |
+ |
Future<String> f2 = it.next(); |
1070 |
+ |
Future<String> f3 = it.next(); |
1071 |
+ |
assertTrue(f1.isDone()); |
1072 |
+ |
assertTrue(f2.isDone()); |
1073 |
+ |
assertTrue(f3.isDone()); |
1074 |
+ |
assertFalse(f1.isCancelled()); |
1075 |
+ |
assertTrue(f2.isCancelled()); |
1076 |
+ |
} catch(Exception ex) { |
1077 |
+ |
unexpectedException(); |
1078 |
+ |
} finally { |
1079 |
|
joinPool(e); |
1080 |
|
} |
1081 |
|
} |