7 |
|
|
8 |
|
import java.util.concurrent.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.List; |
10 |
> |
import java.util.*; |
11 |
|
|
12 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
332 |
|
public void testGetQueue() { |
333 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
334 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
335 |
< |
CancellableTask[] tasks = new CancellableTask[5]; |
335 |
> |
FutureTask[] tasks = new FutureTask[5]; |
336 |
|
for(int i = 0; i < 5; i++){ |
337 |
< |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
337 |
> |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
338 |
|
p1.execute(tasks[i]); |
339 |
|
} |
340 |
|
try { |
357 |
|
public void testRemove() { |
358 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
359 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
360 |
< |
CancellableTask[] tasks = new CancellableTask[5]; |
360 |
> |
FutureTask[] tasks = new FutureTask[5]; |
361 |
|
for(int i = 0; i < 5; i++){ |
362 |
< |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
362 |
> |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
363 |
|
p1.execute(tasks[i]); |
364 |
|
} |
365 |
|
try { |
386 |
|
*/ |
387 |
|
public void testPurge() { |
388 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
389 |
< |
CancellableTask[] tasks = new CancellableTask[5]; |
389 |
> |
FutureTask[] tasks = new FutureTask[5]; |
390 |
|
for(int i = 0; i < 5; i++){ |
391 |
< |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
391 |
> |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
392 |
|
p1.execute(tasks[i]); |
393 |
|
} |
394 |
|
tasks[4].cancel(true); |
1025 |
|
joinPool(tpe); |
1026 |
|
} |
1027 |
|
} |
1028 |
+ |
|
1029 |
+ |
/** |
1030 |
+ |
* completed submit of callable returns result |
1031 |
+ |
*/ |
1032 |
+ |
public void testSubmitCallable() { |
1033 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1034 |
+ |
try { |
1035 |
+ |
Future<String> future = e.submit(new StringTask()); |
1036 |
+ |
String result = future.get(); |
1037 |
+ |
assertSame(TEST_STRING, result); |
1038 |
+ |
} |
1039 |
+ |
catch (ExecutionException ex) { |
1040 |
+ |
unexpectedException(); |
1041 |
+ |
} |
1042 |
+ |
catch (InterruptedException ex) { |
1043 |
+ |
unexpectedException(); |
1044 |
+ |
} finally { |
1045 |
+ |
joinPool(e); |
1046 |
+ |
} |
1047 |
+ |
} |
1048 |
+ |
|
1049 |
+ |
/** |
1050 |
+ |
* completed submit of runnable returns successfully |
1051 |
+ |
*/ |
1052 |
+ |
public void testSubmitRunnable() { |
1053 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1054 |
+ |
try { |
1055 |
+ |
Future<?> future = e.submit(new NoOpRunnable()); |
1056 |
+ |
future.get(); |
1057 |
+ |
assertTrue(future.isDone()); |
1058 |
+ |
} |
1059 |
+ |
catch (ExecutionException ex) { |
1060 |
+ |
unexpectedException(); |
1061 |
+ |
} |
1062 |
+ |
catch (InterruptedException ex) { |
1063 |
+ |
unexpectedException(); |
1064 |
+ |
} finally { |
1065 |
+ |
joinPool(e); |
1066 |
+ |
} |
1067 |
+ |
} |
1068 |
+ |
|
1069 |
+ |
/** |
1070 |
+ |
* completed submit of (runnable, result) returns result |
1071 |
+ |
*/ |
1072 |
+ |
public void testSubmitRunnable2() { |
1073 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1074 |
+ |
try { |
1075 |
+ |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1076 |
+ |
String result = future.get(); |
1077 |
+ |
assertSame(TEST_STRING, result); |
1078 |
+ |
} |
1079 |
+ |
catch (ExecutionException ex) { |
1080 |
+ |
unexpectedException(); |
1081 |
+ |
} |
1082 |
+ |
catch (InterruptedException ex) { |
1083 |
+ |
unexpectedException(); |
1084 |
+ |
} finally { |
1085 |
+ |
joinPool(e); |
1086 |
+ |
} |
1087 |
+ |
} |
1088 |
+ |
|
1089 |
+ |
|
1090 |
+ |
|
1091 |
+ |
|
1092 |
+ |
|
1093 |
+ |
/** |
1094 |
+ |
* invokeAny(null) throws NPE |
1095 |
+ |
*/ |
1096 |
+ |
public void testInvokeAny1() { |
1097 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1098 |
+ |
try { |
1099 |
+ |
e.invokeAny(null); |
1100 |
+ |
} catch (NullPointerException success) { |
1101 |
+ |
} catch(Exception ex) { |
1102 |
+ |
unexpectedException(); |
1103 |
+ |
} finally { |
1104 |
+ |
joinPool(e); |
1105 |
+ |
} |
1106 |
+ |
} |
1107 |
+ |
|
1108 |
+ |
/** |
1109 |
+ |
* invokeAny(empty collection) throws IAE |
1110 |
+ |
*/ |
1111 |
+ |
public void testInvokeAny2() { |
1112 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1113 |
+ |
try { |
1114 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
1115 |
+ |
} catch (IllegalArgumentException success) { |
1116 |
+ |
} catch(Exception ex) { |
1117 |
+ |
unexpectedException(); |
1118 |
+ |
} finally { |
1119 |
+ |
joinPool(e); |
1120 |
+ |
} |
1121 |
+ |
} |
1122 |
+ |
|
1123 |
+ |
/** |
1124 |
+ |
* invokeAny(c) throws NPE if c has null elements |
1125 |
+ |
*/ |
1126 |
+ |
public void testInvokeAny3() { |
1127 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1128 |
+ |
try { |
1129 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1130 |
+ |
l.add(new StringTask()); |
1131 |
+ |
l.add(null); |
1132 |
+ |
e.invokeAny(l); |
1133 |
+ |
} catch (NullPointerException success) { |
1134 |
+ |
} catch(Exception ex) { |
1135 |
+ |
unexpectedException(); |
1136 |
+ |
} finally { |
1137 |
+ |
joinPool(e); |
1138 |
+ |
} |
1139 |
+ |
} |
1140 |
+ |
|
1141 |
+ |
/** |
1142 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
1143 |
+ |
*/ |
1144 |
+ |
public void testInvokeAny4() { |
1145 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1146 |
+ |
try { |
1147 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1148 |
+ |
l.add(new NPETask()); |
1149 |
+ |
e.invokeAny(l); |
1150 |
+ |
} catch (ExecutionException success) { |
1151 |
+ |
} catch(Exception ex) { |
1152 |
+ |
unexpectedException(); |
1153 |
+ |
} finally { |
1154 |
+ |
joinPool(e); |
1155 |
+ |
} |
1156 |
+ |
} |
1157 |
+ |
|
1158 |
+ |
/** |
1159 |
+ |
* invokeAny(c) returns result of some task |
1160 |
+ |
*/ |
1161 |
+ |
public void testInvokeAny5() { |
1162 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1163 |
+ |
try { |
1164 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1165 |
+ |
l.add(new StringTask()); |
1166 |
+ |
l.add(new StringTask()); |
1167 |
+ |
String result = e.invokeAny(l); |
1168 |
+ |
assertSame(TEST_STRING, result); |
1169 |
+ |
} catch (ExecutionException success) { |
1170 |
+ |
} catch(Exception ex) { |
1171 |
+ |
unexpectedException(); |
1172 |
+ |
} finally { |
1173 |
+ |
joinPool(e); |
1174 |
+ |
} |
1175 |
+ |
} |
1176 |
+ |
|
1177 |
+ |
/** |
1178 |
+ |
* invokeAll(null) throws NPE |
1179 |
+ |
*/ |
1180 |
+ |
public void testInvokeAll1() { |
1181 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1182 |
+ |
try { |
1183 |
+ |
e.invokeAll(null); |
1184 |
+ |
} catch (NullPointerException success) { |
1185 |
+ |
} catch(Exception ex) { |
1186 |
+ |
unexpectedException(); |
1187 |
+ |
} finally { |
1188 |
+ |
joinPool(e); |
1189 |
+ |
} |
1190 |
+ |
} |
1191 |
+ |
|
1192 |
+ |
/** |
1193 |
+ |
* invokeAll(empty collection) returns empty collection |
1194 |
+ |
*/ |
1195 |
+ |
public void testInvokeAll2() { |
1196 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1197 |
+ |
try { |
1198 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1199 |
+ |
assertTrue(r.isEmpty()); |
1200 |
+ |
} catch(Exception ex) { |
1201 |
+ |
unexpectedException(); |
1202 |
+ |
} finally { |
1203 |
+ |
joinPool(e); |
1204 |
+ |
} |
1205 |
+ |
} |
1206 |
+ |
|
1207 |
+ |
/** |
1208 |
+ |
* invokeAll(c) throws NPE if c has null elements |
1209 |
+ |
*/ |
1210 |
+ |
public void testInvokeAll3() { |
1211 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1212 |
+ |
try { |
1213 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1214 |
+ |
l.add(new StringTask()); |
1215 |
+ |
l.add(null); |
1216 |
+ |
e.invokeAll(l); |
1217 |
+ |
} catch (NullPointerException success) { |
1218 |
+ |
} catch(Exception ex) { |
1219 |
+ |
unexpectedException(); |
1220 |
+ |
} finally { |
1221 |
+ |
joinPool(e); |
1222 |
+ |
} |
1223 |
+ |
} |
1224 |
+ |
|
1225 |
+ |
/** |
1226 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1227 |
+ |
*/ |
1228 |
+ |
public void testInvokeAll4() { |
1229 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1230 |
+ |
try { |
1231 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1232 |
+ |
l.add(new NPETask()); |
1233 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1234 |
+ |
assertEquals(1, result.size()); |
1235 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1236 |
+ |
it.next().get(); |
1237 |
+ |
} catch(ExecutionException success) { |
1238 |
+ |
} catch(Exception ex) { |
1239 |
+ |
unexpectedException(); |
1240 |
+ |
} finally { |
1241 |
+ |
joinPool(e); |
1242 |
+ |
} |
1243 |
+ |
} |
1244 |
+ |
|
1245 |
+ |
/** |
1246 |
+ |
* invokeAll(c) returns results of all completed tasks |
1247 |
+ |
*/ |
1248 |
+ |
public void testInvokeAll5() { |
1249 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1250 |
+ |
try { |
1251 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1252 |
+ |
l.add(new StringTask()); |
1253 |
+ |
l.add(new StringTask()); |
1254 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1255 |
+ |
assertEquals(2, result.size()); |
1256 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1257 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1258 |
+ |
} catch (ExecutionException success) { |
1259 |
+ |
} catch(Exception ex) { |
1260 |
+ |
unexpectedException(); |
1261 |
+ |
} finally { |
1262 |
+ |
joinPool(e); |
1263 |
+ |
} |
1264 |
+ |
} |
1265 |
+ |
|
1266 |
+ |
|
1267 |
+ |
|
1268 |
+ |
/** |
1269 |
+ |
* timed invokeAny(null) throws NPE |
1270 |
+ |
*/ |
1271 |
+ |
public void testTimedInvokeAny1() { |
1272 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1273 |
+ |
try { |
1274 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1275 |
+ |
} catch (NullPointerException success) { |
1276 |
+ |
} catch(Exception ex) { |
1277 |
+ |
unexpectedException(); |
1278 |
+ |
} finally { |
1279 |
+ |
joinPool(e); |
1280 |
+ |
} |
1281 |
+ |
} |
1282 |
+ |
|
1283 |
+ |
/** |
1284 |
+ |
* timed invokeAny(,,null) throws NPE |
1285 |
+ |
*/ |
1286 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
1287 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1288 |
+ |
try { |
1289 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1290 |
+ |
l.add(new StringTask()); |
1291 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1292 |
+ |
} catch (NullPointerException success) { |
1293 |
+ |
} catch(Exception ex) { |
1294 |
+ |
unexpectedException(); |
1295 |
+ |
} finally { |
1296 |
+ |
joinPool(e); |
1297 |
+ |
} |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
/** |
1301 |
+ |
* timed invokeAny(empty collection) throws IAE |
1302 |
+ |
*/ |
1303 |
+ |
public void testTimedInvokeAny2() { |
1304 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1305 |
+ |
try { |
1306 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1307 |
+ |
} catch (IllegalArgumentException success) { |
1308 |
+ |
} catch(Exception ex) { |
1309 |
+ |
unexpectedException(); |
1310 |
+ |
} finally { |
1311 |
+ |
joinPool(e); |
1312 |
+ |
} |
1313 |
+ |
} |
1314 |
+ |
|
1315 |
+ |
/** |
1316 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
1317 |
+ |
*/ |
1318 |
+ |
public void testTimedInvokeAny3() { |
1319 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1320 |
+ |
try { |
1321 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1322 |
+ |
l.add(new StringTask()); |
1323 |
+ |
l.add(null); |
1324 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1325 |
+ |
} catch (NullPointerException success) { |
1326 |
+ |
} catch(Exception ex) { |
1327 |
+ |
ex.printStackTrace(); |
1328 |
+ |
unexpectedException(); |
1329 |
+ |
} finally { |
1330 |
+ |
joinPool(e); |
1331 |
+ |
} |
1332 |
+ |
} |
1333 |
+ |
|
1334 |
+ |
/** |
1335 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
1336 |
+ |
*/ |
1337 |
+ |
public void testTimedInvokeAny4() { |
1338 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1339 |
+ |
try { |
1340 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1341 |
+ |
l.add(new NPETask()); |
1342 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1343 |
+ |
} catch(ExecutionException success) { |
1344 |
+ |
} catch(Exception ex) { |
1345 |
+ |
unexpectedException(); |
1346 |
+ |
} finally { |
1347 |
+ |
joinPool(e); |
1348 |
+ |
} |
1349 |
+ |
} |
1350 |
+ |
|
1351 |
+ |
/** |
1352 |
+ |
* timed invokeAny(c) returns result of some task |
1353 |
+ |
*/ |
1354 |
+ |
public void testTimedInvokeAny5() { |
1355 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1356 |
+ |
try { |
1357 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1358 |
+ |
l.add(new StringTask()); |
1359 |
+ |
l.add(new StringTask()); |
1360 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1361 |
+ |
assertSame(TEST_STRING, result); |
1362 |
+ |
} catch (ExecutionException success) { |
1363 |
+ |
} catch(Exception ex) { |
1364 |
+ |
unexpectedException(); |
1365 |
+ |
} finally { |
1366 |
+ |
joinPool(e); |
1367 |
+ |
} |
1368 |
+ |
} |
1369 |
+ |
|
1370 |
+ |
/** |
1371 |
+ |
* timed invokeAll(null) throws NPE |
1372 |
+ |
*/ |
1373 |
+ |
public void testTimedInvokeAll1() { |
1374 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1375 |
+ |
try { |
1376 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1377 |
+ |
} catch (NullPointerException success) { |
1378 |
+ |
} catch(Exception ex) { |
1379 |
+ |
unexpectedException(); |
1380 |
+ |
} finally { |
1381 |
+ |
joinPool(e); |
1382 |
+ |
} |
1383 |
+ |
} |
1384 |
+ |
|
1385 |
+ |
/** |
1386 |
+ |
* timed invokeAll(,,null) throws NPE |
1387 |
+ |
*/ |
1388 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
1389 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1390 |
+ |
try { |
1391 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1392 |
+ |
l.add(new StringTask()); |
1393 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1394 |
+ |
} catch (NullPointerException success) { |
1395 |
+ |
} catch(Exception ex) { |
1396 |
+ |
unexpectedException(); |
1397 |
+ |
} finally { |
1398 |
+ |
joinPool(e); |
1399 |
+ |
} |
1400 |
+ |
} |
1401 |
+ |
|
1402 |
+ |
/** |
1403 |
+ |
* timed invokeAll(empty collection) returns empty collection |
1404 |
+ |
*/ |
1405 |
+ |
public void testTimedInvokeAll2() { |
1406 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1407 |
+ |
try { |
1408 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1409 |
+ |
assertTrue(r.isEmpty()); |
1410 |
+ |
} catch(Exception ex) { |
1411 |
+ |
unexpectedException(); |
1412 |
+ |
} finally { |
1413 |
+ |
joinPool(e); |
1414 |
+ |
} |
1415 |
+ |
} |
1416 |
+ |
|
1417 |
+ |
/** |
1418 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1419 |
+ |
*/ |
1420 |
+ |
public void testTimedInvokeAll3() { |
1421 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1422 |
+ |
try { |
1423 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1424 |
+ |
l.add(new StringTask()); |
1425 |
+ |
l.add(null); |
1426 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1427 |
+ |
} catch (NullPointerException success) { |
1428 |
+ |
} catch(Exception ex) { |
1429 |
+ |
unexpectedException(); |
1430 |
+ |
} finally { |
1431 |
+ |
joinPool(e); |
1432 |
+ |
} |
1433 |
+ |
} |
1434 |
+ |
|
1435 |
+ |
/** |
1436 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1437 |
+ |
*/ |
1438 |
+ |
public void testTimedInvokeAll4() { |
1439 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1440 |
+ |
try { |
1441 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1442 |
+ |
l.add(new NPETask()); |
1443 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1444 |
+ |
assertEquals(1, result.size()); |
1445 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1446 |
+ |
it.next().get(); |
1447 |
+ |
} catch(ExecutionException success) { |
1448 |
+ |
} catch(Exception ex) { |
1449 |
+ |
unexpectedException(); |
1450 |
+ |
} finally { |
1451 |
+ |
joinPool(e); |
1452 |
+ |
} |
1453 |
+ |
} |
1454 |
+ |
|
1455 |
+ |
/** |
1456 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1457 |
+ |
*/ |
1458 |
+ |
public void testTimedInvokeAll5() { |
1459 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1460 |
+ |
try { |
1461 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1462 |
+ |
l.add(new StringTask()); |
1463 |
+ |
l.add(new StringTask()); |
1464 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1465 |
+ |
assertEquals(2, result.size()); |
1466 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1467 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1468 |
+ |
} catch (ExecutionException success) { |
1469 |
+ |
} catch(Exception ex) { |
1470 |
+ |
unexpectedException(); |
1471 |
+ |
} finally { |
1472 |
+ |
joinPool(e); |
1473 |
+ |
} |
1474 |
+ |
} |
1475 |
+ |
|
1476 |
+ |
/** |
1477 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1478 |
+ |
*/ |
1479 |
+ |
public void testTimedInvokeAll6() { |
1480 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1481 |
+ |
try { |
1482 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1483 |
+ |
l.add(new StringTask()); |
1484 |
+ |
l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING)); |
1485 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1486 |
+ |
assertEquals(2, result.size()); |
1487 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1488 |
+ |
Future<String> f1 = it.next(); |
1489 |
+ |
Future<String> f2 = it.next(); |
1490 |
+ |
assertTrue(f1.isDone()); |
1491 |
+ |
assertFalse(f1.isCancelled()); |
1492 |
+ |
assertTrue(f2.isDone()); |
1493 |
+ |
assertTrue(f2.isCancelled()); |
1494 |
+ |
} catch(Exception ex) { |
1495 |
+ |
unexpectedException(); |
1496 |
+ |
} finally { |
1497 |
+ |
joinPool(e); |
1498 |
+ |
} |
1499 |
+ |
} |
1500 |
+ |
|
1501 |
+ |
|
1502 |
|
} |