ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountedCompleterTest.java
Revision: 1.1
Committed: Thu Mar 21 00:27:10 2013 UTC (11 years, 2 months ago) by dl
Branch: MAIN
Log Message:
Add CountedCompleters

File Contents

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