ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.41
Committed: Fri May 27 19:42:42 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +0 -6 lines
Log Message:
whitespace

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