ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.19
Committed: Sun Feb 28 13:35:22 2010 UTC (14 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.18: +1 -0 lines
Log Message:
testSetUncaughtExceptionHandler fix

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