ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.23
Committed: Sat Sep 11 19:04:12 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.22: +80 -59 lines
Log Message:
shut down all pools to prevent "thread leaks"

File Contents

# User Rev Content
1 dl 1.1 /*
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     */
6    
7     import junit.framework.*;
8     import java.util.*;
9 dl 1.20 import java.util.concurrent.Executor;
10     import java.util.concurrent.Executors;
11     import java.util.concurrent.ExecutorService;
12     import java.util.concurrent.AbstractExecutorService;
13     import java.util.concurrent.CountDownLatch;
14     import java.util.concurrent.Callable;
15     import java.util.concurrent.Future;
16     import java.util.concurrent.ExecutionException;
17     import java.util.concurrent.CancellationException;
18     import java.util.concurrent.RejectedExecutionException;
19     import java.util.concurrent.ForkJoinPool;
20     import java.util.concurrent.ForkJoinTask;
21     import java.util.concurrent.ForkJoinWorkerThread;
22     import java.util.concurrent.RecursiveAction;
23     import java.util.concurrent.RecursiveTask;
24     import java.util.concurrent.TimeUnit;
25 dl 1.1 import java.util.concurrent.locks.*;
26     import java.security.*;
27    
28 jsr166 1.3 public class ForkJoinPoolTest extends JSR166TestCase {
29 dl 1.1 public static void main(String[] args) {
30 jsr166 1.21 junit.textui.TestRunner.run(suite());
31 dl 1.1 }
32 jsr166 1.23
33 dl 1.1 public static Test suite() {
34     return new TestSuite(ForkJoinPoolTest.class);
35     }
36    
37     /**
38     * Testing coverage notes:
39     *
40     * 1. shutdown and related methods are tested via super.joinPool.
41     *
42     * 2. newTaskFor and adapters are tested in submit/invoke tests
43 jsr166 1.2 *
44 dl 1.1 * 3. We cannot portably test monitoring methods such as
45     * getStealCount() since they rely ultimately on random task
46     * stealing that may cause tasks not to be stolen/propagated
47     * across threads, especially on uniprocessors.
48 jsr166 1.2 *
49 dl 1.1 * 4. There are no independently testable ForkJoinWorkerThread
50     * methods, but they are covered here and in task tests.
51     */
52    
53     // Some classes to test extension and factory methods
54    
55     static class MyHandler implements Thread.UncaughtExceptionHandler {
56 dl 1.20 volatile int catches = 0;
57 dl 1.1 public void uncaughtException(Thread t, Throwable e) {
58     ++catches;
59     }
60     }
61    
62     // to test handlers
63     static class FailingFJWSubclass extends ForkJoinWorkerThread {
64     public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
65 dl 1.20 protected void onStart() { super.onStart(); throw new Error(); }
66 dl 1.1 }
67    
68 jsr166 1.6 static class FailingThreadFactory
69     implements ForkJoinPool.ForkJoinWorkerThreadFactory {
70 dl 1.20 volatile int calls = 0;
71 jsr166 1.2 public ForkJoinWorkerThread newThread(ForkJoinPool p) {
72 dl 1.1 if (++calls > 1) return null;
73     return new FailingFJWSubclass(p);
74 jsr166 1.2 }
75 dl 1.1 }
76    
77 jsr166 1.2 static class SubFJP extends ForkJoinPool { // to expose protected
78 dl 1.1 SubFJP() { super(1); }
79     public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
80     return super.drainTasksTo(c);
81     }
82     public ForkJoinTask<?> pollSubmission() {
83     return super.pollSubmission();
84     }
85     }
86    
87     static class ManagedLocker implements ForkJoinPool.ManagedBlocker {
88     final ReentrantLock lock;
89     boolean hasLock = false;
90     ManagedLocker(ReentrantLock lock) { this.lock = lock; }
91     public boolean block() {
92     if (!hasLock)
93     lock.lock();
94     return true;
95     }
96     public boolean isReleasable() {
97     return hasLock || (hasLock = lock.tryLock());
98     }
99     }
100    
101     // A simple recursive task for testing
102 jsr166 1.2 static final class FibTask extends RecursiveTask<Integer> {
103 dl 1.1 final int number;
104     FibTask(int n) { number = n; }
105     public Integer compute() {
106     int n = number;
107     if (n <= 1)
108     return n;
109     FibTask f1 = new FibTask(n - 1);
110     f1.fork();
111     return (new FibTask(n - 2)).compute() + f1.join();
112     }
113     }
114    
115     // A failing task for testing
116 jsr166 1.2 static final class FailingTask extends ForkJoinTask<Void> {
117 dl 1.1 public final Void getRawResult() { return null; }
118     protected final void setRawResult(Void mustBeNull) { }
119     protected final boolean exec() { throw new Error(); }
120     FailingTask() {}
121     }
122    
123     // Fib needlessly using locking to test ManagedBlockers
124 jsr166 1.2 static final class LockingFibTask extends RecursiveTask<Integer> {
125 dl 1.1 final int number;
126     final ManagedLocker locker;
127     final ReentrantLock lock;
128 jsr166 1.2 LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) {
129     number = n;
130 dl 1.1 this.locker = locker;
131     this.lock = lock;
132     }
133     public Integer compute() {
134     int n;
135     LockingFibTask f1 = null;
136     LockingFibTask f2 = null;
137     locker.block();
138 jsr166 1.2 n = number;
139 dl 1.1 if (n > 1) {
140     f1 = new LockingFibTask(n - 1, locker, lock);
141     f2 = new LockingFibTask(n - 2, locker, lock);
142     }
143     lock.unlock();
144     if (n <= 1)
145     return n;
146     else {
147     f1.fork();
148     return f2.compute() + f1.join();
149     }
150     }
151     }
152    
153 jsr166 1.2 /**
154 jsr166 1.5 * Successfully constructed pool reports default factory,
155 dl 1.1 * parallelism and async mode policies, no active threads or
156     * tasks, and quiescent running state.
157     */
158     public void testDefaultInitialState() {
159 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
160 dl 1.1 try {
161 jsr166 1.6 assertTrue(p.getFactory() ==
162     ForkJoinPool.defaultForkJoinWorkerThreadFactory);
163 dl 1.1 assertTrue(p.isQuiescent());
164     assertFalse(p.getAsyncMode());
165     assertTrue(p.getActiveThreadCount() == 0);
166     assertTrue(p.getStealCount() == 0);
167     assertTrue(p.getQueuedTaskCount() == 0);
168     assertTrue(p.getQueuedSubmissionCount() == 0);
169     assertFalse(p.hasQueuedSubmissions());
170     assertFalse(p.isShutdown());
171     assertFalse(p.isTerminating());
172     assertFalse(p.isTerminated());
173     } finally {
174     joinPool(p);
175     }
176     }
177    
178 jsr166 1.2 /**
179     * Constructor throws if size argument is less than zero
180 dl 1.1 */
181     public void testConstructor1() {
182     try {
183     new ForkJoinPool(-1);
184     shouldThrow();
185 jsr166 1.10 } catch (IllegalArgumentException success) {}
186 dl 1.1 }
187    
188 jsr166 1.2 /**
189     * Constructor throws if factory argument is null
190 dl 1.1 */
191     public void testConstructor2() {
192     try {
193 dl 1.20 new ForkJoinPool(1, null, null, false);
194 dl 1.1 shouldThrow();
195 jsr166 1.10 } catch (NullPointerException success) {}
196 dl 1.1 }
197    
198    
199 jsr166 1.2 /**
200     * getParallelism returns size set in constructor
201 dl 1.1 */
202     public void testGetParallelism() {
203 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
204 dl 1.1 try {
205     assertTrue(p.getParallelism() == 1);
206     } finally {
207     joinPool(p);
208     }
209     }
210    
211 jsr166 1.2 /**
212 dl 1.1 * getPoolSize returns number of started workers.
213     */
214     public void testGetPoolSize() {
215 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
216 dl 1.1 try {
217 dl 1.18 assertTrue(p.getActiveThreadCount() == 0);
218 dl 1.1 Future<String> future = p.submit(new StringTask());
219     assertTrue(p.getPoolSize() == 1);
220     } finally {
221     joinPool(p);
222     }
223     }
224    
225 jsr166 1.2 /**
226 dl 1.1 * setUncaughtExceptionHandler changes handler for uncaught exceptions.
227     *
228     * Additionally tests: Overriding ForkJoinWorkerThread.onStart
229     * performs its defined action
230     */
231 jsr166 1.6 public void testSetUncaughtExceptionHandler() throws InterruptedException {
232 jsr166 1.23 MyHandler eh = new MyHandler();
233     ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false);
234 dl 1.1 try {
235 dl 1.20 assert(eh == p.getUncaughtExceptionHandler());
236 dl 1.1 p.execute(new FailingTask());
237     Thread.sleep(MEDIUM_DELAY_MS);
238     assertTrue(eh.catches > 0);
239     } finally {
240 dl 1.19 p.shutdownNow();
241 dl 1.1 joinPool(p);
242     }
243     }
244    
245 jsr166 1.2 /**
246 dl 1.1 * After invoking a single task, isQuiescent is true,
247     * queues are empty, threads are not active, and
248     * construction parameters continue to hold
249     */
250 jsr166 1.6 public void testisQuiescent() throws InterruptedException {
251 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(2);
252 dl 1.1 try {
253     p.invoke(new FibTask(20));
254 jsr166 1.6 assertTrue(p.getFactory() ==
255     ForkJoinPool.defaultForkJoinWorkerThreadFactory);
256 dl 1.1 Thread.sleep(MEDIUM_DELAY_MS);
257     assertTrue(p.isQuiescent());
258     assertFalse(p.getAsyncMode());
259     assertTrue(p.getActiveThreadCount() == 0);
260     assertTrue(p.getQueuedTaskCount() == 0);
261     assertTrue(p.getQueuedSubmissionCount() == 0);
262     assertFalse(p.hasQueuedSubmissions());
263     assertFalse(p.isShutdown());
264     assertFalse(p.isTerminating());
265     assertFalse(p.isTerminated());
266     } finally {
267     joinPool(p);
268     }
269     }
270    
271     /**
272     * Completed submit(ForkJoinTask) returns result
273     */
274 jsr166 1.6 public void testSubmitForkJoinTask() throws Throwable {
275 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
276 dl 1.1 try {
277     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
278     int r = f.get();
279     assertTrue(r == 21);
280     } finally {
281     joinPool(p);
282     }
283     }
284    
285     /**
286     * A task submitted after shutdown is rejected
287     */
288     public void testSubmitAfterShutdown() {
289 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
290 dl 1.1 try {
291     p.shutdown();
292     assertTrue(p.isShutdown());
293     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
294     shouldThrow();
295     } catch (RejectedExecutionException success) {
296     } finally {
297     joinPool(p);
298     }
299     }
300    
301     /**
302     * Pool maintains parallelism when using ManagedBlocker
303     */
304 jsr166 1.6 public void testBlockingForkJoinTask() throws Throwable {
305 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(4);
306 dl 1.1 try {
307     ReentrantLock lock = new ReentrantLock();
308     ManagedLocker locker = new ManagedLocker(lock);
309     ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
310     p.execute(f);
311     int r = f.get();
312 jsr166 1.14 assertTrue(r == 832040);
313 dl 1.1 } finally {
314 dl 1.7 p.shutdownNow(); // don't wait out shutdown
315 dl 1.1 }
316     }
317    
318     /**
319     * pollSubmission returns unexecuted submitted task, if present
320     */
321     public void testPollSubmission() {
322 jsr166 1.23 SubFJP p = new SubFJP();
323 dl 1.1 try {
324     ForkJoinTask a = p.submit(new MediumRunnable());
325     ForkJoinTask b = p.submit(new MediumRunnable());
326     ForkJoinTask c = p.submit(new MediumRunnable());
327     ForkJoinTask r = p.pollSubmission();
328     assertTrue(r == a || r == b || r == c);
329     assertFalse(r.isDone());
330     } finally {
331     joinPool(p);
332     }
333     }
334    
335     /**
336     * drainTasksTo transfers unexecuted submitted tasks, if present
337     */
338     public void testDrainTasksTo() {
339 jsr166 1.23 SubFJP p = new SubFJP();
340 dl 1.1 try {
341     ForkJoinTask a = p.submit(new MediumRunnable());
342     ForkJoinTask b = p.submit(new MediumRunnable());
343     ForkJoinTask c = p.submit(new MediumRunnable());
344     ArrayList<ForkJoinTask> al = new ArrayList();
345     p.drainTasksTo(al);
346     assertTrue(al.size() > 0);
347     for (ForkJoinTask r : al) {
348     assertTrue(r == a || r == b || r == c);
349     assertFalse(r.isDone());
350     }
351     } finally {
352     joinPool(p);
353     }
354     }
355    
356 jsr166 1.2
357 dl 1.1 // FJ Versions of AbstractExecutorService tests
358    
359     /**
360     * execute(runnable) runs it to completion
361     */
362 jsr166 1.6 public void testExecuteRunnable() throws Throwable {
363     ExecutorService e = new ForkJoinPool(1);
364 jsr166 1.23 try {
365     TrackedShortRunnable task = new TrackedShortRunnable();
366     assertFalse(task.done);
367     Future<?> future = e.submit(task);
368     future.get();
369     assertTrue(task.done);
370     } finally {
371     joinPool(e);
372     }
373 dl 1.1 }
374    
375    
376     /**
377     * Completed submit(callable) returns result
378     */
379 jsr166 1.6 public void testSubmitCallable() throws Throwable {
380     ExecutorService e = new ForkJoinPool(1);
381 jsr166 1.23 try {
382     Future<String> future = e.submit(new StringTask());
383     String result = future.get();
384     assertSame(TEST_STRING, result);
385     } finally {
386     joinPool(e);
387     }
388 dl 1.1 }
389    
390     /**
391     * Completed submit(runnable) returns successfully
392     */
393 jsr166 1.6 public void testSubmitRunnable() throws Throwable {
394     ExecutorService e = new ForkJoinPool(1);
395 jsr166 1.23 try {
396     Future<?> future = e.submit(new NoOpRunnable());
397     future.get();
398     assertTrue(future.isDone());
399     } finally {
400     joinPool(e);
401     }
402 dl 1.1 }
403    
404     /**
405     * Completed submit(runnable, result) returns result
406     */
407 jsr166 1.6 public void testSubmitRunnable2() throws Throwable {
408     ExecutorService e = new ForkJoinPool(1);
409 jsr166 1.23 try {
410     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
411     String result = future.get();
412     assertSame(TEST_STRING, result);
413     } finally {
414     joinPool(e);
415     }
416 dl 1.1 }
417    
418 dl 1.20
419 dl 1.1 /**
420 dl 1.20 * A submitted privileged action to completion
421 dl 1.1 */
422 jsr166 1.6 public void testSubmitPrivilegedAction() throws Throwable {
423 dl 1.20 Policy savedPolicy = null;
424     try {
425     savedPolicy = Policy.getPolicy();
426     AdjustablePolicy policy = new AdjustablePolicy();
427     policy.addPermission(new RuntimePermission("getContextClassLoader"));
428     policy.addPermission(new RuntimePermission("setContextClassLoader"));
429     Policy.setPolicy(policy);
430     } catch (AccessControlException ok) {
431     return;
432     }
433 jsr166 1.23
434 dl 1.20 try {
435     ExecutorService e = new ForkJoinPool(1);
436 jsr166 1.23 try {
437     Future future = e.submit(Executors.callable(new PrivilegedAction() {
438 dl 1.1 public Object run() {
439     return TEST_STRING;
440     }}));
441    
442 jsr166 1.23 Object result = future.get();
443     assertSame(TEST_STRING, result);
444     } finally {
445     joinPool(e);
446     }
447     } finally {
448 dl 1.20 Policy.setPolicy(savedPolicy);
449     }
450 dl 1.1 }
451    
452     /**
453 dl 1.20 * A submitted a privileged exception action runs to completion
454 dl 1.1 */
455 jsr166 1.6 public void testSubmitPrivilegedExceptionAction() throws Throwable {
456 dl 1.20 Policy savedPolicy = null;
457     try {
458     savedPolicy = Policy.getPolicy();
459     AdjustablePolicy policy = new AdjustablePolicy();
460     policy.addPermission(new RuntimePermission("getContextClassLoader"));
461     policy.addPermission(new RuntimePermission("setContextClassLoader"));
462     Policy.setPolicy(policy);
463     } catch (AccessControlException ok) {
464     return;
465     }
466    
467     try {
468     ExecutorService e = new ForkJoinPool(1);
469 jsr166 1.23 try {
470     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
471 dl 1.1 public Object run() {
472     return TEST_STRING;
473     }}));
474    
475 jsr166 1.23 Object result = future.get();
476     assertSame(TEST_STRING, result);
477     } finally {
478     joinPool(e);
479     }
480     } finally {
481 dl 1.20 Policy.setPolicy(savedPolicy);
482     }
483 dl 1.1 }
484    
485     /**
486     * A submitted failed privileged exception action reports exception
487     */
488 jsr166 1.6 public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
489 dl 1.20 Policy savedPolicy = null;
490     try {
491     savedPolicy = Policy.getPolicy();
492     AdjustablePolicy policy = new AdjustablePolicy();
493     policy.addPermission(new RuntimePermission("getContextClassLoader"));
494     policy.addPermission(new RuntimePermission("setContextClassLoader"));
495     Policy.setPolicy(policy);
496     } catch (AccessControlException ok) {
497     return;
498     }
499    
500     try {
501     ExecutorService e = new ForkJoinPool(1);
502 jsr166 1.23 try {
503     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
504 dl 1.1 public Object run() throws Exception {
505     throw new IndexOutOfBoundsException();
506     }}));
507    
508 jsr166 1.23 Object result = future.get();
509     shouldThrow();
510     } catch (ExecutionException success) {
511     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
512     } finally {
513     joinPool(e);
514     }
515 dl 1.20 } finally {
516     Policy.setPolicy(savedPolicy);
517     }
518 dl 1.1 }
519    
520     /**
521 jsr166 1.6 * execute(null runnable) throws NullPointerException
522 dl 1.1 */
523     public void testExecuteNullRunnable() {
524 jsr166 1.23 ExecutorService e = new ForkJoinPool(1);
525 dl 1.1 try {
526     TrackedShortRunnable task = null;
527     Future<?> future = e.submit(task);
528     shouldThrow();
529 jsr166 1.23 } catch (NullPointerException success) {
530     } finally {
531     joinPool(e);
532     }
533 dl 1.1 }
534    
535    
536     /**
537 jsr166 1.6 * submit(null callable) throws NullPointerException
538 dl 1.1 */
539     public void testSubmitNullCallable() {
540 jsr166 1.23 ExecutorService e = new ForkJoinPool(1);
541 dl 1.1 try {
542     StringTask t = null;
543     Future<String> future = e.submit(t);
544     shouldThrow();
545 jsr166 1.23 } catch (NullPointerException success) {
546     } finally {
547     joinPool(e);
548     }
549 dl 1.1 }
550    
551    
552     /**
553 jsr166 1.4 * Blocking on submit(callable) throws InterruptedException if
554     * caller interrupted.
555 dl 1.1 */
556 jsr166 1.6 public void testInterruptedSubmit() throws InterruptedException {
557 dl 1.1 final ForkJoinPool p = new ForkJoinPool(1);
558    
559 jsr166 1.6 Thread t = new Thread(new CheckedInterruptedRunnable() {
560 jsr166 1.13 public void realRun() throws Throwable {
561 jsr166 1.6 p.submit(new CheckedCallable<Object>() {
562     public Object realCall() throws Throwable {
563 jsr166 1.8 try {
564     Thread.sleep(MEDIUM_DELAY_MS);
565     } catch (InterruptedException ok) {
566     }
567 jsr166 1.6 return null;
568     }}).get();
569     }});
570    
571     t.start();
572 jsr166 1.8 Thread.sleep(SHORT_DELAY_MS);
573 jsr166 1.6 t.interrupt();
574 jsr166 1.8 t.join();
575 dl 1.7 p.shutdownNow();
576 jsr166 1.8 joinPool(p);
577 dl 1.1 }
578    
579     /**
580 jsr166 1.4 * get of submit(callable) throws ExecutionException if callable
581     * throws exception
582 dl 1.1 */
583 jsr166 1.6 public void testSubmitEE() throws Throwable {
584 dl 1.1 ForkJoinPool p = new ForkJoinPool(1);
585     try {
586 jsr166 1.8 p.submit(new Callable() {
587     public Object call() {
588     int i = 5/0;
589     return Boolean.TRUE;
590     }}).get();
591 dl 1.1 shouldThrow();
592 jsr166 1.12 } catch (ExecutionException success) {
593     assertTrue(success.getCause() instanceof ArithmeticException);
594 jsr166 1.23 } finally {
595     joinPool(p);
596 jsr166 1.12 }
597 dl 1.1 }
598    
599     /**
600 jsr166 1.6 * invokeAny(null) throws NullPointerException
601 dl 1.1 */
602 jsr166 1.6 public void testInvokeAny1() throws Throwable {
603 dl 1.1 ExecutorService e = new ForkJoinPool(1);
604     try {
605     e.invokeAny(null);
606 jsr166 1.6 shouldThrow();
607 dl 1.1 } catch (NullPointerException success) {
608     } finally {
609     joinPool(e);
610     }
611     }
612    
613     /**
614 jsr166 1.6 * invokeAny(empty collection) throws IllegalArgumentException
615 dl 1.1 */
616 jsr166 1.6 public void testInvokeAny2() throws Throwable {
617 dl 1.1 ExecutorService e = new ForkJoinPool(1);
618     try {
619     e.invokeAny(new ArrayList<Callable<String>>());
620 jsr166 1.6 shouldThrow();
621 dl 1.1 } catch (IllegalArgumentException success) {
622     } finally {
623     joinPool(e);
624     }
625     }
626    
627     /**
628 jsr166 1.9 * invokeAny(c) throws NullPointerException if c has a single null element
629     */
630     public void testInvokeAny3() throws Throwable {
631     ExecutorService e = new ForkJoinPool(1);
632 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
633     l.add(null);
634 jsr166 1.9 try {
635     e.invokeAny(l);
636     shouldThrow();
637     } catch (NullPointerException success) {
638     } finally {
639     joinPool(e);
640     }
641     }
642    
643     /**
644 jsr166 1.6 * invokeAny(c) throws NullPointerException if c has null elements
645 dl 1.1 */
646 jsr166 1.9 public void testInvokeAny4() throws Throwable {
647 jsr166 1.16 CountDownLatch latch = new CountDownLatch(1);
648 dl 1.1 ExecutorService e = new ForkJoinPool(1);
649 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
650     l.add(latchAwaitingStringTask(latch));
651     l.add(null);
652 dl 1.1 try {
653     e.invokeAny(l);
654 jsr166 1.6 shouldThrow();
655 dl 1.1 } catch (NullPointerException success) {
656     } finally {
657 jsr166 1.16 latch.countDown();
658 dl 1.1 joinPool(e);
659     }
660     }
661    
662     /**
663     * invokeAny(c) throws ExecutionException if no task in c completes
664     */
665 jsr166 1.9 public void testInvokeAny5() throws Throwable {
666 dl 1.1 ExecutorService e = new ForkJoinPool(1);
667 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
668     l.add(new NPETask());
669 dl 1.1 try {
670     e.invokeAny(l);
671 jsr166 1.6 shouldThrow();
672 jsr166 1.2 } catch (ExecutionException success) {
673 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
674 dl 1.1 } finally {
675     joinPool(e);
676     }
677     }
678    
679     /**
680     * invokeAny(c) returns result of some task in c if at least one completes
681     */
682 jsr166 1.9 public void testInvokeAny6() throws Throwable {
683 dl 1.1 ExecutorService e = new ForkJoinPool(1);
684     try {
685 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
686 dl 1.1 l.add(new StringTask());
687     l.add(new StringTask());
688     String result = e.invokeAny(l);
689     assertSame(TEST_STRING, result);
690     } finally {
691     joinPool(e);
692     }
693     }
694    
695     /**
696 jsr166 1.6 * invokeAll(null) throws NullPointerException
697 dl 1.1 */
698 jsr166 1.6 public void testInvokeAll1() throws Throwable {
699 dl 1.1 ExecutorService e = new ForkJoinPool(1);
700     try {
701     e.invokeAll(null);
702 jsr166 1.6 shouldThrow();
703 dl 1.1 } catch (NullPointerException success) {
704     } finally {
705     joinPool(e);
706     }
707     }
708    
709     /**
710     * invokeAll(empty collection) returns empty collection
711     */
712 jsr166 1.6 public void testInvokeAll2() throws InterruptedException {
713 dl 1.1 ExecutorService e = new ForkJoinPool(1);
714     try {
715 jsr166 1.6 List<Future<String>> r
716     = e.invokeAll(new ArrayList<Callable<String>>());
717 dl 1.1 assertTrue(r.isEmpty());
718     } finally {
719     joinPool(e);
720     }
721     }
722    
723     /**
724 jsr166 1.6 * invokeAll(c) throws NullPointerException if c has null elements
725 dl 1.1 */
726 jsr166 1.6 public void testInvokeAll3() throws InterruptedException {
727 dl 1.1 ExecutorService e = new ForkJoinPool(1);
728 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
729     l.add(new StringTask());
730     l.add(null);
731 dl 1.1 try {
732     e.invokeAll(l);
733 jsr166 1.6 shouldThrow();
734 dl 1.1 } catch (NullPointerException success) {
735     } finally {
736     joinPool(e);
737     }
738     }
739    
740     /**
741 jsr166 1.6 * get of returned element of invokeAll(c) throws
742     * ExecutionException on failed task
743 dl 1.1 */
744 jsr166 1.6 public void testInvokeAll4() throws Throwable {
745 dl 1.1 ExecutorService e = new ForkJoinPool(1);
746 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
747     l.add(new NPETask());
748     List<Future<String>> futures = e.invokeAll(l);
749     assertEquals(1, futures.size());
750 dl 1.1 try {
751 jsr166 1.16 futures.get(0).get();
752 jsr166 1.6 shouldThrow();
753 jsr166 1.2 } catch (ExecutionException success) {
754 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
755 dl 1.1 } finally {
756     joinPool(e);
757     }
758     }
759    
760     /**
761     * invokeAll(c) returns results of all completed tasks in c
762     */
763 jsr166 1.6 public void testInvokeAll5() throws Throwable {
764 dl 1.1 ExecutorService e = new ForkJoinPool(1);
765     try {
766 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
767 dl 1.1 l.add(new StringTask());
768     l.add(new StringTask());
769 jsr166 1.16 List<Future<String>> futures = e.invokeAll(l);
770     assertEquals(2, futures.size());
771     for (Future<String> future : futures)
772 jsr166 1.6 assertSame(TEST_STRING, future.get());
773 dl 1.1 } finally {
774     joinPool(e);
775     }
776     }
777    
778    
779     /**
780 jsr166 1.6 * timed invokeAny(null) throws NullPointerException
781 dl 1.1 */
782 jsr166 1.6 public void testTimedInvokeAny1() throws Throwable {
783 dl 1.1 ExecutorService e = new ForkJoinPool(1);
784     try {
785 dl 1.20 e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
786 jsr166 1.6 shouldThrow();
787 dl 1.1 } catch (NullPointerException success) {
788     } finally {
789     joinPool(e);
790     }
791     }
792    
793     /**
794 jsr166 1.6 * timed invokeAny(null time unit) throws NullPointerException
795 dl 1.1 */
796 jsr166 1.6 public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
797 dl 1.1 ExecutorService e = new ForkJoinPool(1);
798 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
799     l.add(new StringTask());
800 dl 1.1 try {
801     e.invokeAny(l, MEDIUM_DELAY_MS, null);
802 jsr166 1.6 shouldThrow();
803 dl 1.1 } catch (NullPointerException success) {
804     } finally {
805     joinPool(e);
806     }
807     }
808    
809     /**
810 jsr166 1.6 * timed invokeAny(empty collection) throws IllegalArgumentException
811 dl 1.1 */
812 jsr166 1.6 public void testTimedInvokeAny2() throws Throwable {
813 dl 1.1 ExecutorService e = new ForkJoinPool(1);
814     try {
815 jsr166 1.6 e.invokeAny(new ArrayList<Callable<String>>(),
816 dl 1.20 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
817 jsr166 1.6 shouldThrow();
818 dl 1.1 } catch (IllegalArgumentException success) {
819     } finally {
820     joinPool(e);
821     }
822     }
823    
824     /**
825 jsr166 1.6 * timed invokeAny(c) throws NullPointerException if c has null elements
826 dl 1.1 */
827 jsr166 1.6 public void testTimedInvokeAny3() throws Throwable {
828 jsr166 1.16 CountDownLatch latch = new CountDownLatch(1);
829 dl 1.1 ExecutorService e = new ForkJoinPool(1);
830 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
831     l.add(latchAwaitingStringTask(latch));
832     l.add(null);
833 dl 1.1 try {
834 dl 1.20 e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
835 jsr166 1.6 shouldThrow();
836 dl 1.1 } catch (NullPointerException success) {
837     } finally {
838 jsr166 1.15 latch.countDown();
839 dl 1.1 joinPool(e);
840     }
841     }
842    
843     /**
844     * timed invokeAny(c) throws ExecutionException if no task completes
845     */
846 jsr166 1.6 public void testTimedInvokeAny4() throws Throwable {
847 dl 1.1 ExecutorService e = new ForkJoinPool(1);
848 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
849     l.add(new NPETask());
850 dl 1.1 try {
851 dl 1.20 e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
852 jsr166 1.6 shouldThrow();
853 jsr166 1.2 } catch (ExecutionException success) {
854 jsr166 1.11 assertTrue(success.getCause() instanceof NullPointerException);
855 dl 1.1 } finally {
856     joinPool(e);
857     }
858     }
859    
860     /**
861     * timed invokeAny(c) returns result of some task in c
862     */
863 jsr166 1.6 public void testTimedInvokeAny5() throws Throwable {
864 dl 1.1 ExecutorService e = new ForkJoinPool(1);
865     try {
866 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
867 dl 1.1 l.add(new StringTask());
868     l.add(new StringTask());
869 dl 1.20 String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
870 dl 1.1 assertSame(TEST_STRING, result);
871     } finally {
872     joinPool(e);
873     }
874     }
875    
876     /**
877 jsr166 1.6 * timed invokeAll(null) throws NullPointerException
878 dl 1.1 */
879 jsr166 1.6 public void testTimedInvokeAll1() throws Throwable {
880 dl 1.1 ExecutorService e = new ForkJoinPool(1);
881     try {
882 dl 1.20 e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
883 jsr166 1.6 shouldThrow();
884 dl 1.1 } catch (NullPointerException success) {
885     } finally {
886     joinPool(e);
887     }
888     }
889    
890     /**
891 jsr166 1.6 * timed invokeAll(null time unit) throws NullPointerException
892 dl 1.1 */
893 jsr166 1.6 public void testTimedInvokeAllNullTimeUnit() throws Throwable {
894 dl 1.1 ExecutorService e = new ForkJoinPool(1);
895 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
896     l.add(new StringTask());
897 dl 1.1 try {
898     e.invokeAll(l, MEDIUM_DELAY_MS, null);
899 jsr166 1.6 shouldThrow();
900 dl 1.1 } catch (NullPointerException success) {
901     } finally {
902     joinPool(e);
903     }
904     }
905    
906     /**
907     * timed invokeAll(empty collection) returns empty collection
908     */
909 jsr166 1.6 public void testTimedInvokeAll2() throws InterruptedException {
910 dl 1.1 ExecutorService e = new ForkJoinPool(1);
911     try {
912 jsr166 1.6 List<Future<String>> r
913     = e.invokeAll(new ArrayList<Callable<String>>(),
914 dl 1.20 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
915 dl 1.1 assertTrue(r.isEmpty());
916     } finally {
917     joinPool(e);
918     }
919     }
920    
921     /**
922 jsr166 1.6 * timed invokeAll(c) throws NullPointerException if c has null elements
923 dl 1.1 */
924 jsr166 1.6 public void testTimedInvokeAll3() throws InterruptedException {
925 dl 1.1 ExecutorService e = new ForkJoinPool(1);
926 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
927     l.add(new StringTask());
928     l.add(null);
929 dl 1.1 try {
930 dl 1.20 e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
931 jsr166 1.6 shouldThrow();
932 dl 1.1 } catch (NullPointerException success) {
933     } finally {
934     joinPool(e);
935     }
936     }
937    
938     /**
939     * get of returned element of invokeAll(c) throws exception on failed task
940     */
941 jsr166 1.6 public void testTimedInvokeAll4() throws Throwable {
942 dl 1.1 ExecutorService e = new ForkJoinPool(1);
943 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
944     l.add(new NPETask());
945     List<Future<String>> futures
946 dl 1.20 = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
947 jsr166 1.16 assertEquals(1, futures.size());
948 dl 1.1 try {
949 jsr166 1.16 futures.get(0).get();
950 jsr166 1.6 shouldThrow();
951 jsr166 1.2 } catch (ExecutionException success) {
952 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
953 dl 1.1 } finally {
954     joinPool(e);
955     }
956     }
957    
958     /**
959     * timed invokeAll(c) returns results of all completed tasks in c
960     */
961 jsr166 1.6 public void testTimedInvokeAll5() throws Throwable {
962 dl 1.1 ExecutorService e = new ForkJoinPool(1);
963     try {
964 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
965 dl 1.1 l.add(new StringTask());
966     l.add(new StringTask());
967 jsr166 1.16 List<Future<String>> futures
968 dl 1.20 = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
969 jsr166 1.16 assertEquals(2, futures.size());
970     for (Future<String> future : futures)
971 jsr166 1.6 assertSame(TEST_STRING, future.get());
972 dl 1.1 } finally {
973     joinPool(e);
974     }
975     }
976    
977     }