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 |
|
|
18 |
|
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(ExecutorsTest.class); |
21 |
> |
return new TestSuite(AbstractExecutorServiceTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
25 |
|
* A no-frills implementation of AbstractExecutorService, designed |
26 |
< |
* to test the submit/invoke methods only. |
26 |
> |
* to test the submit methods only. |
27 |
|
*/ |
28 |
|
static class DirectExecutorService extends AbstractExecutorService { |
29 |
|
public void execute(Runnable r) { r.run(); } |
35 |
|
private volatile boolean shutdown = false; |
36 |
|
} |
37 |
|
|
37 |
– |
private static final String TEST_STRING = "a test string"; |
38 |
– |
|
39 |
– |
private static class StringTask implements Callable<String> { |
40 |
– |
public String call() { return TEST_STRING; } |
41 |
– |
} |
42 |
– |
|
38 |
|
/** |
39 |
|
* execute of runnable runs it to completion |
40 |
|
*/ |
43 |
|
ExecutorService e = new DirectExecutorService(); |
44 |
|
TrackedShortRunnable task = new TrackedShortRunnable(); |
45 |
|
assertFalse(task.done); |
46 |
< |
Future<?> future = e.submit(task, null); |
46 |
> |
Future<?> future = e.submit(task); |
47 |
|
future.get(); |
48 |
|
assertTrue(task.done); |
49 |
|
} |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
+ |
|
59 |
|
/** |
60 |
< |
* invoke of a runnable runs it to completion |
60 |
> |
* completed submit of callable returns result |
61 |
|
*/ |
62 |
< |
public void testInvokeRunnable() { |
62 |
> |
public void testSubmitCallable() { |
63 |
|
try { |
64 |
|
ExecutorService e = new DirectExecutorService(); |
65 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
66 |
< |
assertFalse(task.done); |
67 |
< |
e.invoke(task); |
72 |
< |
assertTrue(task.done); |
65 |
> |
Future<String> future = e.submit(new StringTask()); |
66 |
> |
String result = future.get(); |
67 |
> |
assertSame(TEST_STRING, result); |
68 |
|
} |
69 |
|
catch (ExecutionException ex) { |
70 |
|
unexpectedException(); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* execute of a callable runs it to completion |
78 |
> |
* completed submit of runnable returns successfully |
79 |
|
*/ |
80 |
< |
public void testExecuteCallable() { |
80 |
> |
public void testSubmitRunnable() { |
81 |
|
try { |
82 |
|
ExecutorService e = new DirectExecutorService(); |
83 |
< |
Future<String> future = e.submit(new StringTask()); |
83 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
84 |
> |
future.get(); |
85 |
> |
assertTrue(future.isDone()); |
86 |
> |
} |
87 |
> |
catch (ExecutionException ex) { |
88 |
> |
unexpectedException(); |
89 |
> |
} |
90 |
> |
catch (InterruptedException ex) { |
91 |
> |
unexpectedException(); |
92 |
> |
} |
93 |
> |
} |
94 |
> |
|
95 |
> |
/** |
96 |
> |
* completed submit of (runnable, result) returns result |
97 |
> |
*/ |
98 |
> |
public void testSubmitRunnable2() { |
99 |
> |
try { |
100 |
> |
ExecutorService e = new DirectExecutorService(); |
101 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
102 |
|
String result = future.get(); |
103 |
|
assertSame(TEST_STRING, result); |
104 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
/** |
115 |
< |
* execute of a privileged action runs it to completion |
115 |
> |
* submit of a privileged action runs it to completion |
116 |
|
*/ |
117 |
< |
public void testExecutePrivilegedAction() { |
117 |
> |
public void testSubmitPrivilegedAction() { |
118 |
|
Policy savedPolicy = Policy.getPolicy(); |
119 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
120 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
122 |
|
Policy.setPolicy(policy); |
123 |
|
try { |
124 |
|
ExecutorService e = new DirectExecutorService(); |
125 |
< |
Future future = e.submit(new PrivilegedAction() { |
125 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
126 |
|
public Object run() { |
127 |
|
return TEST_STRING; |
128 |
< |
}}); |
128 |
> |
}})); |
129 |
|
|
130 |
|
Object result = future.get(); |
131 |
|
assertSame(TEST_STRING, result); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* execute of a privileged exception action runs it to completion |
145 |
> |
* submit of a privileged exception action runs it to completion |
146 |
|
*/ |
147 |
< |
public void testExecutePrivilegedExceptionAction() { |
147 |
> |
public void testSubmitPrivilegedExceptionAction() { |
148 |
|
Policy savedPolicy = Policy.getPolicy(); |
149 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
150 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
152 |
|
Policy.setPolicy(policy); |
153 |
|
try { |
154 |
|
ExecutorService e = new DirectExecutorService(); |
155 |
< |
Future future = e.submit(new PrivilegedExceptionAction() { |
155 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
156 |
|
public Object run() { |
157 |
|
return TEST_STRING; |
158 |
< |
}}); |
158 |
> |
}})); |
159 |
|
|
160 |
|
Object result = future.get(); |
161 |
|
assertSame(TEST_STRING, result); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* execute of a failed privileged exception action reports exception |
175 |
> |
* submit of a failed privileged exception action reports exception |
176 |
|
*/ |
177 |
< |
public void testExecuteFailedPrivilegedExceptionAction() { |
177 |
> |
public void testSubmitFailedPrivilegedExceptionAction() { |
178 |
|
Policy savedPolicy = Policy.getPolicy(); |
179 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
180 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
182 |
|
Policy.setPolicy(policy); |
183 |
|
try { |
184 |
|
ExecutorService e = new DirectExecutorService(); |
185 |
< |
Future future = e.submit(new PrivilegedExceptionAction() { |
185 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
186 |
|
public Object run() throws Exception { |
187 |
|
throw new IndexOutOfBoundsException(); |
188 |
< |
}}); |
188 |
> |
}})); |
189 |
|
|
190 |
|
Object result = future.get(); |
191 |
|
shouldThrow(); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
191 |
– |
* invoke of a collable runs it to completion |
192 |
– |
*/ |
193 |
– |
public void testInvokeCallable() { |
194 |
– |
try { |
195 |
– |
ExecutorService e = new DirectExecutorService(); |
196 |
– |
String result = e.invoke(new StringTask()); |
197 |
– |
|
198 |
– |
assertSame(TEST_STRING, result); |
199 |
– |
} |
200 |
– |
catch (ExecutionException ex) { |
201 |
– |
unexpectedException(); |
202 |
– |
} |
203 |
– |
catch (InterruptedException ex) { |
204 |
– |
unexpectedException(); |
205 |
– |
} |
206 |
– |
} |
207 |
– |
|
208 |
– |
/** |
204 |
|
* execute with a null runnable throws NPE |
205 |
|
*/ |
206 |
|
public void testExecuteNullRunnable() { |
207 |
|
try { |
208 |
|
ExecutorService e = new DirectExecutorService(); |
209 |
|
TrackedShortRunnable task = null; |
210 |
< |
Future<?> future = e.submit(task, null); |
210 |
> |
Future<?> future = e.submit(task); |
211 |
|
shouldThrow(); |
212 |
|
} |
213 |
|
catch (NullPointerException success) { |
217 |
|
} |
218 |
|
} |
219 |
|
|
225 |
– |
/** |
226 |
– |
* invoke of a null runnable throws NPE |
227 |
– |
*/ |
228 |
– |
public void testInvokeNullRunnable() { |
229 |
– |
try { |
230 |
– |
ExecutorService e = new DirectExecutorService(); |
231 |
– |
TrackedShortRunnable task = null; |
232 |
– |
e.invoke(task); |
233 |
– |
shouldThrow(); |
234 |
– |
} |
235 |
– |
catch (NullPointerException success) { |
236 |
– |
} |
237 |
– |
catch (Exception ex) { |
238 |
– |
unexpectedException(); |
239 |
– |
} |
240 |
– |
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* execute of a null callable throws NPE |
222 |
> |
* submit of a null callable throws NPE |
223 |
|
*/ |
224 |
< |
public void testExecuteNullCallable() { |
224 |
> |
public void testSubmitNullCallable() { |
225 |
|
try { |
226 |
|
ExecutorService e = new DirectExecutorService(); |
227 |
|
StringTask t = null; |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
< |
* invoke of a null callable throws NPE |
240 |
< |
*/ |
262 |
< |
public void testInvokeNullCallable() { |
263 |
< |
try { |
264 |
< |
ExecutorService e = new DirectExecutorService(); |
265 |
< |
StringTask t = null; |
266 |
< |
String result = e.invoke(t); |
267 |
< |
shouldThrow(); |
268 |
< |
} |
269 |
< |
catch (NullPointerException success) { |
270 |
< |
} |
271 |
< |
catch (Exception ex) { |
272 |
< |
unexpectedException(); |
273 |
< |
} |
274 |
< |
} |
275 |
< |
|
276 |
< |
/** |
277 |
< |
* execute(Executor, Runnable) throws RejectedExecutionException |
278 |
< |
* if saturated. |
239 |
> |
* submit of Runnable throws RejectedExecutionException if |
240 |
> |
* saturated. |
241 |
|
*/ |
242 |
|
public void testExecute1() { |
243 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
244 |
|
try { |
245 |
|
|
246 |
|
for(int i = 0; i < 5; ++i){ |
247 |
< |
p.submit(new MediumRunnable(), null); |
247 |
> |
p.submit(new MediumRunnable()); |
248 |
|
} |
249 |
|
shouldThrow(); |
250 |
|
} catch(RejectedExecutionException success){} |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* execute(Executor, Callable)throws RejectedExecutionException |
255 |
> |
* Completed submit of Callable throws RejectedExecutionException |
256 |
|
* if saturated. |
257 |
|
*/ |
258 |
|
public void testExecute2() { |
268 |
|
|
269 |
|
|
270 |
|
/** |
271 |
< |
* invoke(Executor, Runnable) throws InterruptedException if |
271 |
> |
* blocking on submit of Callable throws InterruptedException if |
272 |
|
* caller interrupted. |
273 |
|
*/ |
274 |
< |
public void testInterruptedInvoke() { |
274 |
> |
public void testInterruptedSubmit() { |
275 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
|
Thread t = new Thread(new Runnable() { |
277 |
|
public void run() { |
278 |
|
try { |
279 |
< |
p.invoke(new Runnable() { |
280 |
< |
public void run() { |
279 |
> |
p.submit(new Callable<Object>() { |
280 |
> |
public Object call() { |
281 |
|
try { |
282 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
283 |
|
shouldThrow(); |
284 |
|
} catch(InterruptedException e){ |
285 |
|
} |
286 |
+ |
return null; |
287 |
|
} |
288 |
< |
}); |
288 |
> |
}).get(); |
289 |
|
} catch(InterruptedException success){ |
290 |
|
} catch(Exception e) { |
291 |
|
unexpectedException(); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* invoke(Executor, Runnable) throws ExecutionException if |
308 |
< |
* runnable throws exception. |
346 |
< |
*/ |
347 |
< |
public void testInvoke3() { |
348 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
349 |
< |
try { |
350 |
< |
Runnable r = new Runnable() { |
351 |
< |
public void run() { |
352 |
< |
int i = 5/0; |
353 |
< |
} |
354 |
< |
}; |
355 |
< |
|
356 |
< |
for(int i =0; i < 5; i++){ |
357 |
< |
p.invoke(r); |
358 |
< |
} |
359 |
< |
|
360 |
< |
shouldThrow(); |
361 |
< |
} catch(ExecutionException success){ |
362 |
< |
} catch(Exception e){ |
363 |
< |
unexpectedException(); |
364 |
< |
} |
365 |
< |
joinPool(p); |
366 |
< |
} |
367 |
< |
|
368 |
< |
|
369 |
< |
|
370 |
< |
/** |
371 |
< |
* invoke(Executor, Callable) throws InterruptedException if |
372 |
< |
* callable throws exception |
307 |
> |
* get of submit of Callable throws Exception if callable |
308 |
> |
* interrupted |
309 |
|
*/ |
310 |
< |
public void testInvoke5() { |
310 |
> |
public void testSubmitIE() { |
311 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
312 |
|
|
313 |
|
final Callable c = new Callable() { |
314 |
|
public Object call() { |
315 |
|
try { |
316 |
< |
p.invoke(new SmallCallable()); |
316 |
> |
p.submit(new SmallCallable()).get(); |
317 |
|
shouldThrow(); |
318 |
|
} catch(InterruptedException e){} |
319 |
|
catch(RejectedExecutionException e2){} |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
348 |
< |
* if callable throws exception |
347 |
> |
* completed submit of Callable throws ExecutionException if |
348 |
> |
* callable throws exception |
349 |
|
*/ |
350 |
< |
public void testInvoke6() { |
350 |
> |
public void testSubmitEE() { |
351 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
352 |
|
|
353 |
|
try { |
359 |
|
}; |
360 |
|
|
361 |
|
for(int i =0; i < 5; i++){ |
362 |
< |
p.invoke(c); |
362 |
> |
p.submit(c).get(); |
363 |
|
} |
364 |
|
|
365 |
|
shouldThrow(); |
371 |
|
joinPool(p); |
372 |
|
} |
373 |
|
|
374 |
+ |
/** |
375 |
+ |
* invokeAny(null) throws NPE |
376 |
+ |
*/ |
377 |
+ |
public void testInvokeAny1() { |
378 |
+ |
ExecutorService e = new DirectExecutorService(); |
379 |
+ |
try { |
380 |
+ |
e.invokeAny(null); |
381 |
+ |
} catch (NullPointerException success) { |
382 |
+ |
} catch(Exception ex) { |
383 |
+ |
unexpectedException(); |
384 |
+ |
} finally { |
385 |
+ |
joinPool(e); |
386 |
+ |
} |
387 |
+ |
} |
388 |
+ |
|
389 |
+ |
/** |
390 |
+ |
* invokeAny(empty collection) throws IAE |
391 |
+ |
*/ |
392 |
+ |
public void testInvokeAny2() { |
393 |
+ |
ExecutorService e = new DirectExecutorService(); |
394 |
+ |
try { |
395 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
396 |
+ |
} catch (IllegalArgumentException success) { |
397 |
+ |
} catch(Exception ex) { |
398 |
+ |
unexpectedException(); |
399 |
+ |
} finally { |
400 |
+ |
joinPool(e); |
401 |
+ |
} |
402 |
+ |
} |
403 |
+ |
|
404 |
+ |
/** |
405 |
+ |
* invokeAny(c) throws NPE if c has null elements |
406 |
+ |
*/ |
407 |
+ |
public void testInvokeAny3() { |
408 |
+ |
ExecutorService e = new DirectExecutorService(); |
409 |
+ |
try { |
410 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
411 |
+ |
l.add(new StringTask()); |
412 |
+ |
l.add(null); |
413 |
+ |
e.invokeAny(l); |
414 |
+ |
} catch (NullPointerException success) { |
415 |
+ |
} catch(Exception ex) { |
416 |
+ |
ex.printStackTrace(); |
417 |
+ |
unexpectedException(); |
418 |
+ |
} finally { |
419 |
+ |
joinPool(e); |
420 |
+ |
} |
421 |
+ |
} |
422 |
+ |
|
423 |
+ |
/** |
424 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
425 |
+ |
*/ |
426 |
+ |
public void testInvokeAny4() { |
427 |
+ |
ExecutorService e = new DirectExecutorService(); |
428 |
+ |
try { |
429 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
430 |
+ |
l.add(new NPETask()); |
431 |
+ |
e.invokeAny(l); |
432 |
+ |
} catch(ExecutionException success) { |
433 |
+ |
} catch(Exception ex) { |
434 |
+ |
unexpectedException(); |
435 |
+ |
} finally { |
436 |
+ |
joinPool(e); |
437 |
+ |
} |
438 |
+ |
} |
439 |
+ |
|
440 |
+ |
/** |
441 |
+ |
* invokeAny(c) returns result of some task |
442 |
+ |
*/ |
443 |
+ |
public void testInvokeAny5() { |
444 |
+ |
ExecutorService e = new DirectExecutorService(); |
445 |
+ |
try { |
446 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
447 |
+ |
l.add(new StringTask()); |
448 |
+ |
l.add(new StringTask()); |
449 |
+ |
String result = e.invokeAny(l); |
450 |
+ |
assertSame(TEST_STRING, result); |
451 |
+ |
} catch (ExecutionException success) { |
452 |
+ |
} catch(Exception ex) { |
453 |
+ |
unexpectedException(); |
454 |
+ |
} finally { |
455 |
+ |
joinPool(e); |
456 |
+ |
} |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
/** |
460 |
+ |
* invokeAll(null) throws NPE |
461 |
+ |
*/ |
462 |
+ |
public void testInvokeAll1() { |
463 |
+ |
ExecutorService e = new DirectExecutorService(); |
464 |
+ |
try { |
465 |
+ |
e.invokeAll(null); |
466 |
+ |
} catch (NullPointerException success) { |
467 |
+ |
} catch(Exception ex) { |
468 |
+ |
unexpectedException(); |
469 |
+ |
} finally { |
470 |
+ |
joinPool(e); |
471 |
+ |
} |
472 |
+ |
} |
473 |
+ |
|
474 |
+ |
/** |
475 |
+ |
* invokeAll(empty collection) returns empty collection |
476 |
+ |
*/ |
477 |
+ |
public void testInvokeAll2() { |
478 |
+ |
ExecutorService e = new DirectExecutorService(); |
479 |
+ |
try { |
480 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
481 |
+ |
assertTrue(r.isEmpty()); |
482 |
+ |
} catch(Exception ex) { |
483 |
+ |
unexpectedException(); |
484 |
+ |
} finally { |
485 |
+ |
joinPool(e); |
486 |
+ |
} |
487 |
+ |
} |
488 |
+ |
|
489 |
+ |
/** |
490 |
+ |
* invokeAll(c) throws NPE if c has null elements |
491 |
+ |
*/ |
492 |
+ |
public void testInvokeAll3() { |
493 |
+ |
ExecutorService e = new DirectExecutorService(); |
494 |
+ |
try { |
495 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
496 |
+ |
l.add(new StringTask()); |
497 |
+ |
l.add(null); |
498 |
+ |
e.invokeAll(l); |
499 |
+ |
} catch (NullPointerException success) { |
500 |
+ |
} catch(Exception ex) { |
501 |
+ |
unexpectedException(); |
502 |
+ |
} finally { |
503 |
+ |
joinPool(e); |
504 |
+ |
} |
505 |
+ |
} |
506 |
+ |
|
507 |
+ |
/** |
508 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
509 |
+ |
*/ |
510 |
+ |
public void testInvokeAll4() { |
511 |
+ |
ExecutorService e = new DirectExecutorService(); |
512 |
+ |
try { |
513 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
514 |
+ |
l.add(new NPETask()); |
515 |
+ |
List<Future<String>> result = e.invokeAll(l); |
516 |
+ |
assertEquals(1, result.size()); |
517 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
518 |
+ |
it.next().get(); |
519 |
+ |
} catch(ExecutionException success) { |
520 |
+ |
} catch(Exception ex) { |
521 |
+ |
unexpectedException(); |
522 |
+ |
} finally { |
523 |
+ |
joinPool(e); |
524 |
+ |
} |
525 |
+ |
} |
526 |
+ |
|
527 |
+ |
/** |
528 |
+ |
* invokeAll(c) returns results of all completed tasks |
529 |
+ |
*/ |
530 |
+ |
public void testInvokeAll5() { |
531 |
+ |
ExecutorService e = new DirectExecutorService(); |
532 |
+ |
try { |
533 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
534 |
+ |
l.add(new StringTask()); |
535 |
+ |
l.add(new StringTask()); |
536 |
+ |
List<Future<String>> result = e.invokeAll(l); |
537 |
+ |
assertEquals(2, result.size()); |
538 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
539 |
+ |
assertSame(TEST_STRING, it.next().get()); |
540 |
+ |
} catch (ExecutionException success) { |
541 |
+ |
} catch(Exception ex) { |
542 |
+ |
unexpectedException(); |
543 |
+ |
} finally { |
544 |
+ |
joinPool(e); |
545 |
+ |
} |
546 |
+ |
} |
547 |
+ |
|
548 |
+ |
|
549 |
+ |
/** |
550 |
+ |
* timed invokeAny(null) throws NPE |
551 |
+ |
*/ |
552 |
+ |
public void testTimedInvokeAny1() { |
553 |
+ |
ExecutorService e = new DirectExecutorService(); |
554 |
+ |
try { |
555 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
556 |
+ |
} catch (NullPointerException success) { |
557 |
+ |
} catch(Exception ex) { |
558 |
+ |
unexpectedException(); |
559 |
+ |
} finally { |
560 |
+ |
joinPool(e); |
561 |
+ |
} |
562 |
+ |
} |
563 |
+ |
|
564 |
+ |
/** |
565 |
+ |
* timed invokeAny(,,null) throws NPE |
566 |
+ |
*/ |
567 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
568 |
+ |
ExecutorService e = new DirectExecutorService(); |
569 |
+ |
try { |
570 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
571 |
+ |
l.add(new StringTask()); |
572 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
573 |
+ |
} catch (NullPointerException success) { |
574 |
+ |
} catch(Exception ex) { |
575 |
+ |
unexpectedException(); |
576 |
+ |
} finally { |
577 |
+ |
joinPool(e); |
578 |
+ |
} |
579 |
+ |
} |
580 |
+ |
|
581 |
+ |
/** |
582 |
+ |
* timed invokeAny(empty collection) throws IAE |
583 |
+ |
*/ |
584 |
+ |
public void testTimedInvokeAny2() { |
585 |
+ |
ExecutorService e = new DirectExecutorService(); |
586 |
+ |
try { |
587 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
588 |
+ |
} catch (IllegalArgumentException success) { |
589 |
+ |
} catch(Exception ex) { |
590 |
+ |
unexpectedException(); |
591 |
+ |
} finally { |
592 |
+ |
joinPool(e); |
593 |
+ |
} |
594 |
+ |
} |
595 |
+ |
|
596 |
+ |
/** |
597 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
598 |
+ |
*/ |
599 |
+ |
public void testTimedInvokeAny3() { |
600 |
+ |
ExecutorService e = new DirectExecutorService(); |
601 |
+ |
try { |
602 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
603 |
+ |
l.add(new StringTask()); |
604 |
+ |
l.add(null); |
605 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
606 |
+ |
} catch (NullPointerException success) { |
607 |
+ |
} catch(Exception ex) { |
608 |
+ |
ex.printStackTrace(); |
609 |
+ |
unexpectedException(); |
610 |
+ |
} finally { |
611 |
+ |
joinPool(e); |
612 |
+ |
} |
613 |
+ |
} |
614 |
+ |
|
615 |
+ |
/** |
616 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
617 |
+ |
*/ |
618 |
+ |
public void testTimedInvokeAny4() { |
619 |
+ |
ExecutorService e = new DirectExecutorService(); |
620 |
+ |
try { |
621 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
622 |
+ |
l.add(new NPETask()); |
623 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
624 |
+ |
} catch(ExecutionException success) { |
625 |
+ |
} catch(Exception ex) { |
626 |
+ |
unexpectedException(); |
627 |
+ |
} finally { |
628 |
+ |
joinPool(e); |
629 |
+ |
} |
630 |
+ |
} |
631 |
+ |
|
632 |
+ |
/** |
633 |
+ |
* timed invokeAny(c) returns result of some task |
634 |
+ |
*/ |
635 |
+ |
public void testTimedInvokeAny5() { |
636 |
+ |
ExecutorService e = new DirectExecutorService(); |
637 |
+ |
try { |
638 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
639 |
+ |
l.add(new StringTask()); |
640 |
+ |
l.add(new StringTask()); |
641 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
642 |
+ |
assertSame(TEST_STRING, result); |
643 |
+ |
} catch (ExecutionException success) { |
644 |
+ |
} catch(Exception ex) { |
645 |
+ |
unexpectedException(); |
646 |
+ |
} finally { |
647 |
+ |
joinPool(e); |
648 |
+ |
} |
649 |
+ |
} |
650 |
+ |
|
651 |
+ |
/** |
652 |
+ |
* timed invokeAll(null) throws NPE |
653 |
+ |
*/ |
654 |
+ |
public void testTimedInvokeAll1() { |
655 |
+ |
ExecutorService e = new DirectExecutorService(); |
656 |
+ |
try { |
657 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
658 |
+ |
} catch (NullPointerException success) { |
659 |
+ |
} catch(Exception ex) { |
660 |
+ |
unexpectedException(); |
661 |
+ |
} finally { |
662 |
+ |
joinPool(e); |
663 |
+ |
} |
664 |
+ |
} |
665 |
+ |
|
666 |
+ |
/** |
667 |
+ |
* timed invokeAll(,,null) throws NPE |
668 |
+ |
*/ |
669 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
670 |
+ |
ExecutorService e = new DirectExecutorService(); |
671 |
+ |
try { |
672 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
673 |
+ |
l.add(new StringTask()); |
674 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
675 |
+ |
} catch (NullPointerException success) { |
676 |
+ |
} catch(Exception ex) { |
677 |
+ |
unexpectedException(); |
678 |
+ |
} finally { |
679 |
+ |
joinPool(e); |
680 |
+ |
} |
681 |
+ |
} |
682 |
+ |
|
683 |
+ |
/** |
684 |
+ |
* timed invokeAll(empty collection) returns empty collection |
685 |
+ |
*/ |
686 |
+ |
public void testTimedInvokeAll2() { |
687 |
+ |
ExecutorService e = new DirectExecutorService(); |
688 |
+ |
try { |
689 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
690 |
+ |
assertTrue(r.isEmpty()); |
691 |
+ |
} catch(Exception ex) { |
692 |
+ |
unexpectedException(); |
693 |
+ |
} finally { |
694 |
+ |
joinPool(e); |
695 |
+ |
} |
696 |
+ |
} |
697 |
+ |
|
698 |
+ |
/** |
699 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
700 |
+ |
*/ |
701 |
+ |
public void testTimedInvokeAll3() { |
702 |
+ |
ExecutorService e = new DirectExecutorService(); |
703 |
+ |
try { |
704 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
705 |
+ |
l.add(new StringTask()); |
706 |
+ |
l.add(null); |
707 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
708 |
+ |
} catch (NullPointerException success) { |
709 |
+ |
} catch(Exception ex) { |
710 |
+ |
unexpectedException(); |
711 |
+ |
} finally { |
712 |
+ |
joinPool(e); |
713 |
+ |
} |
714 |
+ |
} |
715 |
+ |
|
716 |
+ |
/** |
717 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
718 |
+ |
*/ |
719 |
+ |
public void testTimedInvokeAll4() { |
720 |
+ |
ExecutorService e = new DirectExecutorService(); |
721 |
+ |
try { |
722 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
723 |
+ |
l.add(new NPETask()); |
724 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
725 |
+ |
assertEquals(1, result.size()); |
726 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
727 |
+ |
it.next().get(); |
728 |
+ |
} catch(ExecutionException success) { |
729 |
+ |
} catch(Exception ex) { |
730 |
+ |
unexpectedException(); |
731 |
+ |
} finally { |
732 |
+ |
joinPool(e); |
733 |
+ |
} |
734 |
+ |
} |
735 |
+ |
|
736 |
+ |
/** |
737 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
738 |
+ |
*/ |
739 |
+ |
public void testTimedInvokeAll5() { |
740 |
+ |
ExecutorService e = new DirectExecutorService(); |
741 |
+ |
try { |
742 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
743 |
+ |
l.add(new StringTask()); |
744 |
+ |
l.add(new StringTask()); |
745 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
746 |
+ |
assertEquals(2, result.size()); |
747 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
748 |
+ |
assertSame(TEST_STRING, it.next().get()); |
749 |
+ |
} catch (ExecutionException success) { |
750 |
+ |
} catch(Exception ex) { |
751 |
+ |
unexpectedException(); |
752 |
+ |
} finally { |
753 |
+ |
joinPool(e); |
754 |
+ |
} |
755 |
+ |
} |
756 |
+ |
|
757 |
+ |
/** |
758 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
759 |
+ |
*/ |
760 |
+ |
public void testTimedInvokeAll6() { |
761 |
+ |
ExecutorService e = new DirectExecutorService(); |
762 |
+ |
try { |
763 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
764 |
+ |
l.add(new StringTask()); |
765 |
+ |
l.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
766 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
767 |
+ |
assertEquals(2, result.size()); |
768 |
+ |
Iterator<Future<String>> it = result.iterator(); |
769 |
+ |
Future<String> f1 = it.next(); |
770 |
+ |
Future<String> f2 = it.next(); |
771 |
+ |
assertTrue(f1.isDone()); |
772 |
+ |
assertFalse(f1.isCancelled()); |
773 |
+ |
assertTrue(f2.isDone()); |
774 |
+ |
// assertTrue(f2.isCancelled()); |
775 |
+ |
} catch(Exception ex) { |
776 |
+ |
unexpectedException(); |
777 |
+ |
} finally { |
778 |
+ |
joinPool(e); |
779 |
+ |
} |
780 |
+ |
} |
781 |
+ |
|
782 |
|
} |