ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.20
Committed: Wed Aug 11 19:50:02 2010 UTC (13 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.19: +97 -160 lines
Log Message:
Sync with API simplifications

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