ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.9
Committed: Wed Aug 5 00:49:40 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +26 -4 lines
Log Message:
Fix flaky test testInvokeAny3

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