ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.1
Committed: Fri Jul 31 23:02:49 2009 UTC (14 years, 9 months ago) by dl
Branch: MAIN
Log Message:
Add new TCK tests for JDK7

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