ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.49
Committed: Sat Feb 16 20:50:29 2013 UTC (11 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +1 -1 lines
Log Message:
javadoc comment correctness

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 jsr166 1.49 /*
43 dl 1.1 * 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 dl 1.44 try {
246     p.execute(new FibTask(8));
247 dl 1.45 assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
248 jsr166 1.46 } catch (RejectedExecutionException ok) {
249 dl 1.44 }
250 dl 1.1 } finally {
251 dl 1.28 p.shutdownNow(); // failure might have prevented processing task
252 dl 1.1 joinPool(p);
253     }
254     }
255    
256 jsr166 1.2 /**
257 jsr166 1.40 * After invoking a single task, isQuiescent eventually becomes
258     * true, at which time queues are empty, threads are not active,
259     * the task has completed successfully, and construction
260     * parameters continue to hold
261 dl 1.1 */
262 jsr166 1.43 public void testIsQuiescent() throws Exception {
263 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(2);
264 dl 1.1 try {
265 jsr166 1.30 assertTrue(p.isQuiescent());
266 jsr166 1.40 long startTime = System.nanoTime();
267     FibTask f = new FibTask(20);
268     p.invoke(f);
269 jsr166 1.26 assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
270     p.getFactory());
271 jsr166 1.40 while (! p.isQuiescent()) {
272     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
273     throw new AssertionFailedError("timed out");
274     assertFalse(p.getAsyncMode());
275     assertFalse(p.isShutdown());
276     assertFalse(p.isTerminating());
277     assertFalse(p.isTerminated());
278     Thread.yield();
279     }
280    
281 dl 1.1 assertTrue(p.isQuiescent());
282     assertFalse(p.getAsyncMode());
283 jsr166 1.26 assertEquals(0, p.getActiveThreadCount());
284     assertEquals(0, p.getQueuedTaskCount());
285     assertEquals(0, p.getQueuedSubmissionCount());
286 dl 1.1 assertFalse(p.hasQueuedSubmissions());
287     assertFalse(p.isShutdown());
288     assertFalse(p.isTerminating());
289     assertFalse(p.isTerminated());
290 jsr166 1.40 assertTrue(f.isDone());
291     assertEquals(6765, (int) f.get());
292 dl 1.1 } finally {
293     joinPool(p);
294     }
295     }
296    
297     /**
298     * Completed submit(ForkJoinTask) returns result
299     */
300 jsr166 1.6 public void testSubmitForkJoinTask() throws Throwable {
301 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
302 dl 1.1 try {
303     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
304 jsr166 1.26 assertEquals(21, (int) f.get());
305 dl 1.1 } finally {
306     joinPool(p);
307     }
308     }
309    
310     /**
311     * A task submitted after shutdown is rejected
312     */
313     public void testSubmitAfterShutdown() {
314 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(1);
315 dl 1.1 try {
316     p.shutdown();
317     assertTrue(p.isShutdown());
318 jsr166 1.26 try {
319     ForkJoinTask<Integer> f = p.submit(new FibTask(8));
320     shouldThrow();
321     } catch (RejectedExecutionException success) {}
322 dl 1.1 } finally {
323     joinPool(p);
324     }
325     }
326    
327     /**
328     * Pool maintains parallelism when using ManagedBlocker
329     */
330 jsr166 1.6 public void testBlockingForkJoinTask() throws Throwable {
331 jsr166 1.23 ForkJoinPool p = new ForkJoinPool(4);
332 dl 1.1 try {
333     ReentrantLock lock = new ReentrantLock();
334     ManagedLocker locker = new ManagedLocker(lock);
335 jsr166 1.32 ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
336 dl 1.1 p.execute(f);
337 jsr166 1.32 assertEquals(6765, (int) f.get());
338 dl 1.1 } finally {
339 dl 1.7 p.shutdownNow(); // don't wait out shutdown
340 dl 1.1 }
341     }
342    
343     /**
344     * pollSubmission returns unexecuted submitted task, if present
345     */
346     public void testPollSubmission() {
347 jsr166 1.36 final CountDownLatch done = new CountDownLatch(1);
348 jsr166 1.23 SubFJP p = new SubFJP();
349 dl 1.1 try {
350 jsr166 1.36 ForkJoinTask a = p.submit(awaiter(done));
351     ForkJoinTask b = p.submit(awaiter(done));
352     ForkJoinTask c = p.submit(awaiter(done));
353 dl 1.1 ForkJoinTask r = p.pollSubmission();
354     assertTrue(r == a || r == b || r == c);
355     assertFalse(r.isDone());
356     } finally {
357 jsr166 1.36 done.countDown();
358 dl 1.1 joinPool(p);
359     }
360     }
361    
362     /**
363     * drainTasksTo transfers unexecuted submitted tasks, if present
364     */
365     public void testDrainTasksTo() {
366 jsr166 1.36 final CountDownLatch done = new CountDownLatch(1);
367 jsr166 1.23 SubFJP p = new SubFJP();
368 dl 1.1 try {
369 jsr166 1.36 ForkJoinTask a = p.submit(awaiter(done));
370     ForkJoinTask b = p.submit(awaiter(done));
371     ForkJoinTask c = p.submit(awaiter(done));
372 dl 1.1 ArrayList<ForkJoinTask> al = new ArrayList();
373     p.drainTasksTo(al);
374     assertTrue(al.size() > 0);
375     for (ForkJoinTask r : al) {
376     assertTrue(r == a || r == b || r == c);
377     assertFalse(r.isDone());
378     }
379     } finally {
380 jsr166 1.36 done.countDown();
381 dl 1.1 joinPool(p);
382     }
383     }
384    
385     // FJ Versions of AbstractExecutorService tests
386    
387     /**
388     * execute(runnable) runs it to completion
389     */
390 jsr166 1.6 public void testExecuteRunnable() throws Throwable {
391     ExecutorService e = new ForkJoinPool(1);
392 jsr166 1.23 try {
393 jsr166 1.42 final AtomicBoolean done = new AtomicBoolean(false);
394     CheckedRunnable task = new CheckedRunnable() {
395     public void realRun() {
396     done.set(true);
397     }};
398 jsr166 1.23 Future<?> future = e.submit(task);
399 jsr166 1.34 assertNull(future.get());
400 jsr166 1.42 assertNull(future.get(0, MILLISECONDS));
401     assertTrue(done.get());
402 jsr166 1.36 assertTrue(future.isDone());
403 jsr166 1.34 assertFalse(future.isCancelled());
404 jsr166 1.23 } finally {
405     joinPool(e);
406     }
407 dl 1.1 }
408    
409     /**
410     * Completed submit(callable) returns result
411     */
412 jsr166 1.6 public void testSubmitCallable() throws Throwable {
413     ExecutorService e = new ForkJoinPool(1);
414 jsr166 1.23 try {
415     Future<String> future = e.submit(new StringTask());
416 jsr166 1.34 assertSame(TEST_STRING, future.get());
417     assertTrue(future.isDone());
418     assertFalse(future.isCancelled());
419 jsr166 1.23 } finally {
420     joinPool(e);
421     }
422 dl 1.1 }
423    
424     /**
425     * Completed submit(runnable) returns successfully
426     */
427 jsr166 1.6 public void testSubmitRunnable() throws Throwable {
428     ExecutorService e = new ForkJoinPool(1);
429 jsr166 1.23 try {
430     Future<?> future = e.submit(new NoOpRunnable());
431 jsr166 1.34 assertNull(future.get());
432 jsr166 1.23 assertTrue(future.isDone());
433 jsr166 1.34 assertFalse(future.isCancelled());
434 jsr166 1.23 } finally {
435     joinPool(e);
436     }
437 dl 1.1 }
438    
439     /**
440     * Completed submit(runnable, result) returns result
441     */
442 jsr166 1.6 public void testSubmitRunnable2() throws Throwable {
443     ExecutorService e = new ForkJoinPool(1);
444 jsr166 1.23 try {
445     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
446 jsr166 1.34 assertSame(TEST_STRING, future.get());
447     assertTrue(future.isDone());
448     assertFalse(future.isCancelled());
449 jsr166 1.23 } finally {
450     joinPool(e);
451     }
452 dl 1.1 }
453    
454     /**
455 jsr166 1.33 * A submitted privileged action runs to completion
456 dl 1.1 */
457 dl 1.35 public void testSubmitPrivilegedAction() throws Exception {
458 jsr166 1.48 final Callable callable = Executors.callable(new PrivilegedAction() {
459     public Object run() { return TEST_STRING; }});
460 dl 1.35 Runnable r = new CheckedRunnable() {
461 jsr166 1.48 public void realRun() throws Exception {
462     ExecutorService e = new ForkJoinPool(1);
463     try {
464     Future future = e.submit(callable);
465 dl 1.35 assertSame(TEST_STRING, future.get());
466 jsr166 1.48 } finally {
467     joinPool(e);
468     }
469     }};
470 dl 1.35
471 jsr166 1.48 runWithPermissions(r, new RuntimePermission("modifyThread"));
472 dl 1.1 }
473    
474     /**
475 jsr166 1.33 * A submitted privileged exception action runs to completion
476 dl 1.1 */
477 dl 1.35 public void testSubmitPrivilegedExceptionAction() throws Exception {
478 jsr166 1.48 final Callable callable =
479     Executors.callable(new PrivilegedExceptionAction() {
480     public Object run() { return TEST_STRING; }});
481 dl 1.35 Runnable r = new CheckedRunnable() {
482 jsr166 1.48 public void realRun() throws Exception {
483     ExecutorService e = new ForkJoinPool(1);
484     try {
485     Future future = e.submit(callable);
486 dl 1.35 assertSame(TEST_STRING, future.get());
487 jsr166 1.48 } finally {
488     joinPool(e);
489     }
490     }};
491 dl 1.35
492     runWithPermissions(r, new RuntimePermission("modifyThread"));
493 dl 1.1 }
494    
495     /**
496     * A submitted failed privileged exception action reports exception
497     */
498 dl 1.35 public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
499 jsr166 1.48 final Callable callable =
500     Executors.callable(new PrivilegedExceptionAction() {
501     public Object run() { throw new IndexOutOfBoundsException(); }});
502 dl 1.35 Runnable r = new CheckedRunnable() {
503 jsr166 1.48 public void realRun() throws Exception {
504     ExecutorService e = new ForkJoinPool(1);
505     try {
506     Future future = e.submit(callable);
507 dl 1.35 try {
508     future.get();
509     shouldThrow();
510     } catch (ExecutionException success) {
511     assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
512 jsr166 1.48 }
513     } finally {
514     joinPool(e);
515     }
516     }};
517 dl 1.35
518     runWithPermissions(r, new RuntimePermission("modifyThread"));
519 dl 1.1 }
520    
521     /**
522 jsr166 1.6 * execute(null runnable) throws NullPointerException
523 dl 1.1 */
524     public void testExecuteNullRunnable() {
525 jsr166 1.23 ExecutorService e = new ForkJoinPool(1);
526 dl 1.1 try {
527 jsr166 1.31 Future<?> future = e.submit((Runnable) null);
528 dl 1.1 shouldThrow();
529 jsr166 1.23 } catch (NullPointerException success) {
530     } finally {
531     joinPool(e);
532     }
533 dl 1.1 }
534    
535     /**
536 jsr166 1.6 * submit(null callable) throws NullPointerException
537 dl 1.1 */
538     public void testSubmitNullCallable() {
539 jsr166 1.23 ExecutorService e = new ForkJoinPool(1);
540 dl 1.1 try {
541 jsr166 1.31 Future<String> future = e.submit((Callable) null);
542 dl 1.1 shouldThrow();
543 jsr166 1.23 } catch (NullPointerException success) {
544     } finally {
545     joinPool(e);
546     }
547 dl 1.1 }
548    
549     /**
550 jsr166 1.27 * submit(callable).get() throws InterruptedException if interrupted
551 dl 1.1 */
552 jsr166 1.6 public void testInterruptedSubmit() throws InterruptedException {
553 jsr166 1.27 final CountDownLatch submitted = new CountDownLatch(1);
554     final CountDownLatch quittingTime = new CountDownLatch(1);
555     final ExecutorService p = new ForkJoinPool(1);
556     final Callable<Void> awaiter = new CheckedCallable<Void>() {
557     public Void realCall() throws InterruptedException {
558     assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
559     return null;
560     }};
561     try {
562     Thread t = new Thread(new CheckedInterruptedRunnable() {
563     public void realRun() throws Exception {
564     Future<Void> future = p.submit(awaiter);
565     submitted.countDown();
566     future.get();
567     }});
568     t.start();
569     assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
570     t.interrupt();
571     t.join();
572     } finally {
573     quittingTime.countDown();
574     joinPool(p);
575     }
576 dl 1.1 }
577    
578     /**
579 jsr166 1.4 * get of submit(callable) throws ExecutionException if callable
580     * throws exception
581 dl 1.1 */
582 jsr166 1.6 public void testSubmitEE() throws Throwable {
583 dl 1.1 ForkJoinPool p = new ForkJoinPool(1);
584     try {
585 jsr166 1.8 p.submit(new Callable() {
586 jsr166 1.47 public Object call() { throw new ArithmeticException(); }})
587     .get();
588 dl 1.1 shouldThrow();
589 jsr166 1.12 } catch (ExecutionException success) {
590     assertTrue(success.getCause() instanceof ArithmeticException);
591 jsr166 1.23 } finally {
592     joinPool(p);
593 jsr166 1.12 }
594 dl 1.1 }
595    
596     /**
597 jsr166 1.6 * invokeAny(null) throws NullPointerException
598 dl 1.1 */
599 jsr166 1.6 public void testInvokeAny1() throws Throwable {
600 dl 1.1 ExecutorService e = new ForkJoinPool(1);
601     try {
602     e.invokeAny(null);
603 jsr166 1.6 shouldThrow();
604 dl 1.1 } catch (NullPointerException success) {
605     } finally {
606     joinPool(e);
607     }
608     }
609    
610     /**
611 jsr166 1.6 * invokeAny(empty collection) throws IllegalArgumentException
612 dl 1.1 */
613 jsr166 1.6 public void testInvokeAny2() throws Throwable {
614 dl 1.1 ExecutorService e = new ForkJoinPool(1);
615     try {
616     e.invokeAny(new ArrayList<Callable<String>>());
617 jsr166 1.6 shouldThrow();
618 dl 1.1 } catch (IllegalArgumentException success) {
619     } finally {
620     joinPool(e);
621     }
622     }
623    
624     /**
625 jsr166 1.9 * invokeAny(c) throws NullPointerException if c has a single null element
626     */
627     public void testInvokeAny3() throws Throwable {
628     ExecutorService e = new ForkJoinPool(1);
629 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
630     l.add(null);
631 jsr166 1.9 try {
632     e.invokeAny(l);
633     shouldThrow();
634     } catch (NullPointerException success) {
635     } finally {
636     joinPool(e);
637     }
638     }
639    
640     /**
641 jsr166 1.6 * invokeAny(c) throws NullPointerException if c has null elements
642 dl 1.1 */
643 jsr166 1.9 public void testInvokeAny4() throws Throwable {
644 jsr166 1.16 CountDownLatch latch = new CountDownLatch(1);
645 dl 1.1 ExecutorService e = new ForkJoinPool(1);
646 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
647     l.add(latchAwaitingStringTask(latch));
648     l.add(null);
649 dl 1.1 try {
650     e.invokeAny(l);
651 jsr166 1.6 shouldThrow();
652 dl 1.1 } catch (NullPointerException success) {
653     } finally {
654 jsr166 1.16 latch.countDown();
655 dl 1.1 joinPool(e);
656     }
657     }
658    
659     /**
660     * invokeAny(c) throws ExecutionException if no task in c completes
661     */
662 jsr166 1.9 public void testInvokeAny5() throws Throwable {
663 dl 1.1 ExecutorService e = new ForkJoinPool(1);
664 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
665     l.add(new NPETask());
666 dl 1.1 try {
667     e.invokeAny(l);
668 jsr166 1.6 shouldThrow();
669 jsr166 1.2 } catch (ExecutionException success) {
670 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
671 dl 1.1 } finally {
672     joinPool(e);
673     }
674     }
675    
676     /**
677     * invokeAny(c) returns result of some task in c if at least one completes
678     */
679 jsr166 1.9 public void testInvokeAny6() throws Throwable {
680 dl 1.1 ExecutorService e = new ForkJoinPool(1);
681     try {
682 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
683 dl 1.1 l.add(new StringTask());
684     l.add(new StringTask());
685     String result = e.invokeAny(l);
686     assertSame(TEST_STRING, result);
687     } finally {
688     joinPool(e);
689     }
690     }
691    
692     /**
693 jsr166 1.6 * invokeAll(null) throws NullPointerException
694 dl 1.1 */
695 jsr166 1.6 public void testInvokeAll1() throws Throwable {
696 dl 1.1 ExecutorService e = new ForkJoinPool(1);
697     try {
698     e.invokeAll(null);
699 jsr166 1.6 shouldThrow();
700 dl 1.1 } catch (NullPointerException success) {
701     } finally {
702     joinPool(e);
703     }
704     }
705    
706     /**
707     * invokeAll(empty collection) returns empty collection
708     */
709 jsr166 1.6 public void testInvokeAll2() throws InterruptedException {
710 dl 1.1 ExecutorService e = new ForkJoinPool(1);
711     try {
712 jsr166 1.6 List<Future<String>> r
713     = e.invokeAll(new ArrayList<Callable<String>>());
714 dl 1.1 assertTrue(r.isEmpty());
715     } finally {
716     joinPool(e);
717     }
718     }
719    
720     /**
721 jsr166 1.6 * invokeAll(c) throws NullPointerException if c has null elements
722 dl 1.1 */
723 jsr166 1.6 public void testInvokeAll3() throws InterruptedException {
724 dl 1.1 ExecutorService e = new ForkJoinPool(1);
725 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
726     l.add(new StringTask());
727     l.add(null);
728 dl 1.1 try {
729     e.invokeAll(l);
730 jsr166 1.6 shouldThrow();
731 dl 1.1 } catch (NullPointerException success) {
732     } finally {
733     joinPool(e);
734     }
735     }
736    
737     /**
738 jsr166 1.6 * get of returned element of invokeAll(c) throws
739     * ExecutionException on failed task
740 dl 1.1 */
741 jsr166 1.6 public void testInvokeAll4() throws Throwable {
742 dl 1.1 ExecutorService e = new ForkJoinPool(1);
743 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
744     l.add(new NPETask());
745     List<Future<String>> futures = e.invokeAll(l);
746     assertEquals(1, futures.size());
747 dl 1.1 try {
748 jsr166 1.16 futures.get(0).get();
749 jsr166 1.6 shouldThrow();
750 jsr166 1.2 } catch (ExecutionException success) {
751 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
752 dl 1.1 } finally {
753     joinPool(e);
754     }
755     }
756    
757     /**
758     * invokeAll(c) returns results of all completed tasks in c
759     */
760 jsr166 1.6 public void testInvokeAll5() throws Throwable {
761 dl 1.1 ExecutorService e = new ForkJoinPool(1);
762     try {
763 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
764 dl 1.1 l.add(new StringTask());
765     l.add(new StringTask());
766 jsr166 1.16 List<Future<String>> futures = e.invokeAll(l);
767     assertEquals(2, futures.size());
768     for (Future<String> future : futures)
769 jsr166 1.6 assertSame(TEST_STRING, future.get());
770 dl 1.1 } finally {
771     joinPool(e);
772     }
773     }
774    
775     /**
776 jsr166 1.6 * timed invokeAny(null) throws NullPointerException
777 dl 1.1 */
778 jsr166 1.6 public void testTimedInvokeAny1() throws Throwable {
779 dl 1.1 ExecutorService e = new ForkJoinPool(1);
780     try {
781 jsr166 1.27 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
782 jsr166 1.6 shouldThrow();
783 dl 1.1 } catch (NullPointerException success) {
784     } finally {
785     joinPool(e);
786     }
787     }
788    
789     /**
790 jsr166 1.6 * timed invokeAny(null time unit) throws NullPointerException
791 dl 1.1 */
792 jsr166 1.6 public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
793 dl 1.1 ExecutorService e = new ForkJoinPool(1);
794 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
795     l.add(new StringTask());
796 dl 1.1 try {
797     e.invokeAny(l, MEDIUM_DELAY_MS, null);
798 jsr166 1.6 shouldThrow();
799 dl 1.1 } catch (NullPointerException success) {
800     } finally {
801     joinPool(e);
802     }
803     }
804    
805     /**
806 jsr166 1.6 * timed invokeAny(empty collection) throws IllegalArgumentException
807 dl 1.1 */
808 jsr166 1.6 public void testTimedInvokeAny2() throws Throwable {
809 dl 1.1 ExecutorService e = new ForkJoinPool(1);
810     try {
811 jsr166 1.6 e.invokeAny(new ArrayList<Callable<String>>(),
812 jsr166 1.27 MEDIUM_DELAY_MS, MILLISECONDS);
813 jsr166 1.6 shouldThrow();
814 dl 1.1 } catch (IllegalArgumentException success) {
815     } finally {
816     joinPool(e);
817     }
818     }
819    
820     /**
821 jsr166 1.6 * timed invokeAny(c) throws NullPointerException if c has null elements
822 dl 1.1 */
823 jsr166 1.6 public void testTimedInvokeAny3() throws Throwable {
824 jsr166 1.16 CountDownLatch latch = new CountDownLatch(1);
825 dl 1.1 ExecutorService e = new ForkJoinPool(1);
826 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
827     l.add(latchAwaitingStringTask(latch));
828     l.add(null);
829 dl 1.1 try {
830 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
831 jsr166 1.6 shouldThrow();
832 dl 1.1 } catch (NullPointerException success) {
833     } finally {
834 jsr166 1.15 latch.countDown();
835 dl 1.1 joinPool(e);
836     }
837     }
838    
839     /**
840     * timed invokeAny(c) throws ExecutionException if no task completes
841     */
842 jsr166 1.6 public void testTimedInvokeAny4() throws Throwable {
843 dl 1.1 ExecutorService e = new ForkJoinPool(1);
844 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
845     l.add(new NPETask());
846 dl 1.1 try {
847 jsr166 1.27 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
848 jsr166 1.6 shouldThrow();
849 jsr166 1.2 } catch (ExecutionException success) {
850 jsr166 1.11 assertTrue(success.getCause() instanceof NullPointerException);
851 dl 1.1 } finally {
852     joinPool(e);
853     }
854     }
855    
856     /**
857     * timed invokeAny(c) returns result of some task in c
858     */
859 jsr166 1.6 public void testTimedInvokeAny5() throws Throwable {
860 dl 1.1 ExecutorService e = new ForkJoinPool(1);
861     try {
862 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
863 dl 1.1 l.add(new StringTask());
864     l.add(new StringTask());
865 jsr166 1.27 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
866 dl 1.1 assertSame(TEST_STRING, result);
867     } finally {
868     joinPool(e);
869     }
870     }
871    
872     /**
873 jsr166 1.6 * timed invokeAll(null) throws NullPointerException
874 dl 1.1 */
875 jsr166 1.6 public void testTimedInvokeAll1() throws Throwable {
876 dl 1.1 ExecutorService e = new ForkJoinPool(1);
877     try {
878 jsr166 1.27 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
879 jsr166 1.6 shouldThrow();
880 dl 1.1 } catch (NullPointerException success) {
881     } finally {
882     joinPool(e);
883     }
884     }
885    
886     /**
887 jsr166 1.6 * timed invokeAll(null time unit) throws NullPointerException
888 dl 1.1 */
889 jsr166 1.6 public void testTimedInvokeAllNullTimeUnit() throws Throwable {
890 dl 1.1 ExecutorService e = new ForkJoinPool(1);
891 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
892     l.add(new StringTask());
893 dl 1.1 try {
894     e.invokeAll(l, MEDIUM_DELAY_MS, null);
895 jsr166 1.6 shouldThrow();
896 dl 1.1 } catch (NullPointerException success) {
897     } finally {
898     joinPool(e);
899     }
900     }
901    
902     /**
903     * timed invokeAll(empty collection) returns empty collection
904     */
905 jsr166 1.6 public void testTimedInvokeAll2() throws InterruptedException {
906 dl 1.1 ExecutorService e = new ForkJoinPool(1);
907     try {
908 jsr166 1.6 List<Future<String>> r
909     = e.invokeAll(new ArrayList<Callable<String>>(),
910 jsr166 1.27 MEDIUM_DELAY_MS, MILLISECONDS);
911 dl 1.1 assertTrue(r.isEmpty());
912     } finally {
913     joinPool(e);
914     }
915     }
916    
917     /**
918 jsr166 1.6 * timed invokeAll(c) throws NullPointerException if c has null elements
919 dl 1.1 */
920 jsr166 1.6 public void testTimedInvokeAll3() throws InterruptedException {
921 dl 1.1 ExecutorService e = new ForkJoinPool(1);
922 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
923     l.add(new StringTask());
924     l.add(null);
925 dl 1.1 try {
926 jsr166 1.27 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
927 jsr166 1.6 shouldThrow();
928 dl 1.1 } catch (NullPointerException success) {
929     } finally {
930     joinPool(e);
931     }
932     }
933    
934     /**
935     * get of returned element of invokeAll(c) throws exception on failed task
936     */
937 jsr166 1.6 public void testTimedInvokeAll4() throws Throwable {
938 dl 1.1 ExecutorService e = new ForkJoinPool(1);
939 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
940     l.add(new NPETask());
941     List<Future<String>> futures
942 jsr166 1.27 = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
943 jsr166 1.16 assertEquals(1, futures.size());
944 dl 1.1 try {
945 jsr166 1.16 futures.get(0).get();
946 jsr166 1.6 shouldThrow();
947 jsr166 1.2 } catch (ExecutionException success) {
948 jsr166 1.12 assertTrue(success.getCause() instanceof NullPointerException);
949 dl 1.1 } finally {
950     joinPool(e);
951     }
952     }
953    
954     /**
955     * timed invokeAll(c) returns results of all completed tasks in c
956     */
957 jsr166 1.6 public void testTimedInvokeAll5() throws Throwable {
958 dl 1.1 ExecutorService e = new ForkJoinPool(1);
959     try {
960 jsr166 1.16 List<Callable<String>> l = new ArrayList<Callable<String>>();
961 dl 1.1 l.add(new StringTask());
962     l.add(new StringTask());
963 jsr166 1.16 List<Future<String>> futures
964 jsr166 1.27 = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
965 jsr166 1.16 assertEquals(2, futures.size());
966     for (Future<String> future : futures)
967 jsr166 1.6 assertSame(TEST_STRING, future.get());
968 dl 1.1 } finally {
969     joinPool(e);
970     }
971     }
972    
973     }