ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.8
Committed: Tue Jun 4 23:07:11 2013 UTC (10 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +302 -225 lines
Log Message:
various improvements

File Contents

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