ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.21
Committed: Sun Oct 18 16:43:53 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +19 -10 lines
Log Message:
improve testDecrementPendingCount; fix testCompleteExceptionally_null

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