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 |
|
|
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, a.getException()); |
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, expected); |
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, success.getCause()); |
190 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
+ |
|
192 |
+ |
try { |
193 |
+ |
a.get(5L, SECONDS); |
194 |
+ |
shouldThrow(); |
195 |
+ |
} catch (ExecutionException success) { |
196 |
+ |
assertSame(t, success.getCause()); |
197 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
198 |
+ |
} |
199 |
+ |
|
200 |
|
/* |
201 |
|
* Testing coverage notes: |
202 |
|
* |
381 |
|
/** |
382 |
|
* invoke returns when task completes normally. |
383 |
|
* isCompletedAbnormally and isCancelled return false for normally |
384 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
384 |
> |
* completed tasks; getRawResult returns null. |
385 |
|
*/ |
386 |
|
public void testInvoke() { |
387 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
389 |
|
AsyncFib f = new AsyncFib(8); |
390 |
|
assertNull(f.invoke()); |
391 |
|
assertEquals(21, f.number); |
392 |
< |
assertTrue(f.isDone()); |
259 |
< |
assertFalse(f.isCancelled()); |
260 |
< |
assertFalse(f.isCompletedAbnormally()); |
261 |
< |
assertNull(f.getRawResult()); |
392 |
> |
checkCompletedNormally(f); |
393 |
|
}}; |
394 |
|
testInvokeOnPool(mainPool(), a); |
395 |
|
} |
405 |
|
AsyncFib f = new AsyncFib(8); |
406 |
|
f.quietlyInvoke(); |
407 |
|
assertEquals(21, f.number); |
408 |
< |
assertTrue(f.isDone()); |
278 |
< |
assertFalse(f.isCancelled()); |
279 |
< |
assertFalse(f.isCompletedAbnormally()); |
280 |
< |
assertNull(f.getRawResult()); |
408 |
> |
checkCompletedNormally(f); |
409 |
|
}}; |
410 |
|
testInvokeOnPool(mainPool(), a); |
411 |
|
} |
420 |
|
assertSame(f, f.fork()); |
421 |
|
assertNull(f.join()); |
422 |
|
assertEquals(21, f.number); |
423 |
< |
assertTrue(f.isDone()); |
296 |
< |
assertNull(f.getRawResult()); |
423 |
> |
checkCompletedNormally(f); |
424 |
|
}}; |
425 |
|
testInvokeOnPool(mainPool(), a); |
426 |
|
} |
435 |
|
assertSame(f, f.fork()); |
436 |
|
assertNull(f.get()); |
437 |
|
assertEquals(21, f.number); |
438 |
< |
assertTrue(f.isDone()); |
438 |
> |
checkCompletedNormally(f); |
439 |
|
}}; |
440 |
|
testInvokeOnPool(mainPool(), a); |
441 |
|
} |
450 |
|
assertSame(f, f.fork()); |
451 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
452 |
|
assertEquals(21, f.number); |
453 |
< |
assertTrue(f.isDone()); |
453 |
> |
checkCompletedNormally(f); |
454 |
|
}}; |
455 |
|
testInvokeOnPool(mainPool(), a); |
456 |
|
} |
481 |
|
assertSame(f, f.fork()); |
482 |
|
f.quietlyJoin(); |
483 |
|
assertEquals(21, f.number); |
484 |
< |
assertTrue(f.isDone()); |
484 |
> |
checkCompletedNormally(f); |
485 |
|
}}; |
486 |
|
testInvokeOnPool(mainPool(), a); |
487 |
|
} |
498 |
|
assertSame(f, f.fork()); |
499 |
|
f.helpQuiesce(); |
500 |
|
assertEquals(21, f.number); |
374 |
– |
assertTrue(f.isDone()); |
501 |
|
assertEquals(0, getQueuedTaskCount()); |
502 |
+ |
checkCompletedNormally(f); |
503 |
|
}}; |
504 |
|
testInvokeOnPool(mainPool(), a); |
505 |
|
} |
515 |
|
try { |
516 |
|
f.invoke(); |
517 |
|
shouldThrow(); |
518 |
< |
} catch (FJException success) {} |
518 |
> |
} catch (FJException success) { |
519 |
> |
checkCompletedAbnormally(f, success); |
520 |
> |
} |
521 |
|
}}; |
522 |
|
testInvokeOnPool(mainPool(), a); |
523 |
|
} |
530 |
|
public void realCompute() { |
531 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
532 |
|
f.quietlyInvoke(); |
533 |
< |
assertTrue(f.isDone()); |
533 |
> |
assertTrue(f.getException() instanceof FJException); |
534 |
> |
checkCompletedAbnormally(f, f.getException()); |
535 |
|
}}; |
536 |
|
testInvokeOnPool(mainPool(), a); |
537 |
|
} |
547 |
|
try { |
548 |
|
f.join(); |
549 |
|
shouldThrow(); |
550 |
< |
} catch (FJException success) {} |
550 |
> |
} catch (FJException success) { |
551 |
> |
checkCompletedAbnormally(f, success); |
552 |
> |
} |
553 |
|
}}; |
554 |
|
testInvokeOnPool(mainPool(), a); |
555 |
|
} |
568 |
|
} catch (ExecutionException success) { |
569 |
|
Throwable cause = success.getCause(); |
570 |
|
assertTrue(cause instanceof FJException); |
571 |
< |
assertTrue(f.isDone()); |
440 |
< |
assertTrue(f.isCompletedAbnormally()); |
441 |
< |
assertSame(cause, f.getException()); |
571 |
> |
checkCompletedAbnormally(f, cause); |
572 |
|
} |
573 |
|
}}; |
574 |
|
testInvokeOnPool(mainPool(), a); |
588 |
|
} catch (ExecutionException success) { |
589 |
|
Throwable cause = success.getCause(); |
590 |
|
assertTrue(cause instanceof FJException); |
591 |
< |
assertTrue(f.isDone()); |
462 |
< |
assertTrue(f.isCompletedAbnormally()); |
463 |
< |
assertSame(cause, f.getException()); |
591 |
> |
checkCompletedAbnormally(f, cause); |
592 |
|
} |
593 |
|
}}; |
594 |
|
testInvokeOnPool(mainPool(), a); |
603 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
604 |
|
assertSame(f, f.fork()); |
605 |
|
f.quietlyJoin(); |
478 |
– |
assertTrue(f.isDone()); |
479 |
– |
assertTrue(f.isCompletedAbnormally()); |
606 |
|
assertTrue(f.getException() instanceof FJException); |
607 |
+ |
checkCompletedAbnormally(f, f.getException()); |
608 |
|
}}; |
609 |
|
testInvokeOnPool(mainPool(), a); |
610 |
|
} |
621 |
|
f.invoke(); |
622 |
|
shouldThrow(); |
623 |
|
} catch (CancellationException success) { |
624 |
< |
assertTrue(f.isDone()); |
498 |
< |
assertTrue(f.isCancelled()); |
499 |
< |
assertTrue(f.isCompletedAbnormally()); |
500 |
< |
assertTrue(f.getException() instanceof CancellationException); |
624 |
> |
checkCancelled(f); |
625 |
|
} |
626 |
|
}}; |
627 |
|
testInvokeOnPool(mainPool(), a); |
640 |
|
f.join(); |
641 |
|
shouldThrow(); |
642 |
|
} catch (CancellationException success) { |
643 |
< |
assertTrue(f.isDone()); |
520 |
< |
assertTrue(f.isCancelled()); |
521 |
< |
assertTrue(f.isCompletedAbnormally()); |
522 |
< |
assertTrue(f.getException() instanceof CancellationException); |
643 |
> |
checkCancelled(f); |
644 |
|
} |
645 |
|
}}; |
646 |
|
testInvokeOnPool(mainPool(), a); |
659 |
|
f.get(); |
660 |
|
shouldThrow(); |
661 |
|
} catch (CancellationException success) { |
662 |
< |
assertTrue(f.isDone()); |
542 |
< |
assertTrue(f.isCancelled()); |
543 |
< |
assertTrue(f.isCompletedAbnormally()); |
544 |
< |
assertTrue(f.getException() instanceof CancellationException); |
662 |
> |
checkCancelled(f); |
663 |
|
} |
664 |
|
}}; |
665 |
|
testInvokeOnPool(mainPool(), a); |
678 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
679 |
|
shouldThrow(); |
680 |
|
} catch (CancellationException success) { |
681 |
< |
assertTrue(f.isDone()); |
564 |
< |
assertTrue(f.isCancelled()); |
565 |
< |
assertTrue(f.isCompletedAbnormally()); |
566 |
< |
assertTrue(f.getException() instanceof CancellationException); |
681 |
> |
checkCancelled(f); |
682 |
|
} |
683 |
|
}}; |
684 |
|
testInvokeOnPool(mainPool(), a); |
694 |
|
assertTrue(f.cancel(true)); |
695 |
|
assertSame(f, f.fork()); |
696 |
|
f.quietlyJoin(); |
697 |
< |
assertTrue(f.isDone()); |
583 |
< |
assertTrue(f.isCompletedAbnormally()); |
584 |
< |
assertTrue(f.isCancelled()); |
585 |
< |
assertTrue(f.getException() instanceof CancellationException); |
697 |
> |
checkCancelled(f); |
698 |
|
}}; |
699 |
|
testInvokeOnPool(mainPool(), a); |
700 |
|
} |
739 |
|
public void testInForkJoinPool2() { |
740 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
741 |
|
public void realCompute() { |
742 |
< |
assertTrue(!inForkJoinPool()); |
742 |
> |
assertFalse(inForkJoinPool()); |
743 |
|
}}; |
744 |
|
assertNull(a.invoke()); |
745 |
|
} |
751 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
752 |
|
public void realCompute() { |
753 |
|
setRawResult(null); |
754 |
+ |
assertNull(getRawResult()); |
755 |
|
}}; |
756 |
|
assertNull(a.invoke()); |
757 |
|
} |
767 |
|
try { |
768 |
|
f.invoke(); |
769 |
|
shouldThrow(); |
770 |
< |
} catch (FJException success) {} |
770 |
> |
} catch (FJException success) { |
771 |
> |
checkCompletedAbnormally(f, success); |
772 |
> |
} |
773 |
|
}}; |
774 |
|
testInvokeOnPool(mainPool(), a); |
775 |
|
} |
783 |
|
AsyncFib f = new AsyncFib(8); |
784 |
|
AsyncFib g = new AsyncFib(9); |
785 |
|
invokeAll(f, g); |
671 |
– |
assertTrue(f.isDone()); |
786 |
|
assertEquals(21, f.number); |
673 |
– |
assertTrue(g.isDone()); |
787 |
|
assertEquals(34, g.number); |
788 |
+ |
checkCompletedNormally(f); |
789 |
+ |
checkCompletedNormally(g); |
790 |
|
}}; |
791 |
|
testInvokeOnPool(mainPool(), a); |
792 |
|
} |
799 |
|
public void realCompute() { |
800 |
|
AsyncFib f = new AsyncFib(8); |
801 |
|
invokeAll(f); |
802 |
< |
assertTrue(f.isDone()); |
802 |
> |
checkCompletedNormally(f); |
803 |
|
assertEquals(21, f.number); |
804 |
|
}}; |
805 |
|
testInvokeOnPool(mainPool(), a); |
815 |
|
AsyncFib g = new AsyncFib(9); |
816 |
|
AsyncFib h = new AsyncFib(7); |
817 |
|
invokeAll(f, g, h); |
703 |
– |
assertTrue(f.isDone()); |
818 |
|
assertEquals(21, f.number); |
705 |
– |
assertTrue(g.isDone()); |
819 |
|
assertEquals(34, g.number); |
707 |
– |
assertTrue(h.isDone()); |
820 |
|
assertEquals(13, h.number); |
821 |
+ |
checkCompletedNormally(f); |
822 |
+ |
checkCompletedNormally(g); |
823 |
+ |
checkCompletedNormally(h); |
824 |
|
}}; |
825 |
|
testInvokeOnPool(mainPool(), a); |
826 |
|
} |
839 |
|
set.add(g); |
840 |
|
set.add(h); |
841 |
|
invokeAll(set); |
727 |
– |
assertTrue(f.isDone()); |
842 |
|
assertEquals(21, f.number); |
729 |
– |
assertTrue(g.isDone()); |
843 |
|
assertEquals(34, g.number); |
731 |
– |
assertTrue(h.isDone()); |
844 |
|
assertEquals(13, h.number); |
845 |
+ |
checkCompletedNormally(f); |
846 |
+ |
checkCompletedNormally(g); |
847 |
+ |
checkCompletedNormally(h); |
848 |
|
}}; |
849 |
|
testInvokeOnPool(mainPool(), a); |
850 |
|
} |
878 |
|
try { |
879 |
|
invokeAll(f, g); |
880 |
|
shouldThrow(); |
881 |
< |
} catch (FJException success) {} |
881 |
> |
} catch (FJException success) { |
882 |
> |
checkCompletedAbnormally(g, success); |
883 |
> |
} |
884 |
|
}}; |
885 |
|
testInvokeOnPool(mainPool(), a); |
886 |
|
} |
895 |
|
try { |
896 |
|
invokeAll(g); |
897 |
|
shouldThrow(); |
898 |
< |
} catch (FJException success) {} |
898 |
> |
} catch (FJException success) { |
899 |
> |
checkCompletedAbnormally(g, success); |
900 |
> |
} |
901 |
|
}}; |
902 |
|
testInvokeOnPool(mainPool(), a); |
903 |
|
} |
914 |
|
try { |
915 |
|
invokeAll(f, g, h); |
916 |
|
shouldThrow(); |
917 |
< |
} catch (FJException success) {} |
917 |
> |
} catch (FJException success) { |
918 |
> |
checkCompletedAbnormally(g, success); |
919 |
> |
} |
920 |
|
}}; |
921 |
|
testInvokeOnPool(mainPool(), a); |
922 |
|
} |
937 |
|
try { |
938 |
|
invokeAll(set); |
939 |
|
shouldThrow(); |
940 |
< |
} catch (FJException success) {} |
940 |
> |
} catch (FJException success) { |
941 |
> |
checkCompletedAbnormally(f, success); |
942 |
> |
} |
943 |
|
}}; |
944 |
|
testInvokeOnPool(mainPool(), a); |
945 |
|
} |
957 |
|
assertSame(f, f.fork()); |
958 |
|
assertTrue(f.tryUnfork()); |
959 |
|
helpQuiesce(); |
960 |
< |
assertFalse(f.isDone()); |
961 |
< |
assertTrue(g.isDone()); |
960 |
> |
checkNotDone(f); |
961 |
> |
checkCompletedNormally(g); |
962 |
|
}}; |
963 |
|
testInvokeOnPool(singletonPool(), a); |
964 |
|
} |
978 |
|
assertSame(f, f.fork()); |
979 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
980 |
|
helpQuiesce(); |
981 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
982 |
+ |
checkCompletedNormally(f); |
983 |
+ |
checkCompletedNormally(g); |
984 |
+ |
checkCompletedNormally(h); |
985 |
|
}}; |
986 |
|
testInvokeOnPool(singletonPool(), a); |
987 |
|
} |
998 |
|
assertSame(f, f.fork()); |
999 |
|
assertSame(f, peekNextLocalTask()); |
1000 |
|
assertNull(f.join()); |
1001 |
< |
assertTrue(f.isDone()); |
1001 |
> |
checkCompletedNormally(f); |
1002 |
|
helpQuiesce(); |
1003 |
+ |
checkCompletedNormally(g); |
1004 |
|
}}; |
1005 |
|
testInvokeOnPool(singletonPool(), a); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1009 |
< |
* pollNextLocalTask returns most recent unexecuted task |
1010 |
< |
* without executing it |
1009 |
> |
* pollNextLocalTask returns most recent unexecuted task without |
1010 |
> |
* executing it |
1011 |
|
*/ |
1012 |
|
public void testPollNextLocalTask() { |
1013 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1018 |
|
assertSame(f, f.fork()); |
1019 |
|
assertSame(f, pollNextLocalTask()); |
1020 |
|
helpQuiesce(); |
1021 |
< |
assertFalse(f.isDone()); |
1021 |
> |
checkNotDone(f); |
1022 |
> |
assertEquals(34, g.number); |
1023 |
> |
checkCompletedNormally(g); |
1024 |
|
}}; |
1025 |
|
testInvokeOnPool(singletonPool(), a); |
1026 |
|
} |
1027 |
|
|
1028 |
|
/** |
1029 |
< |
* pollTask returns an unexecuted task |
900 |
< |
* without executing it |
1029 |
> |
* pollTask returns an unexecuted task without executing it |
1030 |
|
*/ |
1031 |
|
public void testPollTask() { |
1032 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1037 |
|
assertSame(f, f.fork()); |
1038 |
|
assertSame(f, pollTask()); |
1039 |
|
helpQuiesce(); |
1040 |
< |
assertFalse(f.isDone()); |
1041 |
< |
assertTrue(g.isDone()); |
1040 |
> |
checkNotDone(f); |
1041 |
> |
checkCompletedNormally(g); |
1042 |
|
}}; |
1043 |
|
testInvokeOnPool(singletonPool(), a); |
1044 |
|
} |
1056 |
|
assertSame(g, peekNextLocalTask()); |
1057 |
|
assertNull(f.join()); |
1058 |
|
helpQuiesce(); |
1059 |
< |
assertTrue(f.isDone()); |
1059 |
> |
checkCompletedNormally(f); |
1060 |
> |
assertEquals(34, g.number); |
1061 |
> |
checkCompletedNormally(g); |
1062 |
|
}}; |
1063 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1064 |
|
} |
1065 |
|
|
1066 |
|
/** |
1067 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1068 |
< |
* without executing it, in async mode |
1067 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1068 |
> |
* executing it, in async mode |
1069 |
|
*/ |
1070 |
|
public void testPollNextLocalTaskAsync() { |
1071 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1076 |
|
assertSame(f, f.fork()); |
1077 |
|
assertSame(g, pollNextLocalTask()); |
1078 |
|
helpQuiesce(); |
1079 |
< |
assertTrue(f.isDone()); |
1080 |
< |
assertFalse(g.isDone()); |
1079 |
> |
assertEquals(21, f.number); |
1080 |
> |
checkCompletedNormally(f); |
1081 |
> |
checkNotDone(g); |
1082 |
|
}}; |
1083 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1084 |
|
} |
1085 |
|
|
1086 |
|
/** |
1087 |
< |
* pollTask returns an unexecuted task |
1088 |
< |
* without executing it, in async mode |
1087 |
> |
* pollTask returns an unexecuted task without executing it, in |
1088 |
> |
* async mode |
1089 |
|
*/ |
1090 |
|
public void testPollTaskAsync() { |
1091 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1096 |
|
assertSame(f, f.fork()); |
1097 |
|
assertSame(g, pollTask()); |
1098 |
|
helpQuiesce(); |
1099 |
< |
assertTrue(f.isDone()); |
1100 |
< |
assertFalse(g.isDone()); |
1099 |
> |
assertEquals(21, f.number); |
1100 |
> |
checkCompletedNormally(f); |
1101 |
> |
checkNotDone(g); |
1102 |
|
}}; |
1103 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1104 |
|
} |
1108 |
|
/** |
1109 |
|
* invoke returns when task completes normally. |
1110 |
|
* isCompletedAbnormally and isCancelled return false for normally |
1111 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
1111 |
> |
* completed tasks; getRawResult returns null. |
1112 |
|
*/ |
1113 |
|
public void testInvokeSingleton() { |
1114 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1116 |
|
AsyncFib f = new AsyncFib(8); |
1117 |
|
assertNull(f.invoke()); |
1118 |
|
assertEquals(21, f.number); |
1119 |
< |
assertTrue(f.isDone()); |
987 |
< |
assertFalse(f.isCancelled()); |
988 |
< |
assertFalse(f.isCompletedAbnormally()); |
989 |
< |
assertNull(f.getRawResult()); |
1119 |
> |
checkCompletedNormally(f); |
1120 |
|
}}; |
1121 |
|
testInvokeOnPool(singletonPool(), a); |
1122 |
|
} |
1132 |
|
AsyncFib f = new AsyncFib(8); |
1133 |
|
f.quietlyInvoke(); |
1134 |
|
assertEquals(21, f.number); |
1135 |
< |
assertTrue(f.isDone()); |
1006 |
< |
assertFalse(f.isCancelled()); |
1007 |
< |
assertFalse(f.isCompletedAbnormally()); |
1008 |
< |
assertNull(f.getRawResult()); |
1135 |
> |
checkCompletedNormally(f); |
1136 |
|
}}; |
1137 |
|
testInvokeOnPool(singletonPool(), a); |
1138 |
|
} |
1147 |
|
assertSame(f, f.fork()); |
1148 |
|
assertNull(f.join()); |
1149 |
|
assertEquals(21, f.number); |
1150 |
< |
assertTrue(f.isDone()); |
1024 |
< |
assertNull(f.getRawResult()); |
1150 |
> |
checkCompletedNormally(f); |
1151 |
|
}}; |
1152 |
|
testInvokeOnPool(singletonPool(), a); |
1153 |
|
} |
1162 |
|
assertSame(f, f.fork()); |
1163 |
|
assertNull(f.get()); |
1164 |
|
assertEquals(21, f.number); |
1165 |
< |
assertTrue(f.isDone()); |
1165 |
> |
checkCompletedNormally(f); |
1166 |
|
}}; |
1167 |
|
testInvokeOnPool(singletonPool(), a); |
1168 |
|
} |
1177 |
|
assertSame(f, f.fork()); |
1178 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1179 |
|
assertEquals(21, f.number); |
1180 |
< |
assertTrue(f.isDone()); |
1180 |
> |
checkCompletedNormally(f); |
1181 |
|
}}; |
1182 |
|
testInvokeOnPool(singletonPool(), a); |
1183 |
|
} |
1208 |
|
assertSame(f, f.fork()); |
1209 |
|
f.quietlyJoin(); |
1210 |
|
assertEquals(21, f.number); |
1211 |
< |
assertTrue(f.isDone()); |
1211 |
> |
checkCompletedNormally(f); |
1212 |
|
}}; |
1213 |
|
testInvokeOnPool(singletonPool(), a); |
1214 |
|
} |
1224 |
|
AsyncFib f = new AsyncFib(8); |
1225 |
|
assertSame(f, f.fork()); |
1226 |
|
f.helpQuiesce(); |
1101 |
– |
assertEquals(21, f.number); |
1102 |
– |
assertTrue(f.isDone()); |
1227 |
|
assertEquals(0, getQueuedTaskCount()); |
1228 |
+ |
assertEquals(21, f.number); |
1229 |
+ |
checkCompletedNormally(f); |
1230 |
|
}}; |
1231 |
|
testInvokeOnPool(singletonPool(), a); |
1232 |
|
} |
1242 |
|
try { |
1243 |
|
f.invoke(); |
1244 |
|
shouldThrow(); |
1245 |
< |
} catch (FJException success) {} |
1245 |
> |
} catch (FJException success) { |
1246 |
> |
checkCompletedAbnormally(f, success); |
1247 |
> |
} |
1248 |
|
}}; |
1249 |
|
testInvokeOnPool(singletonPool(), a); |
1250 |
|
} |
1257 |
|
public void realCompute() { |
1258 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1259 |
|
f.quietlyInvoke(); |
1260 |
< |
assertTrue(f.isDone()); |
1260 |
> |
assertTrue(f.getException() instanceof FJException); |
1261 |
> |
checkCompletedAbnormally(f, f.getException()); |
1262 |
|
}}; |
1263 |
|
testInvokeOnPool(singletonPool(), a); |
1264 |
|
} |
1274 |
|
try { |
1275 |
|
f.join(); |
1276 |
|
shouldThrow(); |
1277 |
< |
} catch (FJException success) {} |
1277 |
> |
} catch (FJException success) { |
1278 |
> |
checkCompletedAbnormally(f, success); |
1279 |
> |
} |
1280 |
|
}}; |
1281 |
|
testInvokeOnPool(singletonPool(), a); |
1282 |
|
} |
1295 |
|
} catch (ExecutionException success) { |
1296 |
|
Throwable cause = success.getCause(); |
1297 |
|
assertTrue(cause instanceof FJException); |
1298 |
< |
assertTrue(f.isDone()); |
1168 |
< |
assertTrue(f.isCompletedAbnormally()); |
1169 |
< |
assertSame(cause, f.getException()); |
1298 |
> |
checkCompletedAbnormally(f, cause); |
1299 |
|
} |
1300 |
|
}}; |
1301 |
|
testInvokeOnPool(singletonPool(), a); |
1315 |
|
} catch (ExecutionException success) { |
1316 |
|
Throwable cause = success.getCause(); |
1317 |
|
assertTrue(cause instanceof FJException); |
1318 |
< |
assertTrue(f.isDone()); |
1190 |
< |
assertTrue(f.isCompletedAbnormally()); |
1191 |
< |
assertSame(cause, f.getException()); |
1318 |
> |
checkCompletedAbnormally(f, cause); |
1319 |
|
} |
1320 |
|
}}; |
1321 |
|
testInvokeOnPool(singletonPool(), a); |
1330 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1331 |
|
assertSame(f, f.fork()); |
1332 |
|
f.quietlyJoin(); |
1206 |
– |
assertTrue(f.isDone()); |
1207 |
– |
assertTrue(f.isCompletedAbnormally()); |
1333 |
|
assertTrue(f.getException() instanceof FJException); |
1334 |
+ |
checkCompletedAbnormally(f, f.getException()); |
1335 |
|
}}; |
1336 |
|
testInvokeOnPool(singletonPool(), a); |
1337 |
|
} |
1348 |
|
f.invoke(); |
1349 |
|
shouldThrow(); |
1350 |
|
} catch (CancellationException success) { |
1351 |
< |
assertTrue(f.isDone()); |
1226 |
< |
assertTrue(f.isCancelled()); |
1227 |
< |
assertTrue(f.isCompletedAbnormally()); |
1228 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1351 |
> |
checkCancelled(f); |
1352 |
|
} |
1353 |
|
}}; |
1354 |
|
testInvokeOnPool(singletonPool(), a); |
1367 |
|
f.join(); |
1368 |
|
shouldThrow(); |
1369 |
|
} catch (CancellationException success) { |
1370 |
< |
assertTrue(f.isDone()); |
1248 |
< |
assertTrue(f.isCancelled()); |
1249 |
< |
assertTrue(f.isCompletedAbnormally()); |
1250 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1370 |
> |
checkCancelled(f); |
1371 |
|
} |
1372 |
|
}}; |
1373 |
|
testInvokeOnPool(singletonPool(), a); |
1386 |
|
f.get(); |
1387 |
|
shouldThrow(); |
1388 |
|
} catch (CancellationException success) { |
1389 |
< |
assertTrue(f.isDone()); |
1270 |
< |
assertTrue(f.isCancelled()); |
1271 |
< |
assertTrue(f.isCompletedAbnormally()); |
1272 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1389 |
> |
checkCancelled(f); |
1390 |
|
} |
1391 |
|
}}; |
1392 |
|
testInvokeOnPool(singletonPool(), a); |
1405 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1406 |
|
shouldThrow(); |
1407 |
|
} catch (CancellationException success) { |
1408 |
< |
assertTrue(f.isDone()); |
1292 |
< |
assertTrue(f.isCancelled()); |
1293 |
< |
assertTrue(f.isCompletedAbnormally()); |
1294 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1408 |
> |
checkCancelled(f); |
1409 |
|
} |
1410 |
|
}}; |
1411 |
|
testInvokeOnPool(singletonPool(), a); |
1421 |
|
assertTrue(f.cancel(true)); |
1422 |
|
assertSame(f, f.fork()); |
1423 |
|
f.quietlyJoin(); |
1424 |
< |
assertTrue(f.isDone()); |
1311 |
< |
assertTrue(f.isCompletedAbnormally()); |
1312 |
< |
assertTrue(f.isCancelled()); |
1313 |
< |
assertTrue(f.getException() instanceof CancellationException); |
1424 |
> |
checkCancelled(f); |
1425 |
|
}}; |
1426 |
|
testInvokeOnPool(singletonPool(), a); |
1427 |
|
} |
1437 |
|
try { |
1438 |
|
f.invoke(); |
1439 |
|
shouldThrow(); |
1440 |
< |
} catch (FJException success) {} |
1440 |
> |
} catch (FJException success) { |
1441 |
> |
checkCompletedAbnormally(f, success); |
1442 |
> |
} |
1443 |
|
}}; |
1444 |
|
testInvokeOnPool(singletonPool(), a); |
1445 |
|
} |
1453 |
|
AsyncFib f = new AsyncFib(8); |
1454 |
|
AsyncFib g = new AsyncFib(9); |
1455 |
|
invokeAll(f, g); |
1343 |
– |
assertTrue(f.isDone()); |
1456 |
|
assertEquals(21, f.number); |
1345 |
– |
assertTrue(g.isDone()); |
1457 |
|
assertEquals(34, g.number); |
1458 |
+ |
checkCompletedNormally(f); |
1459 |
+ |
checkCompletedNormally(g); |
1460 |
|
}}; |
1461 |
|
testInvokeOnPool(singletonPool(), a); |
1462 |
|
} |
1469 |
|
public void realCompute() { |
1470 |
|
AsyncFib f = new AsyncFib(8); |
1471 |
|
invokeAll(f); |
1472 |
< |
assertTrue(f.isDone()); |
1472 |
> |
checkCompletedNormally(f); |
1473 |
|
assertEquals(21, f.number); |
1474 |
|
}}; |
1475 |
|
testInvokeOnPool(singletonPool(), a); |
1485 |
|
AsyncFib g = new AsyncFib(9); |
1486 |
|
AsyncFib h = new AsyncFib(7); |
1487 |
|
invokeAll(f, g, h); |
1375 |
– |
assertTrue(f.isDone()); |
1488 |
|
assertEquals(21, f.number); |
1377 |
– |
assertTrue(g.isDone()); |
1489 |
|
assertEquals(34, g.number); |
1379 |
– |
assertTrue(h.isDone()); |
1490 |
|
assertEquals(13, h.number); |
1491 |
+ |
checkCompletedNormally(f); |
1492 |
+ |
checkCompletedNormally(g); |
1493 |
+ |
checkCompletedNormally(h); |
1494 |
|
}}; |
1495 |
|
testInvokeOnPool(singletonPool(), a); |
1496 |
|
} |
1509 |
|
set.add(g); |
1510 |
|
set.add(h); |
1511 |
|
invokeAll(set); |
1399 |
– |
assertTrue(f.isDone()); |
1512 |
|
assertEquals(21, f.number); |
1401 |
– |
assertTrue(g.isDone()); |
1513 |
|
assertEquals(34, g.number); |
1403 |
– |
assertTrue(h.isDone()); |
1514 |
|
assertEquals(13, h.number); |
1515 |
+ |
checkCompletedNormally(f); |
1516 |
+ |
checkCompletedNormally(g); |
1517 |
+ |
checkCompletedNormally(h); |
1518 |
|
}}; |
1519 |
|
testInvokeOnPool(singletonPool(), a); |
1520 |
|
} |
1548 |
|
try { |
1549 |
|
invokeAll(f, g); |
1550 |
|
shouldThrow(); |
1551 |
< |
} catch (FJException success) {} |
1551 |
> |
} catch (FJException success) { |
1552 |
> |
checkCompletedAbnormally(g, success); |
1553 |
> |
} |
1554 |
|
}}; |
1555 |
|
testInvokeOnPool(singletonPool(), a); |
1556 |
|
} |
1565 |
|
try { |
1566 |
|
invokeAll(g); |
1567 |
|
shouldThrow(); |
1568 |
< |
} catch (FJException success) {} |
1568 |
> |
} catch (FJException success) { |
1569 |
> |
checkCompletedAbnormally(g, success); |
1570 |
> |
} |
1571 |
|
}}; |
1572 |
|
testInvokeOnPool(singletonPool(), a); |
1573 |
|
} |
1584 |
|
try { |
1585 |
|
invokeAll(f, g, h); |
1586 |
|
shouldThrow(); |
1587 |
< |
} catch (FJException success) {} |
1587 |
> |
} catch (FJException success) { |
1588 |
> |
checkCompletedAbnormally(g, success); |
1589 |
> |
} |
1590 |
|
}}; |
1591 |
|
testInvokeOnPool(singletonPool(), a); |
1592 |
|
} |
1607 |
|
try { |
1608 |
|
invokeAll(set); |
1609 |
|
shouldThrow(); |
1610 |
< |
} catch (FJException success) {} |
1610 |
> |
} catch (FJException success) { |
1611 |
> |
checkCompletedAbnormally(f, success); |
1612 |
> |
} |
1613 |
|
}}; |
1614 |
|
testInvokeOnPool(singletonPool(), a); |
1615 |
|
} |