ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.17
Committed: Fri Feb 27 21:43:18 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +2 -2 lines
Log Message:
StampedLockTest.java

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