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 |
return new TestSuite(AbstractExecutorServiceTest.class); |
21 |
} |
22 |
|
23 |
/** |
24 |
* A no-frills implementation of AbstractExecutorService, designed |
25 |
* to test the submit methods only. |
26 |
*/ |
27 |
static class DirectExecutorService extends AbstractExecutorService { |
28 |
public void execute(Runnable r) { r.run(); } |
29 |
public void shutdown() { shutdown = true; } |
30 |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
31 |
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 |
Future<?> future = e.submit(task); |
46 |
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 |
* completed submit of callable returns result |
60 |
*/ |
61 |
public void testSubmitCallable() { |
62 |
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 |
/** |
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 |
|
113 |
/** |
114 |
* submit of a privileged action runs it to completion |
115 |
*/ |
116 |
public void testSubmitPrivilegedAction() { |
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); |
122 |
try { |
123 |
ExecutorService e = new DirectExecutorService(); |
124 |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
125 |
public Object run() { |
126 |
return TEST_STRING; |
127 |
}})); |
128 |
|
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 |
* submit of a privileged exception action runs it to completion |
145 |
*/ |
146 |
public void testSubmitPrivilegedExceptionAction() { |
147 |
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 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
155 |
public Object run() { |
156 |
return TEST_STRING; |
157 |
}})); |
158 |
|
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 |
* submit of a failed privileged exception action reports exception |
175 |
*/ |
176 |
public void testSubmitFailedPrivilegedExceptionAction() { |
177 |
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 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
185 |
public Object run() throws Exception { |
186 |
throw new IndexOutOfBoundsException(); |
187 |
}})); |
188 |
|
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 |
Future<?> future = e.submit(task); |
210 |
shouldThrow(); |
211 |
} |
212 |
catch (NullPointerException success) { |
213 |
} |
214 |
catch (Exception ex) { |
215 |
unexpectedException(); |
216 |
} |
217 |
} |
218 |
|
219 |
|
220 |
/** |
221 |
* submit of a null callable throws NPE |
222 |
*/ |
223 |
public void testSubmitNullCallable() { |
224 |
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 |
* submit of Runnable throws RejectedExecutionException if |
239 |
* saturated. |
240 |
*/ |
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 |
p.submit(new MediumRunnable()); |
247 |
} |
248 |
shouldThrow(); |
249 |
} catch(RejectedExecutionException success){} |
250 |
joinPool(p); |
251 |
} |
252 |
|
253 |
/** |
254 |
* Completed submit of Callable throws RejectedExecutionException |
255 |
* 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 |
* blocking on submit of Callable throws InterruptedException if |
271 |
* caller interrupted. |
272 |
*/ |
273 |
public void testInterruptedSubmit() { |
274 |
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 |
p.submit(new Callable<Object>() { |
279 |
public Object call() { |
280 |
try { |
281 |
Thread.sleep(MEDIUM_DELAY_MS); |
282 |
shouldThrow(); |
283 |
} catch(InterruptedException e){ |
284 |
} |
285 |
return null; |
286 |
} |
287 |
}).get(); |
288 |
} 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 |
* get of submit of Callable throws Exception if callable |
307 |
* interrupted |
308 |
*/ |
309 |
public void testSubmitIE() { |
310 |
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 |
p.submit(new SmallCallable()).get(); |
316 |
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 |
* completed submit of Callable throws ExecutionException if |
347 |
* callable throws exception |
348 |
*/ |
349 |
public void testSubmitEE() { |
350 |
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 |
p.submit(c).get(); |
362 |
} |
363 |
|
364 |
shouldThrow(); |
365 |
} |
366 |
catch(ExecutionException success){ |
367 |
} catch(Exception e) { |
368 |
unexpectedException(); |
369 |
} |
370 |
joinPool(p); |
371 |
} |
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 |
ex.printStackTrace(); |
416 |
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 |
List<Future<String>> result = e.invokeAll(l); |
431 |
assertEquals(1, result.size()); |
432 |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
433 |
it.next().get(); |
434 |
} catch(ExecutionException success) { |
435 |
} catch(Exception ex) { |
436 |
unexpectedException(); |
437 |
} finally { |
438 |
joinPool(e); |
439 |
} |
440 |
} |
441 |
|
442 |
/** |
443 |
* invokeAny(c) returns result of some task |
444 |
*/ |
445 |
public void testInvokeAny5() { |
446 |
ExecutorService e = new DirectExecutorService(); |
447 |
try { |
448 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
449 |
l.add(new StringTask()); |
450 |
l.add(new StringTask()); |
451 |
String result = e.invokeAny(l); |
452 |
assertSame(TEST_STRING, result); |
453 |
} catch (ExecutionException success) { |
454 |
} catch(Exception ex) { |
455 |
unexpectedException(); |
456 |
} finally { |
457 |
joinPool(e); |
458 |
} |
459 |
} |
460 |
|
461 |
/** |
462 |
* invokeAll(null) throws NPE |
463 |
*/ |
464 |
public void testInvokeAll1() { |
465 |
ExecutorService e = new DirectExecutorService(); |
466 |
try { |
467 |
e.invokeAll(null); |
468 |
} catch (NullPointerException success) { |
469 |
} catch(Exception ex) { |
470 |
unexpectedException(); |
471 |
} finally { |
472 |
joinPool(e); |
473 |
} |
474 |
} |
475 |
|
476 |
/** |
477 |
* invokeAll(empty collection) returns empty collection |
478 |
*/ |
479 |
public void testInvokeAll2() { |
480 |
ExecutorService e = new DirectExecutorService(); |
481 |
try { |
482 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
483 |
assertTrue(r.isEmpty()); |
484 |
} catch(Exception ex) { |
485 |
unexpectedException(); |
486 |
} finally { |
487 |
joinPool(e); |
488 |
} |
489 |
} |
490 |
|
491 |
/** |
492 |
* invokeAll(c) throws NPE if c has null elements |
493 |
*/ |
494 |
public void testInvokeAll3() { |
495 |
ExecutorService e = new DirectExecutorService(); |
496 |
try { |
497 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
498 |
l.add(new StringTask()); |
499 |
l.add(null); |
500 |
e.invokeAll(l); |
501 |
} catch (NullPointerException success) { |
502 |
} catch(Exception ex) { |
503 |
unexpectedException(); |
504 |
} finally { |
505 |
joinPool(e); |
506 |
} |
507 |
} |
508 |
|
509 |
/** |
510 |
* get of element of invokeAll(c) throws exception on failed task |
511 |
*/ |
512 |
public void testInvokeAll4() { |
513 |
ExecutorService e = new DirectExecutorService(); |
514 |
try { |
515 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
516 |
l.add(new NPETask()); |
517 |
List<Future<String>> result = e.invokeAll(l); |
518 |
assertEquals(1, result.size()); |
519 |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
520 |
it.next().get(); |
521 |
} catch(ExecutionException success) { |
522 |
} catch(Exception ex) { |
523 |
unexpectedException(); |
524 |
} finally { |
525 |
joinPool(e); |
526 |
} |
527 |
} |
528 |
|
529 |
/** |
530 |
* invokeAll(c) returns results of all completed tasks |
531 |
*/ |
532 |
public void testInvokeAll5() { |
533 |
ExecutorService e = new DirectExecutorService(); |
534 |
try { |
535 |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
536 |
l.add(new StringTask()); |
537 |
l.add(new StringTask()); |
538 |
List<Future<String>> result = e.invokeAll(l); |
539 |
assertEquals(2, result.size()); |
540 |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
541 |
assertSame(TEST_STRING, it.next().get()); |
542 |
} catch (ExecutionException success) { |
543 |
} catch(Exception ex) { |
544 |
unexpectedException(); |
545 |
} finally { |
546 |
joinPool(e); |
547 |
} |
548 |
} |
549 |
|
550 |
} |