ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPoolTest.java
Revision: 1.58
Committed: Sat Oct 3 19:18:37 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.57: +1 -1 lines
Log Message:
use <>

File Contents

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