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