ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.15
Committed: Wed Dec 31 17:00:58 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +3 -3 lines
Log Message:
lexicographic import order

File Contents

# Content
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/publicdomain/zero/1.0/
5 */
6 import java.util.HashSet;
7 import java.util.concurrent.CancellationException;
8 import java.util.concurrent.CountedCompleter;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.ForkJoinPool;
11 import java.util.concurrent.ForkJoinTask;
12 import java.util.concurrent.TimeoutException;
13 import java.util.concurrent.atomic.AtomicInteger;
14 import java.util.concurrent.atomic.AtomicReference;
15 import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 import static java.util.concurrent.TimeUnit.SECONDS;
17 import junit.framework.*;
18
19 public class CountedCompleterTest extends JSR166TestCase {
20
21 public static void main(String[] args) {
22 junit.textui.TestRunner.run(suite());
23 }
24
25 public static Test suite() {
26 return new TestSuite(CountedCompleterTest.class);
27 }
28
29 // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
30 static final int mainPoolSize =
31 Math.max(2, Runtime.getRuntime().availableProcessors());
32
33 private static ForkJoinPool mainPool() {
34 return new ForkJoinPool(mainPoolSize);
35 }
36
37 private static ForkJoinPool singletonPool() {
38 return new ForkJoinPool(1);
39 }
40
41 private static ForkJoinPool asyncSingletonPool() {
42 return new ForkJoinPool(1,
43 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
44 null, true);
45 }
46
47 private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) {
48 try {
49 assertFalse(a.isDone());
50 assertFalse(a.isCompletedNormally());
51 assertFalse(a.isCompletedAbnormally());
52 assertFalse(a.isCancelled());
53 assertNull(a.getException());
54 assertNull(a.getRawResult());
55
56 assertNull(pool.invoke(a));
57
58 assertTrue(a.isDone());
59 assertTrue(a.isCompletedNormally());
60 assertFalse(a.isCompletedAbnormally());
61 assertFalse(a.isCancelled());
62 assertNull(a.getException());
63 assertNull(a.getRawResult());
64 } finally {
65 joinPool(pool);
66 }
67 }
68
69 void checkNotDone(CountedCompleter a) {
70 assertFalse(a.isDone());
71 assertFalse(a.isCompletedNormally());
72 assertFalse(a.isCompletedAbnormally());
73 assertFalse(a.isCancelled());
74 assertNull(a.getException());
75 assertNull(a.getRawResult());
76
77 try {
78 a.get(0L, SECONDS);
79 shouldThrow();
80 } catch (TimeoutException success) {
81 } catch (Throwable fail) { threadUnexpectedException(fail); }
82 }
83
84 void checkCompletedNormally(CountedCompleter<?> a) {
85 assertTrue(a.isDone());
86 assertFalse(a.isCancelled());
87 assertTrue(a.isCompletedNormally());
88 assertFalse(a.isCompletedAbnormally());
89 assertNull(a.getException());
90 assertNull(a.getRawResult());
91
92 {
93 Thread.currentThread().interrupt();
94 long t0 = System.nanoTime();
95 assertNull(a.join());
96 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
97 Thread.interrupted();
98 }
99
100 {
101 Thread.currentThread().interrupt();
102 long t0 = System.nanoTime();
103 a.quietlyJoin(); // should be no-op
104 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
105 Thread.interrupted();
106 }
107
108 assertFalse(a.cancel(false));
109 assertFalse(a.cancel(true));
110 try {
111 assertNull(a.get());
112 } catch (Throwable fail) { threadUnexpectedException(fail); }
113 try {
114 assertNull(a.get(5L, SECONDS));
115 } catch (Throwable fail) { threadUnexpectedException(fail); }
116 }
117
118 void checkCancelled(CountedCompleter a) {
119 assertTrue(a.isDone());
120 assertTrue(a.isCancelled());
121 assertFalse(a.isCompletedNormally());
122 assertTrue(a.isCompletedAbnormally());
123 assertTrue(a.getException() instanceof CancellationException);
124 assertNull(a.getRawResult());
125 assertTrue(a.cancel(false));
126 assertTrue(a.cancel(true));
127
128 try {
129 Thread.currentThread().interrupt();
130 a.join();
131 shouldThrow();
132 } catch (CancellationException success) {
133 } catch (Throwable fail) { threadUnexpectedException(fail); }
134 Thread.interrupted();
135
136 {
137 long t0 = System.nanoTime();
138 a.quietlyJoin(); // should be no-op
139 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
140 }
141
142 try {
143 a.get();
144 shouldThrow();
145 } catch (CancellationException success) {
146 } catch (Throwable fail) { threadUnexpectedException(fail); }
147
148 try {
149 a.get(5L, SECONDS);
150 shouldThrow();
151 } catch (CancellationException success) {
152 } catch (Throwable fail) { threadUnexpectedException(fail); }
153 }
154
155 void checkCompletedAbnormally(CountedCompleter a, Throwable t) {
156 assertTrue(a.isDone());
157 assertFalse(a.isCancelled());
158 assertFalse(a.isCompletedNormally());
159 assertTrue(a.isCompletedAbnormally());
160 assertSame(t.getClass(), a.getException().getClass());
161 assertNull(a.getRawResult());
162 assertFalse(a.cancel(false));
163 assertFalse(a.cancel(true));
164
165 try {
166 Thread.currentThread().interrupt();
167 a.join();
168 shouldThrow();
169 } catch (Throwable expected) {
170 assertSame(t.getClass(), expected.getClass());
171 }
172 Thread.interrupted();
173
174 {
175 long t0 = System.nanoTime();
176 a.quietlyJoin(); // should be no-op
177 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
178 }
179
180 try {
181 a.get();
182 shouldThrow();
183 } catch (ExecutionException success) {
184 assertSame(t.getClass(), success.getCause().getClass());
185 } catch (Throwable fail) { threadUnexpectedException(fail); }
186
187 try {
188 a.get(5L, SECONDS);
189 shouldThrow();
190 } catch (ExecutionException success) {
191 assertSame(t.getClass(), success.getCause().getClass());
192 } catch (Throwable fail) { threadUnexpectedException(fail); }
193
194 try {
195 a.invoke();
196 shouldThrow();
197 } catch (Throwable ex) {
198 assertSame(t, ex);
199 }
200 }
201
202 public static final class FJException extends RuntimeException {
203 FJException() { super(); }
204 }
205
206 abstract class CheckedCC extends CountedCompleter<Object> {
207 final AtomicInteger computeN = new AtomicInteger(0);
208 final AtomicInteger onCompletionN = new AtomicInteger(0);
209 final AtomicInteger onExceptionalCompletionN = new AtomicInteger(0);
210 final AtomicInteger setRawResultN = new AtomicInteger(0);
211 final AtomicReference<Object> rawResult = new AtomicReference<Object>(null);
212 int computeN() { return computeN.get(); }
213 int onCompletionN() { return onCompletionN.get(); }
214 int onExceptionalCompletionN() { return onExceptionalCompletionN.get(); }
215 int setRawResultN() { return setRawResultN.get(); }
216
217 CheckedCC() { super(); }
218 CheckedCC(CountedCompleter p) { super(p); }
219 CheckedCC(CountedCompleter p, int n) { super(p, n); }
220 abstract void realCompute();
221 public final void compute() {
222 computeN.incrementAndGet();
223 realCompute();
224 }
225 public void onCompletion(CountedCompleter caller) {
226 onCompletionN.incrementAndGet();
227 super.onCompletion(caller);
228 }
229 public boolean onExceptionalCompletion(Throwable ex,
230 CountedCompleter caller) {
231 onExceptionalCompletionN.incrementAndGet();
232 assertNotNull(ex);
233 assertTrue(isCompletedAbnormally());
234 assertTrue(super.onExceptionalCompletion(ex, caller));
235 return true;
236 }
237 protected void setRawResult(Object t) {
238 setRawResultN.incrementAndGet();
239 rawResult.set(t);
240 super.setRawResult(t);
241 }
242 void checkIncomplete() {
243 assertEquals(0, computeN());
244 assertEquals(0, onCompletionN());
245 assertEquals(0, onExceptionalCompletionN());
246 assertEquals(0, setRawResultN());
247 checkNotDone(this);
248 }
249 void checkCompletes(Object rawResult) {
250 checkIncomplete();
251 int pendingCount = getPendingCount();
252 complete(rawResult);
253 assertEquals(pendingCount, getPendingCount());
254 assertEquals(0, computeN());
255 assertEquals(1, onCompletionN());
256 assertEquals(0, onExceptionalCompletionN());
257 assertEquals(1, setRawResultN());
258 assertSame(rawResult, this.rawResult.get());
259 checkCompletedNormally(this);
260 }
261 void checkCompletesExceptionally(Throwable ex) {
262 checkIncomplete();
263 completeExceptionally(ex);
264 checkCompletedExceptionally(ex);
265 }
266 void checkCompletedExceptionally(Throwable ex) {
267 assertEquals(0, computeN());
268 assertEquals(0, onCompletionN());
269 assertEquals(1, onExceptionalCompletionN());
270 assertEquals(0, setRawResultN());
271 assertNull(this.rawResult.get());
272 checkCompletedAbnormally(this, ex);
273 }
274 }
275
276 final class NoopCC extends CheckedCC {
277 NoopCC() { super(); }
278 NoopCC(CountedCompleter p) { super(p); }
279 protected void realCompute() {}
280 }
281
282 /**
283 * A newly constructed CountedCompleter is not completed;
284 * complete() causes completion. pendingCount is ignored.
285 */
286 public void testComplete() {
287 for (Object x : new Object[] { Boolean.TRUE, null }) {
288 for (int pendingCount : new int[] { 0, 42 }) {
289 testComplete(new NoopCC(), x, pendingCount);
290 testComplete(new NoopCC(new NoopCC()), x, pendingCount);
291 }
292 }
293 }
294 void testComplete(NoopCC cc, Object x, int pendingCount) {
295 cc.setPendingCount(pendingCount);
296 cc.checkCompletes(x);
297 }
298
299 /**
300 * completeExceptionally completes exceptionally
301 */
302 public void testCompleteExceptionally() {
303 new NoopCC()
304 .checkCompletesExceptionally(new FJException());
305 new NoopCC(new NoopCC())
306 .checkCompletesExceptionally(new FJException());
307 }
308
309 /**
310 * completeExceptionally(null) throws NullPointerException
311 */
312 public void testCompleteExceptionally_null() {
313 try {
314 new NoopCC()
315 .checkCompletesExceptionally(null);
316 shouldThrow();
317 } catch (NullPointerException success) {}
318 }
319
320 /**
321 * setPendingCount sets the reported pending count
322 */
323 public void testSetPendingCount() {
324 NoopCC a = new NoopCC();
325 assertEquals(0, a.getPendingCount());
326 a.setPendingCount(1);
327 assertEquals(1, a.getPendingCount());
328 a.setPendingCount(27);
329 assertEquals(27, a.getPendingCount());
330 }
331
332 /**
333 * addToPendingCount adds to the reported pending count
334 */
335 public void testAddToPendingCount() {
336 NoopCC a = new NoopCC();
337 assertEquals(0, a.getPendingCount());
338 a.addToPendingCount(1);
339 assertEquals(1, a.getPendingCount());
340 a.addToPendingCount(27);
341 assertEquals(28, a.getPendingCount());
342 }
343
344 /**
345 * decrementPendingCountUnlessZero decrements reported pending
346 * count unless zero
347 */
348 public void testDecrementPendingCount() {
349 NoopCC a = new NoopCC();
350 assertEquals(0, a.getPendingCount());
351 a.addToPendingCount(1);
352 assertEquals(1, a.getPendingCount());
353 a.decrementPendingCountUnlessZero();
354 assertEquals(0, a.getPendingCount());
355 a.decrementPendingCountUnlessZero();
356 assertEquals(0, a.getPendingCount());
357 }
358
359 /**
360 * compareAndSetPendingCount compares and sets the reported
361 * pending count
362 */
363 public void testCompareAndSetPendingCount() {
364 NoopCC a = new NoopCC();
365 assertEquals(0, a.getPendingCount());
366 assertTrue(a.compareAndSetPendingCount(0, 1));
367 assertEquals(1, a.getPendingCount());
368 assertTrue(a.compareAndSetPendingCount(1, 2));
369 assertEquals(2, a.getPendingCount());
370 assertFalse(a.compareAndSetPendingCount(1, 3));
371 assertEquals(2, a.getPendingCount());
372 }
373
374 /**
375 * getCompleter returns parent or null if at root
376 */
377 public void testGetCompleter() {
378 NoopCC a = new NoopCC();
379 assertNull(a.getCompleter());
380 CountedCompleter b = new NoopCC(a);
381 assertSame(a, b.getCompleter());
382 CountedCompleter c = new NoopCC(b);
383 assertSame(b, c.getCompleter());
384 }
385
386 /**
387 * getRoot returns self if no parent, else parent's root
388 */
389 public void testGetRoot() {
390 NoopCC a = new NoopCC();
391 NoopCC b = new NoopCC(a);
392 NoopCC c = new NoopCC(b);
393 assertSame(a, a.getRoot());
394 assertSame(a, b.getRoot());
395 assertSame(a, c.getRoot());
396 }
397
398 /**
399 * tryComplete decrements pending count unless zero, in which case
400 * causes completion
401 */
402 public void testTryComplete() {
403 NoopCC a = new NoopCC();
404 assertEquals(0, a.getPendingCount());
405 int n = 3;
406 a.setPendingCount(n);
407 for (; n > 0; n--) {
408 assertEquals(n, a.getPendingCount());
409 a.tryComplete();
410 a.checkIncomplete();
411 assertEquals(n - 1, a.getPendingCount());
412 }
413 a.tryComplete();
414 assertEquals(0, a.computeN());
415 assertEquals(1, a.onCompletionN());
416 assertEquals(0, a.onExceptionalCompletionN());
417 assertEquals(0, a.setRawResultN());
418 checkCompletedNormally(a);
419 }
420
421 /**
422 * propagateCompletion decrements pending count unless zero, in
423 * which case causes completion, without invoking onCompletion
424 */
425 public void testPropagateCompletion() {
426 NoopCC a = new NoopCC();
427 assertEquals(0, a.getPendingCount());
428 int n = 3;
429 a.setPendingCount(n);
430 for (; n > 0; n--) {
431 assertEquals(n, a.getPendingCount());
432 a.propagateCompletion();
433 a.checkIncomplete();
434 assertEquals(n - 1, a.getPendingCount());
435 }
436 a.propagateCompletion();
437 assertEquals(0, a.computeN());
438 assertEquals(0, a.onCompletionN());
439 assertEquals(0, a.onExceptionalCompletionN());
440 assertEquals(0, a.setRawResultN());
441 checkCompletedNormally(a);
442 }
443
444 /**
445 * firstComplete returns this if pending count is zero else null
446 */
447 public void testFirstComplete() {
448 NoopCC a = new NoopCC();
449 a.setPendingCount(1);
450 assertNull(a.firstComplete());
451 a.checkIncomplete();
452 assertSame(a, a.firstComplete());
453 a.checkIncomplete();
454 }
455
456 /**
457 * firstComplete.nextComplete returns parent if pending count is
458 * zero else null
459 */
460 public void testNextComplete() {
461 NoopCC a = new NoopCC();
462 NoopCC b = new NoopCC(a);
463 a.setPendingCount(1);
464 b.setPendingCount(1);
465 assertNull(b.firstComplete());
466 assertSame(b, b.firstComplete());
467 assertNull(b.nextComplete());
468 a.checkIncomplete();
469 b.checkIncomplete();
470 assertSame(a, b.nextComplete());
471 assertSame(a, b.nextComplete());
472 a.checkIncomplete();
473 b.checkIncomplete();
474 assertNull(a.nextComplete());
475 b.checkIncomplete();
476 checkCompletedNormally(a);
477 }
478
479 /**
480 * quietlyCompleteRoot completes root task
481 */
482 public void testQuietlyCompleteRoot() {
483 NoopCC a = new NoopCC();
484 NoopCC b = new NoopCC(a);
485 NoopCC c = new NoopCC(b);
486 a.setPendingCount(1);
487 b.setPendingCount(1);
488 c.setPendingCount(1);
489 c.quietlyCompleteRoot();
490 assertTrue(a.isDone());
491 assertFalse(b.isDone());
492 assertFalse(c.isDone());
493 }
494
495 // Invocation tests use some interdependent task classes
496 // to better test propagation etc
497
498 /**
499 * Version of Fibonacci with different classes for left vs right forks
500 */
501 abstract class CCF extends CheckedCC {
502 int number;
503 int rnumber;
504
505 public CCF(CountedCompleter parent, int n) {
506 super(parent, 1);
507 this.number = n;
508 }
509
510 protected final void realCompute() {
511 CCF f = this;
512 int n = number;
513 while (n >= 2) {
514 new RCCF(f, n - 2).fork();
515 f = new LCCF(f, --n);
516 }
517 f.complete(null);
518 }
519 }
520
521 final class LCCF extends CCF {
522 public LCCF(int n) { this(null, n); }
523 public LCCF(CountedCompleter parent, int n) {
524 super(parent, n);
525 }
526 public final void onCompletion(CountedCompleter caller) {
527 super.onCompletion(caller);
528 CCF p = (CCF)getCompleter();
529 int n = number + rnumber;
530 if (p != null)
531 p.number = n;
532 else
533 number = n;
534 }
535 }
536 final class RCCF extends CCF {
537 public RCCF(CountedCompleter parent, int n) {
538 super(parent, n);
539 }
540 public final void onCompletion(CountedCompleter caller) {
541 super.onCompletion(caller);
542 CCF p = (CCF)getCompleter();
543 int n = number + rnumber;
544 if (p != null)
545 p.rnumber = n;
546 else
547 number = n;
548 }
549 }
550
551 // Version of CCF with forced failure in left completions
552 abstract class FailingCCF extends CheckedCC {
553 int number;
554 int rnumber;
555
556 public FailingCCF(CountedCompleter parent, int n) {
557 super(parent, 1);
558 this.number = n;
559 }
560
561 protected final void realCompute() {
562 FailingCCF f = this;
563 int n = number;
564 while (n >= 2) {
565 new RFCCF(f, n - 2).fork();
566 f = new LFCCF(f, --n);
567 }
568 f.complete(null);
569 }
570 }
571
572 final class LFCCF extends FailingCCF {
573 public LFCCF(int n) { this(null, n); }
574 public LFCCF(CountedCompleter parent, int n) {
575 super(parent, n);
576 }
577 public final void onCompletion(CountedCompleter caller) {
578 super.onCompletion(caller);
579 FailingCCF p = (FailingCCF)getCompleter();
580 int n = number + rnumber;
581 if (p != null)
582 p.number = n;
583 else
584 number = n;
585 }
586 }
587 final class RFCCF extends FailingCCF {
588 public RFCCF(CountedCompleter parent, int n) {
589 super(parent, n);
590 }
591 public final void onCompletion(CountedCompleter caller) {
592 super.onCompletion(caller);
593 completeExceptionally(new FJException());
594 }
595 }
596
597 /**
598 * invoke returns when task completes normally.
599 * isCompletedAbnormally and isCancelled return false for normally
600 * completed tasks; getRawResult returns null.
601 */
602 public void testInvoke() {
603 ForkJoinTask a = new CheckedRecursiveAction() {
604 protected void realCompute() {
605 CCF f = new LCCF(8);
606 assertNull(f.invoke());
607 assertEquals(21, f.number);
608 checkCompletedNormally(f);
609 }};
610 testInvokeOnPool(mainPool(), a);
611 }
612
613 /**
614 * quietlyInvoke task returns when task completes normally.
615 * isCompletedAbnormally and isCancelled return false for normally
616 * completed tasks
617 */
618 public void testQuietlyInvoke() {
619 ForkJoinTask a = new CheckedRecursiveAction() {
620 protected void realCompute() {
621 CCF f = new LCCF(8);
622 f.quietlyInvoke();
623 assertEquals(21, f.number);
624 checkCompletedNormally(f);
625 }};
626 testInvokeOnPool(mainPool(), a);
627 }
628
629 /**
630 * join of a forked task returns when task completes
631 */
632 public void testForkJoin() {
633 ForkJoinTask a = new CheckedRecursiveAction() {
634 protected void realCompute() {
635 CCF f = new LCCF(8);
636 assertSame(f, f.fork());
637 assertNull(f.join());
638 assertEquals(21, f.number);
639 checkCompletedNormally(f);
640 }};
641 testInvokeOnPool(mainPool(), a);
642 }
643
644 /**
645 * get of a forked task returns when task completes
646 */
647 public void testForkGet() {
648 ForkJoinTask a = new CheckedRecursiveAction() {
649 protected void realCompute() throws Exception {
650 CCF f = new LCCF(8);
651 assertSame(f, f.fork());
652 assertNull(f.get());
653 assertEquals(21, f.number);
654 checkCompletedNormally(f);
655 }};
656 testInvokeOnPool(mainPool(), a);
657 }
658
659 /**
660 * timed get of a forked task returns when task completes
661 */
662 public void testForkTimedGet() {
663 ForkJoinTask a = new CheckedRecursiveAction() {
664 protected void realCompute() throws Exception {
665 CCF f = new LCCF(8);
666 assertSame(f, f.fork());
667 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
668 assertEquals(21, f.number);
669 checkCompletedNormally(f);
670 }};
671 testInvokeOnPool(mainPool(), a);
672 }
673
674 /**
675 * timed get with null time unit throws NPE
676 */
677 public void testForkTimedGetNPE() {
678 ForkJoinTask a = new CheckedRecursiveAction() {
679 protected void realCompute() throws Exception {
680 CCF f = new LCCF(8);
681 assertSame(f, f.fork());
682 try {
683 f.get(5L, null);
684 shouldThrow();
685 } catch (NullPointerException success) {}
686 }};
687 testInvokeOnPool(mainPool(), a);
688 }
689
690 /**
691 * quietlyJoin of a forked task returns when task completes
692 */
693 public void testForkQuietlyJoin() {
694 ForkJoinTask a = new CheckedRecursiveAction() {
695 protected void realCompute() {
696 CCF f = new LCCF(8);
697 assertSame(f, f.fork());
698 f.quietlyJoin();
699 assertEquals(21, f.number);
700 checkCompletedNormally(f);
701 }};
702 testInvokeOnPool(mainPool(), a);
703 }
704
705 /**
706 * helpQuiesce returns when tasks are complete.
707 * getQueuedTaskCount returns 0 when quiescent
708 */
709 public void testForkHelpQuiesce() {
710 ForkJoinTask a = new CheckedRecursiveAction() {
711 protected void realCompute() {
712 CCF f = new LCCF(8);
713 assertSame(f, f.fork());
714 helpQuiesce();
715 assertEquals(21, f.number);
716 assertEquals(0, getQueuedTaskCount());
717 checkCompletedNormally(f);
718 }};
719 testInvokeOnPool(mainPool(), a);
720 }
721
722 /**
723 * invoke task throws exception when task completes abnormally
724 */
725 public void testAbnormalInvoke() {
726 ForkJoinTask a = new CheckedRecursiveAction() {
727 protected void realCompute() {
728 FailingCCF f = new LFCCF(8);
729 try {
730 f.invoke();
731 shouldThrow();
732 } catch (FJException success) {
733 checkCompletedAbnormally(f, success);
734 }
735 }};
736 testInvokeOnPool(mainPool(), a);
737 }
738
739 /**
740 * quietlyInvoke task returns when task completes abnormally
741 */
742 public void testAbnormalQuietlyInvoke() {
743 ForkJoinTask a = new CheckedRecursiveAction() {
744 protected void realCompute() {
745 FailingCCF f = new LFCCF(8);
746 f.quietlyInvoke();
747 assertTrue(f.getException() instanceof FJException);
748 checkCompletedAbnormally(f, f.getException());
749 }};
750 testInvokeOnPool(mainPool(), a);
751 }
752
753 /**
754 * join of a forked task throws exception when task completes abnormally
755 */
756 public void testAbnormalForkJoin() {
757 ForkJoinTask a = new CheckedRecursiveAction() {
758 protected void realCompute() {
759 FailingCCF f = new LFCCF(8);
760 assertSame(f, f.fork());
761 try {
762 f.join();
763 shouldThrow();
764 } catch (FJException success) {
765 checkCompletedAbnormally(f, success);
766 }
767 }};
768 testInvokeOnPool(mainPool(), a);
769 }
770
771 /**
772 * get of a forked task throws exception when task completes abnormally
773 */
774 public void testAbnormalForkGet() {
775 ForkJoinTask a = new CheckedRecursiveAction() {
776 protected void realCompute() throws Exception {
777 FailingCCF f = new LFCCF(8);
778 assertSame(f, f.fork());
779 try {
780 f.get();
781 shouldThrow();
782 } catch (ExecutionException success) {
783 Throwable cause = success.getCause();
784 assertTrue(cause instanceof FJException);
785 checkCompletedAbnormally(f, cause);
786 }
787 }};
788 testInvokeOnPool(mainPool(), a);
789 }
790
791 /**
792 * timed get of a forked task throws exception when task completes abnormally
793 */
794 public void testAbnormalForkTimedGet() {
795 ForkJoinTask a = new CheckedRecursiveAction() {
796 protected void realCompute() throws Exception {
797 FailingCCF f = new LFCCF(8);
798 assertSame(f, f.fork());
799 try {
800 f.get(LONG_DELAY_MS, MILLISECONDS);
801 shouldThrow();
802 } catch (ExecutionException success) {
803 Throwable cause = success.getCause();
804 assertTrue(cause instanceof FJException);
805 checkCompletedAbnormally(f, cause);
806 }
807 }};
808 testInvokeOnPool(mainPool(), a);
809 }
810
811 /**
812 * quietlyJoin of a forked task returns when task completes abnormally
813 */
814 public void testAbnormalForkQuietlyJoin() {
815 ForkJoinTask a = new CheckedRecursiveAction() {
816 protected void realCompute() {
817 FailingCCF f = new LFCCF(8);
818 assertSame(f, f.fork());
819 f.quietlyJoin();
820 assertTrue(f.getException() instanceof FJException);
821 checkCompletedAbnormally(f, f.getException());
822 }};
823 testInvokeOnPool(mainPool(), a);
824 }
825
826 /**
827 * invoke task throws exception when task cancelled
828 */
829 public void testCancelledInvoke() {
830 ForkJoinTask a = new CheckedRecursiveAction() {
831 protected void realCompute() {
832 CCF f = new LCCF(8);
833 assertTrue(f.cancel(true));
834 try {
835 f.invoke();
836 shouldThrow();
837 } catch (CancellationException success) {
838 checkCancelled(f);
839 }
840 }};
841 testInvokeOnPool(mainPool(), a);
842 }
843
844 /**
845 * join of a forked task throws exception when task cancelled
846 */
847 public void testCancelledForkJoin() {
848 ForkJoinTask a = new CheckedRecursiveAction() {
849 protected void realCompute() {
850 CCF f = new LCCF(8);
851 assertTrue(f.cancel(true));
852 assertSame(f, f.fork());
853 try {
854 f.join();
855 shouldThrow();
856 } catch (CancellationException success) {
857 checkCancelled(f);
858 }
859 }};
860 testInvokeOnPool(mainPool(), a);
861 }
862
863 /**
864 * get of a forked task throws exception when task cancelled
865 */
866 public void testCancelledForkGet() {
867 ForkJoinTask a = new CheckedRecursiveAction() {
868 protected void realCompute() throws Exception {
869 CCF f = new LCCF(8);
870 assertTrue(f.cancel(true));
871 assertSame(f, f.fork());
872 try {
873 f.get();
874 shouldThrow();
875 } catch (CancellationException success) {
876 checkCancelled(f);
877 }
878 }};
879 testInvokeOnPool(mainPool(), a);
880 }
881
882 /**
883 * timed get of a forked task throws exception when task cancelled
884 */
885 public void testCancelledForkTimedGet() throws Exception {
886 ForkJoinTask a = new CheckedRecursiveAction() {
887 protected void realCompute() throws Exception {
888 CCF f = new LCCF(8);
889 assertTrue(f.cancel(true));
890 assertSame(f, f.fork());
891 try {
892 f.get(LONG_DELAY_MS, MILLISECONDS);
893 shouldThrow();
894 } catch (CancellationException success) {
895 checkCancelled(f);
896 }
897 }};
898 testInvokeOnPool(mainPool(), a);
899 }
900
901 /**
902 * quietlyJoin of a forked task returns when task cancelled
903 */
904 public void testCancelledForkQuietlyJoin() {
905 ForkJoinTask a = new CheckedRecursiveAction() {
906 protected void realCompute() {
907 CCF f = new LCCF(8);
908 assertTrue(f.cancel(true));
909 assertSame(f, f.fork());
910 f.quietlyJoin();
911 checkCancelled(f);
912 }};
913 testInvokeOnPool(mainPool(), a);
914 }
915
916 /**
917 * getPool of executing task returns its pool
918 */
919 public void testGetPool() {
920 final ForkJoinPool mainPool = mainPool();
921 ForkJoinTask a = new CheckedRecursiveAction() {
922 protected void realCompute() {
923 assertSame(mainPool, getPool());
924 }};
925 testInvokeOnPool(mainPool, a);
926 }
927
928 /**
929 * getPool of non-FJ task returns null
930 */
931 public void testGetPool2() {
932 ForkJoinTask a = new CheckedRecursiveAction() {
933 protected void realCompute() {
934 assertNull(getPool());
935 }};
936 assertNull(a.invoke());
937 }
938
939 /**
940 * inForkJoinPool of executing task returns true
941 */
942 public void testInForkJoinPool() {
943 ForkJoinTask a = new CheckedRecursiveAction() {
944 protected void realCompute() {
945 assertTrue(inForkJoinPool());
946 }};
947 testInvokeOnPool(mainPool(), a);
948 }
949
950 /**
951 * inForkJoinPool of non-FJ task returns false
952 */
953 public void testInForkJoinPool2() {
954 ForkJoinTask a = new CheckedRecursiveAction() {
955 protected void realCompute() {
956 assertFalse(inForkJoinPool());
957 }};
958 assertNull(a.invoke());
959 }
960
961 /**
962 * setRawResult(null) succeeds
963 */
964 public void testSetRawResult() {
965 ForkJoinTask a = new CheckedRecursiveAction() {
966 protected void realCompute() {
967 setRawResult(null);
968 assertNull(getRawResult());
969 }};
970 assertNull(a.invoke());
971 }
972
973 /**
974 * invoke task throws exception after invoking completeExceptionally
975 */
976 public void testCompleteExceptionally2() {
977 ForkJoinTask a = new CheckedRecursiveAction() {
978 protected void realCompute() {
979 CCF n = new LCCF(8);
980 CCF f = new LCCF(n, 8);
981 FJException ex = new FJException();
982 f.completeExceptionally(ex);
983 f.checkCompletedExceptionally(ex);
984 n.checkCompletedExceptionally(ex);
985 }};
986 testInvokeOnPool(mainPool(), a);
987 }
988
989 /**
990 * invokeAll(t1, t2) invokes all task arguments
991 */
992 public void testInvokeAll2() {
993 ForkJoinTask a = new CheckedRecursiveAction() {
994 protected void realCompute() {
995 CCF f = new LCCF(8);
996 CCF g = new LCCF(9);
997 invokeAll(f, g);
998 assertEquals(21, f.number);
999 assertEquals(34, g.number);
1000 checkCompletedNormally(f);
1001 checkCompletedNormally(g);
1002 }};
1003 testInvokeOnPool(mainPool(), a);
1004 }
1005
1006 /**
1007 * invokeAll(tasks) with 1 argument invokes task
1008 */
1009 public void testInvokeAll1() {
1010 ForkJoinTask a = new CheckedRecursiveAction() {
1011 protected void realCompute() {
1012 CCF f = new LCCF(8);
1013 invokeAll(f);
1014 checkCompletedNormally(f);
1015 assertEquals(21, f.number);
1016 }};
1017 testInvokeOnPool(mainPool(), a);
1018 }
1019
1020 /**
1021 * invokeAll(tasks) with > 2 argument invokes tasks
1022 */
1023 public void testInvokeAll3() {
1024 ForkJoinTask a = new CheckedRecursiveAction() {
1025 protected void realCompute() {
1026 CCF f = new LCCF(8);
1027 CCF g = new LCCF(9);
1028 CCF h = new LCCF(7);
1029 invokeAll(f, g, h);
1030 assertEquals(21, f.number);
1031 assertEquals(34, g.number);
1032 assertEquals(13, h.number);
1033 checkCompletedNormally(f);
1034 checkCompletedNormally(g);
1035 checkCompletedNormally(h);
1036 }};
1037 testInvokeOnPool(mainPool(), a);
1038 }
1039
1040 /**
1041 * invokeAll(collection) invokes all tasks in the collection
1042 */
1043 public void testInvokeAllCollection() {
1044 ForkJoinTask a = new CheckedRecursiveAction() {
1045 protected void realCompute() {
1046 CCF f = new LCCF(8);
1047 CCF g = new LCCF(9);
1048 CCF h = new LCCF(7);
1049 HashSet set = new HashSet();
1050 set.add(f);
1051 set.add(g);
1052 set.add(h);
1053 invokeAll(set);
1054 assertEquals(21, f.number);
1055 assertEquals(34, g.number);
1056 assertEquals(13, h.number);
1057 checkCompletedNormally(f);
1058 checkCompletedNormally(g);
1059 checkCompletedNormally(h);
1060 }};
1061 testInvokeOnPool(mainPool(), a);
1062 }
1063
1064 /**
1065 * invokeAll(tasks) with any null task throws NPE
1066 */
1067 public void testInvokeAllNPE() {
1068 ForkJoinTask a = new CheckedRecursiveAction() {
1069 protected void realCompute() {
1070 CCF f = new LCCF(8);
1071 CCF g = new LCCF(9);
1072 CCF h = null;
1073 try {
1074 invokeAll(f, g, h);
1075 shouldThrow();
1076 } catch (NullPointerException success) {}
1077 }};
1078 testInvokeOnPool(mainPool(), a);
1079 }
1080
1081 /**
1082 * invokeAll(t1, t2) throw exception if any task does
1083 */
1084 public void testAbnormalInvokeAll2() {
1085 ForkJoinTask a = new CheckedRecursiveAction() {
1086 protected void realCompute() {
1087 CCF f = new LCCF(8);
1088 FailingCCF g = new LFCCF(9);
1089 try {
1090 invokeAll(f, g);
1091 shouldThrow();
1092 } catch (FJException success) {
1093 checkCompletedAbnormally(g, success);
1094 }
1095 }};
1096 testInvokeOnPool(mainPool(), a);
1097 }
1098
1099 /**
1100 * invokeAll(tasks) with 1 argument throws exception if task does
1101 */
1102 public void testAbnormalInvokeAll1() {
1103 ForkJoinTask a = new CheckedRecursiveAction() {
1104 protected void realCompute() {
1105 FailingCCF g = new LFCCF(9);
1106 try {
1107 invokeAll(g);
1108 shouldThrow();
1109 } catch (FJException success) {
1110 checkCompletedAbnormally(g, success);
1111 }
1112 }};
1113 testInvokeOnPool(mainPool(), a);
1114 }
1115
1116 /**
1117 * invokeAll(tasks) with > 2 argument throws exception if any task does
1118 */
1119 public void testAbnormalInvokeAll3() {
1120 ForkJoinTask a = new CheckedRecursiveAction() {
1121 protected void realCompute() {
1122 CCF f = new LCCF(8);
1123 FailingCCF g = new LFCCF(9);
1124 CCF h = new LCCF(7);
1125 try {
1126 invokeAll(f, g, h);
1127 shouldThrow();
1128 } catch (FJException success) {
1129 checkCompletedAbnormally(g, success);
1130 }
1131 }};
1132 testInvokeOnPool(mainPool(), a);
1133 }
1134
1135 /**
1136 * invokeAll(collection) throws exception if any task does
1137 */
1138 public void testAbnormalInvokeAllCollection() {
1139 ForkJoinTask a = new CheckedRecursiveAction() {
1140 protected void realCompute() {
1141 FailingCCF f = new LFCCF(8);
1142 CCF g = new LCCF(9);
1143 CCF h = new LCCF(7);
1144 HashSet set = new HashSet();
1145 set.add(f);
1146 set.add(g);
1147 set.add(h);
1148 try {
1149 invokeAll(set);
1150 shouldThrow();
1151 } catch (FJException success) {
1152 checkCompletedAbnormally(f, success);
1153 }
1154 }};
1155 testInvokeOnPool(mainPool(), a);
1156 }
1157
1158 /**
1159 * tryUnfork returns true for most recent unexecuted task,
1160 * and suppresses execution
1161 */
1162 public void testTryUnfork() {
1163 ForkJoinTask a = new CheckedRecursiveAction() {
1164 protected void realCompute() {
1165 CCF g = new LCCF(9);
1166 assertSame(g, g.fork());
1167 CCF f = new LCCF(8);
1168 assertSame(f, f.fork());
1169 assertTrue(f.tryUnfork());
1170 helpQuiesce();
1171 checkNotDone(f);
1172 checkCompletedNormally(g);
1173 }};
1174 testInvokeOnPool(singletonPool(), a);
1175 }
1176
1177 /**
1178 * getSurplusQueuedTaskCount returns > 0 when
1179 * there are more tasks than threads
1180 */
1181 public void testGetSurplusQueuedTaskCount() {
1182 ForkJoinTask a = new CheckedRecursiveAction() {
1183 protected void realCompute() {
1184 CCF h = new LCCF(7);
1185 assertSame(h, h.fork());
1186 CCF g = new LCCF(9);
1187 assertSame(g, g.fork());
1188 CCF f = new LCCF(8);
1189 assertSame(f, f.fork());
1190 assertTrue(getSurplusQueuedTaskCount() > 0);
1191 helpQuiesce();
1192 assertEquals(0, getSurplusQueuedTaskCount());
1193 checkCompletedNormally(f);
1194 checkCompletedNormally(g);
1195 checkCompletedNormally(h);
1196 }};
1197 testInvokeOnPool(singletonPool(), a);
1198 }
1199
1200 /**
1201 * peekNextLocalTask returns most recent unexecuted task.
1202 */
1203 public void testPeekNextLocalTask() {
1204 ForkJoinTask a = new CheckedRecursiveAction() {
1205 protected void realCompute() {
1206 CCF g = new LCCF(9);
1207 assertSame(g, g.fork());
1208 CCF f = new LCCF(8);
1209 assertSame(f, f.fork());
1210 assertSame(f, peekNextLocalTask());
1211 assertNull(f.join());
1212 checkCompletedNormally(f);
1213 helpQuiesce();
1214 checkCompletedNormally(g);
1215 }};
1216 testInvokeOnPool(singletonPool(), a);
1217 }
1218
1219 /**
1220 * pollNextLocalTask returns most recent unexecuted task without
1221 * executing it
1222 */
1223 public void testPollNextLocalTask() {
1224 ForkJoinTask a = new CheckedRecursiveAction() {
1225 protected void realCompute() {
1226 CCF g = new LCCF(9);
1227 assertSame(g, g.fork());
1228 CCF f = new LCCF(8);
1229 assertSame(f, f.fork());
1230 assertSame(f, pollNextLocalTask());
1231 helpQuiesce();
1232 checkNotDone(f);
1233 assertEquals(34, g.number);
1234 checkCompletedNormally(g);
1235 }};
1236 testInvokeOnPool(singletonPool(), a);
1237 }
1238
1239 /**
1240 * pollTask returns an unexecuted task without executing it
1241 */
1242 public void testPollTask() {
1243 ForkJoinTask a = new CheckedRecursiveAction() {
1244 protected void realCompute() {
1245 CCF g = new LCCF(9);
1246 assertSame(g, g.fork());
1247 CCF f = new LCCF(8);
1248 assertSame(f, f.fork());
1249 assertSame(f, pollTask());
1250 helpQuiesce();
1251 checkNotDone(f);
1252 checkCompletedNormally(g);
1253 }};
1254 testInvokeOnPool(singletonPool(), a);
1255 }
1256
1257 /**
1258 * peekNextLocalTask returns least recent unexecuted task in async mode
1259 */
1260 public void testPeekNextLocalTaskAsync() {
1261 ForkJoinTask a = new CheckedRecursiveAction() {
1262 protected void realCompute() {
1263 CCF g = new LCCF(9);
1264 assertSame(g, g.fork());
1265 CCF f = new LCCF(8);
1266 assertSame(f, f.fork());
1267 assertSame(g, peekNextLocalTask());
1268 assertNull(f.join());
1269 helpQuiesce();
1270 checkCompletedNormally(f);
1271 assertEquals(34, g.number);
1272 checkCompletedNormally(g);
1273 }};
1274 testInvokeOnPool(asyncSingletonPool(), a);
1275 }
1276
1277 /**
1278 * pollNextLocalTask returns least recent unexecuted task without
1279 * executing it, in async mode
1280 */
1281 public void testPollNextLocalTaskAsync() {
1282 ForkJoinTask a = new CheckedRecursiveAction() {
1283 protected void realCompute() {
1284 CCF g = new LCCF(9);
1285 assertSame(g, g.fork());
1286 CCF f = new LCCF(8);
1287 assertSame(f, f.fork());
1288 assertSame(g, pollNextLocalTask());
1289 helpQuiesce();
1290 assertEquals(21, f.number);
1291 checkCompletedNormally(f);
1292 checkNotDone(g);
1293 }};
1294 testInvokeOnPool(asyncSingletonPool(), a);
1295 }
1296
1297 /**
1298 * pollTask returns an unexecuted task without executing it, in
1299 * async mode
1300 */
1301 public void testPollTaskAsync() {
1302 ForkJoinTask a = new CheckedRecursiveAction() {
1303 protected void realCompute() {
1304 CCF g = new LCCF(9);
1305 assertSame(g, g.fork());
1306 CCF f = new LCCF(8);
1307 assertSame(f, f.fork());
1308 assertSame(g, pollTask());
1309 helpQuiesce();
1310 assertEquals(21, f.number);
1311 checkCompletedNormally(f);
1312 checkNotDone(g);
1313 }};
1314 testInvokeOnPool(asyncSingletonPool(), a);
1315 }
1316
1317 // versions for singleton pools
1318
1319 /**
1320 * invoke returns when task completes normally.
1321 * isCompletedAbnormally and isCancelled return false for normally
1322 * completed tasks; getRawResult returns null.
1323 */
1324 public void testInvokeSingleton() {
1325 ForkJoinTask a = new CheckedRecursiveAction() {
1326 protected void realCompute() {
1327 CCF f = new LCCF(8);
1328 assertNull(f.invoke());
1329 assertEquals(21, f.number);
1330 checkCompletedNormally(f);
1331 }};
1332 testInvokeOnPool(singletonPool(), a);
1333 }
1334
1335 /**
1336 * quietlyInvoke task returns when task completes normally.
1337 * isCompletedAbnormally and isCancelled return false for normally
1338 * completed tasks
1339 */
1340 public void testQuietlyInvokeSingleton() {
1341 ForkJoinTask a = new CheckedRecursiveAction() {
1342 protected void realCompute() {
1343 CCF f = new LCCF(8);
1344 f.quietlyInvoke();
1345 assertEquals(21, f.number);
1346 checkCompletedNormally(f);
1347 }};
1348 testInvokeOnPool(singletonPool(), a);
1349 }
1350
1351 /**
1352 * join of a forked task returns when task completes
1353 */
1354 public void testForkJoinSingleton() {
1355 ForkJoinTask a = new CheckedRecursiveAction() {
1356 protected void realCompute() {
1357 CCF f = new LCCF(8);
1358 assertSame(f, f.fork());
1359 assertNull(f.join());
1360 assertEquals(21, f.number);
1361 checkCompletedNormally(f);
1362 }};
1363 testInvokeOnPool(singletonPool(), a);
1364 }
1365
1366 /**
1367 * get of a forked task returns when task completes
1368 */
1369 public void testForkGetSingleton() {
1370 ForkJoinTask a = new CheckedRecursiveAction() {
1371 protected void realCompute() throws Exception {
1372 CCF f = new LCCF(8);
1373 assertSame(f, f.fork());
1374 assertNull(f.get());
1375 assertEquals(21, f.number);
1376 checkCompletedNormally(f);
1377 }};
1378 testInvokeOnPool(singletonPool(), a);
1379 }
1380
1381 /**
1382 * timed get of a forked task returns when task completes
1383 */
1384 public void testForkTimedGetSingleton() {
1385 ForkJoinTask a = new CheckedRecursiveAction() {
1386 protected void realCompute() throws Exception {
1387 CCF f = new LCCF(8);
1388 assertSame(f, f.fork());
1389 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1390 assertEquals(21, f.number);
1391 checkCompletedNormally(f);
1392 }};
1393 testInvokeOnPool(singletonPool(), a);
1394 }
1395
1396 /**
1397 * timed get with null time unit throws NPE
1398 */
1399 public void testForkTimedGetNPESingleton() {
1400 ForkJoinTask a = new CheckedRecursiveAction() {
1401 protected void realCompute() throws Exception {
1402 CCF f = new LCCF(8);
1403 assertSame(f, f.fork());
1404 try {
1405 f.get(5L, null);
1406 shouldThrow();
1407 } catch (NullPointerException success) {}
1408 }};
1409 testInvokeOnPool(singletonPool(), a);
1410 }
1411
1412 /**
1413 * quietlyJoin of a forked task returns when task completes
1414 */
1415 public void testForkQuietlyJoinSingleton() {
1416 ForkJoinTask a = new CheckedRecursiveAction() {
1417 protected void realCompute() {
1418 CCF f = new LCCF(8);
1419 assertSame(f, f.fork());
1420 f.quietlyJoin();
1421 assertEquals(21, f.number);
1422 checkCompletedNormally(f);
1423 }};
1424 testInvokeOnPool(singletonPool(), a);
1425 }
1426
1427 /**
1428 * helpQuiesce returns when tasks are complete.
1429 * getQueuedTaskCount returns 0 when quiescent
1430 */
1431 public void testForkHelpQuiesceSingleton() {
1432 ForkJoinTask a = new CheckedRecursiveAction() {
1433 protected void realCompute() {
1434 CCF f = new LCCF(8);
1435 assertSame(f, f.fork());
1436 helpQuiesce();
1437 assertEquals(0, getQueuedTaskCount());
1438 assertEquals(21, f.number);
1439 checkCompletedNormally(f);
1440 }};
1441 testInvokeOnPool(singletonPool(), a);
1442 }
1443
1444 /**
1445 * invoke task throws exception when task completes abnormally
1446 */
1447 public void testAbnormalInvokeSingleton() {
1448 ForkJoinTask a = new CheckedRecursiveAction() {
1449 protected void realCompute() {
1450 FailingCCF f = new LFCCF(8);
1451 try {
1452 f.invoke();
1453 shouldThrow();
1454 } catch (FJException success) {
1455 checkCompletedAbnormally(f, success);
1456 }
1457 }};
1458 testInvokeOnPool(singletonPool(), a);
1459 }
1460
1461 /**
1462 * quietlyInvoke task returns when task completes abnormally
1463 */
1464 public void testAbnormalQuietlyInvokeSingleton() {
1465 ForkJoinTask a = new CheckedRecursiveAction() {
1466 protected void realCompute() {
1467 FailingCCF f = new LFCCF(8);
1468 f.quietlyInvoke();
1469 assertTrue(f.getException() instanceof FJException);
1470 checkCompletedAbnormally(f, f.getException());
1471 }};
1472 testInvokeOnPool(singletonPool(), a);
1473 }
1474
1475 /**
1476 * join of a forked task throws exception when task completes abnormally
1477 */
1478 public void testAbnormalForkJoinSingleton() {
1479 ForkJoinTask a = new CheckedRecursiveAction() {
1480 protected void realCompute() {
1481 FailingCCF f = new LFCCF(8);
1482 assertSame(f, f.fork());
1483 try {
1484 f.join();
1485 shouldThrow();
1486 } catch (FJException success) {
1487 checkCompletedAbnormally(f, success);
1488 }
1489 }};
1490 testInvokeOnPool(singletonPool(), a);
1491 }
1492
1493 /**
1494 * get of a forked task throws exception when task completes abnormally
1495 */
1496 public void testAbnormalForkGetSingleton() {
1497 ForkJoinTask a = new CheckedRecursiveAction() {
1498 protected void realCompute() throws Exception {
1499 FailingCCF f = new LFCCF(8);
1500 assertSame(f, f.fork());
1501 try {
1502 f.get();
1503 shouldThrow();
1504 } catch (ExecutionException success) {
1505 Throwable cause = success.getCause();
1506 assertTrue(cause instanceof FJException);
1507 checkCompletedAbnormally(f, cause);
1508 }
1509 }};
1510 testInvokeOnPool(singletonPool(), a);
1511 }
1512
1513 /**
1514 * timed get of a forked task throws exception when task completes abnormally
1515 */
1516 public void testAbnormalForkTimedGetSingleton() {
1517 ForkJoinTask a = new CheckedRecursiveAction() {
1518 protected void realCompute() throws Exception {
1519 FailingCCF f = new LFCCF(8);
1520 assertSame(f, f.fork());
1521 try {
1522 f.get(LONG_DELAY_MS, MILLISECONDS);
1523 shouldThrow();
1524 } catch (ExecutionException success) {
1525 Throwable cause = success.getCause();
1526 assertTrue(cause instanceof FJException);
1527 checkCompletedAbnormally(f, cause);
1528 }
1529 }};
1530 testInvokeOnPool(singletonPool(), a);
1531 }
1532
1533 /**
1534 * quietlyJoin of a forked task returns when task completes abnormally
1535 */
1536 public void testAbnormalForkQuietlyJoinSingleton() {
1537 ForkJoinTask a = new CheckedRecursiveAction() {
1538 protected void realCompute() {
1539 FailingCCF f = new LFCCF(8);
1540 assertSame(f, f.fork());
1541 f.quietlyJoin();
1542 assertTrue(f.getException() instanceof FJException);
1543 checkCompletedAbnormally(f, f.getException());
1544 }};
1545 testInvokeOnPool(singletonPool(), a);
1546 }
1547
1548 /**
1549 * invoke task throws exception when task cancelled
1550 */
1551 public void testCancelledInvokeSingleton() {
1552 ForkJoinTask a = new CheckedRecursiveAction() {
1553 protected void realCompute() {
1554 CCF f = new LCCF(8);
1555 assertTrue(f.cancel(true));
1556 try {
1557 f.invoke();
1558 shouldThrow();
1559 } catch (CancellationException success) {
1560 checkCancelled(f);
1561 }
1562 }};
1563 testInvokeOnPool(singletonPool(), a);
1564 }
1565
1566 /**
1567 * join of a forked task throws exception when task cancelled
1568 */
1569 public void testCancelledForkJoinSingleton() {
1570 ForkJoinTask a = new CheckedRecursiveAction() {
1571 protected void realCompute() {
1572 CCF f = new LCCF(8);
1573 assertTrue(f.cancel(true));
1574 assertSame(f, f.fork());
1575 try {
1576 f.join();
1577 shouldThrow();
1578 } catch (CancellationException success) {
1579 checkCancelled(f);
1580 }
1581 }};
1582 testInvokeOnPool(singletonPool(), a);
1583 }
1584
1585 /**
1586 * get of a forked task throws exception when task cancelled
1587 */
1588 public void testCancelledForkGetSingleton() {
1589 ForkJoinTask a = new CheckedRecursiveAction() {
1590 protected void realCompute() throws Exception {
1591 CCF f = new LCCF(8);
1592 assertTrue(f.cancel(true));
1593 assertSame(f, f.fork());
1594 try {
1595 f.get();
1596 shouldThrow();
1597 } catch (CancellationException success) {
1598 checkCancelled(f);
1599 }
1600 }};
1601 testInvokeOnPool(singletonPool(), a);
1602 }
1603
1604 /**
1605 * timed get of a forked task throws exception when task cancelled
1606 */
1607 public void testCancelledForkTimedGetSingleton() throws Exception {
1608 ForkJoinTask a = new CheckedRecursiveAction() {
1609 protected void realCompute() throws Exception {
1610 CCF f = new LCCF(8);
1611 assertTrue(f.cancel(true));
1612 assertSame(f, f.fork());
1613 try {
1614 f.get(LONG_DELAY_MS, MILLISECONDS);
1615 shouldThrow();
1616 } catch (CancellationException success) {
1617 checkCancelled(f);
1618 }
1619 }};
1620 testInvokeOnPool(singletonPool(), a);
1621 }
1622
1623 /**
1624 * quietlyJoin of a forked task returns when task cancelled
1625 */
1626 public void testCancelledForkQuietlyJoinSingleton() {
1627 ForkJoinTask a = new CheckedRecursiveAction() {
1628 protected void realCompute() {
1629 CCF f = new LCCF(8);
1630 assertTrue(f.cancel(true));
1631 assertSame(f, f.fork());
1632 f.quietlyJoin();
1633 checkCancelled(f);
1634 }};
1635 testInvokeOnPool(singletonPool(), a);
1636 }
1637
1638 /**
1639 * invoke task throws exception after invoking completeExceptionally
1640 */
1641 public void testCompleteExceptionallySingleton() {
1642 ForkJoinTask a = new CheckedRecursiveAction() {
1643 protected void realCompute() {
1644 CCF n = new LCCF(8);
1645 CCF f = new LCCF(n, 8);
1646 FJException ex = new FJException();
1647 f.completeExceptionally(ex);
1648 f.checkCompletedExceptionally(ex);
1649 n.checkCompletedExceptionally(ex);
1650 }};
1651 testInvokeOnPool(singletonPool(), a);
1652 }
1653
1654 /**
1655 * invokeAll(t1, t2) invokes all task arguments
1656 */
1657 public void testInvokeAll2Singleton() {
1658 ForkJoinTask a = new CheckedRecursiveAction() {
1659 protected void realCompute() {
1660 CCF f = new LCCF(8);
1661 CCF g = new LCCF(9);
1662 invokeAll(f, g);
1663 assertEquals(21, f.number);
1664 assertEquals(34, g.number);
1665 checkCompletedNormally(f);
1666 checkCompletedNormally(g);
1667 }};
1668 testInvokeOnPool(singletonPool(), a);
1669 }
1670
1671 /**
1672 * invokeAll(tasks) with 1 argument invokes task
1673 */
1674 public void testInvokeAll1Singleton() {
1675 ForkJoinTask a = new CheckedRecursiveAction() {
1676 protected void realCompute() {
1677 CCF f = new LCCF(8);
1678 invokeAll(f);
1679 checkCompletedNormally(f);
1680 assertEquals(21, f.number);
1681 }};
1682 testInvokeOnPool(singletonPool(), a);
1683 }
1684
1685 /**
1686 * invokeAll(tasks) with > 2 argument invokes tasks
1687 */
1688 public void testInvokeAll3Singleton() {
1689 ForkJoinTask a = new CheckedRecursiveAction() {
1690 protected void realCompute() {
1691 CCF f = new LCCF(8);
1692 CCF g = new LCCF(9);
1693 CCF h = new LCCF(7);
1694 invokeAll(f, g, h);
1695 assertEquals(21, f.number);
1696 assertEquals(34, g.number);
1697 assertEquals(13, h.number);
1698 checkCompletedNormally(f);
1699 checkCompletedNormally(g);
1700 checkCompletedNormally(h);
1701 }};
1702 testInvokeOnPool(singletonPool(), a);
1703 }
1704
1705 /**
1706 * invokeAll(collection) invokes all tasks in the collection
1707 */
1708 public void testInvokeAllCollectionSingleton() {
1709 ForkJoinTask a = new CheckedRecursiveAction() {
1710 protected void realCompute() {
1711 CCF f = new LCCF(8);
1712 CCF g = new LCCF(9);
1713 CCF h = new LCCF(7);
1714 HashSet set = new HashSet();
1715 set.add(f);
1716 set.add(g);
1717 set.add(h);
1718 invokeAll(set);
1719 assertEquals(21, f.number);
1720 assertEquals(34, g.number);
1721 assertEquals(13, h.number);
1722 checkCompletedNormally(f);
1723 checkCompletedNormally(g);
1724 checkCompletedNormally(h);
1725 }};
1726 testInvokeOnPool(singletonPool(), a);
1727 }
1728
1729 /**
1730 * invokeAll(tasks) with any null task throws NPE
1731 */
1732 public void testInvokeAllNPESingleton() {
1733 ForkJoinTask a = new CheckedRecursiveAction() {
1734 protected void realCompute() {
1735 CCF f = new LCCF(8);
1736 CCF g = new LCCF(9);
1737 CCF h = null;
1738 try {
1739 invokeAll(f, g, h);
1740 shouldThrow();
1741 } catch (NullPointerException success) {}
1742 }};
1743 testInvokeOnPool(singletonPool(), a);
1744 }
1745
1746 /**
1747 * invokeAll(t1, t2) throw exception if any task does
1748 */
1749 public void testAbnormalInvokeAll2Singleton() {
1750 ForkJoinTask a = new CheckedRecursiveAction() {
1751 protected void realCompute() {
1752 CCF f = new LCCF(8);
1753 FailingCCF g = new LFCCF(9);
1754 try {
1755 invokeAll(f, g);
1756 shouldThrow();
1757 } catch (FJException success) {
1758 checkCompletedAbnormally(g, success);
1759 }
1760 }};
1761 testInvokeOnPool(singletonPool(), a);
1762 }
1763
1764 /**
1765 * invokeAll(tasks) with 1 argument throws exception if task does
1766 */
1767 public void testAbnormalInvokeAll1Singleton() {
1768 ForkJoinTask a = new CheckedRecursiveAction() {
1769 protected void realCompute() {
1770 FailingCCF g = new LFCCF(9);
1771 try {
1772 invokeAll(g);
1773 shouldThrow();
1774 } catch (FJException success) {
1775 checkCompletedAbnormally(g, success);
1776 }
1777 }};
1778 testInvokeOnPool(singletonPool(), a);
1779 }
1780
1781 /**
1782 * invokeAll(tasks) with > 2 argument throws exception if any task does
1783 */
1784 public void testAbnormalInvokeAll3Singleton() {
1785 ForkJoinTask a = new CheckedRecursiveAction() {
1786 protected void realCompute() {
1787 CCF f = new LCCF(8);
1788 FailingCCF g = new LFCCF(9);
1789 CCF h = new LCCF(7);
1790 try {
1791 invokeAll(f, g, h);
1792 shouldThrow();
1793 } catch (FJException success) {
1794 checkCompletedAbnormally(g, success);
1795 }
1796 }};
1797 testInvokeOnPool(singletonPool(), a);
1798 }
1799
1800 /**
1801 * invokeAll(collection) throws exception if any task does
1802 */
1803 public void testAbnormalInvokeAllCollectionSingleton() {
1804 ForkJoinTask a = new CheckedRecursiveAction() {
1805 protected void realCompute() {
1806 FailingCCF f = new LFCCF(8);
1807 CCF g = new LCCF(9);
1808 CCF h = new LCCF(7);
1809 HashSet set = new HashSet();
1810 set.add(f);
1811 set.add(g);
1812 set.add(h);
1813 try {
1814 invokeAll(set);
1815 shouldThrow();
1816 } catch (FJException success) {
1817 checkCompletedAbnormally(f, success);
1818 }
1819 }};
1820 testInvokeOnPool(singletonPool(), a);
1821 }
1822
1823 }