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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
import java.util.concurrent.ExecutionException; |
7 |
|
import java.util.concurrent.CancellationException; |
10 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
11 |
|
import java.util.concurrent.RecursiveAction; |
12 |
|
import java.util.concurrent.TimeUnit; |
13 |
+ |
import java.util.concurrent.TimeoutException; |
14 |
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
15 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
17 |
|
import java.util.HashSet; |
18 |
|
import junit.framework.*; |
19 |
|
|
28 |
|
} |
29 |
|
|
30 |
|
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1 |
31 |
< |
static final int mainPoolSize = |
31 |
> |
static final int mainPoolSize = |
32 |
|
Math.max(2, Runtime.getRuntime().availableProcessors()); |
33 |
|
|
34 |
|
private static ForkJoinPool mainPool() { |
52 |
|
assertFalse(a.isCompletedAbnormally()); |
53 |
|
assertFalse(a.isCancelled()); |
54 |
|
assertNull(a.getException()); |
55 |
+ |
assertNull(a.getRawResult()); |
56 |
|
|
57 |
|
assertNull(pool.invoke(a)); |
58 |
|
|
61 |
|
assertFalse(a.isCompletedAbnormally()); |
62 |
|
assertFalse(a.isCancelled()); |
63 |
|
assertNull(a.getException()); |
64 |
+ |
assertNull(a.getRawResult()); |
65 |
|
} finally { |
66 |
|
joinPool(pool); |
67 |
|
} |
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 |
+ |
try { |
79 |
+ |
a.get(0L, SECONDS); |
80 |
+ |
shouldThrow(); |
81 |
+ |
} catch (TimeoutException success) { |
82 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
<T> void checkCompletedNormally(ForkJoinTask<T> a) { |
86 |
+ |
checkCompletedNormally(a, null); |
87 |
+ |
} |
88 |
+ |
|
89 |
+ |
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) { |
90 |
+ |
assertTrue(a.isDone()); |
91 |
+ |
assertFalse(a.isCancelled()); |
92 |
+ |
assertTrue(a.isCompletedNormally()); |
93 |
+ |
assertFalse(a.isCompletedAbnormally()); |
94 |
+ |
assertNull(a.getException()); |
95 |
+ |
assertSame(expected, a.getRawResult()); |
96 |
+ |
|
97 |
+ |
{ |
98 |
+ |
Thread.currentThread().interrupt(); |
99 |
+ |
long t0 = System.nanoTime(); |
100 |
+ |
assertSame(expected, a.join()); |
101 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
102 |
+ |
Thread.interrupted(); |
103 |
+ |
} |
104 |
+ |
|
105 |
+ |
{ |
106 |
+ |
Thread.currentThread().interrupt(); |
107 |
+ |
long t0 = System.nanoTime(); |
108 |
+ |
a.quietlyJoin(); // should be no-op |
109 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
110 |
+ |
Thread.interrupted(); |
111 |
+ |
} |
112 |
+ |
|
113 |
+ |
assertFalse(a.cancel(false)); |
114 |
+ |
assertFalse(a.cancel(true)); |
115 |
+ |
try { |
116 |
+ |
assertSame(expected, a.get()); |
117 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
+ |
try { |
119 |
+ |
assertSame(expected, a.get(5L, SECONDS)); |
120 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
+ |
} |
122 |
+ |
|
123 |
+ |
void checkCancelled(ForkJoinTask a) { |
124 |
+ |
assertTrue(a.isDone()); |
125 |
+ |
assertTrue(a.isCancelled()); |
126 |
+ |
assertFalse(a.isCompletedNormally()); |
127 |
+ |
assertTrue(a.isCompletedAbnormally()); |
128 |
+ |
assertTrue(a.getException() instanceof CancellationException); |
129 |
+ |
assertNull(a.getRawResult()); |
130 |
+ |
assertTrue(a.cancel(false)); |
131 |
+ |
assertTrue(a.cancel(true)); |
132 |
+ |
|
133 |
+ |
try { |
134 |
+ |
Thread.currentThread().interrupt(); |
135 |
+ |
a.join(); |
136 |
+ |
shouldThrow(); |
137 |
+ |
} catch (CancellationException success) { |
138 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
139 |
+ |
Thread.interrupted(); |
140 |
+ |
|
141 |
+ |
{ |
142 |
+ |
long t0 = System.nanoTime(); |
143 |
+ |
a.quietlyJoin(); // should be no-op |
144 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
try { |
148 |
+ |
a.get(); |
149 |
+ |
shouldThrow(); |
150 |
+ |
} catch (CancellationException success) { |
151 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
152 |
+ |
|
153 |
+ |
try { |
154 |
+ |
a.get(5L, SECONDS); |
155 |
+ |
shouldThrow(); |
156 |
+ |
} catch (CancellationException success) { |
157 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
161 |
+ |
assertTrue(a.isDone()); |
162 |
+ |
assertFalse(a.isCancelled()); |
163 |
+ |
assertFalse(a.isCompletedNormally()); |
164 |
+ |
assertTrue(a.isCompletedAbnormally()); |
165 |
+ |
assertSame(t.getClass(), a.getException().getClass()); |
166 |
+ |
assertNull(a.getRawResult()); |
167 |
+ |
assertFalse(a.cancel(false)); |
168 |
+ |
assertFalse(a.cancel(true)); |
169 |
+ |
|
170 |
+ |
try { |
171 |
+ |
Thread.currentThread().interrupt(); |
172 |
+ |
a.join(); |
173 |
+ |
shouldThrow(); |
174 |
+ |
} catch (Throwable expected) { |
175 |
+ |
assertSame(t.getClass(), expected.getClass()); |
176 |
+ |
} |
177 |
+ |
Thread.interrupted(); |
178 |
+ |
|
179 |
+ |
{ |
180 |
+ |
long t0 = System.nanoTime(); |
181 |
+ |
a.quietlyJoin(); // should be no-op |
182 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
try { |
186 |
+ |
a.get(); |
187 |
+ |
shouldThrow(); |
188 |
+ |
} catch (ExecutionException success) { |
189 |
+ |
assertSame(t.getClass(), success.getCause().getClass()); |
190 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
+ |
|
192 |
+ |
try { |
193 |
+ |
a.get(5L, SECONDS); |
194 |
+ |
shouldThrow(); |
195 |
+ |
} catch (ExecutionException success) { |
196 |
+ |
assertSame(t.getClass(), success.getCause().getClass()); |
197 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
198 |
+ |
} |
199 |
+ |
|
200 |
|
/* |
201 |
|
* Testing coverage notes: |
202 |
|
* |
205 |
|
* differently than supplied Recursive forms. |
206 |
|
*/ |
207 |
|
|
208 |
< |
static final class FJException extends RuntimeException { |
208 |
> |
public static final class FJException extends RuntimeException { |
209 |
|
FJException() { super(); } |
210 |
|
} |
211 |
|
|
349 |
|
} |
350 |
|
} |
351 |
|
|
218 |
– |
|
352 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
353 |
|
int number; |
354 |
|
public FailingAsyncFib(int n) { |
380 |
|
/** |
381 |
|
* invoke returns when task completes normally. |
382 |
|
* isCompletedAbnormally and isCancelled return false for normally |
383 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
383 |
> |
* completed tasks; getRawResult returns null. |
384 |
|
*/ |
385 |
|
public void testInvoke() { |
386 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
388 |
|
AsyncFib f = new AsyncFib(8); |
389 |
|
assertNull(f.invoke()); |
390 |
|
assertEquals(21, f.number); |
391 |
< |
assertTrue(f.isDone()); |
259 |
< |
assertFalse(f.isCancelled()); |
260 |
< |
assertFalse(f.isCompletedAbnormally()); |
261 |
< |
assertNull(f.getRawResult()); |
391 |
> |
checkCompletedNormally(f); |
392 |
|
}}; |
393 |
|
testInvokeOnPool(mainPool(), a); |
394 |
|
} |
404 |
|
AsyncFib f = new AsyncFib(8); |
405 |
|
f.quietlyInvoke(); |
406 |
|
assertEquals(21, f.number); |
407 |
< |
assertTrue(f.isDone()); |
278 |
< |
assertFalse(f.isCancelled()); |
279 |
< |
assertFalse(f.isCompletedAbnormally()); |
280 |
< |
assertNull(f.getRawResult()); |
407 |
> |
checkCompletedNormally(f); |
408 |
|
}}; |
409 |
|
testInvokeOnPool(mainPool(), a); |
410 |
|
} |
419 |
|
assertSame(f, f.fork()); |
420 |
|
assertNull(f.join()); |
421 |
|
assertEquals(21, f.number); |
422 |
< |
assertTrue(f.isDone()); |
296 |
< |
assertNull(f.getRawResult()); |
422 |
> |
checkCompletedNormally(f); |
423 |
|
}}; |
424 |
|
testInvokeOnPool(mainPool(), a); |
425 |
|
} |
434 |
|
assertSame(f, f.fork()); |
435 |
|
assertNull(f.get()); |
436 |
|
assertEquals(21, f.number); |
437 |
< |
assertTrue(f.isDone()); |
437 |
> |
checkCompletedNormally(f); |
438 |
|
}}; |
439 |
|
testInvokeOnPool(mainPool(), a); |
440 |
|
} |
449 |
|
assertSame(f, f.fork()); |
450 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
451 |
|
assertEquals(21, f.number); |
452 |
< |
assertTrue(f.isDone()); |
452 |
> |
checkCompletedNormally(f); |
453 |
|
}}; |
454 |
|
testInvokeOnPool(mainPool(), a); |
455 |
|
} |
480 |
|
assertSame(f, f.fork()); |
481 |
|
f.quietlyJoin(); |
482 |
|
assertEquals(21, f.number); |
483 |
< |
assertTrue(f.isDone()); |
483 |
> |
checkCompletedNormally(f); |
484 |
|
}}; |
485 |
|
testInvokeOnPool(mainPool(), a); |
486 |
|
} |
487 |
|
|
362 |
– |
|
488 |
|
/** |
489 |
|
* helpQuiesce returns when tasks are complete. |
490 |
|
* getQueuedTaskCount returns 0 when quiescent |
494 |
|
public void realCompute() { |
495 |
|
AsyncFib f = new AsyncFib(8); |
496 |
|
assertSame(f, f.fork()); |
497 |
< |
f.helpQuiesce(); |
497 |
> |
helpQuiesce(); |
498 |
|
assertEquals(21, f.number); |
374 |
– |
assertTrue(f.isDone()); |
499 |
|
assertEquals(0, getQueuedTaskCount()); |
500 |
+ |
checkCompletedNormally(f); |
501 |
|
}}; |
502 |
|
testInvokeOnPool(mainPool(), a); |
503 |
|
} |
504 |
|
|
380 |
– |
|
505 |
|
/** |
506 |
|
* invoke task throws exception when task completes abnormally |
507 |
|
*/ |
512 |
|
try { |
513 |
|
f.invoke(); |
514 |
|
shouldThrow(); |
515 |
< |
} catch (FJException success) {} |
515 |
> |
} catch (FJException success) { |
516 |
> |
checkCompletedAbnormally(f, success); |
517 |
> |
} |
518 |
|
}}; |
519 |
|
testInvokeOnPool(mainPool(), a); |
520 |
|
} |
527 |
|
public void realCompute() { |
528 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
529 |
|
f.quietlyInvoke(); |
530 |
< |
assertTrue(f.isDone()); |
530 |
> |
assertTrue(f.getException() instanceof FJException); |
531 |
> |
checkCompletedAbnormally(f, f.getException()); |
532 |
|
}}; |
533 |
|
testInvokeOnPool(mainPool(), a); |
534 |
|
} |
544 |
|
try { |
545 |
|
f.join(); |
546 |
|
shouldThrow(); |
547 |
< |
} catch (FJException success) {} |
547 |
> |
} catch (FJException success) { |
548 |
> |
checkCompletedAbnormally(f, success); |
549 |
> |
} |
550 |
|
}}; |
551 |
|
testInvokeOnPool(mainPool(), a); |
552 |
|
} |
565 |
|
} catch (ExecutionException success) { |
566 |
|
Throwable cause = success.getCause(); |
567 |
|
assertTrue(cause instanceof FJException); |
568 |
< |
assertTrue(f.isDone()); |
440 |
< |
assertTrue(f.isCompletedAbnormally()); |
441 |
< |
assertSame(cause, f.getException()); |
568 |
> |
checkCompletedAbnormally(f, cause); |
569 |
|
} |
570 |
|
}}; |
571 |
|
testInvokeOnPool(mainPool(), a); |
585 |
|
} catch (ExecutionException success) { |
586 |
|
Throwable cause = success.getCause(); |
587 |
|
assertTrue(cause instanceof FJException); |
588 |
< |
assertTrue(f.isDone()); |
462 |
< |
assertTrue(f.isCompletedAbnormally()); |
463 |
< |
assertSame(cause, f.getException()); |
588 |
> |
checkCompletedAbnormally(f, cause); |
589 |
|
} |
590 |
|
}}; |
591 |
|
testInvokeOnPool(mainPool(), a); |
600 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
601 |
|
assertSame(f, f.fork()); |
602 |
|
f.quietlyJoin(); |
478 |
– |
assertTrue(f.isDone()); |
479 |
– |
assertTrue(f.isCompletedAbnormally()); |
603 |
|
assertTrue(f.getException() instanceof FJException); |
604 |
+ |
checkCompletedAbnormally(f, f.getException()); |
605 |
|
}}; |
606 |
|
testInvokeOnPool(mainPool(), a); |
607 |
|
} |
618 |
|
f.invoke(); |
619 |
|
shouldThrow(); |
620 |
|
} catch (CancellationException success) { |
621 |
< |
assertTrue(f.isDone()); |
498 |
< |
assertTrue(f.isCancelled()); |
499 |
< |
assertTrue(f.isCompletedAbnormally()); |
500 |
< |
assertTrue(f.getException() instanceof CancellationException); |
621 |
> |
checkCancelled(f); |
622 |
|
} |
623 |
|
}}; |
624 |
|
testInvokeOnPool(mainPool(), a); |
637 |
|
f.join(); |
638 |
|
shouldThrow(); |
639 |
|
} catch (CancellationException success) { |
640 |
< |
assertTrue(f.isDone()); |
520 |
< |
assertTrue(f.isCancelled()); |
521 |
< |
assertTrue(f.isCompletedAbnormally()); |
522 |
< |
assertTrue(f.getException() instanceof CancellationException); |
640 |
> |
checkCancelled(f); |
641 |
|
} |
642 |
|
}}; |
643 |
|
testInvokeOnPool(mainPool(), a); |
656 |
|
f.get(); |
657 |
|
shouldThrow(); |
658 |
|
} catch (CancellationException success) { |
659 |
< |
assertTrue(f.isDone()); |
542 |
< |
assertTrue(f.isCancelled()); |
543 |
< |
assertTrue(f.isCompletedAbnormally()); |
544 |
< |
assertTrue(f.getException() instanceof CancellationException); |
659 |
> |
checkCancelled(f); |
660 |
|
} |
661 |
|
}}; |
662 |
|
testInvokeOnPool(mainPool(), a); |
675 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
676 |
|
shouldThrow(); |
677 |
|
} catch (CancellationException success) { |
678 |
< |
assertTrue(f.isDone()); |
564 |
< |
assertTrue(f.isCancelled()); |
565 |
< |
assertTrue(f.isCompletedAbnormally()); |
566 |
< |
assertTrue(f.getException() instanceof CancellationException); |
678 |
> |
checkCancelled(f); |
679 |
|
} |
680 |
|
}}; |
681 |
|
testInvokeOnPool(mainPool(), a); |
691 |
|
assertTrue(f.cancel(true)); |
692 |
|
assertSame(f, f.fork()); |
693 |
|
f.quietlyJoin(); |
694 |
< |
assertTrue(f.isDone()); |
583 |
< |
assertTrue(f.isCompletedAbnormally()); |
584 |
< |
assertTrue(f.isCancelled()); |
585 |
< |
assertTrue(f.getException() instanceof CancellationException); |
694 |
> |
checkCancelled(f); |
695 |
|
}}; |
696 |
|
testInvokeOnPool(mainPool(), a); |
697 |
|
} |
736 |
|
public void testInForkJoinPool2() { |
737 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
738 |
|
public void realCompute() { |
739 |
< |
assertTrue(!inForkJoinPool()); |
739 |
> |
assertFalse(inForkJoinPool()); |
740 |
|
}}; |
741 |
|
assertNull(a.invoke()); |
742 |
|
} |
748 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
749 |
|
public void realCompute() { |
750 |
|
setRawResult(null); |
751 |
+ |
assertNull(getRawResult()); |
752 |
|
}}; |
753 |
|
assertNull(a.invoke()); |
754 |
|
} |
764 |
|
try { |
765 |
|
f.invoke(); |
766 |
|
shouldThrow(); |
767 |
< |
} catch (FJException success) {} |
767 |
> |
} catch (FJException success) { |
768 |
> |
checkCompletedAbnormally(f, success); |
769 |
> |
} |
770 |
|
}}; |
771 |
|
testInvokeOnPool(mainPool(), a); |
772 |
|
} |
780 |
|
AsyncFib f = new AsyncFib(8); |
781 |
|
AsyncFib g = new AsyncFib(9); |
782 |
|
invokeAll(f, g); |
671 |
– |
assertTrue(f.isDone()); |
783 |
|
assertEquals(21, f.number); |
673 |
– |
assertTrue(g.isDone()); |
784 |
|
assertEquals(34, g.number); |
785 |
+ |
checkCompletedNormally(f); |
786 |
+ |
checkCompletedNormally(g); |
787 |
|
}}; |
788 |
|
testInvokeOnPool(mainPool(), a); |
789 |
|
} |
796 |
|
public void realCompute() { |
797 |
|
AsyncFib f = new AsyncFib(8); |
798 |
|
invokeAll(f); |
799 |
< |
assertTrue(f.isDone()); |
799 |
> |
checkCompletedNormally(f); |
800 |
|
assertEquals(21, f.number); |
801 |
|
}}; |
802 |
|
testInvokeOnPool(mainPool(), a); |
812 |
|
AsyncFib g = new AsyncFib(9); |
813 |
|
AsyncFib h = new AsyncFib(7); |
814 |
|
invokeAll(f, g, h); |
703 |
– |
assertTrue(f.isDone()); |
815 |
|
assertEquals(21, f.number); |
705 |
– |
assertTrue(g.isDone()); |
816 |
|
assertEquals(34, g.number); |
707 |
– |
assertTrue(h.isDone()); |
817 |
|
assertEquals(13, h.number); |
818 |
+ |
checkCompletedNormally(f); |
819 |
+ |
checkCompletedNormally(g); |
820 |
+ |
checkCompletedNormally(h); |
821 |
|
}}; |
822 |
|
testInvokeOnPool(mainPool(), a); |
823 |
|
} |
836 |
|
set.add(g); |
837 |
|
set.add(h); |
838 |
|
invokeAll(set); |
727 |
– |
assertTrue(f.isDone()); |
839 |
|
assertEquals(21, f.number); |
729 |
– |
assertTrue(g.isDone()); |
840 |
|
assertEquals(34, g.number); |
731 |
– |
assertTrue(h.isDone()); |
841 |
|
assertEquals(13, h.number); |
842 |
+ |
checkCompletedNormally(f); |
843 |
+ |
checkCompletedNormally(g); |
844 |
+ |
checkCompletedNormally(h); |
845 |
|
}}; |
846 |
|
testInvokeOnPool(mainPool(), a); |
847 |
|
} |
848 |
|
|
737 |
– |
|
849 |
|
/** |
850 |
|
* invokeAll(tasks) with any null task throws NPE |
851 |
|
*/ |
874 |
|
try { |
875 |
|
invokeAll(f, g); |
876 |
|
shouldThrow(); |
877 |
< |
} catch (FJException success) {} |
877 |
> |
} catch (FJException success) { |
878 |
> |
checkCompletedAbnormally(g, success); |
879 |
> |
} |
880 |
|
}}; |
881 |
|
testInvokeOnPool(mainPool(), a); |
882 |
|
} |
891 |
|
try { |
892 |
|
invokeAll(g); |
893 |
|
shouldThrow(); |
894 |
< |
} catch (FJException success) {} |
894 |
> |
} catch (FJException success) { |
895 |
> |
checkCompletedAbnormally(g, success); |
896 |
> |
} |
897 |
|
}}; |
898 |
|
testInvokeOnPool(mainPool(), a); |
899 |
|
} |
910 |
|
try { |
911 |
|
invokeAll(f, g, h); |
912 |
|
shouldThrow(); |
913 |
< |
} catch (FJException success) {} |
913 |
> |
} catch (FJException success) { |
914 |
> |
checkCompletedAbnormally(g, success); |
915 |
> |
} |
916 |
|
}}; |
917 |
|
testInvokeOnPool(mainPool(), a); |
918 |
|
} |
933 |
|
try { |
934 |
|
invokeAll(set); |
935 |
|
shouldThrow(); |
936 |
< |
} catch (FJException success) {} |
936 |
> |
} catch (FJException success) { |
937 |
> |
checkCompletedAbnormally(f, success); |
938 |
> |
} |
939 |
|
}}; |
940 |
|
testInvokeOnPool(mainPool(), a); |
941 |
|
} |
953 |
|
assertSame(f, f.fork()); |
954 |
|
assertTrue(f.tryUnfork()); |
955 |
|
helpQuiesce(); |
956 |
< |
assertFalse(f.isDone()); |
957 |
< |
assertTrue(g.isDone()); |
956 |
> |
checkNotDone(f); |
957 |
> |
checkCompletedNormally(g); |
958 |
|
}}; |
959 |
|
testInvokeOnPool(singletonPool(), a); |
960 |
|
} |
974 |
|
assertSame(f, f.fork()); |
975 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
976 |
|
helpQuiesce(); |
977 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
978 |
+ |
checkCompletedNormally(f); |
979 |
+ |
checkCompletedNormally(g); |
980 |
+ |
checkCompletedNormally(h); |
981 |
|
}}; |
982 |
|
testInvokeOnPool(singletonPool(), a); |
983 |
|
} |
994 |
|
assertSame(f, f.fork()); |
995 |
|
assertSame(f, peekNextLocalTask()); |
996 |
|
assertNull(f.join()); |
997 |
< |
assertTrue(f.isDone()); |
997 |
> |
checkCompletedNormally(f); |
998 |
|
helpQuiesce(); |
999 |
+ |
checkCompletedNormally(g); |
1000 |
|
}}; |
1001 |
|
testInvokeOnPool(singletonPool(), a); |
1002 |
|
} |
1003 |
|
|
1004 |
|
/** |
1005 |
< |
* pollNextLocalTask returns most recent unexecuted task |
1006 |
< |
* without executing it |
1005 |
> |
* pollNextLocalTask returns most recent unexecuted task without |
1006 |
> |
* executing it |
1007 |
|
*/ |
1008 |
|
public void testPollNextLocalTask() { |
1009 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1014 |
|
assertSame(f, f.fork()); |
1015 |
|
assertSame(f, pollNextLocalTask()); |
1016 |
|
helpQuiesce(); |
1017 |
< |
assertFalse(f.isDone()); |
1017 |
> |
checkNotDone(f); |
1018 |
> |
assertEquals(34, g.number); |
1019 |
> |
checkCompletedNormally(g); |
1020 |
|
}}; |
1021 |
|
testInvokeOnPool(singletonPool(), a); |
1022 |
|
} |
1023 |
|
|
1024 |
|
/** |
1025 |
< |
* pollTask returns an unexecuted task |
900 |
< |
* without executing it |
1025 |
> |
* pollTask returns an unexecuted task without executing it |
1026 |
|
*/ |
1027 |
|
public void testPollTask() { |
1028 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1033 |
|
assertSame(f, f.fork()); |
1034 |
|
assertSame(f, pollTask()); |
1035 |
|
helpQuiesce(); |
1036 |
< |
assertFalse(f.isDone()); |
1037 |
< |
assertTrue(g.isDone()); |
1036 |
> |
checkNotDone(f); |
1037 |
> |
checkCompletedNormally(g); |
1038 |
|
}}; |
1039 |
|
testInvokeOnPool(singletonPool(), a); |
1040 |
|
} |
1052 |
|
assertSame(g, peekNextLocalTask()); |
1053 |
|
assertNull(f.join()); |
1054 |
|
helpQuiesce(); |
1055 |
< |
assertTrue(f.isDone()); |
1055 |
> |
checkCompletedNormally(f); |
1056 |
> |
assertEquals(34, g.number); |
1057 |
> |
checkCompletedNormally(g); |
1058 |
|
}}; |
1059 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1060 |
|
} |
1061 |
|
|
1062 |
|
/** |
1063 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1064 |
< |
* without executing it, in async mode |
1063 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1064 |
> |
* executing it, in async mode |
1065 |
|
*/ |
1066 |
|
public void testPollNextLocalTaskAsync() { |
1067 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1072 |
|
assertSame(f, f.fork()); |
1073 |
|
assertSame(g, pollNextLocalTask()); |
1074 |
|
helpQuiesce(); |
1075 |
< |
assertTrue(f.isDone()); |
1076 |
< |
assertFalse(g.isDone()); |
1075 |
> |
assertEquals(21, f.number); |
1076 |
> |
checkCompletedNormally(f); |
1077 |
> |
checkNotDone(g); |
1078 |
|
}}; |
1079 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1080 |
|
} |
1081 |
|
|
1082 |
|
/** |
1083 |
< |
* pollTask returns an unexecuted task |
1084 |
< |
* without executing it, in async mode |
1083 |
> |
* pollTask returns an unexecuted task without executing it, in |
1084 |
> |
* async mode |
1085 |
|
*/ |
1086 |
|
public void testPollTaskAsync() { |
1087 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1092 |
|
assertSame(f, f.fork()); |
1093 |
|
assertSame(g, pollTask()); |
1094 |
|
helpQuiesce(); |
1095 |
< |
assertTrue(f.isDone()); |
1096 |
< |
assertFalse(g.isDone()); |
1095 |
> |
assertEquals(21, f.number); |
1096 |
> |
checkCompletedNormally(f); |
1097 |
> |
checkNotDone(g); |
1098 |
|
}}; |
1099 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1100 |
|
} |
1104 |
|
/** |
1105 |
|
* invoke returns when task completes normally. |
1106 |
|
* isCompletedAbnormally and isCancelled return false for normally |
1107 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
1107 |
> |
* completed tasks; getRawResult returns null. |
1108 |
|
*/ |
1109 |
|
public void testInvokeSingleton() { |
1110 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1112 |
|
AsyncFib f = new AsyncFib(8); |
1113 |
|
assertNull(f.invoke()); |
1114 |
|
assertEquals(21, f.number); |
1115 |
< |
assertTrue(f.isDone()); |
987 |
< |
assertFalse(f.isCancelled()); |
988 |
< |
assertFalse(f.isCompletedAbnormally()); |
989 |
< |
assertNull(f.getRawResult()); |
1115 |
> |
checkCompletedNormally(f); |
1116 |
|
}}; |
1117 |
|
testInvokeOnPool(singletonPool(), a); |
1118 |
|
} |
1128 |
|
AsyncFib f = new AsyncFib(8); |
1129 |
|
f.quietlyInvoke(); |
1130 |
|
assertEquals(21, f.number); |
1131 |
< |
assertTrue(f.isDone()); |
1006 |
< |
assertFalse(f.isCancelled()); |
1007 |
< |
assertFalse(f.isCompletedAbnormally()); |
1008 |
< |
assertNull(f.getRawResult()); |
1131 |
> |
checkCompletedNormally(f); |
1132 |
|
}}; |
1133 |
|
testInvokeOnPool(singletonPool(), a); |
1134 |
|
} |
1143 |
|
assertSame(f, f.fork()); |
1144 |
|
assertNull(f.join()); |
1145 |
|
assertEquals(21, f.number); |
1146 |
< |
assertTrue(f.isDone()); |
1024 |
< |
assertNull(f.getRawResult()); |
1146 |
> |
checkCompletedNormally(f); |
1147 |
|
}}; |
1148 |
|
testInvokeOnPool(singletonPool(), a); |
1149 |
|
} |
1158 |
|
assertSame(f, f.fork()); |
1159 |
|
assertNull(f.get()); |
1160 |
|
assertEquals(21, f.number); |
1161 |
< |
assertTrue(f.isDone()); |
1161 |
> |
checkCompletedNormally(f); |
1162 |
|
}}; |
1163 |
|
testInvokeOnPool(singletonPool(), a); |
1164 |
|
} |
1173 |
|
assertSame(f, f.fork()); |
1174 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1175 |
|
assertEquals(21, f.number); |
1176 |
< |
assertTrue(f.isDone()); |
1176 |
> |
checkCompletedNormally(f); |
1177 |
|
}}; |
1178 |
|
testInvokeOnPool(singletonPool(), a); |
1179 |
|
} |
1204 |
|
assertSame(f, f.fork()); |
1205 |
|
f.quietlyJoin(); |
1206 |
|
assertEquals(21, f.number); |
1207 |
< |
assertTrue(f.isDone()); |
1207 |
> |
checkCompletedNormally(f); |
1208 |
|
}}; |
1209 |
|
testInvokeOnPool(singletonPool(), a); |
1210 |
|
} |
1211 |
|
|
1090 |
– |
|
1212 |
|
/** |
1213 |
|
* helpQuiesce returns when tasks are complete. |
1214 |
|
* getQueuedTaskCount returns 0 when quiescent |
1218 |
|
public void realCompute() { |
1219 |
|
AsyncFib f = new AsyncFib(8); |
1220 |
|
assertSame(f, f.fork()); |
1221 |
< |
f.helpQuiesce(); |
1101 |
< |
assertEquals(21, f.number); |
1102 |
< |
assertTrue(f.isDone()); |
1221 |
> |
helpQuiesce(); |
1222 |
|
assertEquals(0, getQueuedTaskCount()); |
1223 |
+ |
assertEquals(21, f.number); |
1224 |
+ |
checkCompletedNormally(f); |
1225 |
|
}}; |
1226 |
|
testInvokeOnPool(singletonPool(), a); |
1227 |
|
} |
1228 |
|
|
1108 |
– |
|
1229 |
|
/** |
1230 |
|
* invoke task throws exception when task completes abnormally |
1231 |
|
*/ |
1236 |
|
try { |
1237 |
|
f.invoke(); |
1238 |
|
shouldThrow(); |
1239 |
< |
} catch (FJException success) {} |
1239 |
> |
} catch (FJException success) { |
1240 |
> |
checkCompletedAbnormally(f, success); |
1241 |
> |
} |
1242 |
|
}}; |
1243 |
|
testInvokeOnPool(singletonPool(), a); |
1244 |
|
} |
1251 |
|
public void realCompute() { |
1252 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1253 |
|
f.quietlyInvoke(); |
1254 |
< |
assertTrue(f.isDone()); |
1254 |
> |
assertTrue(f.getException() instanceof FJException); |
1255 |
> |
checkCompletedAbnormally(f, f.getException()); |
1256 |
|
}}; |
1257 |
|
testInvokeOnPool(singletonPool(), a); |
1258 |
|
} |
1268 |
|
try { |
1269 |
|
f.join(); |
1270 |
|
shouldThrow(); |
1271 |
< |
} catch (FJException success) {} |
1271 |
> |
} catch (FJException success) { |
1272 |
> |
checkCompletedAbnormally(f, success); |
1273 |
> |
} |
1274 |
|
}}; |
1275 |
|
testInvokeOnPool(singletonPool(), a); |
1276 |
|
} |
1289 |
|
} catch (ExecutionException success) { |
1290 |
|
Throwable cause = success.getCause(); |
1291 |
|
assertTrue(cause instanceof FJException); |
1292 |
< |
assertTrue(f.isDone()); |
1168 |
< |
assertTrue(f.isCompletedAbnormally()); |
1169 |
< |
assertSame(cause, f.getException()); |
1292 |
> |
checkCompletedAbnormally(f, cause); |
1293 |
|
} |
1294 |
|
}}; |
1295 |
|
testInvokeOnPool(singletonPool(), a); |
1309 |
|
} catch (ExecutionException success) { |
1310 |
|
Throwable cause = success.getCause(); |
1311 |
|
assertTrue(cause instanceof FJException); |
1312 |
< |
assertTrue(f.isDone()); |
1190 |
< |
assertTrue(f.isCompletedAbnormally()); |
1191 |
< |
assertSame(cause, f.getException()); |
1312 |
> |
checkCompletedAbnormally(f, cause); |
1313 |
|
} |
1314 |
|
}}; |
1315 |
|
testInvokeOnPool(singletonPool(), a); |
1324 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1325 |
|
assertSame(f, f.fork()); |
1326 |
|
f.quietlyJoin(); |
1206 |
– |
assertTrue(f.isDone()); |
1207 |
– |
assertTrue(f.isCompletedAbnormally()); |
1327 |
|
assertTrue(f.getException() instanceof FJException); |
1328 |
+ |
checkCompletedAbnormally(f, f.getException()); |
1329 |
|
}}; |
1330 |
|
testInvokeOnPool(singletonPool(), a); |
1331 |
|
} |
1342 |
|
f.invoke(); |
1343 |
|
shouldThrow(); |
1344 |
|
} catch (CancellationException success) { |
1345 |
< |
assertTrue(f.isDone()); |
1226 |
< |
assertTrue(f.isCancelled()); |
1227 |
< |
assertTrue(f.isCompletedAbnormally()); |
1228 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1345 |
> |
checkCancelled(f); |
1346 |
|
} |
1347 |
|
}}; |
1348 |
|
testInvokeOnPool(singletonPool(), a); |
1361 |
|
f.join(); |
1362 |
|
shouldThrow(); |
1363 |
|
} catch (CancellationException success) { |
1364 |
< |
assertTrue(f.isDone()); |
1248 |
< |
assertTrue(f.isCancelled()); |
1249 |
< |
assertTrue(f.isCompletedAbnormally()); |
1250 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1364 |
> |
checkCancelled(f); |
1365 |
|
} |
1366 |
|
}}; |
1367 |
|
testInvokeOnPool(singletonPool(), a); |
1380 |
|
f.get(); |
1381 |
|
shouldThrow(); |
1382 |
|
} catch (CancellationException success) { |
1383 |
< |
assertTrue(f.isDone()); |
1270 |
< |
assertTrue(f.isCancelled()); |
1271 |
< |
assertTrue(f.isCompletedAbnormally()); |
1272 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1383 |
> |
checkCancelled(f); |
1384 |
|
} |
1385 |
|
}}; |
1386 |
|
testInvokeOnPool(singletonPool(), a); |
1399 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1400 |
|
shouldThrow(); |
1401 |
|
} catch (CancellationException success) { |
1402 |
< |
assertTrue(f.isDone()); |
1292 |
< |
assertTrue(f.isCancelled()); |
1293 |
< |
assertTrue(f.isCompletedAbnormally()); |
1294 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1402 |
> |
checkCancelled(f); |
1403 |
|
} |
1404 |
|
}}; |
1405 |
|
testInvokeOnPool(singletonPool(), a); |
1415 |
|
assertTrue(f.cancel(true)); |
1416 |
|
assertSame(f, f.fork()); |
1417 |
|
f.quietlyJoin(); |
1418 |
< |
assertTrue(f.isDone()); |
1311 |
< |
assertTrue(f.isCompletedAbnormally()); |
1312 |
< |
assertTrue(f.isCancelled()); |
1313 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1418 |
> |
checkCancelled(f); |
1419 |
|
}}; |
1420 |
|
testInvokeOnPool(singletonPool(), a); |
1421 |
|
} |
1431 |
|
try { |
1432 |
|
f.invoke(); |
1433 |
|
shouldThrow(); |
1434 |
< |
} catch (FJException success) {} |
1434 |
> |
} catch (FJException success) { |
1435 |
> |
checkCompletedAbnormally(f, success); |
1436 |
> |
} |
1437 |
|
}}; |
1438 |
|
testInvokeOnPool(singletonPool(), a); |
1439 |
|
} |
1447 |
|
AsyncFib f = new AsyncFib(8); |
1448 |
|
AsyncFib g = new AsyncFib(9); |
1449 |
|
invokeAll(f, g); |
1343 |
– |
assertTrue(f.isDone()); |
1450 |
|
assertEquals(21, f.number); |
1345 |
– |
assertTrue(g.isDone()); |
1451 |
|
assertEquals(34, g.number); |
1452 |
+ |
checkCompletedNormally(f); |
1453 |
+ |
checkCompletedNormally(g); |
1454 |
|
}}; |
1455 |
|
testInvokeOnPool(singletonPool(), a); |
1456 |
|
} |
1463 |
|
public void realCompute() { |
1464 |
|
AsyncFib f = new AsyncFib(8); |
1465 |
|
invokeAll(f); |
1466 |
< |
assertTrue(f.isDone()); |
1466 |
> |
checkCompletedNormally(f); |
1467 |
|
assertEquals(21, f.number); |
1468 |
|
}}; |
1469 |
|
testInvokeOnPool(singletonPool(), a); |
1479 |
|
AsyncFib g = new AsyncFib(9); |
1480 |
|
AsyncFib h = new AsyncFib(7); |
1481 |
|
invokeAll(f, g, h); |
1375 |
– |
assertTrue(f.isDone()); |
1482 |
|
assertEquals(21, f.number); |
1377 |
– |
assertTrue(g.isDone()); |
1483 |
|
assertEquals(34, g.number); |
1379 |
– |
assertTrue(h.isDone()); |
1484 |
|
assertEquals(13, h.number); |
1485 |
+ |
checkCompletedNormally(f); |
1486 |
+ |
checkCompletedNormally(g); |
1487 |
+ |
checkCompletedNormally(h); |
1488 |
|
}}; |
1489 |
|
testInvokeOnPool(singletonPool(), a); |
1490 |
|
} |
1503 |
|
set.add(g); |
1504 |
|
set.add(h); |
1505 |
|
invokeAll(set); |
1399 |
– |
assertTrue(f.isDone()); |
1506 |
|
assertEquals(21, f.number); |
1401 |
– |
assertTrue(g.isDone()); |
1507 |
|
assertEquals(34, g.number); |
1403 |
– |
assertTrue(h.isDone()); |
1508 |
|
assertEquals(13, h.number); |
1509 |
+ |
checkCompletedNormally(f); |
1510 |
+ |
checkCompletedNormally(g); |
1511 |
+ |
checkCompletedNormally(h); |
1512 |
|
}}; |
1513 |
|
testInvokeOnPool(singletonPool(), a); |
1514 |
|
} |
1515 |
|
|
1409 |
– |
|
1516 |
|
/** |
1517 |
|
* invokeAll(tasks) with any null task throws NPE |
1518 |
|
*/ |
1541 |
|
try { |
1542 |
|
invokeAll(f, g); |
1543 |
|
shouldThrow(); |
1544 |
< |
} catch (FJException success) {} |
1544 |
> |
} catch (FJException success) { |
1545 |
> |
checkCompletedAbnormally(g, success); |
1546 |
> |
} |
1547 |
|
}}; |
1548 |
|
testInvokeOnPool(singletonPool(), a); |
1549 |
|
} |
1558 |
|
try { |
1559 |
|
invokeAll(g); |
1560 |
|
shouldThrow(); |
1561 |
< |
} catch (FJException success) {} |
1561 |
> |
} catch (FJException success) { |
1562 |
> |
checkCompletedAbnormally(g, success); |
1563 |
> |
} |
1564 |
|
}}; |
1565 |
|
testInvokeOnPool(singletonPool(), a); |
1566 |
|
} |
1577 |
|
try { |
1578 |
|
invokeAll(f, g, h); |
1579 |
|
shouldThrow(); |
1580 |
< |
} catch (FJException success) {} |
1580 |
> |
} catch (FJException success) { |
1581 |
> |
checkCompletedAbnormally(g, success); |
1582 |
> |
} |
1583 |
|
}}; |
1584 |
|
testInvokeOnPool(singletonPool(), a); |
1585 |
|
} |
1600 |
|
try { |
1601 |
|
invokeAll(set); |
1602 |
|
shouldThrow(); |
1603 |
< |
} catch (FJException success) {} |
1603 |
> |
} catch (FJException success) { |
1604 |
> |
checkCompletedAbnormally(f, success); |
1605 |
> |
} |
1606 |
|
}}; |
1607 |
|
testInvokeOnPool(singletonPool(), a); |
1608 |
|
} |