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