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