ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/CountedCompleterTest.java
Revision: 1.3
Committed: Thu Feb 26 06:53:34 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +0 -2 lines
Log Message:
delete unused imports

File Contents

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