ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
Revision: 1.5
Committed: Fri Mar 22 16:10:19 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.4: +8 -8 lines
Log Message:
whitespace

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 junit.framework.*;
8 import java.util.concurrent.CancellationException;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.ForkJoinPool;
11 import java.util.concurrent.ForkJoinTask;
12 import java.util.concurrent.ForkJoinWorkerThread;
13 import java.util.concurrent.RecursiveAction;
14 import java.util.concurrent.CountedCompleter;
15 import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import static java.util.concurrent.TimeUnit.SECONDS;
19 import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 import java.util.Arrays;
21 import java.util.HashSet;
22
23 public class ForkJoinPool8Test extends JSR166TestCase {
24 public static void main(String[] args) {
25 junit.textui.TestRunner.run(suite());
26 }
27
28 public static Test suite() {
29 return new TestSuite(ForkJoinPool8Test.class);
30 }
31
32 /**
33 * Common pool exists and has expected parallelism.
34 */
35 public void testCommonPoolParallelism() {
36 assertEquals(ForkJoinPool.getCommonPoolParallelism(),
37 ForkJoinPool.commonPool().getParallelism());
38 }
39
40 /**
41 * Common pool cannot be shut down
42 */
43 public void testCommonPoolShutDown() {
44 assertFalse(ForkJoinPool.commonPool().isShutdown());
45 assertFalse(ForkJoinPool.commonPool().isTerminating());
46 assertFalse(ForkJoinPool.commonPool().isTerminated());
47 ForkJoinPool.commonPool().shutdown();
48 assertFalse(ForkJoinPool.commonPool().isShutdown());
49 assertFalse(ForkJoinPool.commonPool().isTerminating());
50 assertFalse(ForkJoinPool.commonPool().isTerminated());
51 ForkJoinPool.commonPool().shutdownNow();
52 assertFalse(ForkJoinPool.commonPool().isShutdown());
53 assertFalse(ForkJoinPool.commonPool().isTerminating());
54 assertFalse(ForkJoinPool.commonPool().isTerminated());
55 }
56
57 /*
58 * All of the following test methods are adaptations of those for
59 * RecursiveAction and CountedCompleter, but with all actions
60 * executed in the common pool, generally implicitly via
61 * checkInvoke.
62 */
63
64 private void checkInvoke(ForkJoinTask a) {
65 checkNotDone(a);
66 assertNull(a.invoke());
67 checkCompletedNormally(a);
68 }
69
70 void checkNotDone(ForkJoinTask a) {
71 assertFalse(a.isDone());
72 assertFalse(a.isCompletedNormally());
73 assertFalse(a.isCompletedAbnormally());
74 assertFalse(a.isCancelled());
75 assertNull(a.getException());
76 assertNull(a.getRawResult());
77
78 if (! ForkJoinTask.inForkJoinPool()) {
79 Thread.currentThread().interrupt();
80 try {
81 a.get();
82 shouldThrow();
83 } catch (InterruptedException success) {
84 } catch (Throwable fail) { threadUnexpectedException(fail); }
85
86 Thread.currentThread().interrupt();
87 try {
88 a.get(5L, SECONDS);
89 shouldThrow();
90 } catch (InterruptedException success) {
91 } catch (Throwable fail) { threadUnexpectedException(fail); }
92 }
93
94 try {
95 a.get(0L, SECONDS);
96 shouldThrow();
97 } catch (TimeoutException success) {
98 } catch (Throwable fail) { threadUnexpectedException(fail); }
99 }
100
101 void checkCompletedNormally(ForkJoinTask a) {
102 assertTrue(a.isDone());
103 assertFalse(a.isCancelled());
104 assertTrue(a.isCompletedNormally());
105 assertFalse(a.isCompletedAbnormally());
106 assertNull(a.getException());
107 assertNull(a.getRawResult());
108 assertNull(a.join());
109 assertFalse(a.cancel(false));
110 assertFalse(a.cancel(true));
111 try {
112 assertNull(a.get());
113 } catch (Throwable fail) { threadUnexpectedException(fail); }
114 try {
115 assertNull(a.get(5L, SECONDS));
116 } catch (Throwable fail) { threadUnexpectedException(fail); }
117 }
118
119 void checkCancelled(ForkJoinTask a) {
120 assertTrue(a.isDone());
121 assertTrue(a.isCancelled());
122 assertFalse(a.isCompletedNormally());
123 assertTrue(a.isCompletedAbnormally());
124 assertTrue(a.getException() instanceof CancellationException);
125 assertNull(a.getRawResult());
126
127 try {
128 a.join();
129 shouldThrow();
130 } catch (CancellationException success) {
131 } catch (Throwable fail) { threadUnexpectedException(fail); }
132
133 try {
134 a.get();
135 shouldThrow();
136 } catch (CancellationException success) {
137 } catch (Throwable fail) { threadUnexpectedException(fail); }
138
139 try {
140 a.get(5L, SECONDS);
141 shouldThrow();
142 } catch (CancellationException success) {
143 } catch (Throwable fail) { threadUnexpectedException(fail); }
144 }
145
146 void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
147 assertTrue(a.isDone());
148 assertFalse(a.isCancelled());
149 assertFalse(a.isCompletedNormally());
150 assertTrue(a.isCompletedAbnormally());
151 assertSame(t.getClass(), a.getException().getClass());
152 assertNull(a.getRawResult());
153 assertFalse(a.cancel(false));
154 assertFalse(a.cancel(true));
155
156 try {
157 a.join();
158 shouldThrow();
159 } catch (Throwable expected) {
160 assertSame(expected.getClass(), t.getClass());
161 }
162
163 try {
164 a.get();
165 shouldThrow();
166 } catch (ExecutionException success) {
167 assertSame(t.getClass(), success.getCause().getClass());
168 } catch (Throwable fail) { threadUnexpectedException(fail); }
169
170 try {
171 a.get(5L, SECONDS);
172 shouldThrow();
173 } catch (ExecutionException success) {
174 assertSame(t.getClass(), success.getCause().getClass());
175 } catch (Throwable fail) { threadUnexpectedException(fail); }
176 }
177
178 public static final class FJException extends RuntimeException {
179 public FJException() { super(); }
180 public FJException(Throwable cause) { super(cause); }
181 }
182
183 // A simple recursive action for testing
184 final class FibAction extends CheckedRecursiveAction {
185 final int number;
186 int result;
187 FibAction(int n) { number = n; }
188 public void realCompute() {
189 int n = number;
190 if (n <= 1)
191 result = n;
192 else {
193 FibAction f1 = new FibAction(n - 1);
194 FibAction f2 = new FibAction(n - 2);
195 invokeAll(f1, f2);
196 result = f1.result + f2.result;
197 }
198 }
199 }
200
201 // A recursive action failing in base case
202 static final class FailingFibAction extends RecursiveAction {
203 final int number;
204 int result;
205 FailingFibAction(int n) { number = n; }
206 public void compute() {
207 int n = number;
208 if (n <= 1)
209 throw new FJException();
210 else {
211 FailingFibAction f1 = new FailingFibAction(n - 1);
212 FailingFibAction f2 = new FailingFibAction(n - 2);
213 invokeAll(f1, f2);
214 result = f1.result + f2.result;
215 }
216 }
217 }
218
219 /**
220 * invoke returns when task completes normally.
221 * isCompletedAbnormally and isCancelled return false for normally
222 * completed tasks. getRawResult of a RecursiveAction returns null;
223 */
224 public void testInvoke() {
225 RecursiveAction a = new CheckedRecursiveAction() {
226 public void realCompute() {
227 FibAction f = new FibAction(8);
228 assertNull(f.invoke());
229 assertEquals(21, f.result);
230 checkCompletedNormally(f);
231 }};
232 checkInvoke(a);
233 }
234
235 /**
236 * quietlyInvoke task returns when task completes normally.
237 * isCompletedAbnormally and isCancelled return false for normally
238 * completed tasks
239 */
240 public void testQuietlyInvoke() {
241 RecursiveAction a = new CheckedRecursiveAction() {
242 public void realCompute() {
243 FibAction f = new FibAction(8);
244 f.quietlyInvoke();
245 assertEquals(21, f.result);
246 checkCompletedNormally(f);
247 }};
248 checkInvoke(a);
249 }
250
251 /**
252 * join of a forked task returns when task completes
253 */
254 public void testForkJoin() {
255 RecursiveAction a = new CheckedRecursiveAction() {
256 public void realCompute() {
257 FibAction f = new FibAction(8);
258 assertSame(f, f.fork());
259 assertNull(f.join());
260 assertEquals(21, f.result);
261 checkCompletedNormally(f);
262 }};
263 checkInvoke(a);
264 }
265
266 /**
267 * join/quietlyJoin of a forked task succeeds in the presence of interrupts
268 */
269 public void testJoinIgnoresInterrupts() {
270 RecursiveAction a = new CheckedRecursiveAction() {
271 public void realCompute() {
272 FibAction f = new FibAction(8);
273 final Thread myself = Thread.currentThread();
274
275 // test join()
276 assertSame(f, f.fork());
277 myself.interrupt();
278 assertTrue(myself.isInterrupted());
279 assertNull(f.join());
280 Thread.interrupted();
281 assertEquals(21, f.result);
282 checkCompletedNormally(f);
283
284 f = new FibAction(8);
285 f.cancel(true);
286 assertSame(f, f.fork());
287 myself.interrupt();
288 assertTrue(myself.isInterrupted());
289 try {
290 f.join();
291 shouldThrow();
292 } catch (CancellationException success) {
293 Thread.interrupted();
294 checkCancelled(f);
295 }
296
297 f = new FibAction(8);
298 f.completeExceptionally(new FJException());
299 assertSame(f, f.fork());
300 myself.interrupt();
301 assertTrue(myself.isInterrupted());
302 try {
303 f.join();
304 shouldThrow();
305 } catch (FJException success) {
306 Thread.interrupted();
307 checkCompletedAbnormally(f, success);
308 }
309
310 // test quietlyJoin()
311 f = new FibAction(8);
312 assertSame(f, f.fork());
313 myself.interrupt();
314 assertTrue(myself.isInterrupted());
315 f.quietlyJoin();
316 Thread.interrupted();
317 assertEquals(21, f.result);
318 checkCompletedNormally(f);
319
320 f = new FibAction(8);
321 f.cancel(true);
322 assertSame(f, f.fork());
323 myself.interrupt();
324 assertTrue(myself.isInterrupted());
325 f.quietlyJoin();
326 Thread.interrupted();
327 checkCancelled(f);
328
329 f = new FibAction(8);
330 f.completeExceptionally(new FJException());
331 assertSame(f, f.fork());
332 myself.interrupt();
333 assertTrue(myself.isInterrupted());
334 f.quietlyJoin();
335 Thread.interrupted();
336 checkCompletedAbnormally(f, f.getException());
337 }};
338 checkInvoke(a);
339 a.reinitialize();
340 checkInvoke(a);
341 }
342
343
344 /**
345 * get of a forked task returns when task completes
346 */
347 public void testForkGet() {
348 RecursiveAction a = new CheckedRecursiveAction() {
349 public void realCompute() throws Exception {
350 FibAction f = new FibAction(8);
351 assertSame(f, f.fork());
352 assertNull(f.get());
353 assertEquals(21, f.result);
354 checkCompletedNormally(f);
355 }};
356 checkInvoke(a);
357 }
358
359 /**
360 * timed get of a forked task returns when task completes
361 */
362 public void testForkTimedGet() {
363 RecursiveAction a = new CheckedRecursiveAction() {
364 public void realCompute() throws Exception {
365 FibAction f = new FibAction(8);
366 assertSame(f, f.fork());
367 assertNull(f.get(5L, SECONDS));
368 assertEquals(21, f.result);
369 checkCompletedNormally(f);
370 }};
371 checkInvoke(a);
372 }
373
374 /**
375 * timed get with null time unit throws NPE
376 */
377 public void testForkTimedGetNPE() {
378 RecursiveAction a = new CheckedRecursiveAction() {
379 public void realCompute() throws Exception {
380 FibAction f = new FibAction(8);
381 assertSame(f, f.fork());
382 try {
383 f.get(5L, null);
384 shouldThrow();
385 } catch (NullPointerException success) {}
386 }};
387 checkInvoke(a);
388 }
389
390 /**
391 * quietlyJoin of a forked task returns when task completes
392 */
393 public void testForkQuietlyJoin() {
394 RecursiveAction a = new CheckedRecursiveAction() {
395 public void realCompute() {
396 FibAction f = new FibAction(8);
397 assertSame(f, f.fork());
398 f.quietlyJoin();
399 assertEquals(21, f.result);
400 checkCompletedNormally(f);
401 }};
402 checkInvoke(a);
403 }
404
405 /**
406 * helpQuiesce returns when tasks are complete.
407 * getQueuedTaskCount returns 0 when quiescent
408 */
409 public void testForkHelpQuiesce() {
410 RecursiveAction a = new CheckedRecursiveAction() {
411 public void realCompute() {
412 FibAction f = new FibAction(8);
413 assertSame(f, f.fork());
414 helpQuiesce();
415 assertEquals(21, f.result);
416 assertEquals(0, getQueuedTaskCount());
417 checkCompletedNormally(f);
418 }};
419 checkInvoke(a);
420 }
421
422 /**
423 * invoke task throws exception when task completes abnormally
424 */
425 public void testAbnormalInvoke() {
426 RecursiveAction a = new CheckedRecursiveAction() {
427 public void realCompute() {
428 FailingFibAction f = new FailingFibAction(8);
429 try {
430 f.invoke();
431 shouldThrow();
432 } catch (FJException success) {
433 checkCompletedAbnormally(f, success);
434 }
435 }};
436 checkInvoke(a);
437 }
438
439 /**
440 * quietlyInvoke task returns when task completes abnormally
441 */
442 public void testAbnormalQuietlyInvoke() {
443 RecursiveAction a = new CheckedRecursiveAction() {
444 public void realCompute() {
445 FailingFibAction f = new FailingFibAction(8);
446 f.quietlyInvoke();
447 assertTrue(f.getException() instanceof FJException);
448 checkCompletedAbnormally(f, f.getException());
449 }};
450 checkInvoke(a);
451 }
452
453 /**
454 * join of a forked task throws exception when task completes abnormally
455 */
456 public void testAbnormalForkJoin() {
457 RecursiveAction a = new CheckedRecursiveAction() {
458 public void realCompute() {
459 FailingFibAction f = new FailingFibAction(8);
460 assertSame(f, f.fork());
461 try {
462 f.join();
463 shouldThrow();
464 } catch (FJException success) {
465 checkCompletedAbnormally(f, success);
466 }
467 }};
468 checkInvoke(a);
469 }
470
471 /**
472 * get of a forked task throws exception when task completes abnormally
473 */
474 public void testAbnormalForkGet() {
475 RecursiveAction a = new CheckedRecursiveAction() {
476 public void realCompute() throws Exception {
477 FailingFibAction f = new FailingFibAction(8);
478 assertSame(f, f.fork());
479 try {
480 f.get();
481 shouldThrow();
482 } catch (ExecutionException success) {
483 Throwable cause = success.getCause();
484 assertTrue(cause instanceof FJException);
485 checkCompletedAbnormally(f, cause);
486 }
487 }};
488 checkInvoke(a);
489 }
490
491 /**
492 * timed get of a forked task throws exception when task completes abnormally
493 */
494 public void testAbnormalForkTimedGet() {
495 RecursiveAction a = new CheckedRecursiveAction() {
496 public void realCompute() throws Exception {
497 FailingFibAction f = new FailingFibAction(8);
498 assertSame(f, f.fork());
499 try {
500 f.get(5L, TimeUnit.SECONDS);
501 shouldThrow();
502 } catch (ExecutionException success) {
503 Throwable cause = success.getCause();
504 assertTrue(cause instanceof FJException);
505 checkCompletedAbnormally(f, cause);
506 }
507 }};
508 checkInvoke(a);
509 }
510
511 /**
512 * quietlyJoin of a forked task returns when task completes abnormally
513 */
514 public void testAbnormalForkQuietlyJoin() {
515 RecursiveAction a = new CheckedRecursiveAction() {
516 public void realCompute() {
517 FailingFibAction f = new FailingFibAction(8);
518 assertSame(f, f.fork());
519 f.quietlyJoin();
520 assertTrue(f.getException() instanceof FJException);
521 checkCompletedAbnormally(f, f.getException());
522 }};
523 checkInvoke(a);
524 }
525
526 /**
527 * invoke task throws exception when task cancelled
528 */
529 public void testCancelledInvoke() {
530 RecursiveAction a = new CheckedRecursiveAction() {
531 public void realCompute() {
532 FibAction f = new FibAction(8);
533 assertTrue(f.cancel(true));
534 try {
535 f.invoke();
536 shouldThrow();
537 } catch (CancellationException success) {
538 checkCancelled(f);
539 }
540 }};
541 checkInvoke(a);
542 }
543
544 /**
545 * join of a forked task throws exception when task cancelled
546 */
547 public void testCancelledForkJoin() {
548 RecursiveAction a = new CheckedRecursiveAction() {
549 public void realCompute() {
550 FibAction f = new FibAction(8);
551 assertTrue(f.cancel(true));
552 assertSame(f, f.fork());
553 try {
554 f.join();
555 shouldThrow();
556 } catch (CancellationException success) {
557 checkCancelled(f);
558 }
559 }};
560 checkInvoke(a);
561 }
562
563 /**
564 * get of a forked task throws exception when task cancelled
565 */
566 public void testCancelledForkGet() {
567 RecursiveAction a = new CheckedRecursiveAction() {
568 public void realCompute() throws Exception {
569 FibAction f = new FibAction(8);
570 assertTrue(f.cancel(true));
571 assertSame(f, f.fork());
572 try {
573 f.get();
574 shouldThrow();
575 } catch (CancellationException success) {
576 checkCancelled(f);
577 }
578 }};
579 checkInvoke(a);
580 }
581
582 /**
583 * timed get of a forked task throws exception when task cancelled
584 */
585 public void testCancelledForkTimedGet() {
586 RecursiveAction a = new CheckedRecursiveAction() {
587 public void realCompute() throws Exception {
588 FibAction f = new FibAction(8);
589 assertTrue(f.cancel(true));
590 assertSame(f, f.fork());
591 try {
592 f.get(5L, SECONDS);
593 shouldThrow();
594 } catch (CancellationException success) {
595 checkCancelled(f);
596 }
597 }};
598 checkInvoke(a);
599 }
600
601 /**
602 * quietlyJoin of a forked task returns when task cancelled
603 */
604 public void testCancelledForkQuietlyJoin() {
605 RecursiveAction a = new CheckedRecursiveAction() {
606 public void realCompute() {
607 FibAction f = new FibAction(8);
608 assertTrue(f.cancel(true));
609 assertSame(f, f.fork());
610 f.quietlyJoin();
611 checkCancelled(f);
612 }};
613 checkInvoke(a);
614 }
615
616 /**
617 * inForkJoinPool of non-FJ task returns false
618 */
619 public void testInForkJoinPool2() {
620 RecursiveAction a = new CheckedRecursiveAction() {
621 public void realCompute() {
622 assertFalse(inForkJoinPool());
623 }};
624 assertNull(a.invoke());
625 }
626
627 /**
628 * A reinitialized normally completed task may be re-invoked
629 */
630 public void testReinitialize() {
631 RecursiveAction a = new CheckedRecursiveAction() {
632 public void realCompute() {
633 FibAction f = new FibAction(8);
634 checkNotDone(f);
635
636 for (int i = 0; i < 3; i++) {
637 assertNull(f.invoke());
638 assertEquals(21, f.result);
639 checkCompletedNormally(f);
640 f.reinitialize();
641 checkNotDone(f);
642 }
643 }};
644 checkInvoke(a);
645 }
646
647 /**
648 * A reinitialized abnormally completed task may be re-invoked
649 */
650 public void testReinitializeAbnormal() {
651 RecursiveAction a = new CheckedRecursiveAction() {
652 public void realCompute() {
653 FailingFibAction f = new FailingFibAction(8);
654 checkNotDone(f);
655
656 for (int i = 0; i < 3; i++) {
657 try {
658 f.invoke();
659 shouldThrow();
660 } catch (FJException success) {
661 checkCompletedAbnormally(f, success);
662 }
663 f.reinitialize();
664 checkNotDone(f);
665 }
666 }};
667 checkInvoke(a);
668 }
669
670 /**
671 * invoke task throws exception after invoking completeExceptionally
672 */
673 public void testCompleteExceptionally() {
674 RecursiveAction a = new CheckedRecursiveAction() {
675 public void realCompute() {
676 FibAction f = new FibAction(8);
677 f.completeExceptionally(new FJException());
678 try {
679 f.invoke();
680 shouldThrow();
681 } catch (FJException success) {
682 checkCompletedAbnormally(f, success);
683 }
684 }};
685 checkInvoke(a);
686 }
687
688 /**
689 * invoke task suppresses execution invoking complete
690 */
691 public void testComplete() {
692 RecursiveAction a = new CheckedRecursiveAction() {
693 public void realCompute() {
694 FibAction f = new FibAction(8);
695 f.complete(null);
696 assertNull(f.invoke());
697 assertEquals(0, f.result);
698 checkCompletedNormally(f);
699 }};
700 checkInvoke(a);
701 }
702
703 /**
704 * invokeAll(t1, t2) invokes all task arguments
705 */
706 public void testInvokeAll2() {
707 RecursiveAction a = new CheckedRecursiveAction() {
708 public void realCompute() {
709 FibAction f = new FibAction(8);
710 FibAction g = new FibAction(9);
711 invokeAll(f, g);
712 checkCompletedNormally(f);
713 assertEquals(21, f.result);
714 checkCompletedNormally(g);
715 assertEquals(34, g.result);
716 }};
717 checkInvoke(a);
718 }
719
720 /**
721 * invokeAll(tasks) with 1 argument invokes task
722 */
723 public void testInvokeAll1() {
724 RecursiveAction a = new CheckedRecursiveAction() {
725 public void realCompute() {
726 FibAction f = new FibAction(8);
727 invokeAll(f);
728 checkCompletedNormally(f);
729 assertEquals(21, f.result);
730 }};
731 checkInvoke(a);
732 }
733
734 /**
735 * invokeAll(tasks) with > 2 argument invokes tasks
736 */
737 public void testInvokeAll3() {
738 RecursiveAction a = new CheckedRecursiveAction() {
739 public void realCompute() {
740 FibAction f = new FibAction(8);
741 FibAction g = new FibAction(9);
742 FibAction h = new FibAction(7);
743 invokeAll(f, g, h);
744 assertTrue(f.isDone());
745 assertTrue(g.isDone());
746 assertTrue(h.isDone());
747 checkCompletedNormally(f);
748 assertEquals(21, f.result);
749 checkCompletedNormally(g);
750 assertEquals(34, g.result);
751 checkCompletedNormally(g);
752 assertEquals(13, h.result);
753 }};
754 checkInvoke(a);
755 }
756
757 /**
758 * invokeAll(collection) invokes all tasks in the collection
759 */
760 public void testInvokeAllCollection() {
761 RecursiveAction a = new CheckedRecursiveAction() {
762 public void realCompute() {
763 FibAction f = new FibAction(8);
764 FibAction g = new FibAction(9);
765 FibAction h = new FibAction(7);
766 HashSet set = new HashSet();
767 set.add(f);
768 set.add(g);
769 set.add(h);
770 invokeAll(set);
771 assertTrue(f.isDone());
772 assertTrue(g.isDone());
773 assertTrue(h.isDone());
774 checkCompletedNormally(f);
775 assertEquals(21, f.result);
776 checkCompletedNormally(g);
777 assertEquals(34, g.result);
778 checkCompletedNormally(g);
779 assertEquals(13, h.result);
780 }};
781 checkInvoke(a);
782 }
783
784 /**
785 * invokeAll(tasks) with any null task throws NPE
786 */
787 public void testInvokeAllNPE() {
788 RecursiveAction a = new CheckedRecursiveAction() {
789 public void realCompute() {
790 FibAction f = new FibAction(8);
791 FibAction g = new FibAction(9);
792 FibAction h = null;
793 try {
794 invokeAll(f, g, h);
795 shouldThrow();
796 } catch (NullPointerException success) {}
797 }};
798 checkInvoke(a);
799 }
800
801 /**
802 * invokeAll(t1, t2) throw exception if any task does
803 */
804 public void testAbnormalInvokeAll2() {
805 RecursiveAction a = new CheckedRecursiveAction() {
806 public void realCompute() {
807 FibAction f = new FibAction(8);
808 FailingFibAction g = new FailingFibAction(9);
809 try {
810 invokeAll(f, g);
811 shouldThrow();
812 } catch (FJException success) {
813 checkCompletedAbnormally(g, success);
814 }
815 }};
816 checkInvoke(a);
817 }
818
819 /**
820 * invokeAll(tasks) with 1 argument throws exception if task does
821 */
822 public void testAbnormalInvokeAll1() {
823 RecursiveAction a = new CheckedRecursiveAction() {
824 public void realCompute() {
825 FailingFibAction g = new FailingFibAction(9);
826 try {
827 invokeAll(g);
828 shouldThrow();
829 } catch (FJException success) {
830 checkCompletedAbnormally(g, success);
831 }
832 }};
833 checkInvoke(a);
834 }
835
836 /**
837 * invokeAll(tasks) with > 2 argument throws exception if any task does
838 */
839 public void testAbnormalInvokeAll3() {
840 RecursiveAction a = new CheckedRecursiveAction() {
841 public void realCompute() {
842 FibAction f = new FibAction(8);
843 FailingFibAction g = new FailingFibAction(9);
844 FibAction h = new FibAction(7);
845 try {
846 invokeAll(f, g, h);
847 shouldThrow();
848 } catch (FJException success) {
849 checkCompletedAbnormally(g, success);
850 }
851 }};
852 checkInvoke(a);
853 }
854
855 /**
856 * invokeAll(collection) throws exception if any task does
857 */
858 public void testAbnormalInvokeAllCollection() {
859 RecursiveAction a = new CheckedRecursiveAction() {
860 public void realCompute() {
861 FailingFibAction f = new FailingFibAction(8);
862 FibAction g = new FibAction(9);
863 FibAction h = new FibAction(7);
864 HashSet set = new HashSet();
865 set.add(f);
866 set.add(g);
867 set.add(h);
868 try {
869 invokeAll(set);
870 shouldThrow();
871 } catch (FJException success) {
872 checkCompletedAbnormally(f, success);
873 }
874 }};
875 checkInvoke(a);
876 }
877
878 // CountedCompleter versions
879
880 public abstract class CheckedFJTask extends RecursiveAction {
881 protected abstract void realCompute() throws Throwable;
882
883 public final void compute() {
884 try {
885 realCompute();
886 } catch (Throwable t) {
887 threadUnexpectedException(t);
888 }
889 }
890 }
891
892 abstract static class CCF extends CountedCompleter {
893 int number;
894 int rnumber;
895
896 public CCF(CountedCompleter parent, int n) {
897 super(parent, 1);
898 this.number = n;
899 }
900
901 public final void compute() {
902 CountedCompleter p;
903 CCF f = this;
904 int n = number;
905 while (n >= 2) {
906 new RCCF(f, n - 2).fork();
907 f = new LCCF(f, --n);
908 }
909 f.number = n;
910 f.onCompletion(f);
911 if ((p = f.getCompleter()) != null)
912 p.tryComplete();
913 else
914 f.quietlyComplete();
915 }
916 }
917
918 static final class LCCF extends CCF {
919 public LCCF(CountedCompleter parent, int n) {
920 super(parent, n);
921 }
922 public final void onCompletion(CountedCompleter caller) {
923 CCF p = (CCF)getCompleter();
924 int n = number + rnumber;
925 if (p != null)
926 p.number = n;
927 else
928 number = n;
929 }
930 }
931 static final class RCCF extends CCF {
932 public RCCF(CountedCompleter parent, int n) {
933 super(parent, n);
934 }
935 public final void onCompletion(CountedCompleter caller) {
936 CCF p = (CCF)getCompleter();
937 int n = number + rnumber;
938 if (p != null)
939 p.rnumber = n;
940 else
941 number = n;
942 }
943 }
944
945 // Version of CCF with forced failure in left completions
946 abstract static class FailingCCF extends CountedCompleter {
947 int number;
948 int rnumber;
949
950 public FailingCCF(CountedCompleter parent, int n) {
951 super(parent, 1);
952 this.number = n;
953 }
954
955 public final void compute() {
956 CountedCompleter p;
957 FailingCCF f = this;
958 int n = number;
959 while (n >= 2) {
960 new RFCCF(f, n - 2).fork();
961 f = new LFCCF(f, --n);
962 }
963 f.number = n;
964 f.onCompletion(f);
965 if ((p = f.getCompleter()) != null)
966 p.tryComplete();
967 else
968 f.quietlyComplete();
969 }
970 }
971
972 static final class LFCCF extends FailingCCF {
973 public LFCCF(CountedCompleter parent, int n) {
974 super(parent, n);
975 }
976 public final void onCompletion(CountedCompleter caller) {
977 FailingCCF p = (FailingCCF)getCompleter();
978 int n = number + rnumber;
979 if (p != null)
980 p.number = n;
981 else
982 number = n;
983 }
984 }
985 static final class RFCCF extends FailingCCF {
986 public RFCCF(CountedCompleter parent, int n) {
987 super(parent, n);
988 }
989 public final void onCompletion(CountedCompleter caller) {
990 completeExceptionally(new FJException());
991 }
992 }
993
994 /**
995 * invoke returns when task completes normally.
996 * isCompletedAbnormally and isCancelled return false for normally
997 * completed tasks; getRawResult returns null.
998 */
999 public void testInvokeCC() {
1000 ForkJoinTask a = new CheckedFJTask() {
1001 public void realCompute() {
1002 CCF f = new LCCF(null, 8);
1003 assertNull(f.invoke());
1004 assertEquals(21, f.number);
1005 checkCompletedNormally(f);
1006 }};
1007 checkInvoke(a);
1008 }
1009
1010 /**
1011 * quietlyInvoke task returns when task completes normally.
1012 * isCompletedAbnormally and isCancelled return false for normally
1013 * completed tasks
1014 */
1015 public void testQuietlyInvokeCC() {
1016 ForkJoinTask a = new CheckedFJTask() {
1017 public void realCompute() {
1018 CCF f = new LCCF(null, 8);
1019 f.quietlyInvoke();
1020 assertEquals(21, f.number);
1021 checkCompletedNormally(f);
1022 }};
1023 checkInvoke(a);
1024 }
1025
1026 /**
1027 * join of a forked task returns when task completes
1028 */
1029 public void testForkJoinCC() {
1030 ForkJoinTask a = new CheckedFJTask() {
1031 public void realCompute() {
1032 CCF f = new LCCF(null, 8);
1033 assertSame(f, f.fork());
1034 assertNull(f.join());
1035 assertEquals(21, f.number);
1036 checkCompletedNormally(f);
1037 }};
1038 checkInvoke(a);
1039 }
1040
1041 /**
1042 * get of a forked task returns when task completes
1043 */
1044 public void testForkGetCC() {
1045 ForkJoinTask a = new CheckedFJTask() {
1046 public void realCompute() throws Exception {
1047 CCF f = new LCCF(null, 8);
1048 assertSame(f, f.fork());
1049 assertNull(f.get());
1050 assertEquals(21, f.number);
1051 checkCompletedNormally(f);
1052 }};
1053 checkInvoke(a);
1054 }
1055
1056 /**
1057 * timed get of a forked task returns when task completes
1058 */
1059 public void testForkTimedGetCC() {
1060 ForkJoinTask a = new CheckedFJTask() {
1061 public void realCompute() throws Exception {
1062 CCF f = new LCCF(null, 8);
1063 assertSame(f, f.fork());
1064 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1065 assertEquals(21, f.number);
1066 checkCompletedNormally(f);
1067 }};
1068 checkInvoke(a);
1069 }
1070
1071 /**
1072 * timed get with null time unit throws NPE
1073 */
1074 public void testForkTimedGetNPECC() {
1075 ForkJoinTask a = new CheckedFJTask() {
1076 public void realCompute() throws Exception {
1077 CCF f = new LCCF(null, 8);
1078 assertSame(f, f.fork());
1079 try {
1080 f.get(5L, null);
1081 shouldThrow();
1082 } catch (NullPointerException success) {}
1083 }};
1084 checkInvoke(a);
1085 }
1086
1087 /**
1088 * quietlyJoin of a forked task returns when task completes
1089 */
1090 public void testForkQuietlyJoinCC() {
1091 ForkJoinTask a = new CheckedFJTask() {
1092 public void realCompute() {
1093 CCF f = new LCCF(null, 8);
1094 assertSame(f, f.fork());
1095 f.quietlyJoin();
1096 assertEquals(21, f.number);
1097 checkCompletedNormally(f);
1098 }};
1099 checkInvoke(a);
1100 }
1101
1102 /**
1103 * helpQuiesce returns when tasks are complete.
1104 * getQueuedTaskCount returns 0 when quiescent
1105 */
1106 public void testForkHelpQuiesceCC() {
1107 ForkJoinTask a = new CheckedFJTask() {
1108 public void realCompute() {
1109 CCF f = new LCCF(null, 8);
1110 assertSame(f, f.fork());
1111 helpQuiesce();
1112 assertEquals(21, f.number);
1113 assertEquals(0, getQueuedTaskCount());
1114 checkCompletedNormally(f);
1115 }};
1116 checkInvoke(a);
1117 }
1118
1119 /**
1120 * invoke task throws exception when task completes abnormally
1121 */
1122 public void testAbnormalInvokeCC() {
1123 ForkJoinTask a = new CheckedFJTask() {
1124 public void realCompute() {
1125 FailingCCF f = new LFCCF(null, 8);
1126 try {
1127 f.invoke();
1128 shouldThrow();
1129 } catch (FJException success) {
1130 checkCompletedAbnormally(f, success);
1131 }
1132 }};
1133 checkInvoke(a);
1134 }
1135
1136 /**
1137 * quietlyInvoke task returns when task completes abnormally
1138 */
1139 public void testAbnormalQuietlyInvokeCC() {
1140 ForkJoinTask a = new CheckedFJTask() {
1141 public void realCompute() {
1142 FailingCCF f = new LFCCF(null, 8);
1143 f.quietlyInvoke();
1144 assertTrue(f.getException() instanceof FJException);
1145 checkCompletedAbnormally(f, f.getException());
1146 }};
1147 checkInvoke(a);
1148 }
1149
1150 /**
1151 * join of a forked task throws exception when task completes abnormally
1152 */
1153 public void testAbnormalForkJoinCC() {
1154 ForkJoinTask a = new CheckedFJTask() {
1155 public void realCompute() {
1156 FailingCCF f = new LFCCF(null, 8);
1157 assertSame(f, f.fork());
1158 try {
1159 f.join();
1160 shouldThrow();
1161 } catch (FJException success) {
1162 checkCompletedAbnormally(f, success);
1163 }
1164 }};
1165 checkInvoke(a);
1166 }
1167
1168 /**
1169 * get of a forked task throws exception when task completes abnormally
1170 */
1171 public void testAbnormalForkGetCC() {
1172 ForkJoinTask a = new CheckedFJTask() {
1173 public void realCompute() throws Exception {
1174 FailingCCF f = new LFCCF(null, 8);
1175 assertSame(f, f.fork());
1176 try {
1177 f.get();
1178 shouldThrow();
1179 } catch (ExecutionException success) {
1180 Throwable cause = success.getCause();
1181 assertTrue(cause instanceof FJException);
1182 checkCompletedAbnormally(f, cause);
1183 }
1184 }};
1185 checkInvoke(a);
1186 }
1187
1188 /**
1189 * timed get of a forked task throws exception when task completes abnormally
1190 */
1191 public void testAbnormalForkTimedGetCC() {
1192 ForkJoinTask a = new CheckedFJTask() {
1193 public void realCompute() throws Exception {
1194 FailingCCF f = new LFCCF(null, 8);
1195 assertSame(f, f.fork());
1196 try {
1197 f.get(LONG_DELAY_MS, MILLISECONDS);
1198 shouldThrow();
1199 } catch (ExecutionException success) {
1200 Throwable cause = success.getCause();
1201 assertTrue(cause instanceof FJException);
1202 checkCompletedAbnormally(f, cause);
1203 }
1204 }};
1205 checkInvoke(a);
1206 }
1207
1208 /**
1209 * quietlyJoin of a forked task returns when task completes abnormally
1210 */
1211 public void testAbnormalForkQuietlyJoinCC() {
1212 ForkJoinTask a = new CheckedFJTask() {
1213 public void realCompute() {
1214 FailingCCF f = new LFCCF(null, 8);
1215 assertSame(f, f.fork());
1216 f.quietlyJoin();
1217 assertTrue(f.getException() instanceof FJException);
1218 checkCompletedAbnormally(f, f.getException());
1219 }};
1220 checkInvoke(a);
1221 }
1222
1223 /**
1224 * invoke task throws exception when task cancelled
1225 */
1226 public void testCancelledInvokeCC() {
1227 ForkJoinTask a = new CheckedFJTask() {
1228 public void realCompute() {
1229 CCF f = new LCCF(null, 8);
1230 assertTrue(f.cancel(true));
1231 try {
1232 f.invoke();
1233 shouldThrow();
1234 } catch (CancellationException success) {
1235 checkCancelled(f);
1236 }
1237 }};
1238 checkInvoke(a);
1239 }
1240
1241 /**
1242 * join of a forked task throws exception when task cancelled
1243 */
1244 public void testCancelledForkJoinCC() {
1245 ForkJoinTask a = new CheckedFJTask() {
1246 public void realCompute() {
1247 CCF f = new LCCF(null, 8);
1248 assertTrue(f.cancel(true));
1249 assertSame(f, f.fork());
1250 try {
1251 f.join();
1252 shouldThrow();
1253 } catch (CancellationException success) {
1254 checkCancelled(f);
1255 }
1256 }};
1257 checkInvoke(a);
1258 }
1259
1260 /**
1261 * get of a forked task throws exception when task cancelled
1262 */
1263 public void testCancelledForkGetCC() {
1264 ForkJoinTask a = new CheckedFJTask() {
1265 public void realCompute() throws Exception {
1266 CCF f = new LCCF(null, 8);
1267 assertTrue(f.cancel(true));
1268 assertSame(f, f.fork());
1269 try {
1270 f.get();
1271 shouldThrow();
1272 } catch (CancellationException success) {
1273 checkCancelled(f);
1274 }
1275 }};
1276 checkInvoke(a);
1277 }
1278
1279 /**
1280 * timed get of a forked task throws exception when task cancelled
1281 */
1282 public void testCancelledForkTimedGetCC() throws Exception {
1283 ForkJoinTask a = new CheckedFJTask() {
1284 public void realCompute() throws Exception {
1285 CCF f = new LCCF(null, 8);
1286 assertTrue(f.cancel(true));
1287 assertSame(f, f.fork());
1288 try {
1289 f.get(LONG_DELAY_MS, MILLISECONDS);
1290 shouldThrow();
1291 } catch (CancellationException success) {
1292 checkCancelled(f);
1293 }
1294 }};
1295 checkInvoke(a);
1296 }
1297
1298 /**
1299 * quietlyJoin of a forked task returns when task cancelled
1300 */
1301 public void testCancelledForkQuietlyJoinCC() {
1302 ForkJoinTask a = new CheckedFJTask() {
1303 public void realCompute() {
1304 CCF f = new LCCF(null, 8);
1305 assertTrue(f.cancel(true));
1306 assertSame(f, f.fork());
1307 f.quietlyJoin();
1308 checkCancelled(f);
1309 }};
1310 checkInvoke(a);
1311 }
1312
1313 /**
1314 * getPool of non-FJ task returns null
1315 */
1316 public void testGetPool2CC() {
1317 ForkJoinTask a = new CheckedFJTask() {
1318 public void realCompute() {
1319 assertNull(getPool());
1320 }};
1321 assertNull(a.invoke());
1322 }
1323
1324 /**
1325 * inForkJoinPool of non-FJ task returns false
1326 */
1327 public void testInForkJoinPool2CC() {
1328 ForkJoinTask a = new CheckedFJTask() {
1329 public void realCompute() {
1330 assertFalse(inForkJoinPool());
1331 }};
1332 assertNull(a.invoke());
1333 }
1334
1335 /**
1336 * setRawResult(null) succeeds
1337 */
1338 public void testSetRawResultCC() {
1339 ForkJoinTask a = new CheckedFJTask() {
1340 public void realCompute() {
1341 setRawResult(null);
1342 assertNull(getRawResult());
1343 }};
1344 assertNull(a.invoke());
1345 }
1346
1347 /**
1348 * invoke task throws exception after invoking completeExceptionally
1349 */
1350 public void testCompleteExceptionally2CC() {
1351 ForkJoinTask a = new CheckedFJTask() {
1352 public void realCompute() {
1353 CCF f = new LCCF(null, 8);
1354 f.completeExceptionally(new FJException());
1355 try {
1356 f.invoke();
1357 shouldThrow();
1358 } catch (FJException success) {
1359 checkCompletedAbnormally(f, success);
1360 }
1361 }};
1362 checkInvoke(a);
1363 }
1364
1365 /**
1366 * invokeAll(t1, t2) invokes all task arguments
1367 */
1368 public void testInvokeAll2CC() {
1369 ForkJoinTask a = new CheckedFJTask() {
1370 public void realCompute() {
1371 CCF f = new LCCF(null, 8);
1372 CCF g = new LCCF(null, 9);
1373 invokeAll(f, g);
1374 assertEquals(21, f.number);
1375 assertEquals(34, g.number);
1376 checkCompletedNormally(f);
1377 checkCompletedNormally(g);
1378 }};
1379 checkInvoke(a);
1380 }
1381
1382 /**
1383 * invokeAll(tasks) with 1 argument invokes task
1384 */
1385 public void testInvokeAll1CC() {
1386 ForkJoinTask a = new CheckedFJTask() {
1387 public void realCompute() {
1388 CCF f = new LCCF(null, 8);
1389 invokeAll(f);
1390 checkCompletedNormally(f);
1391 assertEquals(21, f.number);
1392 }};
1393 checkInvoke(a);
1394 }
1395
1396 /**
1397 * invokeAll(tasks) with > 2 argument invokes tasks
1398 */
1399 public void testInvokeAll3CC() {
1400 ForkJoinTask a = new CheckedFJTask() {
1401 public void realCompute() {
1402 CCF f = new LCCF(null, 8);
1403 CCF g = new LCCF(null, 9);
1404 CCF h = new LCCF(null, 7);
1405 invokeAll(f, g, h);
1406 assertEquals(21, f.number);
1407 assertEquals(34, g.number);
1408 assertEquals(13, h.number);
1409 checkCompletedNormally(f);
1410 checkCompletedNormally(g);
1411 checkCompletedNormally(h);
1412 }};
1413 checkInvoke(a);
1414 }
1415
1416 /**
1417 * invokeAll(collection) invokes all tasks in the collection
1418 */
1419 public void testInvokeAllCollectionCC() {
1420 ForkJoinTask a = new CheckedFJTask() {
1421 public void realCompute() {
1422 CCF f = new LCCF(null, 8);
1423 CCF g = new LCCF(null, 9);
1424 CCF h = new LCCF(null, 7);
1425 HashSet set = new HashSet();
1426 set.add(f);
1427 set.add(g);
1428 set.add(h);
1429 invokeAll(set);
1430 assertEquals(21, f.number);
1431 assertEquals(34, g.number);
1432 assertEquals(13, h.number);
1433 checkCompletedNormally(f);
1434 checkCompletedNormally(g);
1435 checkCompletedNormally(h);
1436 }};
1437 checkInvoke(a);
1438 }
1439
1440 /**
1441 * invokeAll(tasks) with any null task throws NPE
1442 */
1443 public void testInvokeAllNPECC() {
1444 ForkJoinTask a = new CheckedFJTask() {
1445 public void realCompute() {
1446 CCF f = new LCCF(null, 8);
1447 CCF g = new LCCF(null, 9);
1448 CCF h = null;
1449 try {
1450 invokeAll(f, g, h);
1451 shouldThrow();
1452 } catch (NullPointerException success) {}
1453 }};
1454 checkInvoke(a);
1455 }
1456
1457 /**
1458 * invokeAll(t1, t2) throw exception if any task does
1459 */
1460 public void testAbnormalInvokeAll2CC() {
1461 ForkJoinTask a = new CheckedFJTask() {
1462 public void realCompute() {
1463 CCF f = new LCCF(null, 8);
1464 FailingCCF g = new LFCCF(null, 9);
1465 try {
1466 invokeAll(f, g);
1467 shouldThrow();
1468 } catch (FJException success) {
1469 checkCompletedAbnormally(g, success);
1470 }
1471 }};
1472 checkInvoke(a);
1473 }
1474
1475 /**
1476 * invokeAll(tasks) with 1 argument throws exception if task does
1477 */
1478 public void testAbnormalInvokeAll1CC() {
1479 ForkJoinTask a = new CheckedFJTask() {
1480 public void realCompute() {
1481 FailingCCF g = new LFCCF(null, 9);
1482 try {
1483 invokeAll(g);
1484 shouldThrow();
1485 } catch (FJException success) {
1486 checkCompletedAbnormally(g, success);
1487 }
1488 }};
1489 checkInvoke(a);
1490 }
1491
1492 /**
1493 * invokeAll(tasks) with > 2 argument throws exception if any task does
1494 */
1495 public void testAbnormalInvokeAll3CC() {
1496 ForkJoinTask a = new CheckedFJTask() {
1497 public void realCompute() {
1498 CCF f = new LCCF(null, 8);
1499 FailingCCF g = new LFCCF(null, 9);
1500 CCF h = new LCCF(null, 7);
1501 try {
1502 invokeAll(f, g, h);
1503 shouldThrow();
1504 } catch (FJException success) {
1505 checkCompletedAbnormally(g, success);
1506 }
1507 }};
1508 checkInvoke(a);
1509 }
1510
1511 /**
1512 * invokeAll(collection) throws exception if any task does
1513 */
1514 public void testAbnormalInvokeAllCollectionCC() {
1515 ForkJoinTask a = new CheckedFJTask() {
1516 public void realCompute() {
1517 FailingCCF f = new LFCCF(null, 8);
1518 CCF g = new LCCF(null, 9);
1519 CCF h = new LCCF(null, 7);
1520 HashSet set = new HashSet();
1521 set.add(f);
1522 set.add(g);
1523 set.add(h);
1524 try {
1525 invokeAll(set);
1526 shouldThrow();
1527 } catch (FJException success) {
1528 checkCompletedAbnormally(f, success);
1529 }
1530 }};
1531 checkInvoke(a);
1532 }
1533
1534 }