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 { |
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 |
|
} |
352 |
|
} |
353 |
|
} |
354 |
|
|
320 |
– |
|
355 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
356 |
|
int number; |
357 |
|
public FailingAsyncFib(int n) { |
387 |
|
*/ |
388 |
|
public void testInvoke() { |
389 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
390 |
< |
public void realCompute() { |
390 |
> |
protected void realCompute() { |
391 |
|
AsyncFib f = new AsyncFib(8); |
392 |
|
assertNull(f.invoke()); |
393 |
|
assertEquals(21, f.number); |
403 |
|
*/ |
404 |
|
public void testQuietlyInvoke() { |
405 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
406 |
< |
public void realCompute() { |
406 |
> |
protected void realCompute() { |
407 |
|
AsyncFib f = new AsyncFib(8); |
408 |
|
f.quietlyInvoke(); |
409 |
|
assertEquals(21, f.number); |
417 |
|
*/ |
418 |
|
public void testForkJoin() { |
419 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
420 |
< |
public void realCompute() { |
420 |
> |
protected void realCompute() { |
421 |
|
AsyncFib f = new AsyncFib(8); |
422 |
|
assertSame(f, f.fork()); |
423 |
|
assertNull(f.join()); |
432 |
|
*/ |
433 |
|
public void testForkGet() { |
434 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
435 |
< |
public void realCompute() throws Exception { |
435 |
> |
protected void realCompute() throws Exception { |
436 |
|
AsyncFib f = new AsyncFib(8); |
437 |
|
assertSame(f, f.fork()); |
438 |
|
assertNull(f.get()); |
447 |
|
*/ |
448 |
|
public void testForkTimedGet() { |
449 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
450 |
< |
public void realCompute() throws Exception { |
450 |
> |
protected void realCompute() throws Exception { |
451 |
|
AsyncFib f = new AsyncFib(8); |
452 |
|
assertSame(f, f.fork()); |
453 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
462 |
|
*/ |
463 |
|
public void testForkTimedGetNPE() { |
464 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
465 |
< |
public void realCompute() throws Exception { |
465 |
> |
protected void realCompute() throws Exception { |
466 |
|
AsyncFib f = new AsyncFib(8); |
467 |
|
assertSame(f, f.fork()); |
468 |
|
try { |
478 |
|
*/ |
479 |
|
public void testForkQuietlyJoin() { |
480 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
481 |
< |
public void realCompute() { |
481 |
> |
protected void realCompute() { |
482 |
|
AsyncFib f = new AsyncFib(8); |
483 |
|
assertSame(f, f.fork()); |
484 |
|
f.quietlyJoin(); |
488 |
|
testInvokeOnPool(mainPool(), a); |
489 |
|
} |
490 |
|
|
457 |
– |
|
491 |
|
/** |
492 |
|
* helpQuiesce returns when tasks are complete. |
493 |
|
* getQueuedTaskCount returns 0 when quiescent |
494 |
|
*/ |
495 |
|
public void testForkHelpQuiesce() { |
496 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
497 |
< |
public void realCompute() { |
497 |
> |
protected void realCompute() { |
498 |
|
AsyncFib f = new AsyncFib(8); |
499 |
|
assertSame(f, f.fork()); |
500 |
< |
f.helpQuiesce(); |
500 |
> |
helpQuiesce(); |
501 |
|
assertEquals(21, f.number); |
502 |
|
assertEquals(0, getQueuedTaskCount()); |
503 |
|
checkCompletedNormally(f); |
505 |
|
testInvokeOnPool(mainPool(), a); |
506 |
|
} |
507 |
|
|
475 |
– |
|
508 |
|
/** |
509 |
|
* invoke task throws exception when task completes abnormally |
510 |
|
*/ |
511 |
|
public void testAbnormalInvoke() { |
512 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
513 |
< |
public void realCompute() { |
513 |
> |
protected void realCompute() { |
514 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
515 |
|
try { |
516 |
|
f.invoke(); |
527 |
|
*/ |
528 |
|
public void testAbnormalQuietlyInvoke() { |
529 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
530 |
< |
public void realCompute() { |
530 |
> |
protected void realCompute() { |
531 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
532 |
|
f.quietlyInvoke(); |
533 |
|
assertTrue(f.getException() instanceof FJException); |
541 |
|
*/ |
542 |
|
public void testAbnormalForkJoin() { |
543 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
544 |
< |
public void realCompute() { |
544 |
> |
protected void realCompute() { |
545 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
546 |
|
assertSame(f, f.fork()); |
547 |
|
try { |
559 |
|
*/ |
560 |
|
public void testAbnormalForkGet() { |
561 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
562 |
< |
public void realCompute() throws Exception { |
562 |
> |
protected void realCompute() throws Exception { |
563 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
564 |
|
assertSame(f, f.fork()); |
565 |
|
try { |
579 |
|
*/ |
580 |
|
public void testAbnormalForkTimedGet() { |
581 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
582 |
< |
public void realCompute() throws Exception { |
582 |
> |
protected void realCompute() throws Exception { |
583 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
584 |
|
assertSame(f, f.fork()); |
585 |
|
try { |
599 |
|
*/ |
600 |
|
public void testAbnormalForkQuietlyJoin() { |
601 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
602 |
< |
public void realCompute() { |
602 |
> |
protected void realCompute() { |
603 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
604 |
|
assertSame(f, f.fork()); |
605 |
|
f.quietlyJoin(); |
614 |
|
*/ |
615 |
|
public void testCancelledInvoke() { |
616 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
617 |
< |
public void realCompute() { |
617 |
> |
protected void realCompute() { |
618 |
|
AsyncFib f = new AsyncFib(8); |
619 |
|
assertTrue(f.cancel(true)); |
620 |
|
try { |
632 |
|
*/ |
633 |
|
public void testCancelledForkJoin() { |
634 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
635 |
< |
public void realCompute() { |
635 |
> |
protected void realCompute() { |
636 |
|
AsyncFib f = new AsyncFib(8); |
637 |
|
assertTrue(f.cancel(true)); |
638 |
|
assertSame(f, f.fork()); |
651 |
|
*/ |
652 |
|
public void testCancelledForkGet() { |
653 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
654 |
< |
public void realCompute() throws Exception { |
654 |
> |
protected void realCompute() throws Exception { |
655 |
|
AsyncFib f = new AsyncFib(8); |
656 |
|
assertTrue(f.cancel(true)); |
657 |
|
assertSame(f, f.fork()); |
670 |
|
*/ |
671 |
|
public void testCancelledForkTimedGet() throws Exception { |
672 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
673 |
< |
public void realCompute() throws Exception { |
673 |
> |
protected void realCompute() throws Exception { |
674 |
|
AsyncFib f = new AsyncFib(8); |
675 |
|
assertTrue(f.cancel(true)); |
676 |
|
assertSame(f, f.fork()); |
689 |
|
*/ |
690 |
|
public void testCancelledForkQuietlyJoin() { |
691 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
692 |
< |
public void realCompute() { |
692 |
> |
protected void realCompute() { |
693 |
|
AsyncFib f = new AsyncFib(8); |
694 |
|
assertTrue(f.cancel(true)); |
695 |
|
assertSame(f, f.fork()); |
705 |
|
public void testGetPool() { |
706 |
|
final ForkJoinPool mainPool = mainPool(); |
707 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
708 |
< |
public void realCompute() { |
708 |
> |
protected void realCompute() { |
709 |
|
assertSame(mainPool, getPool()); |
710 |
|
}}; |
711 |
|
testInvokeOnPool(mainPool, a); |
716 |
|
*/ |
717 |
|
public void testGetPool2() { |
718 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
719 |
< |
public void realCompute() { |
719 |
> |
protected void realCompute() { |
720 |
|
assertNull(getPool()); |
721 |
|
}}; |
722 |
|
assertNull(a.invoke()); |
727 |
|
*/ |
728 |
|
public void testInForkJoinPool() { |
729 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
730 |
< |
public void realCompute() { |
730 |
> |
protected void realCompute() { |
731 |
|
assertTrue(inForkJoinPool()); |
732 |
|
}}; |
733 |
|
testInvokeOnPool(mainPool(), a); |
738 |
|
*/ |
739 |
|
public void testInForkJoinPool2() { |
740 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
741 |
< |
public void realCompute() { |
741 |
> |
protected void realCompute() { |
742 |
|
assertFalse(inForkJoinPool()); |
743 |
|
}}; |
744 |
|
assertNull(a.invoke()); |
749 |
|
*/ |
750 |
|
public void testSetRawResult() { |
751 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
752 |
< |
public void realCompute() { |
752 |
> |
protected void realCompute() { |
753 |
|
setRawResult(null); |
754 |
|
assertNull(getRawResult()); |
755 |
|
}}; |
761 |
|
*/ |
762 |
|
public void testCompleteExceptionally() { |
763 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
764 |
< |
public void realCompute() { |
764 |
> |
protected void realCompute() { |
765 |
|
AsyncFib f = new AsyncFib(8); |
766 |
|
f.completeExceptionally(new FJException()); |
767 |
|
try { |
779 |
|
*/ |
780 |
|
public void testInvokeAll2() { |
781 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
782 |
< |
public void realCompute() { |
782 |
> |
protected void realCompute() { |
783 |
|
AsyncFib f = new AsyncFib(8); |
784 |
|
AsyncFib g = new AsyncFib(9); |
785 |
|
invokeAll(f, g); |
796 |
|
*/ |
797 |
|
public void testInvokeAll1() { |
798 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
799 |
< |
public void realCompute() { |
799 |
> |
protected void realCompute() { |
800 |
|
AsyncFib f = new AsyncFib(8); |
801 |
|
invokeAll(f); |
802 |
|
checkCompletedNormally(f); |
810 |
|
*/ |
811 |
|
public void testInvokeAll3() { |
812 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
813 |
< |
public void realCompute() { |
813 |
> |
protected void realCompute() { |
814 |
|
AsyncFib f = new AsyncFib(8); |
815 |
|
AsyncFib g = new AsyncFib(9); |
816 |
|
AsyncFib h = new AsyncFib(7); |
830 |
|
*/ |
831 |
|
public void testInvokeAllCollection() { |
832 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
833 |
< |
public void realCompute() { |
833 |
> |
protected void realCompute() { |
834 |
|
AsyncFib f = new AsyncFib(8); |
835 |
|
AsyncFib g = new AsyncFib(9); |
836 |
|
AsyncFib h = new AsyncFib(7); |
849 |
|
testInvokeOnPool(mainPool(), a); |
850 |
|
} |
851 |
|
|
820 |
– |
|
852 |
|
/** |
853 |
|
* invokeAll(tasks) with any null task throws NPE |
854 |
|
*/ |
855 |
|
public void testInvokeAllNPE() { |
856 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
857 |
< |
public void realCompute() { |
857 |
> |
protected void realCompute() { |
858 |
|
AsyncFib f = new AsyncFib(8); |
859 |
|
AsyncFib g = new AsyncFib(9); |
860 |
|
AsyncFib h = null; |
871 |
|
*/ |
872 |
|
public void testAbnormalInvokeAll2() { |
873 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
874 |
< |
public void realCompute() { |
874 |
> |
protected void realCompute() { |
875 |
|
AsyncFib f = new AsyncFib(8); |
876 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
877 |
+ |
ForkJoinTask[] tasks = { f, g }; |
878 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
879 |
|
try { |
880 |
< |
invokeAll(f, g); |
880 |
> |
invokeAll(tasks); |
881 |
|
shouldThrow(); |
882 |
|
} catch (FJException success) { |
883 |
|
checkCompletedAbnormally(g, success); |
891 |
|
*/ |
892 |
|
public void testAbnormalInvokeAll1() { |
893 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
894 |
< |
public void realCompute() { |
894 |
> |
protected void realCompute() { |
895 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
896 |
|
try { |
897 |
|
invokeAll(g); |
908 |
|
*/ |
909 |
|
public void testAbnormalInvokeAll3() { |
910 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
911 |
< |
public void realCompute() { |
911 |
> |
protected void realCompute() { |
912 |
|
AsyncFib f = new AsyncFib(8); |
913 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
914 |
|
AsyncFib h = new AsyncFib(7); |
915 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
916 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
917 |
|
try { |
918 |
< |
invokeAll(f, g, h); |
918 |
> |
invokeAll(tasks); |
919 |
|
shouldThrow(); |
920 |
|
} catch (FJException success) { |
921 |
|
checkCompletedAbnormally(g, success); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
928 |
< |
* invokeAll(collection) throws exception if any task does |
928 |
> |
* invokeAll(collection) throws exception if any task does |
929 |
|
*/ |
930 |
|
public void testAbnormalInvokeAllCollection() { |
931 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
932 |
< |
public void realCompute() { |
932 |
> |
protected void realCompute() { |
933 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
934 |
|
AsyncFib g = new AsyncFib(9); |
935 |
|
AsyncFib h = new AsyncFib(7); |
936 |
< |
HashSet set = new HashSet(); |
937 |
< |
set.add(f); |
938 |
< |
set.add(g); |
904 |
< |
set.add(h); |
936 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
937 |
> |
List taskList = Arrays.asList(tasks); |
938 |
> |
Collections.shuffle(taskList); |
939 |
|
try { |
940 |
< |
invokeAll(set); |
940 |
> |
invokeAll(taskList); |
941 |
|
shouldThrow(); |
942 |
|
} catch (FJException success) { |
943 |
|
checkCompletedAbnormally(f, success); |
952 |
|
*/ |
953 |
|
public void testTryUnfork() { |
954 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
955 |
< |
public void realCompute() { |
955 |
> |
protected void realCompute() { |
956 |
|
AsyncFib g = new AsyncFib(9); |
957 |
|
assertSame(g, g.fork()); |
958 |
|
AsyncFib f = new AsyncFib(8); |
971 |
|
*/ |
972 |
|
public void testGetSurplusQueuedTaskCount() { |
973 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
974 |
< |
public void realCompute() { |
974 |
> |
protected void realCompute() { |
975 |
|
AsyncFib h = new AsyncFib(7); |
976 |
|
assertSame(h, h.fork()); |
977 |
|
AsyncFib g = new AsyncFib(9); |
993 |
|
*/ |
994 |
|
public void testPeekNextLocalTask() { |
995 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
996 |
< |
public void realCompute() { |
996 |
> |
protected void realCompute() { |
997 |
|
AsyncFib g = new AsyncFib(9); |
998 |
|
assertSame(g, g.fork()); |
999 |
|
AsyncFib f = new AsyncFib(8); |
1013 |
|
*/ |
1014 |
|
public void testPollNextLocalTask() { |
1015 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1016 |
< |
public void realCompute() { |
1016 |
> |
protected void realCompute() { |
1017 |
|
AsyncFib g = new AsyncFib(9); |
1018 |
|
assertSame(g, g.fork()); |
1019 |
|
AsyncFib f = new AsyncFib(8); |
1032 |
|
*/ |
1033 |
|
public void testPollTask() { |
1034 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1035 |
< |
public void realCompute() { |
1035 |
> |
protected void realCompute() { |
1036 |
|
AsyncFib g = new AsyncFib(9); |
1037 |
|
assertSame(g, g.fork()); |
1038 |
|
AsyncFib f = new AsyncFib(8); |
1050 |
|
*/ |
1051 |
|
public void testPeekNextLocalTaskAsync() { |
1052 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1053 |
< |
public void realCompute() { |
1053 |
> |
protected void realCompute() { |
1054 |
|
AsyncFib g = new AsyncFib(9); |
1055 |
|
assertSame(g, g.fork()); |
1056 |
|
AsyncFib f = new AsyncFib(8); |
1071 |
|
*/ |
1072 |
|
public void testPollNextLocalTaskAsync() { |
1073 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1074 |
< |
public void realCompute() { |
1074 |
> |
protected void realCompute() { |
1075 |
|
AsyncFib g = new AsyncFib(9); |
1076 |
|
assertSame(g, g.fork()); |
1077 |
|
AsyncFib f = new AsyncFib(8); |
1091 |
|
*/ |
1092 |
|
public void testPollTaskAsync() { |
1093 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1094 |
< |
public void realCompute() { |
1094 |
> |
protected void realCompute() { |
1095 |
|
AsyncFib g = new AsyncFib(9); |
1096 |
|
assertSame(g, g.fork()); |
1097 |
|
AsyncFib f = new AsyncFib(8); |
1114 |
|
*/ |
1115 |
|
public void testInvokeSingleton() { |
1116 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1117 |
< |
public void realCompute() { |
1117 |
> |
protected void realCompute() { |
1118 |
|
AsyncFib f = new AsyncFib(8); |
1119 |
|
assertNull(f.invoke()); |
1120 |
|
assertEquals(21, f.number); |
1130 |
|
*/ |
1131 |
|
public void testQuietlyInvokeSingleton() { |
1132 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1133 |
< |
public void realCompute() { |
1133 |
> |
protected void realCompute() { |
1134 |
|
AsyncFib f = new AsyncFib(8); |
1135 |
|
f.quietlyInvoke(); |
1136 |
|
assertEquals(21, f.number); |
1144 |
|
*/ |
1145 |
|
public void testForkJoinSingleton() { |
1146 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1147 |
< |
public void realCompute() { |
1147 |
> |
protected void realCompute() { |
1148 |
|
AsyncFib f = new AsyncFib(8); |
1149 |
|
assertSame(f, f.fork()); |
1150 |
|
assertNull(f.join()); |
1159 |
|
*/ |
1160 |
|
public void testForkGetSingleton() { |
1161 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1162 |
< |
public void realCompute() throws Exception { |
1162 |
> |
protected void realCompute() throws Exception { |
1163 |
|
AsyncFib f = new AsyncFib(8); |
1164 |
|
assertSame(f, f.fork()); |
1165 |
|
assertNull(f.get()); |
1174 |
|
*/ |
1175 |
|
public void testForkTimedGetSingleton() { |
1176 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1177 |
< |
public void realCompute() throws Exception { |
1177 |
> |
protected void realCompute() throws Exception { |
1178 |
|
AsyncFib f = new AsyncFib(8); |
1179 |
|
assertSame(f, f.fork()); |
1180 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1189 |
|
*/ |
1190 |
|
public void testForkTimedGetNPESingleton() { |
1191 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1192 |
< |
public void realCompute() throws Exception { |
1192 |
> |
protected void realCompute() throws Exception { |
1193 |
|
AsyncFib f = new AsyncFib(8); |
1194 |
|
assertSame(f, f.fork()); |
1195 |
|
try { |
1205 |
|
*/ |
1206 |
|
public void testForkQuietlyJoinSingleton() { |
1207 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1208 |
< |
public void realCompute() { |
1208 |
> |
protected void realCompute() { |
1209 |
|
AsyncFib f = new AsyncFib(8); |
1210 |
|
assertSame(f, f.fork()); |
1211 |
|
f.quietlyJoin(); |
1215 |
|
testInvokeOnPool(singletonPool(), a); |
1216 |
|
} |
1217 |
|
|
1184 |
– |
|
1218 |
|
/** |
1219 |
|
* helpQuiesce returns when tasks are complete. |
1220 |
|
* getQueuedTaskCount returns 0 when quiescent |
1221 |
|
*/ |
1222 |
|
public void testForkHelpQuiesceSingleton() { |
1223 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1224 |
< |
public void realCompute() { |
1224 |
> |
protected void realCompute() { |
1225 |
|
AsyncFib f = new AsyncFib(8); |
1226 |
|
assertSame(f, f.fork()); |
1227 |
< |
f.helpQuiesce(); |
1227 |
> |
helpQuiesce(); |
1228 |
|
assertEquals(0, getQueuedTaskCount()); |
1229 |
|
assertEquals(21, f.number); |
1230 |
|
checkCompletedNormally(f); |
1232 |
|
testInvokeOnPool(singletonPool(), a); |
1233 |
|
} |
1234 |
|
|
1202 |
– |
|
1235 |
|
/** |
1236 |
|
* invoke task throws exception when task completes abnormally |
1237 |
|
*/ |
1238 |
|
public void testAbnormalInvokeSingleton() { |
1239 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1240 |
< |
public void realCompute() { |
1240 |
> |
protected void realCompute() { |
1241 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1242 |
|
try { |
1243 |
|
f.invoke(); |
1254 |
|
*/ |
1255 |
|
public void testAbnormalQuietlyInvokeSingleton() { |
1256 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1257 |
< |
public void realCompute() { |
1257 |
> |
protected void realCompute() { |
1258 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1259 |
|
f.quietlyInvoke(); |
1260 |
|
assertTrue(f.getException() instanceof FJException); |
1268 |
|
*/ |
1269 |
|
public void testAbnormalForkJoinSingleton() { |
1270 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1271 |
< |
public void realCompute() { |
1271 |
> |
protected void realCompute() { |
1272 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1273 |
|
assertSame(f, f.fork()); |
1274 |
|
try { |
1286 |
|
*/ |
1287 |
|
public void testAbnormalForkGetSingleton() { |
1288 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1289 |
< |
public void realCompute() throws Exception { |
1289 |
> |
protected void realCompute() throws Exception { |
1290 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1291 |
|
assertSame(f, f.fork()); |
1292 |
|
try { |
1306 |
|
*/ |
1307 |
|
public void testAbnormalForkTimedGetSingleton() { |
1308 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1309 |
< |
public void realCompute() throws Exception { |
1309 |
> |
protected void realCompute() throws Exception { |
1310 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1311 |
|
assertSame(f, f.fork()); |
1312 |
|
try { |
1326 |
|
*/ |
1327 |
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1328 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1329 |
< |
public void realCompute() { |
1329 |
> |
protected void realCompute() { |
1330 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1331 |
|
assertSame(f, f.fork()); |
1332 |
|
f.quietlyJoin(); |
1341 |
|
*/ |
1342 |
|
public void testCancelledInvokeSingleton() { |
1343 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1344 |
< |
public void realCompute() { |
1344 |
> |
protected void realCompute() { |
1345 |
|
AsyncFib f = new AsyncFib(8); |
1346 |
|
assertTrue(f.cancel(true)); |
1347 |
|
try { |
1359 |
|
*/ |
1360 |
|
public void testCancelledForkJoinSingleton() { |
1361 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1362 |
< |
public void realCompute() { |
1362 |
> |
protected void realCompute() { |
1363 |
|
AsyncFib f = new AsyncFib(8); |
1364 |
|
assertTrue(f.cancel(true)); |
1365 |
|
assertSame(f, f.fork()); |
1378 |
|
*/ |
1379 |
|
public void testCancelledForkGetSingleton() { |
1380 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1381 |
< |
public void realCompute() throws Exception { |
1381 |
> |
protected void realCompute() throws Exception { |
1382 |
|
AsyncFib f = new AsyncFib(8); |
1383 |
|
assertTrue(f.cancel(true)); |
1384 |
|
assertSame(f, f.fork()); |
1397 |
|
*/ |
1398 |
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1399 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1400 |
< |
public void realCompute() throws Exception { |
1400 |
> |
protected void realCompute() throws Exception { |
1401 |
|
AsyncFib f = new AsyncFib(8); |
1402 |
|
assertTrue(f.cancel(true)); |
1403 |
|
assertSame(f, f.fork()); |
1416 |
|
*/ |
1417 |
|
public void testCancelledForkQuietlyJoinSingleton() { |
1418 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1419 |
< |
public void realCompute() { |
1419 |
> |
protected void realCompute() { |
1420 |
|
AsyncFib f = new AsyncFib(8); |
1421 |
|
assertTrue(f.cancel(true)); |
1422 |
|
assertSame(f, f.fork()); |
1431 |
|
*/ |
1432 |
|
public void testCompleteExceptionallySingleton() { |
1433 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1434 |
< |
public void realCompute() { |
1434 |
> |
protected void realCompute() { |
1435 |
|
AsyncFib f = new AsyncFib(8); |
1436 |
|
f.completeExceptionally(new FJException()); |
1437 |
|
try { |
1449 |
|
*/ |
1450 |
|
public void testInvokeAll2Singleton() { |
1451 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1452 |
< |
public void realCompute() { |
1452 |
> |
protected void realCompute() { |
1453 |
|
AsyncFib f = new AsyncFib(8); |
1454 |
|
AsyncFib g = new AsyncFib(9); |
1455 |
|
invokeAll(f, g); |
1466 |
|
*/ |
1467 |
|
public void testInvokeAll1Singleton() { |
1468 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1469 |
< |
public void realCompute() { |
1469 |
> |
protected void realCompute() { |
1470 |
|
AsyncFib f = new AsyncFib(8); |
1471 |
|
invokeAll(f); |
1472 |
|
checkCompletedNormally(f); |
1480 |
|
*/ |
1481 |
|
public void testInvokeAll3Singleton() { |
1482 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1483 |
< |
public void realCompute() { |
1483 |
> |
protected void realCompute() { |
1484 |
|
AsyncFib f = new AsyncFib(8); |
1485 |
|
AsyncFib g = new AsyncFib(9); |
1486 |
|
AsyncFib h = new AsyncFib(7); |
1500 |
|
*/ |
1501 |
|
public void testInvokeAllCollectionSingleton() { |
1502 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1503 |
< |
public void realCompute() { |
1503 |
> |
protected void realCompute() { |
1504 |
|
AsyncFib f = new AsyncFib(8); |
1505 |
|
AsyncFib g = new AsyncFib(9); |
1506 |
|
AsyncFib h = new AsyncFib(7); |
1519 |
|
testInvokeOnPool(singletonPool(), a); |
1520 |
|
} |
1521 |
|
|
1490 |
– |
|
1522 |
|
/** |
1523 |
|
* invokeAll(tasks) with any null task throws NPE |
1524 |
|
*/ |
1525 |
|
public void testInvokeAllNPESingleton() { |
1526 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1527 |
< |
public void realCompute() { |
1527 |
> |
protected void realCompute() { |
1528 |
|
AsyncFib f = new AsyncFib(8); |
1529 |
|
AsyncFib g = new AsyncFib(9); |
1530 |
|
AsyncFib h = null; |
1541 |
|
*/ |
1542 |
|
public void testAbnormalInvokeAll2Singleton() { |
1543 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1544 |
< |
public void realCompute() { |
1544 |
> |
protected void realCompute() { |
1545 |
|
AsyncFib f = new AsyncFib(8); |
1546 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1547 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1548 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1549 |
|
try { |
1550 |
< |
invokeAll(f, g); |
1550 |
> |
invokeAll(tasks); |
1551 |
|
shouldThrow(); |
1552 |
|
} catch (FJException success) { |
1553 |
|
checkCompletedAbnormally(g, success); |
1561 |
|
*/ |
1562 |
|
public void testAbnormalInvokeAll1Singleton() { |
1563 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1564 |
< |
public void realCompute() { |
1564 |
> |
protected void realCompute() { |
1565 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1566 |
|
try { |
1567 |
|
invokeAll(g); |
1578 |
|
*/ |
1579 |
|
public void testAbnormalInvokeAll3Singleton() { |
1580 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1581 |
< |
public void realCompute() { |
1581 |
> |
protected void realCompute() { |
1582 |
|
AsyncFib f = new AsyncFib(8); |
1583 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1584 |
|
AsyncFib h = new AsyncFib(7); |
1585 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1586 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1587 |
|
try { |
1588 |
< |
invokeAll(f, g, h); |
1588 |
> |
invokeAll(tasks); |
1589 |
|
shouldThrow(); |
1590 |
|
} catch (FJException success) { |
1591 |
|
checkCompletedAbnormally(g, success); |
1595 |
|
} |
1596 |
|
|
1597 |
|
/** |
1598 |
< |
* invokeAll(collection) throws exception if any task does |
1598 |
> |
* invokeAll(collection) throws exception if any task does |
1599 |
|
*/ |
1600 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1601 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1602 |
< |
public void realCompute() { |
1602 |
> |
protected void realCompute() { |
1603 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1604 |
|
AsyncFib g = new AsyncFib(9); |
1605 |
|
AsyncFib h = new AsyncFib(7); |
1606 |
< |
HashSet set = new HashSet(); |
1607 |
< |
set.add(f); |
1608 |
< |
set.add(g); |
1574 |
< |
set.add(h); |
1606 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1607 |
> |
List taskList = Arrays.asList(tasks); |
1608 |
> |
Collections.shuffle(taskList); |
1609 |
|
try { |
1610 |
< |
invokeAll(set); |
1610 |
> |
invokeAll(taskList); |
1611 |
|
shouldThrow(); |
1612 |
|
} catch (FJException success) { |
1613 |
|
checkCompletedAbnormally(f, success); |
1616 |
|
testInvokeOnPool(singletonPool(), a); |
1617 |
|
} |
1618 |
|
|
1619 |
+ |
/** |
1620 |
+ |
* ForkJoinTask.quietlyComplete returns when task completes |
1621 |
+ |
* normally without setting a value. The most recent value |
1622 |
+ |
* established by setRawResult(V) (or null by default) is returned |
1623 |
+ |
* from invoke. |
1624 |
+ |
*/ |
1625 |
+ |
public void testQuietlyComplete() { |
1626 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1627 |
+ |
protected void realCompute() { |
1628 |
+ |
AsyncFib f = new AsyncFib(8); |
1629 |
+ |
f.quietlyComplete(); |
1630 |
+ |
assertEquals(8, f.number); |
1631 |
+ |
checkCompletedNormally(f); |
1632 |
+ |
}}; |
1633 |
+ |
testInvokeOnPool(mainPool(), a); |
1634 |
+ |
} |
1635 |
+ |
|
1636 |
|
} |