ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.24
Committed: Sun Oct 18 19:20:05 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.23: +4 -4 lines
Log Message:
improve testSetPendingCount

File Contents

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