ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.42
Committed: Sun May 29 07:01:17 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.41: +8 -4 lines
Log Message:
various test case improvements

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