ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.20
Committed: Tue Oct 6 00:36:55 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +8 -8 lines
Log Message:
t0 -> startTime

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