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; |
6 |
> |
|
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collections; |
12 |
> |
import java.util.HashSet; |
13 |
> |
import java.util.List; |
14 |
|
import java.util.concurrent.CancellationException; |
15 |
+ |
import java.util.concurrent.ExecutionException; |
16 |
|
import java.util.concurrent.ForkJoinPool; |
17 |
|
import java.util.concurrent.ForkJoinTask; |
10 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
18 |
|
import java.util.concurrent.RecursiveAction; |
12 |
– |
import java.util.concurrent.TimeUnit; |
19 |
|
import java.util.concurrent.TimeoutException; |
20 |
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
21 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
23 |
< |
import java.util.HashSet; |
18 |
< |
import junit.framework.*; |
21 |
> |
|
22 |
> |
import junit.framework.Test; |
23 |
> |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
public class ForkJoinTaskTest extends JSR166TestCase { |
26 |
|
|
27 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run(suite()); |
28 |
> |
main(suite(), args); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
51 |
|
} |
52 |
|
|
53 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
54 |
< |
try { |
54 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
55 |
|
assertFalse(a.isDone()); |
56 |
|
assertFalse(a.isCompletedNormally()); |
57 |
|
assertFalse(a.isCompletedAbnormally()); |
67 |
|
assertFalse(a.isCancelled()); |
68 |
|
assertNull(a.getException()); |
69 |
|
assertNull(a.getRawResult()); |
65 |
– |
} finally { |
66 |
– |
joinPool(pool); |
70 |
|
} |
71 |
|
} |
72 |
|
|
96 |
|
assertFalse(a.isCompletedAbnormally()); |
97 |
|
assertNull(a.getException()); |
98 |
|
assertSame(expected, a.getRawResult()); |
99 |
< |
assertSame(expected, a.join()); |
99 |
> |
|
100 |
> |
{ |
101 |
> |
Thread.currentThread().interrupt(); |
102 |
> |
long startTime = System.nanoTime(); |
103 |
> |
assertSame(expected, a.join()); |
104 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
105 |
> |
Thread.interrupted(); |
106 |
> |
} |
107 |
> |
|
108 |
> |
{ |
109 |
> |
Thread.currentThread().interrupt(); |
110 |
> |
long startTime = System.nanoTime(); |
111 |
> |
a.quietlyJoin(); // should be no-op |
112 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
113 |
> |
Thread.interrupted(); |
114 |
> |
} |
115 |
> |
|
116 |
> |
assertFalse(a.cancel(false)); |
117 |
> |
assertFalse(a.cancel(true)); |
118 |
|
try { |
119 |
|
assertSame(expected, a.get()); |
120 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
|
assertTrue(a.isCompletedAbnormally()); |
131 |
|
assertTrue(a.getException() instanceof CancellationException); |
132 |
|
assertNull(a.getRawResult()); |
133 |
+ |
assertTrue(a.cancel(false)); |
134 |
+ |
assertTrue(a.cancel(true)); |
135 |
|
|
136 |
|
try { |
137 |
+ |
Thread.currentThread().interrupt(); |
138 |
|
a.join(); |
139 |
|
shouldThrow(); |
140 |
|
} catch (CancellationException success) { |
141 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
142 |
+ |
Thread.interrupted(); |
143 |
+ |
|
144 |
+ |
{ |
145 |
+ |
long startTime = System.nanoTime(); |
146 |
+ |
a.quietlyJoin(); // should be no-op |
147 |
+ |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
148 |
+ |
} |
149 |
|
|
150 |
|
try { |
151 |
|
a.get(); |
165 |
|
assertFalse(a.isCancelled()); |
166 |
|
assertFalse(a.isCompletedNormally()); |
167 |
|
assertTrue(a.isCompletedAbnormally()); |
168 |
< |
assertSame(t, a.getException()); |
168 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
169 |
|
assertNull(a.getRawResult()); |
170 |
+ |
assertFalse(a.cancel(false)); |
171 |
+ |
assertFalse(a.cancel(true)); |
172 |
|
|
173 |
|
try { |
174 |
+ |
Thread.currentThread().interrupt(); |
175 |
|
a.join(); |
176 |
|
shouldThrow(); |
177 |
|
} catch (Throwable expected) { |
178 |
< |
assertSame(t, expected); |
178 |
> |
assertSame(t.getClass(), expected.getClass()); |
179 |
> |
} |
180 |
> |
Thread.interrupted(); |
181 |
> |
|
182 |
> |
{ |
183 |
> |
long startTime = System.nanoTime(); |
184 |
> |
a.quietlyJoin(); // should be no-op |
185 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
186 |
|
} |
187 |
|
|
188 |
|
try { |
189 |
|
a.get(); |
190 |
|
shouldThrow(); |
191 |
|
} catch (ExecutionException success) { |
192 |
< |
assertSame(t, success.getCause()); |
192 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
193 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
194 |
|
|
195 |
|
try { |
196 |
|
a.get(5L, SECONDS); |
197 |
|
shouldThrow(); |
198 |
|
} catch (ExecutionException success) { |
199 |
< |
assertSame(t, success.getCause()); |
199 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
200 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
201 |
|
} |
202 |
|
|
208 |
|
* differently than supplied Recursive forms. |
209 |
|
*/ |
210 |
|
|
211 |
< |
static final class FJException extends RuntimeException { |
211 |
> |
public static final class FJException extends RuntimeException { |
212 |
|
FJException() { super(); } |
213 |
|
} |
214 |
|
|
219 |
|
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
220 |
|
"controlState"); |
221 |
|
|
222 |
< |
private BinaryAsyncAction parent; |
222 |
> |
private volatile BinaryAsyncAction parent; |
223 |
|
|
224 |
< |
private BinaryAsyncAction sibling; |
224 |
> |
private volatile BinaryAsyncAction sibling; |
225 |
|
|
226 |
|
protected BinaryAsyncAction() { |
227 |
|
} |
256 |
|
super.completeExceptionally(ex); |
257 |
|
} |
258 |
|
|
259 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
260 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
261 |
+ |
completeExceptionally(new FJException()); |
262 |
+ |
return true; |
263 |
+ |
} |
264 |
+ |
return false; |
265 |
+ |
} |
266 |
+ |
|
267 |
|
public final void complete() { |
268 |
|
BinaryAsyncAction a = this; |
269 |
|
for (;;) { |
285 |
|
} |
286 |
|
|
287 |
|
public final void completeExceptionally(Throwable ex) { |
288 |
< |
BinaryAsyncAction a = this; |
240 |
< |
while (!a.isCompletedAbnormally()) { |
288 |
> |
for (BinaryAsyncAction a = this;;) { |
289 |
|
a.completeThisExceptionally(ex); |
290 |
|
BinaryAsyncAction s = a.sibling; |
291 |
< |
if (s != null) |
292 |
< |
s.cancel(false); |
293 |
< |
if (!a.onException() || (a = a.parent) == null) |
291 |
> |
if (s != null && !s.isDone()) |
292 |
> |
s.completeExceptionally(ex); |
293 |
> |
if ((a = a.parent) == null) |
294 |
|
break; |
295 |
|
} |
296 |
|
} |
359 |
|
} |
360 |
|
} |
361 |
|
|
314 |
– |
|
362 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
363 |
|
int number; |
364 |
|
public FailingAsyncFib(int n) { |
390 |
|
/** |
391 |
|
* invoke returns when task completes normally. |
392 |
|
* isCompletedAbnormally and isCancelled return false for normally |
393 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
393 |
> |
* completed tasks; getRawResult returns null. |
394 |
|
*/ |
395 |
|
public void testInvoke() { |
396 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
397 |
< |
public void realCompute() { |
397 |
> |
protected void realCompute() { |
398 |
|
AsyncFib f = new AsyncFib(8); |
399 |
|
assertNull(f.invoke()); |
400 |
|
assertEquals(21, f.number); |
410 |
|
*/ |
411 |
|
public void testQuietlyInvoke() { |
412 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
413 |
< |
public void realCompute() { |
413 |
> |
protected void realCompute() { |
414 |
|
AsyncFib f = new AsyncFib(8); |
415 |
|
f.quietlyInvoke(); |
416 |
|
assertEquals(21, f.number); |
424 |
|
*/ |
425 |
|
public void testForkJoin() { |
426 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
427 |
< |
public void realCompute() { |
427 |
> |
protected void realCompute() { |
428 |
|
AsyncFib f = new AsyncFib(8); |
429 |
|
assertSame(f, f.fork()); |
430 |
|
assertNull(f.join()); |
439 |
|
*/ |
440 |
|
public void testForkGet() { |
441 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
442 |
< |
public void realCompute() throws Exception { |
442 |
> |
protected void realCompute() throws Exception { |
443 |
|
AsyncFib f = new AsyncFib(8); |
444 |
|
assertSame(f, f.fork()); |
445 |
|
assertNull(f.get()); |
454 |
|
*/ |
455 |
|
public void testForkTimedGet() { |
456 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
457 |
< |
public void realCompute() throws Exception { |
457 |
> |
protected void realCompute() throws Exception { |
458 |
|
AsyncFib f = new AsyncFib(8); |
459 |
|
assertSame(f, f.fork()); |
460 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
469 |
|
*/ |
470 |
|
public void testForkTimedGetNPE() { |
471 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
472 |
< |
public void realCompute() throws Exception { |
472 |
> |
protected void realCompute() throws Exception { |
473 |
|
AsyncFib f = new AsyncFib(8); |
474 |
|
assertSame(f, f.fork()); |
475 |
|
try { |
485 |
|
*/ |
486 |
|
public void testForkQuietlyJoin() { |
487 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
488 |
< |
public void realCompute() { |
488 |
> |
protected void realCompute() { |
489 |
|
AsyncFib f = new AsyncFib(8); |
490 |
|
assertSame(f, f.fork()); |
491 |
|
f.quietlyJoin(); |
495 |
|
testInvokeOnPool(mainPool(), a); |
496 |
|
} |
497 |
|
|
451 |
– |
|
498 |
|
/** |
499 |
|
* helpQuiesce returns when tasks are complete. |
500 |
|
* getQueuedTaskCount returns 0 when quiescent |
501 |
|
*/ |
502 |
|
public void testForkHelpQuiesce() { |
503 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
504 |
< |
public void realCompute() { |
504 |
> |
protected void realCompute() { |
505 |
|
AsyncFib f = new AsyncFib(8); |
506 |
|
assertSame(f, f.fork()); |
507 |
< |
f.helpQuiesce(); |
507 |
> |
helpQuiesce(); |
508 |
|
assertEquals(21, f.number); |
509 |
|
assertEquals(0, getQueuedTaskCount()); |
510 |
|
checkCompletedNormally(f); |
512 |
|
testInvokeOnPool(mainPool(), a); |
513 |
|
} |
514 |
|
|
469 |
– |
|
515 |
|
/** |
516 |
|
* invoke task throws exception when task completes abnormally |
517 |
|
*/ |
518 |
|
public void testAbnormalInvoke() { |
519 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
520 |
< |
public void realCompute() { |
520 |
> |
protected void realCompute() { |
521 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
522 |
|
try { |
523 |
|
f.invoke(); |
534 |
|
*/ |
535 |
|
public void testAbnormalQuietlyInvoke() { |
536 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
537 |
< |
public void realCompute() { |
537 |
> |
protected void realCompute() { |
538 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
539 |
|
f.quietlyInvoke(); |
540 |
|
assertTrue(f.getException() instanceof FJException); |
548 |
|
*/ |
549 |
|
public void testAbnormalForkJoin() { |
550 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
551 |
< |
public void realCompute() { |
551 |
> |
protected void realCompute() { |
552 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
553 |
|
assertSame(f, f.fork()); |
554 |
|
try { |
566 |
|
*/ |
567 |
|
public void testAbnormalForkGet() { |
568 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
569 |
< |
public void realCompute() throws Exception { |
569 |
> |
protected void realCompute() throws Exception { |
570 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
571 |
|
assertSame(f, f.fork()); |
572 |
|
try { |
586 |
|
*/ |
587 |
|
public void testAbnormalForkTimedGet() { |
588 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
589 |
< |
public void realCompute() throws Exception { |
589 |
> |
protected void realCompute() throws Exception { |
590 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
591 |
|
assertSame(f, f.fork()); |
592 |
|
try { |
606 |
|
*/ |
607 |
|
public void testAbnormalForkQuietlyJoin() { |
608 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
609 |
< |
public void realCompute() { |
609 |
> |
protected void realCompute() { |
610 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
611 |
|
assertSame(f, f.fork()); |
612 |
|
f.quietlyJoin(); |
621 |
|
*/ |
622 |
|
public void testCancelledInvoke() { |
623 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
624 |
< |
public void realCompute() { |
624 |
> |
protected void realCompute() { |
625 |
|
AsyncFib f = new AsyncFib(8); |
626 |
|
assertTrue(f.cancel(true)); |
627 |
|
try { |
639 |
|
*/ |
640 |
|
public void testCancelledForkJoin() { |
641 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
642 |
< |
public void realCompute() { |
642 |
> |
protected void realCompute() { |
643 |
|
AsyncFib f = new AsyncFib(8); |
644 |
|
assertTrue(f.cancel(true)); |
645 |
|
assertSame(f, f.fork()); |
658 |
|
*/ |
659 |
|
public void testCancelledForkGet() { |
660 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
661 |
< |
public void realCompute() throws Exception { |
661 |
> |
protected void realCompute() throws Exception { |
662 |
|
AsyncFib f = new AsyncFib(8); |
663 |
|
assertTrue(f.cancel(true)); |
664 |
|
assertSame(f, f.fork()); |
677 |
|
*/ |
678 |
|
public void testCancelledForkTimedGet() throws Exception { |
679 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
680 |
< |
public void realCompute() throws Exception { |
680 |
> |
protected void realCompute() throws Exception { |
681 |
|
AsyncFib f = new AsyncFib(8); |
682 |
|
assertTrue(f.cancel(true)); |
683 |
|
assertSame(f, f.fork()); |
696 |
|
*/ |
697 |
|
public void testCancelledForkQuietlyJoin() { |
698 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
699 |
< |
public void realCompute() { |
699 |
> |
protected void realCompute() { |
700 |
|
AsyncFib f = new AsyncFib(8); |
701 |
|
assertTrue(f.cancel(true)); |
702 |
|
assertSame(f, f.fork()); |
712 |
|
public void testGetPool() { |
713 |
|
final ForkJoinPool mainPool = mainPool(); |
714 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
715 |
< |
public void realCompute() { |
715 |
> |
protected void realCompute() { |
716 |
|
assertSame(mainPool, getPool()); |
717 |
|
}}; |
718 |
|
testInvokeOnPool(mainPool, a); |
723 |
|
*/ |
724 |
|
public void testGetPool2() { |
725 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
726 |
< |
public void realCompute() { |
726 |
> |
protected void realCompute() { |
727 |
|
assertNull(getPool()); |
728 |
|
}}; |
729 |
|
assertNull(a.invoke()); |
734 |
|
*/ |
735 |
|
public void testInForkJoinPool() { |
736 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
737 |
< |
public void realCompute() { |
737 |
> |
protected void realCompute() { |
738 |
|
assertTrue(inForkJoinPool()); |
739 |
|
}}; |
740 |
|
testInvokeOnPool(mainPool(), a); |
745 |
|
*/ |
746 |
|
public void testInForkJoinPool2() { |
747 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
748 |
< |
public void realCompute() { |
748 |
> |
protected void realCompute() { |
749 |
|
assertFalse(inForkJoinPool()); |
750 |
|
}}; |
751 |
|
assertNull(a.invoke()); |
756 |
|
*/ |
757 |
|
public void testSetRawResult() { |
758 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
759 |
< |
public void realCompute() { |
759 |
> |
protected void realCompute() { |
760 |
|
setRawResult(null); |
761 |
+ |
assertNull(getRawResult()); |
762 |
|
}}; |
763 |
|
assertNull(a.invoke()); |
764 |
|
} |
768 |
|
*/ |
769 |
|
public void testCompleteExceptionally() { |
770 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
771 |
< |
public void realCompute() { |
771 |
> |
protected void realCompute() { |
772 |
|
AsyncFib f = new AsyncFib(8); |
773 |
|
f.completeExceptionally(new FJException()); |
774 |
|
try { |
786 |
|
*/ |
787 |
|
public void testInvokeAll2() { |
788 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
789 |
< |
public void realCompute() { |
789 |
> |
protected void realCompute() { |
790 |
|
AsyncFib f = new AsyncFib(8); |
791 |
|
AsyncFib g = new AsyncFib(9); |
792 |
|
invokeAll(f, g); |
803 |
|
*/ |
804 |
|
public void testInvokeAll1() { |
805 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
806 |
< |
public void realCompute() { |
806 |
> |
protected void realCompute() { |
807 |
|
AsyncFib f = new AsyncFib(8); |
808 |
|
invokeAll(f); |
809 |
|
checkCompletedNormally(f); |
817 |
|
*/ |
818 |
|
public void testInvokeAll3() { |
819 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
820 |
< |
public void realCompute() { |
820 |
> |
protected void realCompute() { |
821 |
|
AsyncFib f = new AsyncFib(8); |
822 |
|
AsyncFib g = new AsyncFib(9); |
823 |
|
AsyncFib h = new AsyncFib(7); |
837 |
|
*/ |
838 |
|
public void testInvokeAllCollection() { |
839 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
840 |
< |
public void realCompute() { |
840 |
> |
protected void realCompute() { |
841 |
|
AsyncFib f = new AsyncFib(8); |
842 |
|
AsyncFib g = new AsyncFib(9); |
843 |
|
AsyncFib h = new AsyncFib(7); |
856 |
|
testInvokeOnPool(mainPool(), a); |
857 |
|
} |
858 |
|
|
813 |
– |
|
859 |
|
/** |
860 |
|
* invokeAll(tasks) with any null task throws NPE |
861 |
|
*/ |
862 |
|
public void testInvokeAllNPE() { |
863 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
864 |
< |
public void realCompute() { |
864 |
> |
protected void realCompute() { |
865 |
|
AsyncFib f = new AsyncFib(8); |
866 |
|
AsyncFib g = new AsyncFib(9); |
867 |
|
AsyncFib h = null; |
878 |
|
*/ |
879 |
|
public void testAbnormalInvokeAll2() { |
880 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
881 |
< |
public void realCompute() { |
881 |
> |
protected void realCompute() { |
882 |
|
AsyncFib f = new AsyncFib(8); |
883 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
884 |
+ |
ForkJoinTask[] tasks = { f, g }; |
885 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
886 |
|
try { |
887 |
< |
invokeAll(f, g); |
887 |
> |
invokeAll(tasks); |
888 |
|
shouldThrow(); |
889 |
|
} catch (FJException success) { |
890 |
|
checkCompletedAbnormally(g, success); |
898 |
|
*/ |
899 |
|
public void testAbnormalInvokeAll1() { |
900 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
901 |
< |
public void realCompute() { |
901 |
> |
protected void realCompute() { |
902 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
903 |
|
try { |
904 |
|
invokeAll(g); |
915 |
|
*/ |
916 |
|
public void testAbnormalInvokeAll3() { |
917 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
918 |
< |
public void realCompute() { |
918 |
> |
protected void realCompute() { |
919 |
|
AsyncFib f = new AsyncFib(8); |
920 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
921 |
|
AsyncFib h = new AsyncFib(7); |
922 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
923 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
924 |
|
try { |
925 |
< |
invokeAll(f, g, h); |
925 |
> |
invokeAll(tasks); |
926 |
|
shouldThrow(); |
927 |
|
} catch (FJException success) { |
928 |
|
checkCompletedAbnormally(g, success); |
932 |
|
} |
933 |
|
|
934 |
|
/** |
935 |
< |
* invokeAll(collection) throws exception if any task does |
935 |
> |
* invokeAll(collection) throws exception if any task does |
936 |
|
*/ |
937 |
|
public void testAbnormalInvokeAllCollection() { |
938 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
939 |
< |
public void realCompute() { |
939 |
> |
protected void realCompute() { |
940 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
941 |
|
AsyncFib g = new AsyncFib(9); |
942 |
|
AsyncFib h = new AsyncFib(7); |
943 |
< |
HashSet set = new HashSet(); |
944 |
< |
set.add(f); |
945 |
< |
set.add(g); |
897 |
< |
set.add(h); |
943 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
944 |
> |
List taskList = Arrays.asList(tasks); |
945 |
> |
Collections.shuffle(taskList); |
946 |
|
try { |
947 |
< |
invokeAll(set); |
947 |
> |
invokeAll(taskList); |
948 |
|
shouldThrow(); |
949 |
|
} catch (FJException success) { |
950 |
|
checkCompletedAbnormally(f, success); |
959 |
|
*/ |
960 |
|
public void testTryUnfork() { |
961 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
962 |
< |
public void realCompute() { |
962 |
> |
protected void realCompute() { |
963 |
|
AsyncFib g = new AsyncFib(9); |
964 |
|
assertSame(g, g.fork()); |
965 |
|
AsyncFib f = new AsyncFib(8); |
978 |
|
*/ |
979 |
|
public void testGetSurplusQueuedTaskCount() { |
980 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
981 |
< |
public void realCompute() { |
981 |
> |
protected void realCompute() { |
982 |
|
AsyncFib h = new AsyncFib(7); |
983 |
|
assertSame(h, h.fork()); |
984 |
|
AsyncFib g = new AsyncFib(9); |
1000 |
|
*/ |
1001 |
|
public void testPeekNextLocalTask() { |
1002 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1003 |
< |
public void realCompute() { |
1003 |
> |
protected void realCompute() { |
1004 |
|
AsyncFib g = new AsyncFib(9); |
1005 |
|
assertSame(g, g.fork()); |
1006 |
|
AsyncFib f = new AsyncFib(8); |
1020 |
|
*/ |
1021 |
|
public void testPollNextLocalTask() { |
1022 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1023 |
< |
public void realCompute() { |
1023 |
> |
protected void realCompute() { |
1024 |
|
AsyncFib g = new AsyncFib(9); |
1025 |
|
assertSame(g, g.fork()); |
1026 |
|
AsyncFib f = new AsyncFib(8); |
1039 |
|
*/ |
1040 |
|
public void testPollTask() { |
1041 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1042 |
< |
public void realCompute() { |
1042 |
> |
protected void realCompute() { |
1043 |
|
AsyncFib g = new AsyncFib(9); |
1044 |
|
assertSame(g, g.fork()); |
1045 |
|
AsyncFib f = new AsyncFib(8); |
1057 |
|
*/ |
1058 |
|
public void testPeekNextLocalTaskAsync() { |
1059 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1060 |
< |
public void realCompute() { |
1060 |
> |
protected void realCompute() { |
1061 |
|
AsyncFib g = new AsyncFib(9); |
1062 |
|
assertSame(g, g.fork()); |
1063 |
|
AsyncFib f = new AsyncFib(8); |
1078 |
|
*/ |
1079 |
|
public void testPollNextLocalTaskAsync() { |
1080 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1081 |
< |
public void realCompute() { |
1081 |
> |
protected void realCompute() { |
1082 |
|
AsyncFib g = new AsyncFib(9); |
1083 |
|
assertSame(g, g.fork()); |
1084 |
|
AsyncFib f = new AsyncFib(8); |
1098 |
|
*/ |
1099 |
|
public void testPollTaskAsync() { |
1100 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1101 |
< |
public void realCompute() { |
1101 |
> |
protected void realCompute() { |
1102 |
|
AsyncFib g = new AsyncFib(9); |
1103 |
|
assertSame(g, g.fork()); |
1104 |
|
AsyncFib f = new AsyncFib(8); |
1117 |
|
/** |
1118 |
|
* invoke returns when task completes normally. |
1119 |
|
* isCompletedAbnormally and isCancelled return false for normally |
1120 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
1120 |
> |
* completed tasks; getRawResult returns null. |
1121 |
|
*/ |
1122 |
|
public void testInvokeSingleton() { |
1123 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1124 |
< |
public void realCompute() { |
1124 |
> |
protected void realCompute() { |
1125 |
|
AsyncFib f = new AsyncFib(8); |
1126 |
|
assertNull(f.invoke()); |
1127 |
|
assertEquals(21, f.number); |
1137 |
|
*/ |
1138 |
|
public void testQuietlyInvokeSingleton() { |
1139 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1140 |
< |
public void realCompute() { |
1140 |
> |
protected void realCompute() { |
1141 |
|
AsyncFib f = new AsyncFib(8); |
1142 |
|
f.quietlyInvoke(); |
1143 |
|
assertEquals(21, f.number); |
1151 |
|
*/ |
1152 |
|
public void testForkJoinSingleton() { |
1153 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1154 |
< |
public void realCompute() { |
1154 |
> |
protected void realCompute() { |
1155 |
|
AsyncFib f = new AsyncFib(8); |
1156 |
|
assertSame(f, f.fork()); |
1157 |
|
assertNull(f.join()); |
1166 |
|
*/ |
1167 |
|
public void testForkGetSingleton() { |
1168 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1169 |
< |
public void realCompute() throws Exception { |
1169 |
> |
protected void realCompute() throws Exception { |
1170 |
|
AsyncFib f = new AsyncFib(8); |
1171 |
|
assertSame(f, f.fork()); |
1172 |
|
assertNull(f.get()); |
1181 |
|
*/ |
1182 |
|
public void testForkTimedGetSingleton() { |
1183 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1184 |
< |
public void realCompute() throws Exception { |
1184 |
> |
protected void realCompute() throws Exception { |
1185 |
|
AsyncFib f = new AsyncFib(8); |
1186 |
|
assertSame(f, f.fork()); |
1187 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1196 |
|
*/ |
1197 |
|
public void testForkTimedGetNPESingleton() { |
1198 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1199 |
< |
public void realCompute() throws Exception { |
1199 |
> |
protected void realCompute() throws Exception { |
1200 |
|
AsyncFib f = new AsyncFib(8); |
1201 |
|
assertSame(f, f.fork()); |
1202 |
|
try { |
1212 |
|
*/ |
1213 |
|
public void testForkQuietlyJoinSingleton() { |
1214 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1215 |
< |
public void realCompute() { |
1215 |
> |
protected void realCompute() { |
1216 |
|
AsyncFib f = new AsyncFib(8); |
1217 |
|
assertSame(f, f.fork()); |
1218 |
|
f.quietlyJoin(); |
1222 |
|
testInvokeOnPool(singletonPool(), a); |
1223 |
|
} |
1224 |
|
|
1177 |
– |
|
1225 |
|
/** |
1226 |
|
* helpQuiesce returns when tasks are complete. |
1227 |
|
* getQueuedTaskCount returns 0 when quiescent |
1228 |
|
*/ |
1229 |
|
public void testForkHelpQuiesceSingleton() { |
1230 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1231 |
< |
public void realCompute() { |
1231 |
> |
protected void realCompute() { |
1232 |
|
AsyncFib f = new AsyncFib(8); |
1233 |
|
assertSame(f, f.fork()); |
1234 |
< |
f.helpQuiesce(); |
1234 |
> |
helpQuiesce(); |
1235 |
|
assertEquals(0, getQueuedTaskCount()); |
1236 |
|
assertEquals(21, f.number); |
1237 |
|
checkCompletedNormally(f); |
1239 |
|
testInvokeOnPool(singletonPool(), a); |
1240 |
|
} |
1241 |
|
|
1195 |
– |
|
1242 |
|
/** |
1243 |
|
* invoke task throws exception when task completes abnormally |
1244 |
|
*/ |
1245 |
|
public void testAbnormalInvokeSingleton() { |
1246 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1247 |
< |
public void realCompute() { |
1247 |
> |
protected void realCompute() { |
1248 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1249 |
|
try { |
1250 |
|
f.invoke(); |
1261 |
|
*/ |
1262 |
|
public void testAbnormalQuietlyInvokeSingleton() { |
1263 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1264 |
< |
public void realCompute() { |
1264 |
> |
protected void realCompute() { |
1265 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1266 |
|
f.quietlyInvoke(); |
1267 |
|
assertTrue(f.getException() instanceof FJException); |
1275 |
|
*/ |
1276 |
|
public void testAbnormalForkJoinSingleton() { |
1277 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1278 |
< |
public void realCompute() { |
1278 |
> |
protected void realCompute() { |
1279 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1280 |
|
assertSame(f, f.fork()); |
1281 |
|
try { |
1293 |
|
*/ |
1294 |
|
public void testAbnormalForkGetSingleton() { |
1295 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1296 |
< |
public void realCompute() throws Exception { |
1296 |
> |
protected void realCompute() throws Exception { |
1297 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1298 |
|
assertSame(f, f.fork()); |
1299 |
|
try { |
1313 |
|
*/ |
1314 |
|
public void testAbnormalForkTimedGetSingleton() { |
1315 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1316 |
< |
public void realCompute() throws Exception { |
1316 |
> |
protected void realCompute() throws Exception { |
1317 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1318 |
|
assertSame(f, f.fork()); |
1319 |
|
try { |
1333 |
|
*/ |
1334 |
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1335 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1336 |
< |
public void realCompute() { |
1336 |
> |
protected void realCompute() { |
1337 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1338 |
|
assertSame(f, f.fork()); |
1339 |
|
f.quietlyJoin(); |
1348 |
|
*/ |
1349 |
|
public void testCancelledInvokeSingleton() { |
1350 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1351 |
< |
public void realCompute() { |
1351 |
> |
protected void realCompute() { |
1352 |
|
AsyncFib f = new AsyncFib(8); |
1353 |
|
assertTrue(f.cancel(true)); |
1354 |
|
try { |
1366 |
|
*/ |
1367 |
|
public void testCancelledForkJoinSingleton() { |
1368 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1369 |
< |
public void realCompute() { |
1369 |
> |
protected void realCompute() { |
1370 |
|
AsyncFib f = new AsyncFib(8); |
1371 |
|
assertTrue(f.cancel(true)); |
1372 |
|
assertSame(f, f.fork()); |
1385 |
|
*/ |
1386 |
|
public void testCancelledForkGetSingleton() { |
1387 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1388 |
< |
public void realCompute() throws Exception { |
1388 |
> |
protected void realCompute() throws Exception { |
1389 |
|
AsyncFib f = new AsyncFib(8); |
1390 |
|
assertTrue(f.cancel(true)); |
1391 |
|
assertSame(f, f.fork()); |
1404 |
|
*/ |
1405 |
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1406 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1407 |
< |
public void realCompute() throws Exception { |
1407 |
> |
protected void realCompute() throws Exception { |
1408 |
|
AsyncFib f = new AsyncFib(8); |
1409 |
|
assertTrue(f.cancel(true)); |
1410 |
|
assertSame(f, f.fork()); |
1423 |
|
*/ |
1424 |
|
public void testCancelledForkQuietlyJoinSingleton() { |
1425 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1426 |
< |
public void realCompute() { |
1426 |
> |
protected void realCompute() { |
1427 |
|
AsyncFib f = new AsyncFib(8); |
1428 |
|
assertTrue(f.cancel(true)); |
1429 |
|
assertSame(f, f.fork()); |
1438 |
|
*/ |
1439 |
|
public void testCompleteExceptionallySingleton() { |
1440 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1441 |
< |
public void realCompute() { |
1441 |
> |
protected void realCompute() { |
1442 |
|
AsyncFib f = new AsyncFib(8); |
1443 |
|
f.completeExceptionally(new FJException()); |
1444 |
|
try { |
1456 |
|
*/ |
1457 |
|
public void testInvokeAll2Singleton() { |
1458 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1459 |
< |
public void realCompute() { |
1459 |
> |
protected void realCompute() { |
1460 |
|
AsyncFib f = new AsyncFib(8); |
1461 |
|
AsyncFib g = new AsyncFib(9); |
1462 |
|
invokeAll(f, g); |
1473 |
|
*/ |
1474 |
|
public void testInvokeAll1Singleton() { |
1475 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1476 |
< |
public void realCompute() { |
1476 |
> |
protected void realCompute() { |
1477 |
|
AsyncFib f = new AsyncFib(8); |
1478 |
|
invokeAll(f); |
1479 |
|
checkCompletedNormally(f); |
1487 |
|
*/ |
1488 |
|
public void testInvokeAll3Singleton() { |
1489 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1490 |
< |
public void realCompute() { |
1490 |
> |
protected void realCompute() { |
1491 |
|
AsyncFib f = new AsyncFib(8); |
1492 |
|
AsyncFib g = new AsyncFib(9); |
1493 |
|
AsyncFib h = new AsyncFib(7); |
1507 |
|
*/ |
1508 |
|
public void testInvokeAllCollectionSingleton() { |
1509 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1510 |
< |
public void realCompute() { |
1510 |
> |
protected void realCompute() { |
1511 |
|
AsyncFib f = new AsyncFib(8); |
1512 |
|
AsyncFib g = new AsyncFib(9); |
1513 |
|
AsyncFib h = new AsyncFib(7); |
1526 |
|
testInvokeOnPool(singletonPool(), a); |
1527 |
|
} |
1528 |
|
|
1483 |
– |
|
1529 |
|
/** |
1530 |
|
* invokeAll(tasks) with any null task throws NPE |
1531 |
|
*/ |
1532 |
|
public void testInvokeAllNPESingleton() { |
1533 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1534 |
< |
public void realCompute() { |
1534 |
> |
protected void realCompute() { |
1535 |
|
AsyncFib f = new AsyncFib(8); |
1536 |
|
AsyncFib g = new AsyncFib(9); |
1537 |
|
AsyncFib h = null; |
1548 |
|
*/ |
1549 |
|
public void testAbnormalInvokeAll2Singleton() { |
1550 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1551 |
< |
public void realCompute() { |
1551 |
> |
protected void realCompute() { |
1552 |
|
AsyncFib f = new AsyncFib(8); |
1553 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1554 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1555 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1556 |
|
try { |
1557 |
< |
invokeAll(f, g); |
1557 |
> |
invokeAll(tasks); |
1558 |
|
shouldThrow(); |
1559 |
|
} catch (FJException success) { |
1560 |
|
checkCompletedAbnormally(g, success); |
1568 |
|
*/ |
1569 |
|
public void testAbnormalInvokeAll1Singleton() { |
1570 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1571 |
< |
public void realCompute() { |
1571 |
> |
protected void realCompute() { |
1572 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1573 |
|
try { |
1574 |
|
invokeAll(g); |
1585 |
|
*/ |
1586 |
|
public void testAbnormalInvokeAll3Singleton() { |
1587 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1588 |
< |
public void realCompute() { |
1588 |
> |
protected void realCompute() { |
1589 |
|
AsyncFib f = new AsyncFib(8); |
1590 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1591 |
|
AsyncFib h = new AsyncFib(7); |
1592 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1593 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1594 |
|
try { |
1595 |
< |
invokeAll(f, g, h); |
1595 |
> |
invokeAll(tasks); |
1596 |
|
shouldThrow(); |
1597 |
|
} catch (FJException success) { |
1598 |
|
checkCompletedAbnormally(g, success); |
1602 |
|
} |
1603 |
|
|
1604 |
|
/** |
1605 |
< |
* invokeAll(collection) throws exception if any task does |
1605 |
> |
* invokeAll(collection) throws exception if any task does |
1606 |
|
*/ |
1607 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1608 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1609 |
< |
public void realCompute() { |
1609 |
> |
protected void realCompute() { |
1610 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1611 |
|
AsyncFib g = new AsyncFib(9); |
1612 |
|
AsyncFib h = new AsyncFib(7); |
1613 |
< |
HashSet set = new HashSet(); |
1614 |
< |
set.add(f); |
1615 |
< |
set.add(g); |
1567 |
< |
set.add(h); |
1613 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1614 |
> |
List taskList = Arrays.asList(tasks); |
1615 |
> |
Collections.shuffle(taskList); |
1616 |
|
try { |
1617 |
< |
invokeAll(set); |
1617 |
> |
invokeAll(taskList); |
1618 |
|
shouldThrow(); |
1619 |
|
} catch (FJException success) { |
1620 |
|
checkCompletedAbnormally(f, success); |
1623 |
|
testInvokeOnPool(singletonPool(), a); |
1624 |
|
} |
1625 |
|
|
1626 |
+ |
/** |
1627 |
+ |
* ForkJoinTask.quietlyComplete returns when task completes |
1628 |
+ |
* normally without setting a value. The most recent value |
1629 |
+ |
* established by setRawResult(V) (or null by default) is returned |
1630 |
+ |
* from invoke. |
1631 |
+ |
*/ |
1632 |
+ |
public void testQuietlyComplete() { |
1633 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1634 |
+ |
protected void realCompute() { |
1635 |
+ |
AsyncFib f = new AsyncFib(8); |
1636 |
+ |
f.quietlyComplete(); |
1637 |
+ |
assertEquals(8, f.number); |
1638 |
+ |
checkCompletedNormally(f); |
1639 |
+ |
}}; |
1640 |
+ |
testInvokeOnPool(mainPool(), a); |
1641 |
+ |
} |
1642 |
+ |
|
1643 |
|
} |