ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.38
Committed: Tue Mar 15 19:47:06 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

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