ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.24
Committed: Mon Sep 13 15:34:42 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.23: +5 -4 lines
Log Message:
fix import statements

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