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 java.util.concurrent.*; |
10 |
|
import junit.framework.*; |
11 |
< |
import java.util.List; |
11 |
> |
import java.util.*; |
12 |
|
|
13 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
117 |
|
unexpectedException(); |
118 |
|
} |
119 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
120 |
< |
p2.shutdown(); |
120 |
> |
try { p2.shutdown(); } catch(SecurityException ok) { return; } |
121 |
|
joinPool(p2); |
122 |
|
} |
123 |
|
|
147 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
148 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
149 |
|
assertSame(tf, p.getThreadFactory()); |
149 |
– |
p.shutdown(); |
150 |
|
joinPool(p); |
151 |
|
} |
152 |
|
|
158 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
159 |
|
p.setThreadFactory(tf); |
160 |
|
assertSame(tf, p.getThreadFactory()); |
161 |
– |
p.shutdown(); |
161 |
|
joinPool(p); |
162 |
|
} |
163 |
|
|
183 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
184 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
185 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
187 |
– |
p.shutdown(); |
186 |
|
joinPool(p); |
187 |
|
} |
188 |
|
|
195 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
196 |
|
p.setRejectedExecutionHandler(h); |
197 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
200 |
– |
p.shutdown(); |
198 |
|
joinPool(p); |
199 |
|
} |
200 |
|
|
277 |
|
|
278 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
279 |
|
assertFalse(p1.isShutdown()); |
280 |
< |
p1.shutdown(); |
280 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
281 |
|
assertTrue(p1.isShutdown()); |
282 |
|
joinPool(p1); |
283 |
|
} |
292 |
|
try { |
293 |
|
p1.execute(new MediumRunnable()); |
294 |
|
} finally { |
295 |
< |
p1.shutdown(); |
295 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
296 |
|
} |
297 |
|
try { |
298 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
312 |
|
p1.execute(new SmallRunnable()); |
313 |
|
assertFalse(p1.isTerminating()); |
314 |
|
} finally { |
315 |
< |
p1.shutdown(); |
315 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
316 |
|
} |
317 |
|
try { |
318 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
340 |
|
assertSame(q, wq); |
341 |
|
assertFalse(wq.contains(tasks[0])); |
342 |
|
assertTrue(wq.contains(tasks[4])); |
346 |
– |
p1.shutdownNow(); |
343 |
|
} catch(Exception e) { |
344 |
|
unexpectedException(); |
345 |
|
} finally { |
369 |
|
assertTrue(q.contains(tasks[3])); |
370 |
|
assertTrue(p1.remove(tasks[3])); |
371 |
|
assertFalse(q.contains(tasks[3])); |
376 |
– |
p1.shutdownNow(); |
372 |
|
} catch(Exception e) { |
373 |
|
unexpectedException(); |
374 |
|
} finally { |
391 |
|
p1.purge(); |
392 |
|
long count = p1.getTaskCount(); |
393 |
|
assertTrue(count >= 2 && count < 5); |
399 |
– |
p1.shutdownNow(); |
394 |
|
joinPool(p1); |
395 |
|
} |
396 |
|
|
405 |
|
p1.execute(new MediumPossiblyInterruptedRunnable()); |
406 |
|
} |
407 |
|
finally { |
408 |
< |
l = p1.shutdownNow(); |
408 |
> |
try { |
409 |
> |
l = p1.shutdownNow(); |
410 |
> |
} catch (SecurityException ok) { return; } |
411 |
> |
|
412 |
|
} |
413 |
|
assertTrue(p1.isShutdown()); |
414 |
|
assertTrue(l.size() <= 4); |
769 |
|
for(int i = 1; i < 5; ++i) { |
770 |
|
assertTrue(tasks[i].done); |
771 |
|
} |
772 |
< |
p.shutdownNow(); |
772 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
773 |
|
} catch(RejectedExecutionException ex){ |
774 |
|
unexpectedException(); |
775 |
|
} finally { |
796 |
|
for(int i = 0; i < 5; ++i){ |
797 |
|
assertFalse(tasks[i].done); |
798 |
|
} |
799 |
< |
p.shutdownNow(); |
799 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
800 |
|
} catch(RejectedExecutionException ex){ |
801 |
|
unexpectedException(); |
802 |
|
} finally { |
819 |
|
p.execute(r3); |
820 |
|
assertFalse(p.getQueue().contains(r2)); |
821 |
|
assertTrue(p.getQueue().contains(r3)); |
822 |
< |
p.shutdownNow(); |
822 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
823 |
|
} catch(RejectedExecutionException ex){ |
824 |
|
unexpectedException(); |
825 |
|
} finally { |
833 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
834 |
|
ThreadPoolExecutor tpe = |
835 |
|
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
836 |
< |
tpe.shutdown(); |
836 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
837 |
|
try { |
838 |
|
tpe.execute(new NoOpRunnable()); |
839 |
|
shouldThrow(); |
849 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
850 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
851 |
|
|
852 |
< |
p.shutdown(); |
852 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
853 |
|
try { |
854 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
855 |
|
p.execute(r); |
868 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
869 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
870 |
|
|
871 |
< |
p.shutdown(); |
871 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
872 |
|
try { |
873 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
874 |
|
p.execute(r); |
888 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
889 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
890 |
|
|
891 |
< |
p.shutdown(); |
891 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
892 |
|
try { |
893 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
894 |
|
p.execute(r); |
928 |
|
shouldThrow(); |
929 |
|
} catch(IllegalArgumentException success){ |
930 |
|
} finally { |
931 |
< |
tpe.shutdown(); |
931 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
932 |
|
} |
933 |
|
joinPool(tpe); |
934 |
|
} |
947 |
|
shouldThrow(); |
948 |
|
} catch(IllegalArgumentException success){ |
949 |
|
} finally { |
950 |
< |
tpe.shutdown(); |
950 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
951 |
|
} |
952 |
|
joinPool(tpe); |
953 |
|
} |
966 |
|
shouldThrow(); |
967 |
|
} catch(IllegalArgumentException success){ |
968 |
|
} finally { |
969 |
< |
tpe.shutdown(); |
969 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
970 |
|
} |
971 |
|
joinPool(tpe); |
972 |
|
} |
987 |
|
shouldThrow(); |
988 |
|
} catch(IllegalArgumentException success){ |
989 |
|
} finally { |
990 |
< |
tpe.shutdown(); |
990 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
991 |
|
} |
992 |
|
joinPool(tpe); |
993 |
|
} |
997 |
|
*/ |
998 |
|
public void testTerminated() { |
999 |
|
ExtendedTPE tpe = new ExtendedTPE(); |
1000 |
< |
tpe.shutdown(); |
1000 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1001 |
|
assertTrue(tpe.terminatedCalled); |
1002 |
|
joinPool(tpe); |
1003 |
|
} |
1014 |
|
assertTrue(r.done); |
1015 |
|
assertTrue(tpe.beforeCalled); |
1016 |
|
assertTrue(tpe.afterCalled); |
1017 |
< |
tpe.shutdown(); |
1017 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1018 |
|
} |
1019 |
|
catch(Exception ex) { |
1020 |
|
unexpectedException(); |
1022 |
|
joinPool(tpe); |
1023 |
|
} |
1024 |
|
} |
1025 |
+ |
|
1026 |
+ |
/** |
1027 |
+ |
* completed submit of callable returns result |
1028 |
+ |
*/ |
1029 |
+ |
public void testSubmitCallable() { |
1030 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1031 |
+ |
try { |
1032 |
+ |
Future<String> future = e.submit(new StringTask()); |
1033 |
+ |
String result = future.get(); |
1034 |
+ |
assertSame(TEST_STRING, result); |
1035 |
+ |
} |
1036 |
+ |
catch (ExecutionException ex) { |
1037 |
+ |
unexpectedException(); |
1038 |
+ |
} |
1039 |
+ |
catch (InterruptedException ex) { |
1040 |
+ |
unexpectedException(); |
1041 |
+ |
} finally { |
1042 |
+ |
joinPool(e); |
1043 |
+ |
} |
1044 |
+ |
} |
1045 |
+ |
|
1046 |
+ |
/** |
1047 |
+ |
* completed submit of runnable returns successfully |
1048 |
+ |
*/ |
1049 |
+ |
public void testSubmitRunnable() { |
1050 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1051 |
+ |
try { |
1052 |
+ |
Future<?> future = e.submit(new NoOpRunnable()); |
1053 |
+ |
future.get(); |
1054 |
+ |
assertTrue(future.isDone()); |
1055 |
+ |
} |
1056 |
+ |
catch (ExecutionException ex) { |
1057 |
+ |
unexpectedException(); |
1058 |
+ |
} |
1059 |
+ |
catch (InterruptedException ex) { |
1060 |
+ |
unexpectedException(); |
1061 |
+ |
} finally { |
1062 |
+ |
joinPool(e); |
1063 |
+ |
} |
1064 |
+ |
} |
1065 |
+ |
|
1066 |
+ |
/** |
1067 |
+ |
* completed submit of (runnable, result) returns result |
1068 |
+ |
*/ |
1069 |
+ |
public void testSubmitRunnable2() { |
1070 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1071 |
+ |
try { |
1072 |
+ |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1073 |
+ |
String result = future.get(); |
1074 |
+ |
assertSame(TEST_STRING, result); |
1075 |
+ |
} |
1076 |
+ |
catch (ExecutionException ex) { |
1077 |
+ |
unexpectedException(); |
1078 |
+ |
} |
1079 |
+ |
catch (InterruptedException ex) { |
1080 |
+ |
unexpectedException(); |
1081 |
+ |
} finally { |
1082 |
+ |
joinPool(e); |
1083 |
+ |
} |
1084 |
+ |
} |
1085 |
+ |
|
1086 |
+ |
|
1087 |
+ |
|
1088 |
+ |
|
1089 |
+ |
|
1090 |
+ |
/** |
1091 |
+ |
* invokeAny(null) throws NPE |
1092 |
+ |
*/ |
1093 |
+ |
public void testInvokeAny1() { |
1094 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1095 |
+ |
try { |
1096 |
+ |
e.invokeAny(null); |
1097 |
+ |
} catch (NullPointerException success) { |
1098 |
+ |
} catch(Exception ex) { |
1099 |
+ |
unexpectedException(); |
1100 |
+ |
} finally { |
1101 |
+ |
joinPool(e); |
1102 |
+ |
} |
1103 |
+ |
} |
1104 |
+ |
|
1105 |
+ |
/** |
1106 |
+ |
* invokeAny(empty collection) throws IAE |
1107 |
+ |
*/ |
1108 |
+ |
public void testInvokeAny2() { |
1109 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1110 |
+ |
try { |
1111 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
1112 |
+ |
} catch (IllegalArgumentException success) { |
1113 |
+ |
} catch(Exception ex) { |
1114 |
+ |
unexpectedException(); |
1115 |
+ |
} finally { |
1116 |
+ |
joinPool(e); |
1117 |
+ |
} |
1118 |
+ |
} |
1119 |
+ |
|
1120 |
+ |
/** |
1121 |
+ |
* invokeAny(c) throws NPE if c has null elements |
1122 |
+ |
*/ |
1123 |
+ |
public void testInvokeAny3() { |
1124 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1125 |
+ |
try { |
1126 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1127 |
+ |
l.add(new StringTask()); |
1128 |
+ |
l.add(null); |
1129 |
+ |
e.invokeAny(l); |
1130 |
+ |
} catch (NullPointerException success) { |
1131 |
+ |
} catch(Exception ex) { |
1132 |
+ |
unexpectedException(); |
1133 |
+ |
} finally { |
1134 |
+ |
joinPool(e); |
1135 |
+ |
} |
1136 |
+ |
} |
1137 |
+ |
|
1138 |
+ |
/** |
1139 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
1140 |
+ |
*/ |
1141 |
+ |
public void testInvokeAny4() { |
1142 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1143 |
+ |
try { |
1144 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1145 |
+ |
l.add(new NPETask()); |
1146 |
+ |
e.invokeAny(l); |
1147 |
+ |
} catch (ExecutionException success) { |
1148 |
+ |
} catch(Exception ex) { |
1149 |
+ |
unexpectedException(); |
1150 |
+ |
} finally { |
1151 |
+ |
joinPool(e); |
1152 |
+ |
} |
1153 |
+ |
} |
1154 |
+ |
|
1155 |
+ |
/** |
1156 |
+ |
* invokeAny(c) returns result of some task |
1157 |
+ |
*/ |
1158 |
+ |
public void testInvokeAny5() { |
1159 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1160 |
+ |
try { |
1161 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1162 |
+ |
l.add(new StringTask()); |
1163 |
+ |
l.add(new StringTask()); |
1164 |
+ |
String result = e.invokeAny(l); |
1165 |
+ |
assertSame(TEST_STRING, result); |
1166 |
+ |
} catch (ExecutionException success) { |
1167 |
+ |
} catch(Exception ex) { |
1168 |
+ |
unexpectedException(); |
1169 |
+ |
} finally { |
1170 |
+ |
joinPool(e); |
1171 |
+ |
} |
1172 |
+ |
} |
1173 |
+ |
|
1174 |
+ |
/** |
1175 |
+ |
* invokeAll(null) throws NPE |
1176 |
+ |
*/ |
1177 |
+ |
public void testInvokeAll1() { |
1178 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1179 |
+ |
try { |
1180 |
+ |
e.invokeAll(null); |
1181 |
+ |
} catch (NullPointerException success) { |
1182 |
+ |
} catch(Exception ex) { |
1183 |
+ |
unexpectedException(); |
1184 |
+ |
} finally { |
1185 |
+ |
joinPool(e); |
1186 |
+ |
} |
1187 |
+ |
} |
1188 |
+ |
|
1189 |
+ |
/** |
1190 |
+ |
* invokeAll(empty collection) returns empty collection |
1191 |
+ |
*/ |
1192 |
+ |
public void testInvokeAll2() { |
1193 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1194 |
+ |
try { |
1195 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1196 |
+ |
assertTrue(r.isEmpty()); |
1197 |
+ |
} catch(Exception ex) { |
1198 |
+ |
unexpectedException(); |
1199 |
+ |
} finally { |
1200 |
+ |
joinPool(e); |
1201 |
+ |
} |
1202 |
+ |
} |
1203 |
+ |
|
1204 |
+ |
/** |
1205 |
+ |
* invokeAll(c) throws NPE if c has null elements |
1206 |
+ |
*/ |
1207 |
+ |
public void testInvokeAll3() { |
1208 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1209 |
+ |
try { |
1210 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1211 |
+ |
l.add(new StringTask()); |
1212 |
+ |
l.add(null); |
1213 |
+ |
e.invokeAll(l); |
1214 |
+ |
} catch (NullPointerException success) { |
1215 |
+ |
} catch(Exception ex) { |
1216 |
+ |
unexpectedException(); |
1217 |
+ |
} finally { |
1218 |
+ |
joinPool(e); |
1219 |
+ |
} |
1220 |
+ |
} |
1221 |
+ |
|
1222 |
+ |
/** |
1223 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1224 |
+ |
*/ |
1225 |
+ |
public void testInvokeAll4() { |
1226 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1227 |
+ |
try { |
1228 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1229 |
+ |
l.add(new NPETask()); |
1230 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1231 |
+ |
assertEquals(1, result.size()); |
1232 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1233 |
+ |
it.next().get(); |
1234 |
+ |
} catch(ExecutionException success) { |
1235 |
+ |
} catch(Exception ex) { |
1236 |
+ |
unexpectedException(); |
1237 |
+ |
} finally { |
1238 |
+ |
joinPool(e); |
1239 |
+ |
} |
1240 |
+ |
} |
1241 |
+ |
|
1242 |
+ |
/** |
1243 |
+ |
* invokeAll(c) returns results of all completed tasks |
1244 |
+ |
*/ |
1245 |
+ |
public void testInvokeAll5() { |
1246 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1247 |
+ |
try { |
1248 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1249 |
+ |
l.add(new StringTask()); |
1250 |
+ |
l.add(new StringTask()); |
1251 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1252 |
+ |
assertEquals(2, result.size()); |
1253 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1254 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1255 |
+ |
} catch (ExecutionException success) { |
1256 |
+ |
} catch(Exception ex) { |
1257 |
+ |
unexpectedException(); |
1258 |
+ |
} finally { |
1259 |
+ |
joinPool(e); |
1260 |
+ |
} |
1261 |
+ |
} |
1262 |
+ |
|
1263 |
+ |
|
1264 |
+ |
|
1265 |
+ |
/** |
1266 |
+ |
* timed invokeAny(null) throws NPE |
1267 |
+ |
*/ |
1268 |
+ |
public void testTimedInvokeAny1() { |
1269 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1270 |
+ |
try { |
1271 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1272 |
+ |
} catch (NullPointerException success) { |
1273 |
+ |
} catch(Exception ex) { |
1274 |
+ |
unexpectedException(); |
1275 |
+ |
} finally { |
1276 |
+ |
joinPool(e); |
1277 |
+ |
} |
1278 |
+ |
} |
1279 |
+ |
|
1280 |
+ |
/** |
1281 |
+ |
* timed invokeAny(,,null) throws NPE |
1282 |
+ |
*/ |
1283 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
1284 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1285 |
+ |
try { |
1286 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1287 |
+ |
l.add(new StringTask()); |
1288 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1289 |
+ |
} catch (NullPointerException success) { |
1290 |
+ |
} catch(Exception ex) { |
1291 |
+ |
unexpectedException(); |
1292 |
+ |
} finally { |
1293 |
+ |
joinPool(e); |
1294 |
+ |
} |
1295 |
+ |
} |
1296 |
+ |
|
1297 |
+ |
/** |
1298 |
+ |
* timed invokeAny(empty collection) throws IAE |
1299 |
+ |
*/ |
1300 |
+ |
public void testTimedInvokeAny2() { |
1301 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1302 |
+ |
try { |
1303 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1304 |
+ |
} catch (IllegalArgumentException success) { |
1305 |
+ |
} catch(Exception ex) { |
1306 |
+ |
unexpectedException(); |
1307 |
+ |
} finally { |
1308 |
+ |
joinPool(e); |
1309 |
+ |
} |
1310 |
+ |
} |
1311 |
+ |
|
1312 |
+ |
/** |
1313 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
1314 |
+ |
*/ |
1315 |
+ |
public void testTimedInvokeAny3() { |
1316 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1317 |
+ |
try { |
1318 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1319 |
+ |
l.add(new StringTask()); |
1320 |
+ |
l.add(null); |
1321 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1322 |
+ |
} catch (NullPointerException success) { |
1323 |
+ |
} catch(Exception ex) { |
1324 |
+ |
ex.printStackTrace(); |
1325 |
+ |
unexpectedException(); |
1326 |
+ |
} finally { |
1327 |
+ |
joinPool(e); |
1328 |
+ |
} |
1329 |
+ |
} |
1330 |
+ |
|
1331 |
+ |
/** |
1332 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
1333 |
+ |
*/ |
1334 |
+ |
public void testTimedInvokeAny4() { |
1335 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1336 |
+ |
try { |
1337 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1338 |
+ |
l.add(new NPETask()); |
1339 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1340 |
+ |
} catch(ExecutionException success) { |
1341 |
+ |
} catch(Exception ex) { |
1342 |
+ |
unexpectedException(); |
1343 |
+ |
} finally { |
1344 |
+ |
joinPool(e); |
1345 |
+ |
} |
1346 |
+ |
} |
1347 |
+ |
|
1348 |
+ |
/** |
1349 |
+ |
* timed invokeAny(c) returns result of some task |
1350 |
+ |
*/ |
1351 |
+ |
public void testTimedInvokeAny5() { |
1352 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1353 |
+ |
try { |
1354 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1355 |
+ |
l.add(new StringTask()); |
1356 |
+ |
l.add(new StringTask()); |
1357 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1358 |
+ |
assertSame(TEST_STRING, result); |
1359 |
+ |
} catch (ExecutionException success) { |
1360 |
+ |
} catch(Exception ex) { |
1361 |
+ |
unexpectedException(); |
1362 |
+ |
} finally { |
1363 |
+ |
joinPool(e); |
1364 |
+ |
} |
1365 |
+ |
} |
1366 |
+ |
|
1367 |
+ |
/** |
1368 |
+ |
* timed invokeAll(null) throws NPE |
1369 |
+ |
*/ |
1370 |
+ |
public void testTimedInvokeAll1() { |
1371 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1372 |
+ |
try { |
1373 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1374 |
+ |
} catch (NullPointerException success) { |
1375 |
+ |
} catch(Exception ex) { |
1376 |
+ |
unexpectedException(); |
1377 |
+ |
} finally { |
1378 |
+ |
joinPool(e); |
1379 |
+ |
} |
1380 |
+ |
} |
1381 |
+ |
|
1382 |
+ |
/** |
1383 |
+ |
* timed invokeAll(,,null) throws NPE |
1384 |
+ |
*/ |
1385 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
1386 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1387 |
+ |
try { |
1388 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1389 |
+ |
l.add(new StringTask()); |
1390 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1391 |
+ |
} catch (NullPointerException success) { |
1392 |
+ |
} catch(Exception ex) { |
1393 |
+ |
unexpectedException(); |
1394 |
+ |
} finally { |
1395 |
+ |
joinPool(e); |
1396 |
+ |
} |
1397 |
+ |
} |
1398 |
+ |
|
1399 |
+ |
/** |
1400 |
+ |
* timed invokeAll(empty collection) returns empty collection |
1401 |
+ |
*/ |
1402 |
+ |
public void testTimedInvokeAll2() { |
1403 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1404 |
+ |
try { |
1405 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1406 |
+ |
assertTrue(r.isEmpty()); |
1407 |
+ |
} catch(Exception ex) { |
1408 |
+ |
unexpectedException(); |
1409 |
+ |
} finally { |
1410 |
+ |
joinPool(e); |
1411 |
+ |
} |
1412 |
+ |
} |
1413 |
+ |
|
1414 |
+ |
/** |
1415 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1416 |
+ |
*/ |
1417 |
+ |
public void testTimedInvokeAll3() { |
1418 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1419 |
+ |
try { |
1420 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1421 |
+ |
l.add(new StringTask()); |
1422 |
+ |
l.add(null); |
1423 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1424 |
+ |
} catch (NullPointerException success) { |
1425 |
+ |
} catch(Exception ex) { |
1426 |
+ |
unexpectedException(); |
1427 |
+ |
} finally { |
1428 |
+ |
joinPool(e); |
1429 |
+ |
} |
1430 |
+ |
} |
1431 |
+ |
|
1432 |
+ |
/** |
1433 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1434 |
+ |
*/ |
1435 |
+ |
public void testTimedInvokeAll4() { |
1436 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1437 |
+ |
try { |
1438 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1439 |
+ |
l.add(new NPETask()); |
1440 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1441 |
+ |
assertEquals(1, result.size()); |
1442 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1443 |
+ |
it.next().get(); |
1444 |
+ |
} catch(ExecutionException success) { |
1445 |
+ |
} catch(Exception ex) { |
1446 |
+ |
unexpectedException(); |
1447 |
+ |
} finally { |
1448 |
+ |
joinPool(e); |
1449 |
+ |
} |
1450 |
+ |
} |
1451 |
+ |
|
1452 |
+ |
/** |
1453 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1454 |
+ |
*/ |
1455 |
+ |
public void testTimedInvokeAll5() { |
1456 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1457 |
+ |
try { |
1458 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1459 |
+ |
l.add(new StringTask()); |
1460 |
+ |
l.add(new StringTask()); |
1461 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1462 |
+ |
assertEquals(2, result.size()); |
1463 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1464 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1465 |
+ |
} catch (ExecutionException success) { |
1466 |
+ |
} catch(Exception ex) { |
1467 |
+ |
unexpectedException(); |
1468 |
+ |
} finally { |
1469 |
+ |
joinPool(e); |
1470 |
+ |
} |
1471 |
+ |
} |
1472 |
+ |
|
1473 |
+ |
/** |
1474 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1475 |
+ |
*/ |
1476 |
+ |
public void testTimedInvokeAll6() { |
1477 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1478 |
+ |
try { |
1479 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1480 |
+ |
l.add(new StringTask()); |
1481 |
+ |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1482 |
+ |
l.add(new StringTask()); |
1483 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1484 |
+ |
assertEquals(3, result.size()); |
1485 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1486 |
+ |
Future<String> f1 = it.next(); |
1487 |
+ |
Future<String> f2 = it.next(); |
1488 |
+ |
Future<String> f3 = it.next(); |
1489 |
+ |
assertTrue(f1.isDone()); |
1490 |
+ |
assertTrue(f2.isDone()); |
1491 |
+ |
assertTrue(f3.isDone()); |
1492 |
+ |
assertFalse(f1.isCancelled()); |
1493 |
+ |
assertTrue(f2.isCancelled()); |
1494 |
+ |
} catch(Exception ex) { |
1495 |
+ |
unexpectedException(); |
1496 |
+ |
} finally { |
1497 |
+ |
joinPool(e); |
1498 |
+ |
} |
1499 |
+ |
} |
1500 |
+ |
|
1501 |
+ |
|
1502 |
|
} |