ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.39
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.38: +4 -4 lines
Log Message:
use diamond <> pervasively

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