ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.34
Committed: Wed Sep 25 07:39:17 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +2 -3 lines
Log Message:
cosmetic changes

File Contents

# User Rev Content
1 tim 1.1 /*
2 dl 1.10 * 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 jsr166 1.30 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.16 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 tim 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 jsr166 1.32 import java.util.concurrent.atomic.AtomicBoolean;
13 jsr166 1.20 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 tim 1.1 import java.security.*;
15    
16 jsr166 1.18 public class AbstractExecutorServiceTest extends JSR166TestCase {
17 tim 1.1 public static void main(String[] args) {
18 jsr166 1.24 junit.textui.TestRunner.run(suite());
19 tim 1.1 }
20     public static Test suite() {
21 dl 1.6 return new TestSuite(AbstractExecutorServiceTest.class);
22 tim 1.1 }
23    
24 jsr166 1.16 /**
25 tim 1.1 * A no-frills implementation of AbstractExecutorService, designed
26 dl 1.6 * to test the submit methods only.
27 tim 1.1 */
28     static class DirectExecutorService extends AbstractExecutorService {
29     public void execute(Runnable r) { r.run(); }
30     public void shutdown() { shutdown = true; }
31 jsr166 1.26 public List<Runnable> shutdownNow() {
32     shutdown = true;
33     return Collections.EMPTY_LIST;
34     }
35 tim 1.1 public boolean isShutdown() { return shutdown; }
36     public boolean isTerminated() { return isShutdown(); }
37 jsr166 1.26 public boolean awaitTermination(long timeout, TimeUnit unit) {
38     return isShutdown();
39     }
40 tim 1.1 private volatile boolean shutdown = false;
41     }
42    
43     /**
44 dl 1.12 * execute(runnable) runs it to completion
45 tim 1.1 */
46 jsr166 1.19 public void testExecuteRunnable() throws Exception {
47     ExecutorService e = new DirectExecutorService();
48 jsr166 1.32 final AtomicBoolean done = new AtomicBoolean(false);
49 jsr166 1.34 Future<?> future = e.submit(new CheckedRunnable() {
50 jsr166 1.32 public void realRun() {
51     done.set(true);
52 jsr166 1.34 }});
53 jsr166 1.32 assertNull(future.get());
54     assertNull(future.get(0, MILLISECONDS));
55     assertTrue(done.get());
56     assertTrue(future.isDone());
57     assertFalse(future.isCancelled());
58 tim 1.1 }
59    
60     /**
61 dl 1.12 * Completed submit(callable) returns result
62 tim 1.1 */
63 jsr166 1.19 public void testSubmitCallable() throws Exception {
64     ExecutorService e = new DirectExecutorService();
65     Future<String> future = e.submit(new StringTask());
66     String result = future.get();
67     assertSame(TEST_STRING, result);
68 tim 1.1 }
69    
70 dl 1.6 /**
71 dl 1.12 * Completed submit(runnable) returns successfully
72 dl 1.6 */
73 jsr166 1.19 public void testSubmitRunnable() throws Exception {
74     ExecutorService e = new DirectExecutorService();
75     Future<?> future = e.submit(new NoOpRunnable());
76     future.get();
77     assertTrue(future.isDone());
78 dl 1.6 }
79    
80     /**
81 dl 1.12 * Completed submit(runnable, result) returns result
82 dl 1.6 */
83 jsr166 1.19 public void testSubmitRunnable2() throws Exception {
84     ExecutorService e = new DirectExecutorService();
85     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
86     String result = future.get();
87     assertSame(TEST_STRING, result);
88 dl 1.6 }
89    
90 tim 1.1 /**
91 jsr166 1.23 * A submitted privileged action runs to completion
92 tim 1.1 */
93 jsr166 1.19 public void testSubmitPrivilegedAction() throws Exception {
94 jsr166 1.23 Runnable r = new CheckedRunnable() {
95     public void realRun() throws Exception {
96     ExecutorService e = new DirectExecutorService();
97     Future future = e.submit(Executors.callable(new PrivilegedAction() {
98 tim 1.1 public Object run() {
99     return TEST_STRING;
100 dl 1.5 }}));
101 tim 1.1
102 jsr166 1.23 assertSame(TEST_STRING, future.get());
103     }};
104    
105     runWithPermissions(r,
106     new RuntimePermission("getClassLoader"),
107     new RuntimePermission("setContextClassLoader"),
108     new RuntimePermission("modifyThread"));
109 tim 1.1 }
110    
111     /**
112 jsr166 1.23 * A submitted privileged exception action runs to completion
113 tim 1.1 */
114 jsr166 1.19 public void testSubmitPrivilegedExceptionAction() throws Exception {
115 jsr166 1.23 Runnable r = new CheckedRunnable() {
116     public void realRun() throws Exception {
117     ExecutorService e = new DirectExecutorService();
118     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
119 tim 1.1 public Object run() {
120     return TEST_STRING;
121 dl 1.5 }}));
122 tim 1.1
123 jsr166 1.23 assertSame(TEST_STRING, future.get());
124     }};
125    
126     runWithPermissions(r);
127 tim 1.1 }
128    
129     /**
130 dl 1.12 * A submitted failed privileged exception action reports exception
131 tim 1.1 */
132 jsr166 1.19 public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
133 jsr166 1.23 Runnable r = new CheckedRunnable() {
134     public void realRun() throws Exception {
135     ExecutorService e = new DirectExecutorService();
136     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
137 tim 1.1 public Object run() throws Exception {
138     throw new IndexOutOfBoundsException();
139 dl 1.5 }}));
140 tim 1.1
141 jsr166 1.23 try {
142     future.get();
143     shouldThrow();
144     } catch (ExecutionException success) {
145     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
146     }}};
147    
148     runWithPermissions(r);
149 tim 1.1 }
150    
151     /**
152 dl 1.12 * execute(null runnable) throws NPE
153 tim 1.1 */
154     public void testExecuteNullRunnable() {
155     try {
156     ExecutorService e = new DirectExecutorService();
157 jsr166 1.19 e.submit((Runnable) null);
158 tim 1.1 shouldThrow();
159 jsr166 1.19 } catch (NullPointerException success) {}
160 tim 1.1 }
161    
162     /**
163 dl 1.12 * submit(null callable) throws NPE
164 tim 1.1 */
165 dl 1.6 public void testSubmitNullCallable() {
166 tim 1.1 try {
167     ExecutorService e = new DirectExecutorService();
168 jsr166 1.19 e.submit((Callable) null);
169 tim 1.1 shouldThrow();
170 jsr166 1.19 } catch (NullPointerException success) {}
171 tim 1.1 }
172    
173     /**
174 jsr166 1.25 * submit(callable).get() throws InterruptedException if interrupted
175 tim 1.1 */
176 jsr166 1.19 public void testInterruptedSubmit() throws InterruptedException {
177 jsr166 1.25 final CountDownLatch submitted = new CountDownLatch(1);
178     final CountDownLatch quittingTime = new CountDownLatch(1);
179     final ExecutorService p
180     = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
181     new ArrayBlockingQueue<Runnable>(10));
182     final Callable<Void> awaiter = new CheckedCallable<Void>() {
183     public Void realCall() throws InterruptedException {
184     quittingTime.await();
185     return null;
186     }};
187     try {
188     Thread t = new Thread(new CheckedInterruptedRunnable() {
189     public void realRun() throws Exception {
190     Future<Void> future = p.submit(awaiter);
191     submitted.countDown();
192     future.get();
193     }});
194     t.start();
195     submitted.await();
196     t.interrupt();
197     t.join();
198     } finally {
199     quittingTime.countDown();
200     joinPool(p);
201     }
202 tim 1.1 }
203    
204     /**
205 jsr166 1.27 * get of submit(callable) throws ExecutionException if callable
206     * throws exception
207 tim 1.1 */
208 jsr166 1.19 public void testSubmitEE() throws InterruptedException {
209     ThreadPoolExecutor p =
210     new ThreadPoolExecutor(1, 1,
211     60, TimeUnit.SECONDS,
212     new ArrayBlockingQueue<Runnable>(10));
213    
214     Callable c = new Callable() {
215 jsr166 1.33 public Object call() { throw new ArithmeticException(); }};
216 tim 1.1
217     try {
218 jsr166 1.19 p.submit(c).get();
219 tim 1.1 shouldThrow();
220 jsr166 1.19 } catch (ExecutionException success) {
221     assertTrue(success.getCause() instanceof ArithmeticException);
222 tim 1.1 }
223     joinPool(p);
224 dl 1.6 }
225    
226     /**
227     * invokeAny(null) throws NPE
228     */
229 jsr166 1.28 public void testInvokeAny1() throws Exception {
230 dl 1.6 ExecutorService e = new DirectExecutorService();
231     try {
232     e.invokeAny(null);
233 jsr166 1.19 shouldThrow();
234 dl 1.6 } catch (NullPointerException success) {
235     } finally {
236     joinPool(e);
237     }
238     }
239    
240     /**
241     * invokeAny(empty collection) throws IAE
242     */
243 jsr166 1.28 public void testInvokeAny2() throws Exception {
244 dl 1.6 ExecutorService e = new DirectExecutorService();
245     try {
246     e.invokeAny(new ArrayList<Callable<String>>());
247 jsr166 1.19 shouldThrow();
248 dl 1.6 } catch (IllegalArgumentException success) {
249     } finally {
250     joinPool(e);
251     }
252     }
253    
254     /**
255     * invokeAny(c) throws NPE if c has null elements
256     */
257 jsr166 1.19 public void testInvokeAny3() throws Exception {
258 dl 1.6 ExecutorService e = new DirectExecutorService();
259 jsr166 1.33 List<Callable<Long>> l = new ArrayList<Callable<Long>>();
260     l.add(new Callable<Long>() {
261     public Long call() { throw new ArithmeticException(); }});
262 jsr166 1.22 l.add(null);
263 dl 1.6 try {
264     e.invokeAny(l);
265 jsr166 1.19 shouldThrow();
266 dl 1.6 } catch (NullPointerException success) {
267     } finally {
268     joinPool(e);
269     }
270     }
271    
272     /**
273 dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
274 dl 1.6 */
275 jsr166 1.19 public void testInvokeAny4() throws InterruptedException {
276 dl 1.6 ExecutorService e = new DirectExecutorService();
277 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
278     l.add(new NPETask());
279 dl 1.6 try {
280 dl 1.8 e.invokeAny(l);
281 jsr166 1.19 shouldThrow();
282 jsr166 1.17 } catch (ExecutionException success) {
283 jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
284 dl 1.6 } finally {
285     joinPool(e);
286     }
287     }
288    
289     /**
290 dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
291 dl 1.6 */
292 jsr166 1.19 public void testInvokeAny5() throws Exception {
293 dl 1.6 ExecutorService e = new DirectExecutorService();
294     try {
295 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
296 dl 1.6 l.add(new StringTask());
297     l.add(new StringTask());
298     String result = e.invokeAny(l);
299     assertSame(TEST_STRING, result);
300     } finally {
301     joinPool(e);
302     }
303     }
304    
305     /**
306     * invokeAll(null) throws NPE
307     */
308 jsr166 1.19 public void testInvokeAll1() throws InterruptedException {
309 dl 1.6 ExecutorService e = new DirectExecutorService();
310     try {
311     e.invokeAll(null);
312 jsr166 1.19 shouldThrow();
313 dl 1.6 } catch (NullPointerException success) {
314     } finally {
315     joinPool(e);
316     }
317     }
318    
319     /**
320     * invokeAll(empty collection) returns empty collection
321     */
322 jsr166 1.19 public void testInvokeAll2() throws InterruptedException {
323 dl 1.6 ExecutorService e = new DirectExecutorService();
324     try {
325     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
326     assertTrue(r.isEmpty());
327     } finally {
328     joinPool(e);
329     }
330     }
331    
332     /**
333     * invokeAll(c) throws NPE if c has null elements
334     */
335 jsr166 1.19 public void testInvokeAll3() throws InterruptedException {
336 dl 1.6 ExecutorService e = new DirectExecutorService();
337 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
338     l.add(new StringTask());
339     l.add(null);
340 dl 1.6 try {
341     e.invokeAll(l);
342 jsr166 1.19 shouldThrow();
343 dl 1.6 } catch (NullPointerException success) {
344     } finally {
345     joinPool(e);
346     }
347     }
348    
349     /**
350 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
351 dl 1.6 */
352 jsr166 1.19 public void testInvokeAll4() throws Exception {
353 dl 1.6 ExecutorService e = new DirectExecutorService();
354     try {
355 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
356 dl 1.6 l.add(new NPETask());
357 jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
358     assertEquals(1, futures.size());
359     try {
360     futures.get(0).get();
361     shouldThrow();
362     } catch (ExecutionException success) {
363     assertTrue(success.getCause() instanceof NullPointerException);
364 jsr166 1.19 }
365 dl 1.6 } finally {
366     joinPool(e);
367     }
368     }
369    
370     /**
371 dl 1.12 * invokeAll(c) returns results of all completed tasks in c
372 dl 1.6 */
373 jsr166 1.19 public void testInvokeAll5() throws Exception {
374 dl 1.6 ExecutorService e = new DirectExecutorService();
375     try {
376 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
377 dl 1.6 l.add(new StringTask());
378     l.add(new StringTask());
379 jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
380     assertEquals(2, futures.size());
381     for (Future<String> future : futures)
382 jsr166 1.19 assertSame(TEST_STRING, future.get());
383 dl 1.8 } finally {
384     joinPool(e);
385     }
386     }
387    
388     /**
389     * timed invokeAny(null) throws NPE
390     */
391 jsr166 1.19 public void testTimedInvokeAny1() throws Exception {
392 dl 1.8 ExecutorService e = new DirectExecutorService();
393     try {
394 jsr166 1.20 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
395 jsr166 1.19 shouldThrow();
396 dl 1.8 } catch (NullPointerException success) {
397     } finally {
398     joinPool(e);
399     }
400     }
401    
402     /**
403 dl 1.12 * timed invokeAny(null time unit) throws NPE
404 dl 1.8 */
405 jsr166 1.19 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
406 dl 1.8 ExecutorService e = new DirectExecutorService();
407 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
408     l.add(new StringTask());
409 dl 1.8 try {
410     e.invokeAny(l, MEDIUM_DELAY_MS, null);
411 jsr166 1.19 shouldThrow();
412 dl 1.8 } catch (NullPointerException success) {
413     } finally {
414     joinPool(e);
415     }
416     }
417    
418     /**
419     * timed invokeAny(empty collection) throws IAE
420     */
421 jsr166 1.19 public void testTimedInvokeAny2() throws Exception {
422 dl 1.8 ExecutorService e = new DirectExecutorService();
423     try {
424 jsr166 1.20 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
425 jsr166 1.19 shouldThrow();
426 dl 1.8 } catch (IllegalArgumentException success) {
427     } finally {
428     joinPool(e);
429     }
430     }
431    
432     /**
433     * timed invokeAny(c) throws NPE if c has null elements
434     */
435 jsr166 1.19 public void testTimedInvokeAny3() throws Exception {
436 dl 1.8 ExecutorService e = new DirectExecutorService();
437 jsr166 1.33 List<Callable<Long>> l = new ArrayList<Callable<Long>>();
438     l.add(new Callable<Long>() {
439     public Long call() { throw new ArithmeticException(); }});
440 jsr166 1.22 l.add(null);
441 dl 1.8 try {
442 jsr166 1.20 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
443 jsr166 1.19 shouldThrow();
444 dl 1.8 } catch (NullPointerException success) {
445     } finally {
446     joinPool(e);
447     }
448     }
449    
450     /**
451     * timed invokeAny(c) throws ExecutionException if no task completes
452     */
453 jsr166 1.19 public void testTimedInvokeAny4() throws Exception {
454 dl 1.8 ExecutorService e = new DirectExecutorService();
455 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
456     l.add(new NPETask());
457 dl 1.8 try {
458 jsr166 1.20 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
459 jsr166 1.19 shouldThrow();
460 jsr166 1.17 } catch (ExecutionException success) {
461 jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
462 dl 1.8 } finally {
463     joinPool(e);
464     }
465     }
466    
467     /**
468 dl 1.12 * timed invokeAny(c) returns result of some task in c
469 dl 1.8 */
470 jsr166 1.19 public void testTimedInvokeAny5() throws Exception {
471 dl 1.8 ExecutorService e = new DirectExecutorService();
472     try {
473 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
474 dl 1.8 l.add(new StringTask());
475     l.add(new StringTask());
476 jsr166 1.20 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
477 dl 1.8 assertSame(TEST_STRING, result);
478     } finally {
479     joinPool(e);
480     }
481     }
482    
483     /**
484     * timed invokeAll(null) throws NPE
485     */
486 jsr166 1.19 public void testTimedInvokeAll1() throws InterruptedException {
487 dl 1.8 ExecutorService e = new DirectExecutorService();
488     try {
489 jsr166 1.20 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
490 jsr166 1.19 shouldThrow();
491 dl 1.8 } catch (NullPointerException success) {
492     } finally {
493     joinPool(e);
494     }
495     }
496    
497     /**
498 dl 1.12 * timed invokeAll(null time unit) throws NPE
499 dl 1.8 */
500 jsr166 1.19 public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
501 dl 1.8 ExecutorService e = new DirectExecutorService();
502 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
503     l.add(new StringTask());
504 dl 1.8 try {
505     e.invokeAll(l, MEDIUM_DELAY_MS, null);
506 jsr166 1.19 shouldThrow();
507 dl 1.8 } catch (NullPointerException success) {
508     } finally {
509     joinPool(e);
510     }
511     }
512    
513     /**
514     * timed invokeAll(empty collection) returns empty collection
515     */
516 jsr166 1.19 public void testTimedInvokeAll2() throws InterruptedException {
517 dl 1.8 ExecutorService e = new DirectExecutorService();
518     try {
519 jsr166 1.20 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
520 dl 1.8 assertTrue(r.isEmpty());
521     } finally {
522     joinPool(e);
523     }
524     }
525    
526     /**
527     * timed invokeAll(c) throws NPE if c has null elements
528     */
529 jsr166 1.19 public void testTimedInvokeAll3() throws InterruptedException {
530 dl 1.8 ExecutorService e = new DirectExecutorService();
531 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
532     l.add(new StringTask());
533     l.add(null);
534 dl 1.8 try {
535 jsr166 1.20 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
536 jsr166 1.19 shouldThrow();
537 dl 1.8 } catch (NullPointerException success) {
538     } finally {
539     joinPool(e);
540     }
541     }
542    
543     /**
544 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
545 dl 1.8 */
546 jsr166 1.19 public void testTimedInvokeAll4() throws Exception {
547 dl 1.8 ExecutorService e = new DirectExecutorService();
548     try {
549 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
550 dl 1.8 l.add(new NPETask());
551 jsr166 1.22 List<Future<String>> futures =
552     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
553     assertEquals(1, futures.size());
554     try {
555     futures.get(0).get();
556     shouldThrow();
557     } catch (ExecutionException success) {
558     assertTrue(success.getCause() instanceof NullPointerException);
559 jsr166 1.19 }
560 dl 1.8 } finally {
561     joinPool(e);
562     }
563     }
564    
565     /**
566 dl 1.12 * timed invokeAll(c) returns results of all completed tasks in c
567 dl 1.8 */
568 jsr166 1.19 public void testTimedInvokeAll5() throws Exception {
569 dl 1.8 ExecutorService e = new DirectExecutorService();
570     try {
571 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
572 dl 1.8 l.add(new StringTask());
573     l.add(new StringTask());
574 jsr166 1.22 List<Future<String>> futures =
575     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
576     assertEquals(2, futures.size());
577     for (Future<String> future : futures)
578 jsr166 1.19 assertSame(TEST_STRING, future.get());
579 dl 1.8 } finally {
580     joinPool(e);
581     }
582     }
583    
584     /**
585 dl 1.12 * timed invokeAll cancels tasks not completed by timeout
586 dl 1.8 */
587 jsr166 1.19 public void testTimedInvokeAll6() throws InterruptedException {
588 dl 1.9 ExecutorService e = new DirectExecutorService();
589 dl 1.8 try {
590 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
591 dl 1.8 l.add(new StringTask());
592 jsr166 1.29 l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING));
593 dl 1.11 l.add(new StringTask());
594 jsr166 1.22 List<Future<String>> futures =
595 jsr166 1.29 e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
596 jsr166 1.32 assertEquals(l.size(), futures.size());
597     for (Future future : futures)
598     assertTrue(future.isDone());
599     assertFalse(futures.get(0).isCancelled());
600     assertFalse(futures.get(1).isCancelled());
601     assertTrue(futures.get(2).isCancelled());
602 dl 1.6 } finally {
603     joinPool(e);
604     }
605 tim 1.1 }
606    
607 dl 1.3 }