ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.40
Committed: Sun May 15 17:32:29 2011 UTC (13 years ago) by jsr166
Branch: MAIN
Changes since 1.39: +20 -6 lines
Log Message:
improve testisQuiescent

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    
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 jsr166 1.2
383 dl 1.1 // FJ Versions of AbstractExecutorService tests
384    
385     /**
386     * execute(runnable) runs it to completion
387     */
388 jsr166 1.6 public void testExecuteRunnable() throws Throwable {
389     ExecutorService e = new ForkJoinPool(1);
390 jsr166 1.23 try {
391 jsr166 1.32 TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS);
392     assertFalse(task.isDone());
393 jsr166 1.23 Future<?> future = e.submit(task);
394 jsr166 1.34 assertNull(future.get());
395 jsr166 1.36 assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS));
396 jsr166 1.32 assertTrue(task.isDone());
397 jsr166 1.36 assertTrue(future.isDone());
398 jsr166 1.34 assertFalse(future.isCancelled());
399 jsr166 1.23 } finally {
400     joinPool(e);
401     }
402 dl 1.1 }
403    
404    
405     /**
406     * Completed submit(callable) returns result
407     */
408 jsr166 1.6 public void testSubmitCallable() throws Throwable {
409     ExecutorService e = new ForkJoinPool(1);
410 jsr166 1.23 try {
411     Future<String> future = e.submit(new StringTask());
412 jsr166 1.34 assertSame(TEST_STRING, future.get());
413     assertTrue(future.isDone());
414     assertFalse(future.isCancelled());
415 jsr166 1.23 } finally {
416     joinPool(e);
417     }
418 dl 1.1 }
419    
420     /**
421     * Completed submit(runnable) returns successfully
422     */
423 jsr166 1.6 public void testSubmitRunnable() throws Throwable {
424     ExecutorService e = new ForkJoinPool(1);
425 jsr166 1.23 try {
426     Future<?> future = e.submit(new NoOpRunnable());
427 jsr166 1.34 assertNull(future.get());
428 jsr166 1.23 assertTrue(future.isDone());
429 jsr166 1.34 assertFalse(future.isCancelled());
430 jsr166 1.23 } finally {
431     joinPool(e);
432     }
433 dl 1.1 }
434    
435     /**
436     * Completed submit(runnable, result) returns result
437     */
438 jsr166 1.6 public void testSubmitRunnable2() throws Throwable {
439     ExecutorService e = new ForkJoinPool(1);
440 jsr166 1.23 try {
441     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
442 jsr166 1.34 assertSame(TEST_STRING, future.get());
443     assertTrue(future.isDone());
444     assertFalse(future.isCancelled());
445 jsr166 1.23 } finally {
446     joinPool(e);
447     }
448 dl 1.1 }
449    
450     /**
451 jsr166 1.33 * A submitted privileged action runs to completion
452 dl 1.1 */
453 dl 1.35 public void testSubmitPrivilegedAction() throws Exception {
454     Runnable r = new CheckedRunnable() {
455     public void realRun() throws Exception {
456     ExecutorService e = new ForkJoinPool(1);
457 jsr166 1.23 Future future = e.submit(Executors.callable(new PrivilegedAction() {
458 dl 1.1 public Object run() {
459     return TEST_STRING;
460     }}));
461    
462 dl 1.35 assertSame(TEST_STRING, future.get());
463     }};
464    
465     runWithPermissions(r,
466     new RuntimePermission("modifyThread"));
467 dl 1.1 }
468    
469     /**
470 jsr166 1.33 * A submitted privileged exception action runs to completion
471 dl 1.1 */
472 dl 1.35 public void testSubmitPrivilegedExceptionAction() throws Exception {
473     Runnable r = new CheckedRunnable() {
474     public void realRun() throws Exception {
475     ExecutorService e = new ForkJoinPool(1);
476 jsr166 1.23 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
477 dl 1.1 public Object run() {
478     return TEST_STRING;
479     }}));
480    
481 dl 1.35 assertSame(TEST_STRING, future.get());
482     }};
483    
484     runWithPermissions(r, new RuntimePermission("modifyThread"));
485 dl 1.1 }
486    
487     /**
488     * A submitted failed privileged exception action reports exception
489     */
490 dl 1.35 public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
491     Runnable r = new CheckedRunnable() {
492     public void realRun() throws Exception {
493     ExecutorService e = new ForkJoinPool(1);
494 jsr166 1.23 Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
495 dl 1.1 public Object run() throws Exception {
496     throw new IndexOutOfBoundsException();
497     }}));
498    
499 dl 1.35 try {
500     future.get();
501     shouldThrow();
502     } catch (ExecutionException success) {
503     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
504     }}};
505    
506     runWithPermissions(r, new RuntimePermission("modifyThread"));
507 dl 1.1 }
508    
509     /**
510 jsr166 1.6 * execute(null runnable) throws NullPointerException
511 dl 1.1 */
512     public void testExecuteNullRunnable() {
513 jsr166 1.23 ExecutorService e = new ForkJoinPool(1);
514 dl 1.1 try {
515 jsr166 1.31 Future<?> future = e.submit((Runnable) null);
516 dl 1.1 shouldThrow();
517 jsr166 1.23 } catch (NullPointerException success) {
518     } finally {
519     joinPool(e);
520     }
521 dl 1.1 }
522    
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     /**
540 jsr166 1.27 * submit(callable).get() throws InterruptedException if interrupted
541 dl 1.1 */
542 jsr166 1.6 public void testInterruptedSubmit() throws InterruptedException {
543 jsr166 1.27 final CountDownLatch submitted = new CountDownLatch(1);
544     final CountDownLatch quittingTime = new CountDownLatch(1);
545     final ExecutorService p = new ForkJoinPool(1);
546     final Callable<Void> awaiter = new CheckedCallable<Void>() {
547     public Void realCall() throws InterruptedException {
548     assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
549     return null;
550     }};
551     try {
552     Thread t = new Thread(new CheckedInterruptedRunnable() {
553     public void realRun() throws Exception {
554     Future<Void> future = p.submit(awaiter);
555     submitted.countDown();
556     future.get();
557     }});
558     t.start();
559     assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
560     t.interrupt();
561     t.join();
562     } finally {
563     quittingTime.countDown();
564     joinPool(p);
565     }
566 dl 1.1 }
567    
568     /**
569 jsr166 1.4 * get of submit(callable) throws ExecutionException if callable
570     * throws exception
571 dl 1.1 */
572 jsr166 1.6 public void testSubmitEE() throws Throwable {
573 dl 1.1 ForkJoinPool p = new ForkJoinPool(1);
574     try {
575 jsr166 1.8 p.submit(new Callable() {
576     public Object call() {
577     int i = 5/0;
578     return Boolean.TRUE;
579     }}).get();
580 dl 1.1 shouldThrow();
581 jsr166 1.12 } catch (ExecutionException success) {
582     assertTrue(success.getCause() instanceof ArithmeticException);
583 jsr166 1.23 } finally {
584     joinPool(p);
585 jsr166 1.12 }
586 dl 1.1 }
587    
588     /**
589 jsr166 1.6 * invokeAny(null) throws NullPointerException
590 dl 1.1 */
591 jsr166 1.6 public void testInvokeAny1() throws Throwable {
592 dl 1.1 ExecutorService e = new ForkJoinPool(1);
593     try {
594     e.invokeAny(null);
595 jsr166 1.6 shouldThrow();
596 dl 1.1 } catch (NullPointerException success) {
597     } finally {
598     joinPool(e);
599     }
600     }
601    
602     /**
603 jsr166 1.6 * invokeAny(empty collection) throws IllegalArgumentException
604 dl 1.1 */
605 jsr166 1.6 public void testInvokeAny2() throws Throwable {
606 dl 1.1 ExecutorService e = new ForkJoinPool(1);
607     try {
608     e.invokeAny(new ArrayList<Callable<String>>());
609 jsr166 1.6 shouldThrow();
610 dl 1.1 } catch (IllegalArgumentException success) {
611     } finally {
612     joinPool(e);
613     }
614     }
615    
616     /**
617 jsr166 1.9 * invokeAny(c) throws NullPointerException if c has a single null element
618     */
619     public void testInvokeAny3() throws Throwable {
620     ExecutorService e = new ForkJoinPool(1);
621 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
622     l.add(null);
623 jsr166 1.9 try {
624     e.invokeAny(l);
625     shouldThrow();
626     } catch (NullPointerException success) {
627     } finally {
628     joinPool(e);
629     }
630     }
631    
632     /**
633 jsr166 1.6 * invokeAny(c) throws NullPointerException if c has null elements
634 dl 1.1 */
635 jsr166 1.9 public void testInvokeAny4() throws Throwable {
636 jsr166 1.16 CountDownLatch latch = new CountDownLatch(1);
637 dl 1.1 ExecutorService e = new ForkJoinPool(1);
638 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
639     l.add(latchAwaitingStringTask(latch));
640     l.add(null);
641 dl 1.1 try {
642     e.invokeAny(l);
643 jsr166 1.6 shouldThrow();
644 dl 1.1 } catch (NullPointerException success) {
645     } finally {
646 jsr166 1.16 latch.countDown();
647 dl 1.1 joinPool(e);
648     }
649     }
650    
651     /**
652     * invokeAny(c) throws ExecutionException if no task in c completes
653     */
654 jsr166 1.9 public void testInvokeAny5() throws Throwable {
655 dl 1.1 ExecutorService e = new ForkJoinPool(1);
656 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
657     l.add(new NPETask());
658 dl 1.1 try {
659     e.invokeAny(l);
660 jsr166 1.6 shouldThrow();
661 jsr166 1.2 } catch (ExecutionException success) {
662 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
663 dl 1.1 } finally {
664     joinPool(e);
665     }
666     }
667    
668     /**
669     * invokeAny(c) returns result of some task in c if at least one completes
670     */
671 jsr166 1.9 public void testInvokeAny6() throws Throwable {
672 dl 1.1 ExecutorService e = new ForkJoinPool(1);
673     try {
674 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
675 dl 1.1 l.add(new StringTask());
676     l.add(new StringTask());
677     String result = e.invokeAny(l);
678     assertSame(TEST_STRING, result);
679     } finally {
680     joinPool(e);
681     }
682     }
683    
684     /**
685 jsr166 1.6 * invokeAll(null) throws NullPointerException
686 dl 1.1 */
687 jsr166 1.6 public void testInvokeAll1() throws Throwable {
688 dl 1.1 ExecutorService e = new ForkJoinPool(1);
689     try {
690     e.invokeAll(null);
691 jsr166 1.6 shouldThrow();
692 dl 1.1 } catch (NullPointerException success) {
693     } finally {
694     joinPool(e);
695     }
696     }
697    
698     /**
699     * invokeAll(empty collection) returns empty collection
700     */
701 jsr166 1.6 public void testInvokeAll2() throws InterruptedException {
702 dl 1.1 ExecutorService e = new ForkJoinPool(1);
703     try {
704 jsr166 1.6 List<Future<String>> r
705     = e.invokeAll(new ArrayList<Callable<String>>());
706 dl 1.1 assertTrue(r.isEmpty());
707     } finally {
708     joinPool(e);
709     }
710     }
711    
712     /**
713 jsr166 1.6 * invokeAll(c) throws NullPointerException if c has null elements
714 dl 1.1 */
715 jsr166 1.6 public void testInvokeAll3() throws InterruptedException {
716 dl 1.1 ExecutorService e = new ForkJoinPool(1);
717 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
718     l.add(new StringTask());
719     l.add(null);
720 dl 1.1 try {
721     e.invokeAll(l);
722 jsr166 1.6 shouldThrow();
723 dl 1.1 } catch (NullPointerException success) {
724     } finally {
725     joinPool(e);
726     }
727     }
728    
729     /**
730 jsr166 1.6 * get of returned element of invokeAll(c) throws
731     * ExecutionException on failed task
732 dl 1.1 */
733 jsr166 1.6 public void testInvokeAll4() throws Throwable {
734 dl 1.1 ExecutorService e = new ForkJoinPool(1);
735 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
736     l.add(new NPETask());
737     List<Future<String>> futures = e.invokeAll(l);
738     assertEquals(1, futures.size());
739 dl 1.1 try {
740 jsr166 1.16 futures.get(0).get();
741 jsr166 1.6 shouldThrow();
742 jsr166 1.2 } catch (ExecutionException success) {
743 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
744 dl 1.1 } finally {
745     joinPool(e);
746     }
747     }
748    
749     /**
750     * invokeAll(c) returns results of all completed tasks in c
751     */
752 jsr166 1.6 public void testInvokeAll5() throws Throwable {
753 dl 1.1 ExecutorService e = new ForkJoinPool(1);
754     try {
755 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
756 dl 1.1 l.add(new StringTask());
757     l.add(new StringTask());
758 jsr166 1.16 List<Future<String>> futures = e.invokeAll(l);
759     assertEquals(2, futures.size());
760     for (Future<String> future : futures)
761 jsr166 1.6 assertSame(TEST_STRING, future.get());
762 dl 1.1 } finally {
763     joinPool(e);
764     }
765     }
766    
767    
768     /**
769 jsr166 1.6 * timed invokeAny(null) throws NullPointerException
770 dl 1.1 */
771 jsr166 1.6 public void testTimedInvokeAny1() throws Throwable {
772 dl 1.1 ExecutorService e = new ForkJoinPool(1);
773     try {
774 jsr166 1.27 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
775 jsr166 1.6 shouldThrow();
776 dl 1.1 } catch (NullPointerException success) {
777     } finally {
778     joinPool(e);
779     }
780     }
781    
782     /**
783 jsr166 1.6 * timed invokeAny(null time unit) throws NullPointerException
784 dl 1.1 */
785 jsr166 1.6 public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
786 dl 1.1 ExecutorService e = new ForkJoinPool(1);
787 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
788     l.add(new StringTask());
789 dl 1.1 try {
790     e.invokeAny(l, MEDIUM_DELAY_MS, null);
791 jsr166 1.6 shouldThrow();
792 dl 1.1 } catch (NullPointerException success) {
793     } finally {
794     joinPool(e);
795     }
796     }
797    
798     /**
799 jsr166 1.6 * timed invokeAny(empty collection) throws IllegalArgumentException
800 dl 1.1 */
801 jsr166 1.6 public void testTimedInvokeAny2() throws Throwable {
802 dl 1.1 ExecutorService e = new ForkJoinPool(1);
803     try {
804 jsr166 1.6 e.invokeAny(new ArrayList<Callable<String>>(),
805 jsr166 1.27 MEDIUM_DELAY_MS, MILLISECONDS);
806 jsr166 1.6 shouldThrow();
807 dl 1.1 } catch (IllegalArgumentException success) {
808     } finally {
809     joinPool(e);
810     }
811     }
812    
813     /**
814 jsr166 1.6 * timed invokeAny(c) throws NullPointerException if c has null elements
815 dl 1.1 */
816 jsr166 1.6 public void testTimedInvokeAny3() throws Throwable {
817 jsr166 1.16 CountDownLatch latch = new CountDownLatch(1);
818 dl 1.1 ExecutorService e = new ForkJoinPool(1);
819 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
820     l.add(latchAwaitingStringTask(latch));
821     l.add(null);
822 dl 1.1 try {
823 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
824 jsr166 1.6 shouldThrow();
825 dl 1.1 } catch (NullPointerException success) {
826     } finally {
827 jsr166 1.15 latch.countDown();
828 dl 1.1 joinPool(e);
829     }
830     }
831    
832     /**
833     * timed invokeAny(c) throws ExecutionException if no task completes
834     */
835 jsr166 1.6 public void testTimedInvokeAny4() throws Throwable {
836 dl 1.1 ExecutorService e = new ForkJoinPool(1);
837 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
838     l.add(new NPETask());
839 dl 1.1 try {
840 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
841 jsr166 1.6 shouldThrow();
842 jsr166 1.2 } catch (ExecutionException success) {
843 jsr166 1.11 assertTrue(success.getCause() instanceof NullPointerException);
844 dl 1.1 } finally {
845     joinPool(e);
846     }
847     }
848    
849     /**
850     * timed invokeAny(c) returns result of some task in c
851     */
852 jsr166 1.6 public void testTimedInvokeAny5() throws Throwable {
853 dl 1.1 ExecutorService e = new ForkJoinPool(1);
854     try {
855 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
856 dl 1.1 l.add(new StringTask());
857     l.add(new StringTask());
858 jsr166 1.27 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
859 dl 1.1 assertSame(TEST_STRING, result);
860     } finally {
861     joinPool(e);
862     }
863     }
864    
865     /**
866 jsr166 1.6 * timed invokeAll(null) throws NullPointerException
867 dl 1.1 */
868 jsr166 1.6 public void testTimedInvokeAll1() throws Throwable {
869 dl 1.1 ExecutorService e = new ForkJoinPool(1);
870     try {
871 jsr166 1.27 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
872 jsr166 1.6 shouldThrow();
873 dl 1.1 } catch (NullPointerException success) {
874     } finally {
875     joinPool(e);
876     }
877     }
878    
879     /**
880 jsr166 1.6 * timed invokeAll(null time unit) throws NullPointerException
881 dl 1.1 */
882 jsr166 1.6 public void testTimedInvokeAllNullTimeUnit() throws Throwable {
883 dl 1.1 ExecutorService e = new ForkJoinPool(1);
884 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
885     l.add(new StringTask());
886 dl 1.1 try {
887     e.invokeAll(l, MEDIUM_DELAY_MS, null);
888 jsr166 1.6 shouldThrow();
889 dl 1.1 } catch (NullPointerException success) {
890     } finally {
891     joinPool(e);
892     }
893     }
894    
895     /**
896     * timed invokeAll(empty collection) returns empty collection
897     */
898 jsr166 1.6 public void testTimedInvokeAll2() throws InterruptedException {
899 dl 1.1 ExecutorService e = new ForkJoinPool(1);
900     try {
901 jsr166 1.6 List<Future<String>> r
902     = e.invokeAll(new ArrayList<Callable<String>>(),
903 jsr166 1.27 MEDIUM_DELAY_MS, MILLISECONDS);
904 dl 1.1 assertTrue(r.isEmpty());
905     } finally {
906     joinPool(e);
907     }
908     }
909    
910     /**
911 jsr166 1.6 * timed invokeAll(c) throws NullPointerException if c has null elements
912 dl 1.1 */
913 jsr166 1.6 public void testTimedInvokeAll3() throws InterruptedException {
914 dl 1.1 ExecutorService e = new ForkJoinPool(1);
915 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
916     l.add(new StringTask());
917     l.add(null);
918 dl 1.1 try {
919 jsr166 1.27 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
920 jsr166 1.6 shouldThrow();
921 dl 1.1 } catch (NullPointerException success) {
922     } finally {
923     joinPool(e);
924     }
925     }
926    
927     /**
928     * get of returned element of invokeAll(c) throws exception on failed task
929     */
930 jsr166 1.6 public void testTimedInvokeAll4() throws Throwable {
931 dl 1.1 ExecutorService e = new ForkJoinPool(1);
932 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
933     l.add(new NPETask());
934     List<Future<String>> futures
935 jsr166 1.27 = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
936 jsr166 1.16 assertEquals(1, futures.size());
937 dl 1.1 try {
938 jsr166 1.16 futures.get(0).get();
939 jsr166 1.6 shouldThrow();
940 jsr166 1.2 } catch (ExecutionException success) {
941 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
942 dl 1.1 } finally {
943     joinPool(e);
944     }
945     }
946    
947     /**
948     * timed invokeAll(c) returns results of all completed tasks in c
949     */
950 jsr166 1.6 public void testTimedInvokeAll5() throws Throwable {
951 dl 1.1 ExecutorService e = new ForkJoinPool(1);
952     try {
953 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
954 dl 1.1 l.add(new StringTask());
955     l.add(new StringTask());
956 jsr166 1.16 List<Future<String>> futures
957 jsr166 1.27 = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
958 jsr166 1.16 assertEquals(2, futures.size());
959     for (Future<String> future : futures)
960 jsr166 1.6 assertSame(TEST_STRING, future.get());
961 dl 1.1 } finally {
962     joinPool(e);
963     }
964     }
965    
966     }