ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.15
Committed: Tue Dec 1 09:54:17 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +9 -1 lines
Log Message:
testTimedInvokeAny3 had rare intermittent failures

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 jsr166 1.10 } catch (IllegalArgumentException success) {}
174 dl 1.1 }
175    
176 jsr166 1.2 /**
177     * Constructor throws if factory argument is null
178 dl 1.1 */
179     public void testConstructor2() {
180     try {
181     new ForkJoinPool(1, null);
182     shouldThrow();
183 jsr166 1.10 } catch (NullPointerException success) {}
184 dl 1.1 }
185    
186    
187 jsr166 1.2 /**
188     * getParallelism returns size set in constructor
189 dl 1.1 */
190     public void testGetParallelism() {
191     ForkJoinPool p = null;
192     try {
193     p = new ForkJoinPool(1);
194     assertTrue(p.getParallelism() == 1);
195     } finally {
196     joinPool(p);
197     }
198     }
199    
200 jsr166 1.2 /**
201 dl 1.1 * setParallelism changes reported parallelism level.
202     */
203     public void testSetParallelism() {
204     ForkJoinPool p = null;
205     try {
206     p = new ForkJoinPool(1);
207     assertTrue(p.getParallelism() == 1);
208     p.setParallelism(2);
209     assertTrue(p.getParallelism() == 2);
210     } finally {
211     joinPool(p);
212     }
213     }
214    
215 jsr166 1.2 /**
216 dl 1.1 * setParallelism with argument <= 0 throws exception
217     */
218     public void testSetParallelism2() {
219     ForkJoinPool p = null;
220     try {
221     p = new ForkJoinPool(1);
222     assertTrue(p.getParallelism() == 1);
223     p.setParallelism(-2);
224     shouldThrow();
225 jsr166 1.2 } catch (IllegalArgumentException success) {
226 dl 1.1 } finally {
227     joinPool(p);
228     }
229     }
230    
231 jsr166 1.2 /**
232 dl 1.1 * getPoolSize returns number of started workers.
233     */
234     public void testGetPoolSize() {
235     ForkJoinPool p = null;
236     try {
237     p = new ForkJoinPool(1);
238     assertTrue(p.getPoolSize() == 0);
239     Future<String> future = p.submit(new StringTask());
240     assertTrue(p.getPoolSize() == 1);
241    
242     } finally {
243     joinPool(p);
244     }
245     }
246    
247 jsr166 1.2 /**
248 dl 1.1 * setMaximumPoolSize changes size reported by getMaximumPoolSize.
249     */
250     public void testSetMaximumPoolSize() {
251     ForkJoinPool p = null;
252     try {
253     p = new ForkJoinPool(1);
254     p.setMaximumPoolSize(2);
255     assertTrue(p.getMaximumPoolSize() == 2);
256     } finally {
257     joinPool(p);
258     }
259     }
260    
261 jsr166 1.2 /**
262 dl 1.1 * setMaximumPoolSize with argument <= 0 throws exception
263     */
264     public void testSetMaximumPoolSize2() {
265     ForkJoinPool p = null;
266     try {
267     p = new ForkJoinPool(1);
268     p.setMaximumPoolSize(-2);
269     shouldThrow();
270 jsr166 1.2 } catch (IllegalArgumentException success) {
271 dl 1.1 } finally {
272     joinPool(p);
273     }
274     }
275    
276 jsr166 1.2 /**
277 dl 1.1 * setMaintainsParallelism changes policy reported by
278     * getMaintainsParallelism.
279     */
280     public void testSetMaintainsParallelism() {
281     ForkJoinPool p = null;
282     try {
283     p = new ForkJoinPool(1);
284     p.setMaintainsParallelism(false);
285     assertFalse(p.getMaintainsParallelism());
286     } finally {
287     joinPool(p);
288     }
289     }
290 jsr166 1.2
291     /**
292 dl 1.1 * setAsyncMode changes policy reported by
293     * getAsyncMode.
294     */
295     public void testSetAsyncMode() {
296     ForkJoinPool p = null;
297     try {
298     p = new ForkJoinPool(1);
299     p.setAsyncMode(true);
300     assertTrue(p.getAsyncMode());
301     } finally {
302     joinPool(p);
303     }
304     }
305    
306 jsr166 1.2 /**
307 dl 1.1 * setUncaughtExceptionHandler changes handler for uncaught exceptions.
308     *
309     * Additionally tests: Overriding ForkJoinWorkerThread.onStart
310     * performs its defined action
311     */
312 jsr166 1.6 public void testSetUncaughtExceptionHandler() throws InterruptedException {
313 dl 1.1 ForkJoinPool p = null;
314     try {
315     p = new ForkJoinPool(1, new FailingThreadFactory());
316     MyHandler eh = new MyHandler();
317     p.setUncaughtExceptionHandler(eh);
318     assertEquals(eh, p.getUncaughtExceptionHandler());
319     p.execute(new FailingTask());
320     Thread.sleep(MEDIUM_DELAY_MS);
321     assertTrue(eh.catches > 0);
322     } finally {
323     joinPool(p);
324     }
325     }
326    
327 jsr166 1.2 /**
328 dl 1.1 * setUncaughtExceptionHandler of null removes handler
329     */
330     public void testSetUncaughtExceptionHandler2() {
331     ForkJoinPool p = null;
332     try {
333     p = new ForkJoinPool(1);
334     p.setUncaughtExceptionHandler(null);
335     assertNull(p.getUncaughtExceptionHandler());
336     } finally {
337     joinPool(p);
338     }
339     }
340    
341    
342 jsr166 1.2 /**
343 dl 1.1 * After invoking a single task, isQuiescent is true,
344     * queues are empty, threads are not active, and
345     * construction parameters continue to hold
346     */
347 jsr166 1.6 public void testisQuiescent() throws InterruptedException {
348 dl 1.1 ForkJoinPool p = null;
349     try {
350     p = new ForkJoinPool(2);
351     p.invoke(new FibTask(20));
352 jsr166 1.6 assertTrue(p.getFactory() ==
353     ForkJoinPool.defaultForkJoinWorkerThreadFactory);
354 dl 1.1 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     } finally {
366     joinPool(p);
367     }
368     }
369    
370     /**
371     * Completed submit(ForkJoinTask) returns result
372     */
373 jsr166 1.6 public void testSubmitForkJoinTask() throws Throwable {
374 dl 1.1 ForkJoinPool p = null;
375     try {
376     p = new ForkJoinPool(1);
377     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
378     int r = f.get();
379     assertTrue(r == 21);
380     } finally {
381     joinPool(p);
382     }
383     }
384    
385     /**
386     * A task submitted after shutdown is rejected
387     */
388     public void testSubmitAfterShutdown() {
389     ForkJoinPool p = null;
390     try {
391     p = new ForkJoinPool(1);
392     p.shutdown();
393     assertTrue(p.isShutdown());
394     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
395     shouldThrow();
396     } catch (RejectedExecutionException success) {
397     } finally {
398     joinPool(p);
399     }
400     }
401    
402     /**
403     * Pool maintains parallelism when using ManagedBlocker
404     */
405 jsr166 1.6 public void testBlockingForkJoinTask() throws Throwable {
406 dl 1.1 ForkJoinPool p = null;
407     try {
408     p = new ForkJoinPool(4);
409     ReentrantLock lock = new ReentrantLock();
410     ManagedLocker locker = new ManagedLocker(lock);
411     ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
412     p.execute(f);
413     assertTrue(p.getPoolSize() >= 4);
414     int r = f.get();
415 jsr166 1.14 assertTrue(r == 832040);
416 dl 1.1 } finally {
417 dl 1.7 p.shutdownNow(); // don't wait out shutdown
418 dl 1.1 }
419     }
420    
421     /**
422     * pollSubmission returns unexecuted submitted task, if present
423     */
424     public void testPollSubmission() {
425     SubFJP p = null;
426     try {
427     p = new SubFJP();
428     ForkJoinTask a = p.submit(new MediumRunnable());
429     ForkJoinTask b = p.submit(new MediumRunnable());
430     ForkJoinTask c = p.submit(new MediumRunnable());
431     ForkJoinTask r = p.pollSubmission();
432     assertTrue(r == a || r == b || r == c);
433     assertFalse(r.isDone());
434     } finally {
435     joinPool(p);
436     }
437     }
438    
439     /**
440     * drainTasksTo transfers unexecuted submitted tasks, if present
441     */
442     public void testDrainTasksTo() {
443     SubFJP p = null;
444     try {
445     p = new SubFJP();
446     ForkJoinTask a = p.submit(new MediumRunnable());
447     ForkJoinTask b = p.submit(new MediumRunnable());
448     ForkJoinTask c = p.submit(new MediumRunnable());
449     ArrayList<ForkJoinTask> al = new ArrayList();
450     p.drainTasksTo(al);
451     assertTrue(al.size() > 0);
452     for (ForkJoinTask r : al) {
453     assertTrue(r == a || r == b || r == c);
454     assertFalse(r.isDone());
455     }
456     } finally {
457     joinPool(p);
458     }
459     }
460    
461 jsr166 1.2
462 dl 1.1 // FJ Versions of AbstractExecutorService tests
463    
464     /**
465     * execute(runnable) runs it to completion
466     */
467 jsr166 1.6 public void testExecuteRunnable() throws Throwable {
468     ExecutorService e = new ForkJoinPool(1);
469     TrackedShortRunnable task = new TrackedShortRunnable();
470     assertFalse(task.done);
471     Future<?> future = e.submit(task);
472     future.get();
473     assertTrue(task.done);
474 dl 1.1 }
475    
476    
477     /**
478     * Completed submit(callable) returns result
479     */
480 jsr166 1.6 public void testSubmitCallable() throws Throwable {
481     ExecutorService e = new ForkJoinPool(1);
482     Future<String> future = e.submit(new StringTask());
483     String result = future.get();
484     assertSame(TEST_STRING, result);
485 dl 1.1 }
486    
487     /**
488     * Completed submit(runnable) returns successfully
489     */
490 jsr166 1.6 public void testSubmitRunnable() throws Throwable {
491     ExecutorService e = new ForkJoinPool(1);
492     Future<?> future = e.submit(new NoOpRunnable());
493     future.get();
494     assertTrue(future.isDone());
495 dl 1.1 }
496    
497     /**
498     * Completed submit(runnable, result) returns result
499     */
500 jsr166 1.6 public void testSubmitRunnable2() throws Throwable {
501     ExecutorService e = new ForkJoinPool(1);
502     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
503     String result = future.get();
504     assertSame(TEST_STRING, result);
505 dl 1.1 }
506    
507    
508     /**
509     * A submitted privileged action to completion
510     */
511 jsr166 1.6 public void testSubmitPrivilegedAction() throws Throwable {
512 dl 1.1 Policy savedPolicy = null;
513     try {
514     savedPolicy = Policy.getPolicy();
515     AdjustablePolicy policy = new AdjustablePolicy();
516     policy.addPermission(new RuntimePermission("getContextClassLoader"));
517     policy.addPermission(new RuntimePermission("setContextClassLoader"));
518     Policy.setPolicy(policy);
519 jsr166 1.2 } catch (AccessControlException ok) {
520 dl 1.1 return;
521     }
522     try {
523     ExecutorService e = new ForkJoinPool(1);
524     Future future = e.submit(Executors.callable(new PrivilegedAction() {
525     public Object run() {
526     return TEST_STRING;
527     }}));
528    
529     Object result = future.get();
530     assertSame(TEST_STRING, result);
531     }
532     finally {
533 jsr166 1.6 Policy.setPolicy(savedPolicy);
534 dl 1.1 }
535     }
536    
537     /**
538     * A submitted a privileged exception action runs to completion
539     */
540 jsr166 1.6 public void testSubmitPrivilegedExceptionAction() throws Throwable {
541 dl 1.1 Policy savedPolicy = null;
542     try {
543     savedPolicy = Policy.getPolicy();
544     AdjustablePolicy policy = new AdjustablePolicy();
545     policy.addPermission(new RuntimePermission("getContextClassLoader"));
546     policy.addPermission(new RuntimePermission("setContextClassLoader"));
547     Policy.setPolicy(policy);
548 jsr166 1.2 } catch (AccessControlException ok) {
549 dl 1.1 return;
550     }
551    
552     try {
553     ExecutorService e = new ForkJoinPool(1);
554     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
555     public Object run() {
556     return TEST_STRING;
557     }}));
558    
559     Object result = future.get();
560     assertSame(TEST_STRING, result);
561     }
562     finally {
563     Policy.setPolicy(savedPolicy);
564     }
565     }
566    
567     /**
568     * A submitted failed privileged exception action reports exception
569     */
570 jsr166 1.6 public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
571 dl 1.1 Policy savedPolicy = null;
572     try {
573     savedPolicy = Policy.getPolicy();
574     AdjustablePolicy policy = new AdjustablePolicy();
575     policy.addPermission(new RuntimePermission("getContextClassLoader"));
576     policy.addPermission(new RuntimePermission("setContextClassLoader"));
577     Policy.setPolicy(policy);
578 jsr166 1.2 } catch (AccessControlException ok) {
579 dl 1.1 return;
580     }
581    
582    
583     try {
584     ExecutorService e = new ForkJoinPool(1);
585     Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
586     public Object run() throws Exception {
587     throw new IndexOutOfBoundsException();
588     }}));
589    
590     Object result = future.get();
591     shouldThrow();
592 jsr166 1.6 } catch (ExecutionException success) {
593 jsr166 1.12 assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
594 jsr166 1.6 } finally {
595 dl 1.1 Policy.setPolicy(savedPolicy);
596     }
597     }
598    
599     /**
600 jsr166 1.6 * execute(null runnable) throws NullPointerException
601 dl 1.1 */
602     public void testExecuteNullRunnable() {
603     try {
604     ExecutorService e = new ForkJoinPool(1);
605     TrackedShortRunnable task = null;
606     Future<?> future = e.submit(task);
607     shouldThrow();
608 jsr166 1.10 } catch (NullPointerException success) {}
609 dl 1.1 }
610    
611    
612     /**
613 jsr166 1.6 * submit(null callable) throws NullPointerException
614 dl 1.1 */
615     public void testSubmitNullCallable() {
616     try {
617     ExecutorService e = new ForkJoinPool(1);
618     StringTask t = null;
619     Future<String> future = e.submit(t);
620     shouldThrow();
621 jsr166 1.10 } catch (NullPointerException success) {}
622 dl 1.1 }
623    
624    
625     /**
626 jsr166 1.4 * Blocking on submit(callable) throws InterruptedException if
627     * caller interrupted.
628 dl 1.1 */
629 jsr166 1.6 public void testInterruptedSubmit() throws InterruptedException {
630 dl 1.1 final ForkJoinPool p = new ForkJoinPool(1);
631    
632 jsr166 1.6 Thread t = new Thread(new CheckedInterruptedRunnable() {
633 jsr166 1.13 public void realRun() throws Throwable {
634 jsr166 1.6 p.submit(new CheckedCallable<Object>() {
635     public Object realCall() throws Throwable {
636 jsr166 1.8 try {
637     Thread.sleep(MEDIUM_DELAY_MS);
638     } catch (InterruptedException ok) {
639     }
640 jsr166 1.6 return null;
641     }}).get();
642     }});
643    
644     t.start();
645 jsr166 1.8 Thread.sleep(SHORT_DELAY_MS);
646 jsr166 1.6 t.interrupt();
647 jsr166 1.8 t.join();
648 dl 1.7 p.shutdownNow();
649 jsr166 1.8 joinPool(p);
650 dl 1.1 }
651    
652     /**
653 jsr166 1.4 * get of submit(callable) throws ExecutionException if callable
654     * throws exception
655 dl 1.1 */
656 jsr166 1.6 public void testSubmitEE() throws Throwable {
657 dl 1.1 ForkJoinPool p = new ForkJoinPool(1);
658     try {
659 jsr166 1.8 p.submit(new Callable() {
660     public Object call() {
661     int i = 5/0;
662     return Boolean.TRUE;
663     }}).get();
664 dl 1.1 shouldThrow();
665 jsr166 1.12 } catch (ExecutionException success) {
666     assertTrue(success.getCause() instanceof ArithmeticException);
667     }
668 jsr166 1.10
669 dl 1.1 joinPool(p);
670     }
671    
672     /**
673 jsr166 1.6 * invokeAny(null) throws NullPointerException
674 dl 1.1 */
675 jsr166 1.6 public void testInvokeAny1() throws Throwable {
676 dl 1.1 ExecutorService e = new ForkJoinPool(1);
677     try {
678     e.invokeAny(null);
679 jsr166 1.6 shouldThrow();
680 dl 1.1 } catch (NullPointerException success) {
681     } finally {
682     joinPool(e);
683     }
684     }
685    
686     /**
687 jsr166 1.6 * invokeAny(empty collection) throws IllegalArgumentException
688 dl 1.1 */
689 jsr166 1.6 public void testInvokeAny2() throws Throwable {
690 dl 1.1 ExecutorService e = new ForkJoinPool(1);
691     try {
692     e.invokeAny(new ArrayList<Callable<String>>());
693 jsr166 1.6 shouldThrow();
694 dl 1.1 } catch (IllegalArgumentException success) {
695     } finally {
696     joinPool(e);
697     }
698     }
699    
700     /**
701 jsr166 1.9 * invokeAny(c) throws NullPointerException if c has a single null element
702     */
703     public void testInvokeAny3() throws Throwable {
704     ExecutorService e = new ForkJoinPool(1);
705     try {
706     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
707     l.add(null);
708     e.invokeAny(l);
709     shouldThrow();
710     } catch (NullPointerException success) {
711     } finally {
712     joinPool(e);
713     }
714     }
715    
716     /**
717 jsr166 1.6 * invokeAny(c) throws NullPointerException if c has null elements
718 dl 1.1 */
719 jsr166 1.9 public void testInvokeAny4() throws Throwable {
720 dl 1.1 ExecutorService e = new ForkJoinPool(1);
721     try {
722     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
723 jsr166 1.9 l.add(new Callable<String>() {
724     public String call() {
725     // The delay gives the pool a chance to notice
726     // the null element.
727     sleepTillInterrupted(SMALL_DELAY_MS);
728     return "foo";
729     }});
730 dl 1.1 l.add(null);
731     e.invokeAny(l);
732 jsr166 1.6 shouldThrow();
733 dl 1.1 } catch (NullPointerException success) {
734     } finally {
735     joinPool(e);
736     }
737     }
738    
739     /**
740     * invokeAny(c) throws ExecutionException if no task in c completes
741     */
742 jsr166 1.9 public void testInvokeAny5() throws Throwable {
743 dl 1.1 ExecutorService e = new ForkJoinPool(1);
744     try {
745     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
746     l.add(new NPETask());
747     e.invokeAny(l);
748 jsr166 1.6 shouldThrow();
749 jsr166 1.2 } catch (ExecutionException success) {
750 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
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 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
833 dl 1.1 } finally {
834     joinPool(e);
835     }
836     }
837    
838     /**
839     * invokeAll(c) returns results of all completed tasks in c
840     */
841 jsr166 1.6 public void testInvokeAll5() throws Throwable {
842 dl 1.1 ExecutorService e = new ForkJoinPool(1);
843     try {
844     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
845     l.add(new StringTask());
846     l.add(new StringTask());
847     List<Future<String>> result = e.invokeAll(l);
848     assertEquals(2, result.size());
849 jsr166 1.6 for (Future<String> future : result)
850     assertSame(TEST_STRING, future.get());
851 dl 1.1 } finally {
852     joinPool(e);
853     }
854     }
855    
856    
857     /**
858 jsr166 1.6 * timed invokeAny(null) throws NullPointerException
859 dl 1.1 */
860 jsr166 1.6 public void testTimedInvokeAny1() throws Throwable {
861 dl 1.1 ExecutorService e = new ForkJoinPool(1);
862     try {
863 jsr166 1.6 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
864     shouldThrow();
865 dl 1.1 } catch (NullPointerException success) {
866     } finally {
867     joinPool(e);
868     }
869     }
870    
871     /**
872 jsr166 1.6 * timed invokeAny(null time unit) throws NullPointerException
873 dl 1.1 */
874 jsr166 1.6 public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
875 dl 1.1 ExecutorService e = new ForkJoinPool(1);
876     try {
877     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
878     l.add(new StringTask());
879     e.invokeAny(l, MEDIUM_DELAY_MS, null);
880 jsr166 1.6 shouldThrow();
881 dl 1.1 } catch (NullPointerException success) {
882     } finally {
883     joinPool(e);
884     }
885     }
886    
887     /**
888 jsr166 1.6 * timed invokeAny(empty collection) throws IllegalArgumentException
889 dl 1.1 */
890 jsr166 1.6 public void testTimedInvokeAny2() throws Throwable {
891 dl 1.1 ExecutorService e = new ForkJoinPool(1);
892     try {
893 jsr166 1.6 e.invokeAny(new ArrayList<Callable<String>>(),
894     MEDIUM_DELAY_MS, MILLISECONDS);
895     shouldThrow();
896 dl 1.1 } catch (IllegalArgumentException success) {
897     } finally {
898     joinPool(e);
899     }
900     }
901    
902     /**
903 jsr166 1.6 * timed invokeAny(c) throws NullPointerException if c has null elements
904 dl 1.1 */
905 jsr166 1.6 public void testTimedInvokeAny3() throws Throwable {
906 jsr166 1.15 final CountDownLatch latch = new CountDownLatch(1);
907 dl 1.1 ExecutorService e = new ForkJoinPool(1);
908     try {
909     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
910 jsr166 1.15 l.add(new Callable<String>() {
911     public String call() {
912     try {
913     latch.await();
914     } catch (InterruptedException ok) {}
915     return TEST_STRING;
916     }});
917 dl 1.1 l.add(null);
918 jsr166 1.6 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
919     shouldThrow();
920 dl 1.1 } catch (NullPointerException success) {
921     } finally {
922 jsr166 1.15 latch.countDown();
923 dl 1.1 joinPool(e);
924     }
925     }
926    
927     /**
928     * timed invokeAny(c) throws ExecutionException if no task completes
929     */
930 jsr166 1.6 public void testTimedInvokeAny4() throws Throwable {
931 dl 1.1 ExecutorService e = new ForkJoinPool(1);
932     try {
933     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
934     l.add(new NPETask());
935 jsr166 1.6 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
936     shouldThrow();
937 jsr166 1.2 } catch (ExecutionException success) {
938 jsr166 1.11 assertTrue(success.getCause() instanceof NullPointerException);
939 dl 1.1 } finally {
940     joinPool(e);
941     }
942     }
943    
944     /**
945     * timed invokeAny(c) returns result of some task in c
946     */
947 jsr166 1.6 public void testTimedInvokeAny5() throws Throwable {
948 dl 1.1 ExecutorService e = new ForkJoinPool(1);
949     try {
950     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
951     l.add(new StringTask());
952     l.add(new StringTask());
953 jsr166 1.6 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
954 dl 1.1 assertSame(TEST_STRING, result);
955     } finally {
956     joinPool(e);
957     }
958     }
959    
960     /**
961 jsr166 1.6 * timed invokeAll(null) throws NullPointerException
962 dl 1.1 */
963 jsr166 1.6 public void testTimedInvokeAll1() throws Throwable {
964 dl 1.1 ExecutorService e = new ForkJoinPool(1);
965     try {
966 jsr166 1.6 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
967     shouldThrow();
968 dl 1.1 } catch (NullPointerException success) {
969     } finally {
970     joinPool(e);
971     }
972     }
973    
974     /**
975 jsr166 1.6 * timed invokeAll(null time unit) throws NullPointerException
976 dl 1.1 */
977 jsr166 1.6 public void testTimedInvokeAllNullTimeUnit() throws Throwable {
978 dl 1.1 ExecutorService e = new ForkJoinPool(1);
979     try {
980     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
981     l.add(new StringTask());
982     e.invokeAll(l, MEDIUM_DELAY_MS, null);
983 jsr166 1.6 shouldThrow();
984 dl 1.1 } catch (NullPointerException success) {
985     } finally {
986     joinPool(e);
987     }
988     }
989    
990     /**
991     * timed invokeAll(empty collection) returns empty collection
992     */
993 jsr166 1.6 public void testTimedInvokeAll2() throws InterruptedException {
994 dl 1.1 ExecutorService e = new ForkJoinPool(1);
995     try {
996 jsr166 1.6 List<Future<String>> r
997     = e.invokeAll(new ArrayList<Callable<String>>(),
998     MEDIUM_DELAY_MS, MILLISECONDS);
999 dl 1.1 assertTrue(r.isEmpty());
1000     } finally {
1001     joinPool(e);
1002     }
1003     }
1004    
1005     /**
1006 jsr166 1.6 * timed invokeAll(c) throws NullPointerException if c has null elements
1007 dl 1.1 */
1008 jsr166 1.6 public void testTimedInvokeAll3() throws InterruptedException {
1009 dl 1.1 ExecutorService e = new ForkJoinPool(1);
1010     try {
1011     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1012     l.add(new StringTask());
1013     l.add(null);
1014 jsr166 1.6 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1015     shouldThrow();
1016 dl 1.1 } catch (NullPointerException success) {
1017     } finally {
1018     joinPool(e);
1019     }
1020     }
1021    
1022     /**
1023     * get of returned element of invokeAll(c) throws exception on failed task
1024     */
1025 jsr166 1.6 public void testTimedInvokeAll4() throws Throwable {
1026 dl 1.1 ExecutorService e = new ForkJoinPool(1);
1027     try {
1028     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1029     l.add(new NPETask());
1030 jsr166 1.6 List<Future<String>> result
1031     = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1032 dl 1.1 assertEquals(1, result.size());
1033 jsr166 1.6 for (Future<String> future : result)
1034     future.get();
1035     shouldThrow();
1036 jsr166 1.2 } catch (ExecutionException success) {
1037 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
1038 dl 1.1 } finally {
1039     joinPool(e);
1040     }
1041     }
1042    
1043     /**
1044     * timed invokeAll(c) returns results of all completed tasks in c
1045     */
1046 jsr166 1.6 public void testTimedInvokeAll5() throws Throwable {
1047 dl 1.1 ExecutorService e = new ForkJoinPool(1);
1048     try {
1049     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1050     l.add(new StringTask());
1051     l.add(new StringTask());
1052 jsr166 1.6 List<Future<String>> result
1053     = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1054 dl 1.1 assertEquals(2, result.size());
1055 jsr166 1.6 for (Future<String> future : result)
1056     assertSame(TEST_STRING, future.get());
1057 dl 1.1 } finally {
1058     joinPool(e);
1059     }
1060     }
1061    
1062     }