ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinTask8Test.java
Revision: 1.8
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +0 -1 lines
Log Message:
delete extraneous blank lines

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.ExecutionException;
12 import java.util.concurrent.ForkJoinPool;
13 import java.util.concurrent.ForkJoinTask;
14 import java.util.concurrent.RecursiveAction;
15 import java.util.concurrent.TimeoutException;
16
17 import junit.framework.Test;
18 import junit.framework.TestSuite;
19
20 public class ForkJoinTask8Test extends JSR166TestCase {
21
22 /*
23 * Testing notes: This differs from ForkJoinTaskTest mainly by
24 * defining a version of BinaryAsyncAction that uses JDK8 task
25 * tags for control state, thereby testing getForkJoinTaskTag,
26 * setForkJoinTaskTag, and compareAndSetForkJoinTaskTag across
27 * various contexts. Most of the test methods using it are
28 * otherwise identical, but omitting retest of those dealing with
29 * cancellation, which is not represented in this tag scheme.
30 */
31
32 static final short INITIAL_STATE = -1;
33 static final short COMPLETE_STATE = 0;
34 static final short EXCEPTION_STATE = 1;
35
36 public static void main(String[] args) {
37 junit.textui.TestRunner.run(suite());
38 }
39
40 public static Test suite() {
41 return new TestSuite(ForkJoinTask8Test.class);
42 }
43
44 // Runs with "mainPool" use > 1 thread. singletonPool tests use 1
45 static final int mainPoolSize =
46 Math.max(2, Runtime.getRuntime().availableProcessors());
47
48 private static ForkJoinPool mainPool() {
49 return new ForkJoinPool(mainPoolSize);
50 }
51
52 private static ForkJoinPool singletonPool() {
53 return new ForkJoinPool(1);
54 }
55
56 private static ForkJoinPool asyncSingletonPool() {
57 return new ForkJoinPool(1,
58 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
59 null, true);
60 }
61
62 private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
63 try {
64 assertFalse(a.isDone());
65 assertFalse(a.isCompletedNormally());
66 assertFalse(a.isCompletedAbnormally());
67 assertFalse(a.isCancelled());
68 assertNull(a.getException());
69 assertNull(a.getRawResult());
70
71 assertNull(pool.invoke(a));
72
73 assertTrue(a.isDone());
74 assertTrue(a.isCompletedNormally());
75 assertFalse(a.isCompletedAbnormally());
76 assertFalse(a.isCancelled());
77 assertNull(a.getException());
78 assertNull(a.getRawResult());
79 } finally {
80 joinPool(pool);
81 }
82 }
83
84 void checkNotDone(ForkJoinTask a) {
85 assertFalse(a.isDone());
86 assertFalse(a.isCompletedNormally());
87 assertFalse(a.isCompletedAbnormally());
88 assertFalse(a.isCancelled());
89 assertNull(a.getException());
90 assertNull(a.getRawResult());
91 if (a instanceof BinaryAsyncAction)
92 assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE);
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(ForkJoinTask<T> a) {
102 checkCompletedNormally(a, null);
103 }
104
105 <T> void checkCompletedNormally(ForkJoinTask<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 if (a instanceof BinaryAsyncAction)
113 assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE);
114
115 {
116 Thread.currentThread().interrupt();
117 long t0 = System.nanoTime();
118 assertSame(expected, a.join());
119 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
120 Thread.interrupted();
121 }
122
123 {
124 Thread.currentThread().interrupt();
125 long t0 = System.nanoTime();
126 a.quietlyJoin(); // should be no-op
127 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
128 Thread.interrupted();
129 }
130
131 assertFalse(a.cancel(false));
132 assertFalse(a.cancel(true));
133 try {
134 assertSame(expected, a.get());
135 } catch (Throwable fail) { threadUnexpectedException(fail); }
136 try {
137 assertSame(expected, a.get(5L, SECONDS));
138 } catch (Throwable fail) { threadUnexpectedException(fail); }
139 }
140
141 void checkCompletedAbnormally(ForkJoinTask a, Throwable t) {
142 assertTrue(a.isDone());
143 assertFalse(a.isCancelled());
144 assertFalse(a.isCompletedNormally());
145 assertTrue(a.isCompletedAbnormally());
146 assertSame(t.getClass(), a.getException().getClass());
147 assertNull(a.getRawResult());
148 assertFalse(a.cancel(false));
149 assertFalse(a.cancel(true));
150 if (a instanceof BinaryAsyncAction)
151 assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() != INITIAL_STATE);
152
153 try {
154 Thread.currentThread().interrupt();
155 a.join();
156 shouldThrow();
157 } catch (Throwable expected) {
158 assertSame(t.getClass(), expected.getClass());
159 }
160 Thread.interrupted();
161
162 {
163 long t0 = System.nanoTime();
164 a.quietlyJoin(); // should be no-op
165 assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
166 }
167
168 try {
169 a.get();
170 shouldThrow();
171 } catch (ExecutionException success) {
172 assertSame(t.getClass(), success.getCause().getClass());
173 } catch (Throwable fail) { threadUnexpectedException(fail); }
174
175 try {
176 a.get(5L, SECONDS);
177 shouldThrow();
178 } catch (ExecutionException success) {
179 assertSame(t.getClass(), success.getCause().getClass());
180 } catch (Throwable fail) { threadUnexpectedException(fail); }
181 }
182
183 public static final class FJException extends RuntimeException {
184 FJException() { super(); }
185 }
186
187 abstract static class BinaryAsyncAction extends ForkJoinTask<Void> {
188
189 private BinaryAsyncAction parent;
190
191 private BinaryAsyncAction sibling;
192
193 protected BinaryAsyncAction() {
194 setForkJoinTaskTag(INITIAL_STATE);
195 }
196
197 public final Void getRawResult() { return null; }
198 protected final void setRawResult(Void mustBeNull) { }
199
200 public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
201 x.parent = y.parent = this;
202 x.sibling = y;
203 y.sibling = x;
204 }
205
206 protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
207 if (this.getForkJoinTaskTag() != COMPLETE_STATE ||
208 x.getForkJoinTaskTag() != COMPLETE_STATE ||
209 y.getForkJoinTaskTag() != COMPLETE_STATE) {
210 completeThisExceptionally(new FJException());
211 }
212 }
213
214 protected boolean onException() {
215 return true;
216 }
217
218 public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) {
219 linkSubtasks(x, y);
220 y.fork();
221 x.fork();
222 }
223
224 private void completeThis() {
225 setForkJoinTaskTag(COMPLETE_STATE);
226 super.complete(null);
227 }
228
229 private void completeThisExceptionally(Throwable ex) {
230 setForkJoinTaskTag(EXCEPTION_STATE);
231 super.completeExceptionally(ex);
232 }
233
234 public final void complete() {
235 BinaryAsyncAction a = this;
236 for (;;) {
237 BinaryAsyncAction s = a.sibling;
238 BinaryAsyncAction p = a.parent;
239 a.sibling = null;
240 a.parent = null;
241 a.completeThis();
242 if (p == null ||
243 p.compareAndSetForkJoinTaskTag(INITIAL_STATE, COMPLETE_STATE))
244 break;
245 try {
246 p.onComplete(a, s);
247 } catch (Throwable rex) {
248 p.completeExceptionally(rex);
249 return;
250 }
251 a = p;
252 }
253 }
254
255 public final void completeExceptionally(Throwable ex) {
256 BinaryAsyncAction a = this;
257 while (!a.isCompletedAbnormally()) {
258 a.completeThisExceptionally(ex);
259 BinaryAsyncAction s = a.sibling;
260 if (s != null)
261 s.cancel(false);
262 if (!a.onException() || (a = a.parent) == null)
263 break;
264 }
265 }
266
267 public final BinaryAsyncAction getParent() {
268 return parent;
269 }
270
271 public BinaryAsyncAction getSibling() {
272 return sibling;
273 }
274
275 public void reinitialize() {
276 parent = sibling = null;
277 super.reinitialize();
278 }
279
280 }
281
282 static final class AsyncFib extends BinaryAsyncAction {
283 int number;
284 public AsyncFib(int n) {
285 this.number = n;
286 }
287
288 public final boolean exec() {
289 try {
290 AsyncFib f = this;
291 int n = f.number;
292 if (n > 1) {
293 while (n > 1) {
294 AsyncFib p = f;
295 AsyncFib r = new AsyncFib(n - 2);
296 f = new AsyncFib(--n);
297 p.linkSubtasks(r, f);
298 r.fork();
299 }
300 f.number = n;
301 }
302 f.complete();
303 }
304 catch (Throwable ex) {
305 compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE);
306 }
307 return false;
308 }
309
310 protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
311 number = ((AsyncFib)x).number + ((AsyncFib)y).number;
312 super.onComplete(x, y);
313 }
314 }
315
316 static final class FailingAsyncFib extends BinaryAsyncAction {
317 int number;
318 public FailingAsyncFib(int n) {
319 this.number = n;
320 }
321
322 public final boolean exec() {
323 FailingAsyncFib f = this;
324 int n = f.number;
325 if (n > 1) {
326 while (n > 1) {
327 FailingAsyncFib p = f;
328 FailingAsyncFib r = new FailingAsyncFib(n - 2);
329 f = new FailingAsyncFib(--n);
330 p.linkSubtasks(r, f);
331 r.fork();
332 }
333 f.number = n;
334 }
335 f.complete();
336 return false;
337 }
338
339 protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
340 completeExceptionally(new FJException());
341 }
342 }
343
344 /**
345 * invoke returns when task completes normally.
346 * isCompletedAbnormally and isCancelled return false for normally
347 * completed tasks; getRawResult returns null.
348 */
349 public void testInvoke() {
350 RecursiveAction a = new CheckedRecursiveAction() {
351 protected void realCompute() {
352 AsyncFib f = new AsyncFib(8);
353 assertNull(f.invoke());
354 assertEquals(21, f.number);
355 checkCompletedNormally(f);
356 }};
357 testInvokeOnPool(mainPool(), a);
358 }
359
360 /**
361 * quietlyInvoke task returns when task completes normally.
362 * isCompletedAbnormally and isCancelled return false for normally
363 * completed tasks
364 */
365 public void testQuietlyInvoke() {
366 RecursiveAction a = new CheckedRecursiveAction() {
367 protected void realCompute() {
368 AsyncFib f = new AsyncFib(8);
369 f.quietlyInvoke();
370 assertEquals(21, f.number);
371 checkCompletedNormally(f);
372 }};
373 testInvokeOnPool(mainPool(), a);
374 }
375
376 /**
377 * join of a forked task returns when task completes
378 */
379 public void testForkJoin() {
380 RecursiveAction a = new CheckedRecursiveAction() {
381 protected void realCompute() {
382 AsyncFib f = new AsyncFib(8);
383 assertSame(f, f.fork());
384 assertNull(f.join());
385 assertEquals(21, f.number);
386 checkCompletedNormally(f);
387 }};
388 testInvokeOnPool(mainPool(), a);
389 }
390
391 /**
392 * get of a forked task returns when task completes
393 */
394 public void testForkGet() {
395 RecursiveAction a = new CheckedRecursiveAction() {
396 protected void realCompute() throws Exception {
397 AsyncFib f = new AsyncFib(8);
398 assertSame(f, f.fork());
399 assertNull(f.get());
400 assertEquals(21, f.number);
401 checkCompletedNormally(f);
402 }};
403 testInvokeOnPool(mainPool(), a);
404 }
405
406 /**
407 * timed get of a forked task returns when task completes
408 */
409 public void testForkTimedGet() {
410 RecursiveAction a = new CheckedRecursiveAction() {
411 protected void realCompute() throws Exception {
412 AsyncFib f = new AsyncFib(8);
413 assertSame(f, f.fork());
414 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
415 assertEquals(21, f.number);
416 checkCompletedNormally(f);
417 }};
418 testInvokeOnPool(mainPool(), a);
419 }
420
421 /**
422 * timed get with null time unit throws NPE
423 */
424 public void testForkTimedGetNPE() {
425 RecursiveAction a = new CheckedRecursiveAction() {
426 protected void realCompute() throws Exception {
427 AsyncFib f = new AsyncFib(8);
428 assertSame(f, f.fork());
429 try {
430 f.get(5L, null);
431 shouldThrow();
432 } catch (NullPointerException success) {}
433 }};
434 testInvokeOnPool(mainPool(), a);
435 }
436
437 /**
438 * quietlyJoin of a forked task returns when task completes
439 */
440 public void testForkQuietlyJoin() {
441 RecursiveAction a = new CheckedRecursiveAction() {
442 protected void realCompute() {
443 AsyncFib f = new AsyncFib(8);
444 assertSame(f, f.fork());
445 f.quietlyJoin();
446 assertEquals(21, f.number);
447 checkCompletedNormally(f);
448 }};
449 testInvokeOnPool(mainPool(), a);
450 }
451
452 /**
453 * helpQuiesce returns when tasks are complete.
454 * getQueuedTaskCount returns 0 when quiescent
455 */
456 public void testForkHelpQuiesce() {
457 RecursiveAction a = new CheckedRecursiveAction() {
458 protected void realCompute() {
459 AsyncFib f = new AsyncFib(8);
460 assertSame(f, f.fork());
461 helpQuiesce();
462 assertEquals(21, f.number);
463 assertEquals(0, getQueuedTaskCount());
464 checkCompletedNormally(f);
465 }};
466 testInvokeOnPool(mainPool(), a);
467 }
468
469 /**
470 * invoke task throws exception when task completes abnormally
471 */
472 public void testAbnormalInvoke() {
473 RecursiveAction a = new CheckedRecursiveAction() {
474 protected void realCompute() {
475 FailingAsyncFib f = new FailingAsyncFib(8);
476 try {
477 f.invoke();
478 shouldThrow();
479 } catch (FJException success) {
480 checkCompletedAbnormally(f, success);
481 }
482 }};
483 testInvokeOnPool(mainPool(), a);
484 }
485
486 /**
487 * quietlyInvoke task returns when task completes abnormally
488 */
489 public void testAbnormalQuietlyInvoke() {
490 RecursiveAction a = new CheckedRecursiveAction() {
491 protected void realCompute() {
492 FailingAsyncFib f = new FailingAsyncFib(8);
493 f.quietlyInvoke();
494 assertTrue(f.getException() instanceof FJException);
495 checkCompletedAbnormally(f, f.getException());
496 }};
497 testInvokeOnPool(mainPool(), a);
498 }
499
500 /**
501 * join of a forked task throws exception when task completes abnormally
502 */
503 public void testAbnormalForkJoin() {
504 RecursiveAction a = new CheckedRecursiveAction() {
505 protected void realCompute() {
506 FailingAsyncFib f = new FailingAsyncFib(8);
507 assertSame(f, f.fork());
508 try {
509 f.join();
510 shouldThrow();
511 } catch (FJException success) {
512 checkCompletedAbnormally(f, success);
513 }
514 }};
515 testInvokeOnPool(mainPool(), a);
516 }
517
518 /**
519 * get of a forked task throws exception when task completes abnormally
520 */
521 public void testAbnormalForkGet() {
522 RecursiveAction a = new CheckedRecursiveAction() {
523 protected void realCompute() throws Exception {
524 FailingAsyncFib f = new FailingAsyncFib(8);
525 assertSame(f, f.fork());
526 try {
527 f.get();
528 shouldThrow();
529 } catch (ExecutionException success) {
530 Throwable cause = success.getCause();
531 assertTrue(cause instanceof FJException);
532 checkCompletedAbnormally(f, cause);
533 }
534 }};
535 testInvokeOnPool(mainPool(), a);
536 }
537
538 /**
539 * timed get of a forked task throws exception when task completes abnormally
540 */
541 public void testAbnormalForkTimedGet() {
542 RecursiveAction a = new CheckedRecursiveAction() {
543 protected void realCompute() throws Exception {
544 FailingAsyncFib f = new FailingAsyncFib(8);
545 assertSame(f, f.fork());
546 try {
547 f.get(LONG_DELAY_MS, MILLISECONDS);
548 shouldThrow();
549 } catch (ExecutionException success) {
550 Throwable cause = success.getCause();
551 assertTrue(cause instanceof FJException);
552 checkCompletedAbnormally(f, cause);
553 }
554 }};
555 testInvokeOnPool(mainPool(), a);
556 }
557
558 /**
559 * quietlyJoin of a forked task returns when task completes abnormally
560 */
561 public void testAbnormalForkQuietlyJoin() {
562 RecursiveAction a = new CheckedRecursiveAction() {
563 protected void realCompute() {
564 FailingAsyncFib f = new FailingAsyncFib(8);
565 assertSame(f, f.fork());
566 f.quietlyJoin();
567 assertTrue(f.getException() instanceof FJException);
568 checkCompletedAbnormally(f, f.getException());
569 }};
570 testInvokeOnPool(mainPool(), a);
571 }
572
573 /**
574 * getPool of executing task returns its pool
575 */
576 public void testGetPool() {
577 final ForkJoinPool mainPool = mainPool();
578 RecursiveAction a = new CheckedRecursiveAction() {
579 protected void realCompute() {
580 assertSame(mainPool, getPool());
581 }};
582 testInvokeOnPool(mainPool, a);
583 }
584
585 /**
586 * getPool of non-FJ task returns null
587 */
588 public void testGetPool2() {
589 RecursiveAction a = new CheckedRecursiveAction() {
590 protected void realCompute() {
591 assertNull(getPool());
592 }};
593 assertNull(a.invoke());
594 }
595
596 /**
597 * inForkJoinPool of executing task returns true
598 */
599 public void testInForkJoinPool() {
600 RecursiveAction a = new CheckedRecursiveAction() {
601 protected void realCompute() {
602 assertTrue(inForkJoinPool());
603 }};
604 testInvokeOnPool(mainPool(), a);
605 }
606
607 /**
608 * inForkJoinPool of non-FJ task returns false
609 */
610 public void testInForkJoinPool2() {
611 RecursiveAction a = new CheckedRecursiveAction() {
612 protected void realCompute() {
613 assertFalse(inForkJoinPool());
614 }};
615 assertNull(a.invoke());
616 }
617
618 /**
619 * setRawResult(null) succeeds
620 */
621 public void testSetRawResult() {
622 RecursiveAction a = new CheckedRecursiveAction() {
623 protected void realCompute() {
624 setRawResult(null);
625 assertNull(getRawResult());
626 }};
627 assertNull(a.invoke());
628 }
629
630 /**
631 * invoke task throws exception after invoking completeExceptionally
632 */
633 public void testCompleteExceptionally() {
634 RecursiveAction a = new CheckedRecursiveAction() {
635 protected void realCompute() {
636 AsyncFib f = new AsyncFib(8);
637 f.completeExceptionally(new FJException());
638 try {
639 f.invoke();
640 shouldThrow();
641 } catch (FJException success) {
642 checkCompletedAbnormally(f, success);
643 }
644 }};
645 testInvokeOnPool(mainPool(), a);
646 }
647
648 /**
649 * invokeAll(t1, t2) invokes all task arguments
650 */
651 public void testInvokeAll2() {
652 RecursiveAction a = new CheckedRecursiveAction() {
653 protected void realCompute() {
654 AsyncFib f = new AsyncFib(8);
655 AsyncFib g = new AsyncFib(9);
656 invokeAll(f, g);
657 assertEquals(21, f.number);
658 assertEquals(34, g.number);
659 checkCompletedNormally(f);
660 checkCompletedNormally(g);
661 }};
662 testInvokeOnPool(mainPool(), a);
663 }
664
665 /**
666 * invokeAll(tasks) with 1 argument invokes task
667 */
668 public void testInvokeAll1() {
669 RecursiveAction a = new CheckedRecursiveAction() {
670 protected void realCompute() {
671 AsyncFib f = new AsyncFib(8);
672 invokeAll(f);
673 checkCompletedNormally(f);
674 assertEquals(21, f.number);
675 }};
676 testInvokeOnPool(mainPool(), a);
677 }
678
679 /**
680 * invokeAll(tasks) with > 2 argument invokes tasks
681 */
682 public void testInvokeAll3() {
683 RecursiveAction a = new CheckedRecursiveAction() {
684 protected void realCompute() {
685 AsyncFib f = new AsyncFib(8);
686 AsyncFib g = new AsyncFib(9);
687 AsyncFib h = new AsyncFib(7);
688 invokeAll(f, g, h);
689 assertEquals(21, f.number);
690 assertEquals(34, g.number);
691 assertEquals(13, h.number);
692 checkCompletedNormally(f);
693 checkCompletedNormally(g);
694 checkCompletedNormally(h);
695 }};
696 testInvokeOnPool(mainPool(), a);
697 }
698
699 /**
700 * invokeAll(collection) invokes all tasks in the collection
701 */
702 public void testInvokeAllCollection() {
703 RecursiveAction a = new CheckedRecursiveAction() {
704 protected void realCompute() {
705 AsyncFib f = new AsyncFib(8);
706 AsyncFib g = new AsyncFib(9);
707 AsyncFib h = new AsyncFib(7);
708 HashSet set = new HashSet();
709 set.add(f);
710 set.add(g);
711 set.add(h);
712 invokeAll(set);
713 assertEquals(21, f.number);
714 assertEquals(34, g.number);
715 assertEquals(13, h.number);
716 checkCompletedNormally(f);
717 checkCompletedNormally(g);
718 checkCompletedNormally(h);
719 }};
720 testInvokeOnPool(mainPool(), a);
721 }
722
723 /**
724 * invokeAll(tasks) with any null task throws NPE
725 */
726 public void testInvokeAllNPE() {
727 RecursiveAction a = new CheckedRecursiveAction() {
728 protected void realCompute() {
729 AsyncFib f = new AsyncFib(8);
730 AsyncFib g = new AsyncFib(9);
731 AsyncFib h = null;
732 try {
733 invokeAll(f, g, h);
734 shouldThrow();
735 } catch (NullPointerException success) {}
736 }};
737 testInvokeOnPool(mainPool(), a);
738 }
739
740 /**
741 * invokeAll(t1, t2) throw exception if any task does
742 */
743 public void testAbnormalInvokeAll2() {
744 RecursiveAction a = new CheckedRecursiveAction() {
745 protected void realCompute() {
746 AsyncFib f = new AsyncFib(8);
747 FailingAsyncFib g = new FailingAsyncFib(9);
748 try {
749 invokeAll(f, g);
750 shouldThrow();
751 } catch (FJException success) {
752 checkCompletedAbnormally(g, success);
753 }
754 }};
755 testInvokeOnPool(mainPool(), a);
756 }
757
758 /**
759 * invokeAll(tasks) with 1 argument throws exception if task does
760 */
761 public void testAbnormalInvokeAll1() {
762 RecursiveAction a = new CheckedRecursiveAction() {
763 protected void realCompute() {
764 FailingAsyncFib g = new FailingAsyncFib(9);
765 try {
766 invokeAll(g);
767 shouldThrow();
768 } catch (FJException success) {
769 checkCompletedAbnormally(g, success);
770 }
771 }};
772 testInvokeOnPool(mainPool(), a);
773 }
774
775 /**
776 * invokeAll(tasks) with > 2 argument throws exception if any task does
777 */
778 public void testAbnormalInvokeAll3() {
779 RecursiveAction a = new CheckedRecursiveAction() {
780 protected void realCompute() {
781 AsyncFib f = new AsyncFib(8);
782 FailingAsyncFib g = new FailingAsyncFib(9);
783 AsyncFib h = new AsyncFib(7);
784 try {
785 invokeAll(f, g, h);
786 shouldThrow();
787 } catch (FJException success) {
788 checkCompletedAbnormally(g, success);
789 }
790 }};
791 testInvokeOnPool(mainPool(), a);
792 }
793
794 /**
795 * invokeAll(collection) throws exception if any task does
796 */
797 public void testAbnormalInvokeAllCollection() {
798 RecursiveAction a = new CheckedRecursiveAction() {
799 protected void realCompute() {
800 FailingAsyncFib f = new FailingAsyncFib(8);
801 AsyncFib g = new AsyncFib(9);
802 AsyncFib h = new AsyncFib(7);
803 HashSet set = new HashSet();
804 set.add(f);
805 set.add(g);
806 set.add(h);
807 try {
808 invokeAll(set);
809 shouldThrow();
810 } catch (FJException success) {
811 checkCompletedAbnormally(f, success);
812 }
813 }};
814 testInvokeOnPool(mainPool(), a);
815 }
816
817 /**
818 * tryUnfork returns true for most recent unexecuted task,
819 * and suppresses execution
820 */
821 public void testTryUnfork() {
822 RecursiveAction a = new CheckedRecursiveAction() {
823 protected void realCompute() {
824 AsyncFib g = new AsyncFib(9);
825 assertSame(g, g.fork());
826 AsyncFib f = new AsyncFib(8);
827 assertSame(f, f.fork());
828 assertTrue(f.tryUnfork());
829 helpQuiesce();
830 checkNotDone(f);
831 checkCompletedNormally(g);
832 }};
833 testInvokeOnPool(singletonPool(), a);
834 }
835
836 /**
837 * getSurplusQueuedTaskCount returns > 0 when
838 * there are more tasks than threads
839 */
840 public void testGetSurplusQueuedTaskCount() {
841 RecursiveAction a = new CheckedRecursiveAction() {
842 protected void realCompute() {
843 AsyncFib h = new AsyncFib(7);
844 assertSame(h, h.fork());
845 AsyncFib g = new AsyncFib(9);
846 assertSame(g, g.fork());
847 AsyncFib f = new AsyncFib(8);
848 assertSame(f, f.fork());
849 assertTrue(getSurplusQueuedTaskCount() > 0);
850 helpQuiesce();
851 assertEquals(0, getSurplusQueuedTaskCount());
852 checkCompletedNormally(f);
853 checkCompletedNormally(g);
854 checkCompletedNormally(h);
855 }};
856 testInvokeOnPool(singletonPool(), a);
857 }
858
859 /**
860 * peekNextLocalTask returns most recent unexecuted task.
861 */
862 public void testPeekNextLocalTask() {
863 RecursiveAction a = new CheckedRecursiveAction() {
864 protected void realCompute() {
865 AsyncFib g = new AsyncFib(9);
866 assertSame(g, g.fork());
867 AsyncFib f = new AsyncFib(8);
868 assertSame(f, f.fork());
869 assertSame(f, peekNextLocalTask());
870 assertNull(f.join());
871 checkCompletedNormally(f);
872 helpQuiesce();
873 checkCompletedNormally(g);
874 }};
875 testInvokeOnPool(singletonPool(), a);
876 }
877
878 /**
879 * pollNextLocalTask returns most recent unexecuted task without
880 * executing it
881 */
882 public void testPollNextLocalTask() {
883 RecursiveAction a = new CheckedRecursiveAction() {
884 protected void realCompute() {
885 AsyncFib g = new AsyncFib(9);
886 assertSame(g, g.fork());
887 AsyncFib f = new AsyncFib(8);
888 assertSame(f, f.fork());
889 assertSame(f, pollNextLocalTask());
890 helpQuiesce();
891 checkNotDone(f);
892 assertEquals(34, g.number);
893 checkCompletedNormally(g);
894 }};
895 testInvokeOnPool(singletonPool(), a);
896 }
897
898 /**
899 * pollTask returns an unexecuted task without executing it
900 */
901 public void testPollTask() {
902 RecursiveAction a = new CheckedRecursiveAction() {
903 protected void realCompute() {
904 AsyncFib g = new AsyncFib(9);
905 assertSame(g, g.fork());
906 AsyncFib f = new AsyncFib(8);
907 assertSame(f, f.fork());
908 assertSame(f, pollTask());
909 helpQuiesce();
910 checkNotDone(f);
911 checkCompletedNormally(g);
912 }};
913 testInvokeOnPool(singletonPool(), a);
914 }
915
916 /**
917 * peekNextLocalTask returns least recent unexecuted task in async mode
918 */
919 public void testPeekNextLocalTaskAsync() {
920 RecursiveAction a = new CheckedRecursiveAction() {
921 protected void realCompute() {
922 AsyncFib g = new AsyncFib(9);
923 assertSame(g, g.fork());
924 AsyncFib f = new AsyncFib(8);
925 assertSame(f, f.fork());
926 assertSame(g, peekNextLocalTask());
927 assertNull(f.join());
928 helpQuiesce();
929 checkCompletedNormally(f);
930 assertEquals(34, g.number);
931 checkCompletedNormally(g);
932 }};
933 testInvokeOnPool(asyncSingletonPool(), a);
934 }
935
936 /**
937 * pollNextLocalTask returns least recent unexecuted task without
938 * executing it, in async mode
939 */
940 public void testPollNextLocalTaskAsync() {
941 RecursiveAction a = new CheckedRecursiveAction() {
942 protected void realCompute() {
943 AsyncFib g = new AsyncFib(9);
944 assertSame(g, g.fork());
945 AsyncFib f = new AsyncFib(8);
946 assertSame(f, f.fork());
947 assertSame(g, pollNextLocalTask());
948 helpQuiesce();
949 assertEquals(21, f.number);
950 checkCompletedNormally(f);
951 checkNotDone(g);
952 }};
953 testInvokeOnPool(asyncSingletonPool(), a);
954 }
955
956 /**
957 * pollTask returns an unexecuted task without executing it, in
958 * async mode
959 */
960 public void testPollTaskAsync() {
961 RecursiveAction a = new CheckedRecursiveAction() {
962 protected void realCompute() {
963 AsyncFib g = new AsyncFib(9);
964 assertSame(g, g.fork());
965 AsyncFib f = new AsyncFib(8);
966 assertSame(f, f.fork());
967 assertSame(g, pollTask());
968 helpQuiesce();
969 assertEquals(21, f.number);
970 checkCompletedNormally(f);
971 checkNotDone(g);
972 }};
973 testInvokeOnPool(asyncSingletonPool(), a);
974 }
975
976 // versions for singleton pools
977
978 /**
979 * invoke returns when task completes normally.
980 * isCompletedAbnormally and isCancelled return false for normally
981 * completed tasks; getRawResult returns null.
982 */
983 public void testInvokeSingleton() {
984 RecursiveAction a = new CheckedRecursiveAction() {
985 protected void realCompute() {
986 AsyncFib f = new AsyncFib(8);
987 assertNull(f.invoke());
988 assertEquals(21, f.number);
989 checkCompletedNormally(f);
990 }};
991 testInvokeOnPool(singletonPool(), a);
992 }
993
994 /**
995 * quietlyInvoke task returns when task completes normally.
996 * isCompletedAbnormally and isCancelled return false for normally
997 * completed tasks
998 */
999 public void testQuietlyInvokeSingleton() {
1000 RecursiveAction a = new CheckedRecursiveAction() {
1001 protected void realCompute() {
1002 AsyncFib f = new AsyncFib(8);
1003 f.quietlyInvoke();
1004 assertEquals(21, f.number);
1005 checkCompletedNormally(f);
1006 }};
1007 testInvokeOnPool(singletonPool(), a);
1008 }
1009
1010 /**
1011 * join of a forked task returns when task completes
1012 */
1013 public void testForkJoinSingleton() {
1014 RecursiveAction a = new CheckedRecursiveAction() {
1015 protected void realCompute() {
1016 AsyncFib f = new AsyncFib(8);
1017 assertSame(f, f.fork());
1018 assertNull(f.join());
1019 assertEquals(21, f.number);
1020 checkCompletedNormally(f);
1021 }};
1022 testInvokeOnPool(singletonPool(), a);
1023 }
1024
1025 /**
1026 * get of a forked task returns when task completes
1027 */
1028 public void testForkGetSingleton() {
1029 RecursiveAction a = new CheckedRecursiveAction() {
1030 protected void realCompute() throws Exception {
1031 AsyncFib f = new AsyncFib(8);
1032 assertSame(f, f.fork());
1033 assertNull(f.get());
1034 assertEquals(21, f.number);
1035 checkCompletedNormally(f);
1036 }};
1037 testInvokeOnPool(singletonPool(), a);
1038 }
1039
1040 /**
1041 * timed get of a forked task returns when task completes
1042 */
1043 public void testForkTimedGetSingleton() {
1044 RecursiveAction a = new CheckedRecursiveAction() {
1045 protected void realCompute() throws Exception {
1046 AsyncFib f = new AsyncFib(8);
1047 assertSame(f, f.fork());
1048 assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
1049 assertEquals(21, f.number);
1050 checkCompletedNormally(f);
1051 }};
1052 testInvokeOnPool(singletonPool(), a);
1053 }
1054
1055 /**
1056 * timed get with null time unit throws NPE
1057 */
1058 public void testForkTimedGetNPESingleton() {
1059 RecursiveAction a = new CheckedRecursiveAction() {
1060 protected void realCompute() throws Exception {
1061 AsyncFib f = new AsyncFib(8);
1062 assertSame(f, f.fork());
1063 try {
1064 f.get(5L, null);
1065 shouldThrow();
1066 } catch (NullPointerException success) {}
1067 }};
1068 testInvokeOnPool(singletonPool(), a);
1069 }
1070
1071 /**
1072 * quietlyJoin of a forked task returns when task completes
1073 */
1074 public void testForkQuietlyJoinSingleton() {
1075 RecursiveAction a = new CheckedRecursiveAction() {
1076 protected void realCompute() {
1077 AsyncFib f = new AsyncFib(8);
1078 assertSame(f, f.fork());
1079 f.quietlyJoin();
1080 assertEquals(21, f.number);
1081 checkCompletedNormally(f);
1082 }};
1083 testInvokeOnPool(singletonPool(), a);
1084 }
1085
1086 /**
1087 * helpQuiesce returns when tasks are complete.
1088 * getQueuedTaskCount returns 0 when quiescent
1089 */
1090 public void testForkHelpQuiesceSingleton() {
1091 RecursiveAction a = new CheckedRecursiveAction() {
1092 protected void realCompute() {
1093 AsyncFib f = new AsyncFib(8);
1094 assertSame(f, f.fork());
1095 helpQuiesce();
1096 assertEquals(0, getQueuedTaskCount());
1097 assertEquals(21, f.number);
1098 checkCompletedNormally(f);
1099 }};
1100 testInvokeOnPool(singletonPool(), a);
1101 }
1102
1103 /**
1104 * invoke task throws exception when task completes abnormally
1105 */
1106 public void testAbnormalInvokeSingleton() {
1107 RecursiveAction a = new CheckedRecursiveAction() {
1108 protected void realCompute() {
1109 FailingAsyncFib f = new FailingAsyncFib(8);
1110 try {
1111 f.invoke();
1112 shouldThrow();
1113 } catch (FJException success) {
1114 checkCompletedAbnormally(f, success);
1115 }
1116 }};
1117 testInvokeOnPool(singletonPool(), a);
1118 }
1119
1120 /**
1121 * quietlyInvoke task returns when task completes abnormally
1122 */
1123 public void testAbnormalQuietlyInvokeSingleton() {
1124 RecursiveAction a = new CheckedRecursiveAction() {
1125 protected void realCompute() {
1126 FailingAsyncFib f = new FailingAsyncFib(8);
1127 f.quietlyInvoke();
1128 assertTrue(f.getException() instanceof FJException);
1129 checkCompletedAbnormally(f, f.getException());
1130 }};
1131 testInvokeOnPool(singletonPool(), a);
1132 }
1133
1134 /**
1135 * join of a forked task throws exception when task completes abnormally
1136 */
1137 public void testAbnormalForkJoinSingleton() {
1138 RecursiveAction a = new CheckedRecursiveAction() {
1139 protected void realCompute() {
1140 FailingAsyncFib f = new FailingAsyncFib(8);
1141 assertSame(f, f.fork());
1142 try {
1143 f.join();
1144 shouldThrow();
1145 } catch (FJException success) {
1146 checkCompletedAbnormally(f, success);
1147 }
1148 }};
1149 testInvokeOnPool(singletonPool(), a);
1150 }
1151
1152 /**
1153 * get of a forked task throws exception when task completes abnormally
1154 */
1155 public void testAbnormalForkGetSingleton() {
1156 RecursiveAction a = new CheckedRecursiveAction() {
1157 protected void realCompute() throws Exception {
1158 FailingAsyncFib f = new FailingAsyncFib(8);
1159 assertSame(f, f.fork());
1160 try {
1161 f.get();
1162 shouldThrow();
1163 } catch (ExecutionException success) {
1164 Throwable cause = success.getCause();
1165 assertTrue(cause instanceof FJException);
1166 checkCompletedAbnormally(f, cause);
1167 }
1168 }};
1169 testInvokeOnPool(singletonPool(), a);
1170 }
1171
1172 /**
1173 * timed get of a forked task throws exception when task completes abnormally
1174 */
1175 public void testAbnormalForkTimedGetSingleton() {
1176 RecursiveAction a = new CheckedRecursiveAction() {
1177 protected void realCompute() throws Exception {
1178 FailingAsyncFib f = new FailingAsyncFib(8);
1179 assertSame(f, f.fork());
1180 try {
1181 f.get(LONG_DELAY_MS, MILLISECONDS);
1182 shouldThrow();
1183 } catch (ExecutionException success) {
1184 Throwable cause = success.getCause();
1185 assertTrue(cause instanceof FJException);
1186 checkCompletedAbnormally(f, cause);
1187 }
1188 }};
1189 testInvokeOnPool(singletonPool(), a);
1190 }
1191
1192 /**
1193 * quietlyJoin of a forked task returns when task completes abnormally
1194 */
1195 public void testAbnormalForkQuietlyJoinSingleton() {
1196 RecursiveAction a = new CheckedRecursiveAction() {
1197 protected void realCompute() {
1198 FailingAsyncFib f = new FailingAsyncFib(8);
1199 assertSame(f, f.fork());
1200 f.quietlyJoin();
1201 assertTrue(f.getException() instanceof FJException);
1202 checkCompletedAbnormally(f, f.getException());
1203 }};
1204 testInvokeOnPool(singletonPool(), a);
1205 }
1206
1207 /**
1208 * invoke task throws exception after invoking completeExceptionally
1209 */
1210 public void testCompleteExceptionallySingleton() {
1211 RecursiveAction a = new CheckedRecursiveAction() {
1212 protected void realCompute() {
1213 AsyncFib f = new AsyncFib(8);
1214 f.completeExceptionally(new FJException());
1215 try {
1216 f.invoke();
1217 shouldThrow();
1218 } catch (FJException success) {
1219 checkCompletedAbnormally(f, success);
1220 }
1221 }};
1222 testInvokeOnPool(singletonPool(), a);
1223 }
1224
1225 /**
1226 * invokeAll(t1, t2) invokes all task arguments
1227 */
1228 public void testInvokeAll2Singleton() {
1229 RecursiveAction a = new CheckedRecursiveAction() {
1230 protected void realCompute() {
1231 AsyncFib f = new AsyncFib(8);
1232 AsyncFib g = new AsyncFib(9);
1233 invokeAll(f, g);
1234 assertEquals(21, f.number);
1235 assertEquals(34, g.number);
1236 checkCompletedNormally(f);
1237 checkCompletedNormally(g);
1238 }};
1239 testInvokeOnPool(singletonPool(), a);
1240 }
1241
1242 /**
1243 * invokeAll(tasks) with 1 argument invokes task
1244 */
1245 public void testInvokeAll1Singleton() {
1246 RecursiveAction a = new CheckedRecursiveAction() {
1247 protected void realCompute() {
1248 AsyncFib f = new AsyncFib(8);
1249 invokeAll(f);
1250 checkCompletedNormally(f);
1251 assertEquals(21, f.number);
1252 }};
1253 testInvokeOnPool(singletonPool(), a);
1254 }
1255
1256 /**
1257 * invokeAll(tasks) with > 2 argument invokes tasks
1258 */
1259 public void testInvokeAll3Singleton() {
1260 RecursiveAction a = new CheckedRecursiveAction() {
1261 protected void realCompute() {
1262 AsyncFib f = new AsyncFib(8);
1263 AsyncFib g = new AsyncFib(9);
1264 AsyncFib h = new AsyncFib(7);
1265 invokeAll(f, g, h);
1266 assertEquals(21, f.number);
1267 assertEquals(34, g.number);
1268 assertEquals(13, h.number);
1269 checkCompletedNormally(f);
1270 checkCompletedNormally(g);
1271 checkCompletedNormally(h);
1272 }};
1273 testInvokeOnPool(singletonPool(), a);
1274 }
1275
1276 /**
1277 * invokeAll(collection) invokes all tasks in the collection
1278 */
1279 public void testInvokeAllCollectionSingleton() {
1280 RecursiveAction a = new CheckedRecursiveAction() {
1281 protected void realCompute() {
1282 AsyncFib f = new AsyncFib(8);
1283 AsyncFib g = new AsyncFib(9);
1284 AsyncFib h = new AsyncFib(7);
1285 HashSet set = new HashSet();
1286 set.add(f);
1287 set.add(g);
1288 set.add(h);
1289 invokeAll(set);
1290 assertEquals(21, f.number);
1291 assertEquals(34, g.number);
1292 assertEquals(13, h.number);
1293 checkCompletedNormally(f);
1294 checkCompletedNormally(g);
1295 checkCompletedNormally(h);
1296 }};
1297 testInvokeOnPool(singletonPool(), a);
1298 }
1299
1300 /**
1301 * invokeAll(tasks) with any null task throws NPE
1302 */
1303 public void testInvokeAllNPESingleton() {
1304 RecursiveAction a = new CheckedRecursiveAction() {
1305 protected void realCompute() {
1306 AsyncFib f = new AsyncFib(8);
1307 AsyncFib g = new AsyncFib(9);
1308 AsyncFib h = null;
1309 try {
1310 invokeAll(f, g, h);
1311 shouldThrow();
1312 } catch (NullPointerException success) {}
1313 }};
1314 testInvokeOnPool(singletonPool(), a);
1315 }
1316
1317 /**
1318 * invokeAll(t1, t2) throw exception if any task does
1319 */
1320 public void testAbnormalInvokeAll2Singleton() {
1321 RecursiveAction a = new CheckedRecursiveAction() {
1322 protected void realCompute() {
1323 AsyncFib f = new AsyncFib(8);
1324 FailingAsyncFib g = new FailingAsyncFib(9);
1325 try {
1326 invokeAll(f, g);
1327 shouldThrow();
1328 } catch (FJException success) {
1329 checkCompletedAbnormally(g, success);
1330 }
1331 }};
1332 testInvokeOnPool(singletonPool(), a);
1333 }
1334
1335 /**
1336 * invokeAll(tasks) with 1 argument throws exception if task does
1337 */
1338 public void testAbnormalInvokeAll1Singleton() {
1339 RecursiveAction a = new CheckedRecursiveAction() {
1340 protected void realCompute() {
1341 FailingAsyncFib g = new FailingAsyncFib(9);
1342 try {
1343 invokeAll(g);
1344 shouldThrow();
1345 } catch (FJException success) {
1346 checkCompletedAbnormally(g, success);
1347 }
1348 }};
1349 testInvokeOnPool(singletonPool(), a);
1350 }
1351
1352 /**
1353 * invokeAll(tasks) with > 2 argument throws exception if any task does
1354 */
1355 public void testAbnormalInvokeAll3Singleton() {
1356 RecursiveAction a = new CheckedRecursiveAction() {
1357 protected void realCompute() {
1358 AsyncFib f = new AsyncFib(8);
1359 FailingAsyncFib g = new FailingAsyncFib(9);
1360 AsyncFib h = new AsyncFib(7);
1361 try {
1362 invokeAll(f, g, h);
1363 shouldThrow();
1364 } catch (FJException success) {
1365 checkCompletedAbnormally(g, success);
1366 }
1367 }};
1368 testInvokeOnPool(singletonPool(), a);
1369 }
1370
1371 /**
1372 * invokeAll(collection) throws exception if any task does
1373 */
1374 public void testAbnormalInvokeAllCollectionSingleton() {
1375 RecursiveAction a = new CheckedRecursiveAction() {
1376 protected void realCompute() {
1377 FailingAsyncFib f = new FailingAsyncFib(8);
1378 AsyncFib g = new AsyncFib(9);
1379 AsyncFib h = new AsyncFib(7);
1380 HashSet set = new HashSet();
1381 set.add(f);
1382 set.add(g);
1383 set.add(h);
1384 try {
1385 invokeAll(set);
1386 shouldThrow();
1387 } catch (FJException success) {
1388 checkCompletedAbnormally(f, success);
1389 }
1390 }};
1391 testInvokeOnPool(singletonPool(), a);
1392 }
1393
1394 /**
1395 * ForkJoinTask.quietlyComplete returns when task completes
1396 * normally without setting a value. The most recent value
1397 * established by setRawResult(V) (or null by default) is returned
1398 * from invoke.
1399 */
1400 public void testQuietlyComplete() {
1401 RecursiveAction a = new CheckedRecursiveAction() {
1402 protected void realCompute() {
1403 AsyncFib f = new AsyncFib(8);
1404 f.quietlyComplete();
1405 assertEquals(8, f.number);
1406 assertTrue(f.isDone());
1407 assertFalse(f.isCancelled());
1408 assertTrue(f.isCompletedNormally());
1409 assertFalse(f.isCompletedAbnormally());
1410 assertNull(f.getException());
1411 }};
1412 testInvokeOnPool(mainPool(), a);
1413 }
1414
1415 }