ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.29
Committed: Mon Oct 11 08:30:50 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.28: +3 -2 lines
Log Message:
optimize testTimedInvokeAll6

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     * http://creativecommons.org/licenses/publicdomain
5 jsr166 1.16 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 tim 1.1 */
8    
9    
10     import junit.framework.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13 jsr166 1.20 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 tim 1.1 import java.math.BigInteger;
15     import java.security.*;
16    
17 jsr166 1.18 public class AbstractExecutorServiceTest extends JSR166TestCase {
18 tim 1.1 public static void main(String[] args) {
19 jsr166 1.24 junit.textui.TestRunner.run(suite());
20 tim 1.1 }
21     public static Test suite() {
22 dl 1.6 return new TestSuite(AbstractExecutorServiceTest.class);
23 tim 1.1 }
24    
25 jsr166 1.16 /**
26 tim 1.1 * A no-frills implementation of AbstractExecutorService, designed
27 dl 1.6 * to test the submit methods only.
28 tim 1.1 */
29     static class DirectExecutorService extends AbstractExecutorService {
30     public void execute(Runnable r) { r.run(); }
31     public void shutdown() { shutdown = true; }
32 jsr166 1.26 public List<Runnable> shutdownNow() {
33     shutdown = true;
34     return Collections.EMPTY_LIST;
35     }
36 tim 1.1 public boolean isShutdown() { return shutdown; }
37     public boolean isTerminated() { return isShutdown(); }
38 jsr166 1.26 public boolean awaitTermination(long timeout, TimeUnit unit) {
39     return isShutdown();
40     }
41 tim 1.1 private volatile boolean shutdown = false;
42     }
43    
44     /**
45 dl 1.12 * execute(runnable) runs it to completion
46 tim 1.1 */
47 jsr166 1.19 public void testExecuteRunnable() throws Exception {
48     ExecutorService e = new DirectExecutorService();
49     TrackedShortRunnable task = new TrackedShortRunnable();
50     assertFalse(task.done);
51     Future<?> future = e.submit(task);
52     future.get();
53     assertTrue(task.done);
54 tim 1.1 }
55    
56    
57     /**
58 dl 1.12 * Completed submit(callable) returns result
59 tim 1.1 */
60 jsr166 1.19 public void testSubmitCallable() throws Exception {
61     ExecutorService e = new DirectExecutorService();
62     Future<String> future = e.submit(new StringTask());
63     String result = future.get();
64     assertSame(TEST_STRING, result);
65 tim 1.1 }
66    
67 dl 1.6 /**
68 dl 1.12 * Completed submit(runnable) returns successfully
69 dl 1.6 */
70 jsr166 1.19 public void testSubmitRunnable() throws Exception {
71     ExecutorService e = new DirectExecutorService();
72     Future<?> future = e.submit(new NoOpRunnable());
73     future.get();
74     assertTrue(future.isDone());
75 dl 1.6 }
76    
77     /**
78 dl 1.12 * Completed submit(runnable, result) returns result
79 dl 1.6 */
80 jsr166 1.19 public void testSubmitRunnable2() throws Exception {
81     ExecutorService e = new DirectExecutorService();
82     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
83     String result = future.get();
84     assertSame(TEST_STRING, result);
85 dl 1.6 }
86    
87 tim 1.1
88     /**
89 jsr166 1.23 * A submitted privileged action runs to completion
90 tim 1.1 */
91 jsr166 1.19 public void testSubmitPrivilegedAction() throws Exception {
92 jsr166 1.23 Runnable r = new CheckedRunnable() {
93     public void realRun() throws Exception {
94     ExecutorService e = new DirectExecutorService();
95     Future future = e.submit(Executors.callable(new PrivilegedAction() {
96 tim 1.1 public Object run() {
97     return TEST_STRING;
98 dl 1.5 }}));
99 tim 1.1
100 jsr166 1.23 assertSame(TEST_STRING, future.get());
101     }};
102    
103     runWithPermissions(r,
104     new RuntimePermission("getClassLoader"),
105     new RuntimePermission("setContextClassLoader"),
106     new RuntimePermission("modifyThread"));
107 tim 1.1 }
108    
109     /**
110 jsr166 1.23 * A submitted privileged exception action runs to completion
111 tim 1.1 */
112 jsr166 1.19 public void testSubmitPrivilegedExceptionAction() throws Exception {
113 jsr166 1.23 Runnable r = new CheckedRunnable() {
114     public void realRun() throws Exception {
115     ExecutorService e = new DirectExecutorService();
116     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
117 tim 1.1 public Object run() {
118     return TEST_STRING;
119 dl 1.5 }}));
120 tim 1.1
121 jsr166 1.23 assertSame(TEST_STRING, future.get());
122     }};
123    
124     runWithPermissions(r);
125 tim 1.1 }
126    
127     /**
128 dl 1.12 * A submitted failed privileged exception action reports exception
129 tim 1.1 */
130 jsr166 1.19 public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
131 jsr166 1.23 Runnable r = new CheckedRunnable() {
132     public void realRun() throws Exception {
133     ExecutorService e = new DirectExecutorService();
134     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
135 tim 1.1 public Object run() throws Exception {
136     throw new IndexOutOfBoundsException();
137 dl 1.5 }}));
138 tim 1.1
139 jsr166 1.23 try {
140     future.get();
141     shouldThrow();
142     } catch (ExecutionException success) {
143     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
144     }}};
145    
146     runWithPermissions(r);
147 tim 1.1 }
148    
149     /**
150 dl 1.12 * execute(null runnable) throws NPE
151 tim 1.1 */
152     public void testExecuteNullRunnable() {
153     try {
154     ExecutorService e = new DirectExecutorService();
155 jsr166 1.19 e.submit((Runnable) null);
156 tim 1.1 shouldThrow();
157 jsr166 1.19 } catch (NullPointerException success) {}
158 tim 1.1 }
159    
160    
161     /**
162 dl 1.12 * submit(null callable) throws NPE
163 tim 1.1 */
164 dl 1.6 public void testSubmitNullCallable() {
165 tim 1.1 try {
166     ExecutorService e = new DirectExecutorService();
167 jsr166 1.19 e.submit((Callable) null);
168 tim 1.1 shouldThrow();
169 jsr166 1.19 } catch (NullPointerException success) {}
170 tim 1.1 }
171    
172     /**
173 jsr166 1.25 * submit(callable).get() throws InterruptedException if interrupted
174 tim 1.1 */
175 jsr166 1.19 public void testInterruptedSubmit() throws InterruptedException {
176 jsr166 1.25 final CountDownLatch submitted = new CountDownLatch(1);
177     final CountDownLatch quittingTime = new CountDownLatch(1);
178     final ExecutorService p
179     = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
180     new ArrayBlockingQueue<Runnable>(10));
181     final Callable<Void> awaiter = new CheckedCallable<Void>() {
182     public Void realCall() throws InterruptedException {
183     quittingTime.await();
184     return null;
185     }};
186     try {
187     Thread t = new Thread(new CheckedInterruptedRunnable() {
188     public void realRun() throws Exception {
189     Future<Void> future = p.submit(awaiter);
190     submitted.countDown();
191     future.get();
192     }});
193     t.start();
194     submitted.await();
195     t.interrupt();
196     t.join();
197     } finally {
198     quittingTime.countDown();
199     joinPool(p);
200     }
201 tim 1.1 }
202    
203     /**
204 jsr166 1.27 * get of submit(callable) throws ExecutionException if callable
205     * throws exception
206 tim 1.1 */
207 jsr166 1.19 public void testSubmitEE() throws InterruptedException {
208     ThreadPoolExecutor p =
209     new ThreadPoolExecutor(1, 1,
210     60, TimeUnit.SECONDS,
211     new ArrayBlockingQueue<Runnable>(10));
212    
213     Callable c = new Callable() {
214     public Object call() { return 5/0; }};
215 tim 1.1
216     try {
217 jsr166 1.19 p.submit(c).get();
218 tim 1.1 shouldThrow();
219 jsr166 1.19 } catch (ExecutionException success) {
220     assertTrue(success.getCause() instanceof ArithmeticException);
221 tim 1.1 }
222     joinPool(p);
223 dl 1.6 }
224    
225     /**
226     * invokeAny(null) throws NPE
227     */
228 jsr166 1.28 public void testInvokeAny1() throws Exception {
229 dl 1.6 ExecutorService e = new DirectExecutorService();
230     try {
231     e.invokeAny(null);
232 jsr166 1.19 shouldThrow();
233 dl 1.6 } catch (NullPointerException success) {
234     } finally {
235     joinPool(e);
236     }
237     }
238    
239     /**
240     * invokeAny(empty collection) throws IAE
241     */
242 jsr166 1.28 public void testInvokeAny2() throws Exception {
243 dl 1.6 ExecutorService e = new DirectExecutorService();
244     try {
245     e.invokeAny(new ArrayList<Callable<String>>());
246 jsr166 1.19 shouldThrow();
247 dl 1.6 } catch (IllegalArgumentException success) {
248     } finally {
249     joinPool(e);
250     }
251     }
252    
253     /**
254     * invokeAny(c) throws NPE if c has null elements
255     */
256 jsr166 1.19 public void testInvokeAny3() throws Exception {
257 dl 1.6 ExecutorService e = new DirectExecutorService();
258 jsr166 1.22 List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
259     l.add(new Callable<Integer>() {
260     public Integer call() { return 5/0; }});
261     l.add(null);
262 dl 1.6 try {
263     e.invokeAny(l);
264 jsr166 1.19 shouldThrow();
265 dl 1.6 } catch (NullPointerException success) {
266     } finally {
267     joinPool(e);
268     }
269     }
270    
271     /**
272 dl 1.12 * invokeAny(c) throws ExecutionException if no task in c completes
273 dl 1.6 */
274 jsr166 1.19 public void testInvokeAny4() throws InterruptedException {
275 dl 1.6 ExecutorService e = new DirectExecutorService();
276 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
277     l.add(new NPETask());
278 dl 1.6 try {
279 dl 1.8 e.invokeAny(l);
280 jsr166 1.19 shouldThrow();
281 jsr166 1.17 } catch (ExecutionException success) {
282 jsr166 1.19 assertTrue(success.getCause() instanceof NullPointerException);
283 dl 1.6 } finally {
284     joinPool(e);
285     }
286     }
287    
288     /**
289 dl 1.12 * invokeAny(c) returns result of some task in c if at least one completes
290 dl 1.6 */
291 jsr166 1.19 public void testInvokeAny5() throws Exception {
292 dl 1.6 ExecutorService e = new DirectExecutorService();
293     try {
294 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
295 dl 1.6 l.add(new StringTask());
296     l.add(new StringTask());
297     String result = e.invokeAny(l);
298     assertSame(TEST_STRING, result);
299     } finally {
300     joinPool(e);
301     }
302     }
303    
304     /**
305     * invokeAll(null) throws NPE
306     */
307 jsr166 1.19 public void testInvokeAll1() throws InterruptedException {
308 dl 1.6 ExecutorService e = new DirectExecutorService();
309     try {
310     e.invokeAll(null);
311 jsr166 1.19 shouldThrow();
312 dl 1.6 } catch (NullPointerException success) {
313     } finally {
314     joinPool(e);
315     }
316     }
317    
318     /**
319     * invokeAll(empty collection) returns empty collection
320     */
321 jsr166 1.19 public void testInvokeAll2() throws InterruptedException {
322 dl 1.6 ExecutorService e = new DirectExecutorService();
323     try {
324     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
325     assertTrue(r.isEmpty());
326     } finally {
327     joinPool(e);
328     }
329     }
330    
331     /**
332     * invokeAll(c) throws NPE if c has null elements
333     */
334 jsr166 1.19 public void testInvokeAll3() throws InterruptedException {
335 dl 1.6 ExecutorService e = new DirectExecutorService();
336 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
337     l.add(new StringTask());
338     l.add(null);
339 dl 1.6 try {
340     e.invokeAll(l);
341 jsr166 1.19 shouldThrow();
342 dl 1.6 } catch (NullPointerException success) {
343     } finally {
344     joinPool(e);
345     }
346     }
347    
348     /**
349 dl 1.12 * get of returned element of invokeAll(c) throws exception on failed task
350 dl 1.6 */
351 jsr166 1.19 public void testInvokeAll4() throws Exception {
352 dl 1.6 ExecutorService e = new DirectExecutorService();
353     try {
354 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
355 dl 1.6 l.add(new NPETask());
356 jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
357     assertEquals(1, futures.size());
358     try {
359     futures.get(0).get();
360     shouldThrow();
361     } catch (ExecutionException success) {
362     assertTrue(success.getCause() instanceof NullPointerException);
363 jsr166 1.19 }
364 dl 1.6 } finally {
365     joinPool(e);
366     }
367     }
368    
369     /**
370 dl 1.12 * invokeAll(c) returns results of all completed tasks in c
371 dl 1.6 */
372 jsr166 1.19 public void testInvokeAll5() throws Exception {
373 dl 1.6 ExecutorService e = new DirectExecutorService();
374     try {
375 jsr166 1.22 List<Callable<String>> l = new ArrayList<Callable<String>>();
376 dl 1.6 l.add(new StringTask());
377     l.add(new StringTask());
378 jsr166 1.22 List<Future<String>> futures = e.invokeAll(l);
379     assertEquals(2, futures.size());
380     for (Future<String> future : futures)
381 jsr166 1.19 assertSame(TEST_STRING, future.get());
382 dl 1.8 } finally {
383     joinPool(e);
384     }
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.22 List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
438     l.add(new Callable<Integer>() {
439     public Integer call() { return 5/0; }});
440     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.22 assertEquals(3, futures.size());
597     Iterator<Future<String>> it = futures.iterator();
598 dl 1.8 Future<String> f1 = it.next();
599     Future<String> f2 = it.next();
600 dl 1.11 Future<String> f3 = it.next();
601 dl 1.8 assertTrue(f1.isDone());
602     assertFalse(f1.isCancelled());
603     assertTrue(f2.isDone());
604 jsr166 1.29 assertFalse(f2.isCancelled());
605 dl 1.11 assertTrue(f3.isDone());
606     assertTrue(f3.isCancelled());
607 dl 1.6 } finally {
608     joinPool(e);
609     }
610 tim 1.1 }
611    
612 dl 1.3 }