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 |
|
} |
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; |
246 |
< |
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 |
|
} |
340 |
|
public final boolean exec() { |
341 |
|
AsyncFib f = this; |
342 |
|
int n = f.number; |
343 |
< |
if (n > 1) { |
344 |
< |
while (n > 1) { |
345 |
< |
AsyncFib p = f; |
346 |
< |
AsyncFib r = new AsyncFib(n - 2); |
347 |
< |
f = new AsyncFib(--n); |
348 |
< |
p.linkSubtasks(r, f); |
307 |
< |
r.fork(); |
308 |
< |
} |
309 |
< |
f.number = n; |
343 |
> |
while (n > 1) { |
344 |
> |
AsyncFib p = f; |
345 |
> |
AsyncFib r = new AsyncFib(n - 2); |
346 |
> |
f = new AsyncFib(--n); |
347 |
> |
p.linkSubtasks(r, f); |
348 |
> |
r.fork(); |
349 |
|
} |
350 |
|
f.complete(); |
351 |
|
return false; |
356 |
|
} |
357 |
|
} |
358 |
|
|
320 |
– |
|
359 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
360 |
|
int number; |
361 |
|
public FailingAsyncFib(int n) { |
365 |
|
public final boolean exec() { |
366 |
|
FailingAsyncFib f = this; |
367 |
|
int n = f.number; |
368 |
< |
if (n > 1) { |
369 |
< |
while (n > 1) { |
370 |
< |
FailingAsyncFib p = f; |
371 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
372 |
< |
f = new FailingAsyncFib(--n); |
373 |
< |
p.linkSubtasks(r, f); |
336 |
< |
r.fork(); |
337 |
< |
} |
338 |
< |
f.number = n; |
368 |
> |
while (n > 1) { |
369 |
> |
FailingAsyncFib p = f; |
370 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
371 |
> |
f = new FailingAsyncFib(--n); |
372 |
> |
p.linkSubtasks(r, f); |
373 |
> |
r.fork(); |
374 |
|
} |
375 |
|
f.complete(); |
376 |
|
return false; |
388 |
|
*/ |
389 |
|
public void testInvoke() { |
390 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
391 |
< |
public void realCompute() { |
391 |
> |
protected void realCompute() { |
392 |
|
AsyncFib f = new AsyncFib(8); |
393 |
|
assertNull(f.invoke()); |
394 |
|
assertEquals(21, f.number); |
404 |
|
*/ |
405 |
|
public void testQuietlyInvoke() { |
406 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
407 |
< |
public void realCompute() { |
407 |
> |
protected void realCompute() { |
408 |
|
AsyncFib f = new AsyncFib(8); |
409 |
|
f.quietlyInvoke(); |
410 |
|
assertEquals(21, f.number); |
418 |
|
*/ |
419 |
|
public void testForkJoin() { |
420 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
421 |
< |
public void realCompute() { |
421 |
> |
protected void realCompute() { |
422 |
|
AsyncFib f = new AsyncFib(8); |
423 |
|
assertSame(f, f.fork()); |
424 |
|
assertNull(f.join()); |
433 |
|
*/ |
434 |
|
public void testForkGet() { |
435 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
436 |
< |
public void realCompute() throws Exception { |
436 |
> |
protected void realCompute() throws Exception { |
437 |
|
AsyncFib f = new AsyncFib(8); |
438 |
|
assertSame(f, f.fork()); |
439 |
|
assertNull(f.get()); |
448 |
|
*/ |
449 |
|
public void testForkTimedGet() { |
450 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
451 |
< |
public void realCompute() throws Exception { |
451 |
> |
protected void realCompute() throws Exception { |
452 |
|
AsyncFib f = new AsyncFib(8); |
453 |
|
assertSame(f, f.fork()); |
454 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
463 |
|
*/ |
464 |
|
public void testForkTimedGetNPE() { |
465 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
466 |
< |
public void realCompute() throws Exception { |
466 |
> |
protected void realCompute() throws Exception { |
467 |
|
AsyncFib f = new AsyncFib(8); |
468 |
|
assertSame(f, f.fork()); |
469 |
|
try { |
479 |
|
*/ |
480 |
|
public void testForkQuietlyJoin() { |
481 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
482 |
< |
public void realCompute() { |
482 |
> |
protected void realCompute() { |
483 |
|
AsyncFib f = new AsyncFib(8); |
484 |
|
assertSame(f, f.fork()); |
485 |
|
f.quietlyJoin(); |
489 |
|
testInvokeOnPool(mainPool(), a); |
490 |
|
} |
491 |
|
|
457 |
– |
|
492 |
|
/** |
493 |
|
* helpQuiesce returns when tasks are complete. |
494 |
|
* getQueuedTaskCount returns 0 when quiescent |
495 |
|
*/ |
496 |
|
public void testForkHelpQuiesce() { |
497 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
498 |
< |
public void realCompute() { |
498 |
> |
protected void realCompute() { |
499 |
|
AsyncFib f = new AsyncFib(8); |
500 |
|
assertSame(f, f.fork()); |
501 |
< |
f.helpQuiesce(); |
501 |
> |
helpQuiesce(); |
502 |
|
assertEquals(21, f.number); |
503 |
|
assertEquals(0, getQueuedTaskCount()); |
504 |
|
checkCompletedNormally(f); |
506 |
|
testInvokeOnPool(mainPool(), a); |
507 |
|
} |
508 |
|
|
475 |
– |
|
509 |
|
/** |
510 |
|
* invoke task throws exception when task completes abnormally |
511 |
|
*/ |
512 |
|
public void testAbnormalInvoke() { |
513 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
514 |
< |
public void realCompute() { |
514 |
> |
protected void realCompute() { |
515 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
516 |
|
try { |
517 |
|
f.invoke(); |
528 |
|
*/ |
529 |
|
public void testAbnormalQuietlyInvoke() { |
530 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
531 |
< |
public void realCompute() { |
531 |
> |
protected void realCompute() { |
532 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
533 |
|
f.quietlyInvoke(); |
534 |
|
assertTrue(f.getException() instanceof FJException); |
542 |
|
*/ |
543 |
|
public void testAbnormalForkJoin() { |
544 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
545 |
< |
public void realCompute() { |
545 |
> |
protected void realCompute() { |
546 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
547 |
|
assertSame(f, f.fork()); |
548 |
|
try { |
560 |
|
*/ |
561 |
|
public void testAbnormalForkGet() { |
562 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
563 |
< |
public void realCompute() throws Exception { |
563 |
> |
protected void realCompute() throws Exception { |
564 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
565 |
|
assertSame(f, f.fork()); |
566 |
|
try { |
580 |
|
*/ |
581 |
|
public void testAbnormalForkTimedGet() { |
582 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
583 |
< |
public void realCompute() throws Exception { |
583 |
> |
protected void realCompute() throws Exception { |
584 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
585 |
|
assertSame(f, f.fork()); |
586 |
|
try { |
600 |
|
*/ |
601 |
|
public void testAbnormalForkQuietlyJoin() { |
602 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
603 |
< |
public void realCompute() { |
603 |
> |
protected void realCompute() { |
604 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
605 |
|
assertSame(f, f.fork()); |
606 |
|
f.quietlyJoin(); |
615 |
|
*/ |
616 |
|
public void testCancelledInvoke() { |
617 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
618 |
< |
public void realCompute() { |
618 |
> |
protected void realCompute() { |
619 |
|
AsyncFib f = new AsyncFib(8); |
620 |
|
assertTrue(f.cancel(true)); |
621 |
|
try { |
633 |
|
*/ |
634 |
|
public void testCancelledForkJoin() { |
635 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
636 |
< |
public void realCompute() { |
636 |
> |
protected void realCompute() { |
637 |
|
AsyncFib f = new AsyncFib(8); |
638 |
|
assertTrue(f.cancel(true)); |
639 |
|
assertSame(f, f.fork()); |
652 |
|
*/ |
653 |
|
public void testCancelledForkGet() { |
654 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
655 |
< |
public void realCompute() throws Exception { |
655 |
> |
protected void realCompute() throws Exception { |
656 |
|
AsyncFib f = new AsyncFib(8); |
657 |
|
assertTrue(f.cancel(true)); |
658 |
|
assertSame(f, f.fork()); |
671 |
|
*/ |
672 |
|
public void testCancelledForkTimedGet() throws Exception { |
673 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
674 |
< |
public void realCompute() throws Exception { |
674 |
> |
protected void realCompute() throws Exception { |
675 |
|
AsyncFib f = new AsyncFib(8); |
676 |
|
assertTrue(f.cancel(true)); |
677 |
|
assertSame(f, f.fork()); |
690 |
|
*/ |
691 |
|
public void testCancelledForkQuietlyJoin() { |
692 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
693 |
< |
public void realCompute() { |
693 |
> |
protected void realCompute() { |
694 |
|
AsyncFib f = new AsyncFib(8); |
695 |
|
assertTrue(f.cancel(true)); |
696 |
|
assertSame(f, f.fork()); |
706 |
|
public void testGetPool() { |
707 |
|
final ForkJoinPool mainPool = mainPool(); |
708 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
709 |
< |
public void realCompute() { |
709 |
> |
protected void realCompute() { |
710 |
|
assertSame(mainPool, getPool()); |
711 |
|
}}; |
712 |
|
testInvokeOnPool(mainPool, a); |
717 |
|
*/ |
718 |
|
public void testGetPool2() { |
719 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
720 |
< |
public void realCompute() { |
720 |
> |
protected void realCompute() { |
721 |
|
assertNull(getPool()); |
722 |
|
}}; |
723 |
|
assertNull(a.invoke()); |
728 |
|
*/ |
729 |
|
public void testInForkJoinPool() { |
730 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
731 |
< |
public void realCompute() { |
731 |
> |
protected void realCompute() { |
732 |
|
assertTrue(inForkJoinPool()); |
733 |
|
}}; |
734 |
|
testInvokeOnPool(mainPool(), a); |
739 |
|
*/ |
740 |
|
public void testInForkJoinPool2() { |
741 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
742 |
< |
public void realCompute() { |
742 |
> |
protected void realCompute() { |
743 |
|
assertFalse(inForkJoinPool()); |
744 |
|
}}; |
745 |
|
assertNull(a.invoke()); |
750 |
|
*/ |
751 |
|
public void testSetRawResult() { |
752 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
753 |
< |
public void realCompute() { |
753 |
> |
protected void realCompute() { |
754 |
|
setRawResult(null); |
755 |
+ |
assertNull(getRawResult()); |
756 |
|
}}; |
757 |
|
assertNull(a.invoke()); |
758 |
|
} |
762 |
|
*/ |
763 |
|
public void testCompleteExceptionally() { |
764 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
765 |
< |
public void realCompute() { |
765 |
> |
protected void realCompute() { |
766 |
|
AsyncFib f = new AsyncFib(8); |
767 |
|
f.completeExceptionally(new FJException()); |
768 |
|
try { |
780 |
|
*/ |
781 |
|
public void testInvokeAll2() { |
782 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
783 |
< |
public void realCompute() { |
783 |
> |
protected void realCompute() { |
784 |
|
AsyncFib f = new AsyncFib(8); |
785 |
|
AsyncFib g = new AsyncFib(9); |
786 |
|
invokeAll(f, g); |
797 |
|
*/ |
798 |
|
public void testInvokeAll1() { |
799 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
800 |
< |
public void realCompute() { |
800 |
> |
protected void realCompute() { |
801 |
|
AsyncFib f = new AsyncFib(8); |
802 |
|
invokeAll(f); |
803 |
|
checkCompletedNormally(f); |
811 |
|
*/ |
812 |
|
public void testInvokeAll3() { |
813 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
814 |
< |
public void realCompute() { |
814 |
> |
protected void realCompute() { |
815 |
|
AsyncFib f = new AsyncFib(8); |
816 |
|
AsyncFib g = new AsyncFib(9); |
817 |
|
AsyncFib h = new AsyncFib(7); |
831 |
|
*/ |
832 |
|
public void testInvokeAllCollection() { |
833 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
834 |
< |
public void realCompute() { |
834 |
> |
protected void realCompute() { |
835 |
|
AsyncFib f = new AsyncFib(8); |
836 |
|
AsyncFib g = new AsyncFib(9); |
837 |
|
AsyncFib h = new AsyncFib(7); |
850 |
|
testInvokeOnPool(mainPool(), a); |
851 |
|
} |
852 |
|
|
819 |
– |
|
853 |
|
/** |
854 |
|
* invokeAll(tasks) with any null task throws NPE |
855 |
|
*/ |
856 |
|
public void testInvokeAllNPE() { |
857 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
858 |
< |
public void realCompute() { |
858 |
> |
protected void realCompute() { |
859 |
|
AsyncFib f = new AsyncFib(8); |
860 |
|
AsyncFib g = new AsyncFib(9); |
861 |
|
AsyncFib h = null; |
872 |
|
*/ |
873 |
|
public void testAbnormalInvokeAll2() { |
874 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
875 |
< |
public void realCompute() { |
875 |
> |
protected void realCompute() { |
876 |
|
AsyncFib f = new AsyncFib(8); |
877 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
878 |
+ |
ForkJoinTask[] tasks = { f, g }; |
879 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
880 |
|
try { |
881 |
< |
invokeAll(f, g); |
881 |
> |
invokeAll(tasks); |
882 |
|
shouldThrow(); |
883 |
|
} catch (FJException success) { |
884 |
|
checkCompletedAbnormally(g, success); |
892 |
|
*/ |
893 |
|
public void testAbnormalInvokeAll1() { |
894 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
895 |
< |
public void realCompute() { |
895 |
> |
protected void realCompute() { |
896 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
897 |
|
try { |
898 |
|
invokeAll(g); |
909 |
|
*/ |
910 |
|
public void testAbnormalInvokeAll3() { |
911 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
912 |
< |
public void realCompute() { |
912 |
> |
protected void realCompute() { |
913 |
|
AsyncFib f = new AsyncFib(8); |
914 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
915 |
|
AsyncFib h = new AsyncFib(7); |
916 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
917 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
918 |
|
try { |
919 |
< |
invokeAll(f, g, h); |
919 |
> |
invokeAll(tasks); |
920 |
|
shouldThrow(); |
921 |
|
} catch (FJException success) { |
922 |
|
checkCompletedAbnormally(g, success); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
929 |
< |
* invokeAll(collection) throws exception if any task does |
929 |
> |
* invokeAll(collection) throws exception if any task does |
930 |
|
*/ |
931 |
|
public void testAbnormalInvokeAllCollection() { |
932 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
933 |
< |
public void realCompute() { |
933 |
> |
protected void realCompute() { |
934 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
935 |
|
AsyncFib g = new AsyncFib(9); |
936 |
|
AsyncFib h = new AsyncFib(7); |
937 |
< |
HashSet set = new HashSet(); |
938 |
< |
set.add(f); |
939 |
< |
set.add(g); |
903 |
< |
set.add(h); |
937 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
938 |
> |
List taskList = Arrays.asList(tasks); |
939 |
> |
Collections.shuffle(taskList); |
940 |
|
try { |
941 |
< |
invokeAll(set); |
941 |
> |
invokeAll(taskList); |
942 |
|
shouldThrow(); |
943 |
|
} catch (FJException success) { |
944 |
|
checkCompletedAbnormally(f, success); |
953 |
|
*/ |
954 |
|
public void testTryUnfork() { |
955 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
956 |
< |
public void realCompute() { |
956 |
> |
protected void realCompute() { |
957 |
|
AsyncFib g = new AsyncFib(9); |
958 |
|
assertSame(g, g.fork()); |
959 |
|
AsyncFib f = new AsyncFib(8); |
972 |
|
*/ |
973 |
|
public void testGetSurplusQueuedTaskCount() { |
974 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
975 |
< |
public void realCompute() { |
975 |
> |
protected void realCompute() { |
976 |
|
AsyncFib h = new AsyncFib(7); |
977 |
|
assertSame(h, h.fork()); |
978 |
|
AsyncFib g = new AsyncFib(9); |
994 |
|
*/ |
995 |
|
public void testPeekNextLocalTask() { |
996 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
997 |
< |
public void realCompute() { |
997 |
> |
protected void realCompute() { |
998 |
|
AsyncFib g = new AsyncFib(9); |
999 |
|
assertSame(g, g.fork()); |
1000 |
|
AsyncFib f = new AsyncFib(8); |
1014 |
|
*/ |
1015 |
|
public void testPollNextLocalTask() { |
1016 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1017 |
< |
public void realCompute() { |
1017 |
> |
protected void realCompute() { |
1018 |
|
AsyncFib g = new AsyncFib(9); |
1019 |
|
assertSame(g, g.fork()); |
1020 |
|
AsyncFib f = new AsyncFib(8); |
1033 |
|
*/ |
1034 |
|
public void testPollTask() { |
1035 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1036 |
< |
public void realCompute() { |
1036 |
> |
protected void realCompute() { |
1037 |
|
AsyncFib g = new AsyncFib(9); |
1038 |
|
assertSame(g, g.fork()); |
1039 |
|
AsyncFib f = new AsyncFib(8); |
1051 |
|
*/ |
1052 |
|
public void testPeekNextLocalTaskAsync() { |
1053 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1054 |
< |
public void realCompute() { |
1054 |
> |
protected void realCompute() { |
1055 |
|
AsyncFib g = new AsyncFib(9); |
1056 |
|
assertSame(g, g.fork()); |
1057 |
|
AsyncFib f = new AsyncFib(8); |
1072 |
|
*/ |
1073 |
|
public void testPollNextLocalTaskAsync() { |
1074 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1075 |
< |
public void realCompute() { |
1075 |
> |
protected void realCompute() { |
1076 |
|
AsyncFib g = new AsyncFib(9); |
1077 |
|
assertSame(g, g.fork()); |
1078 |
|
AsyncFib f = new AsyncFib(8); |
1092 |
|
*/ |
1093 |
|
public void testPollTaskAsync() { |
1094 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1095 |
< |
public void realCompute() { |
1095 |
> |
protected void realCompute() { |
1096 |
|
AsyncFib g = new AsyncFib(9); |
1097 |
|
assertSame(g, g.fork()); |
1098 |
|
AsyncFib f = new AsyncFib(8); |
1115 |
|
*/ |
1116 |
|
public void testInvokeSingleton() { |
1117 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1118 |
< |
public void realCompute() { |
1118 |
> |
protected void realCompute() { |
1119 |
|
AsyncFib f = new AsyncFib(8); |
1120 |
|
assertNull(f.invoke()); |
1121 |
|
assertEquals(21, f.number); |
1131 |
|
*/ |
1132 |
|
public void testQuietlyInvokeSingleton() { |
1133 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1134 |
< |
public void realCompute() { |
1134 |
> |
protected void realCompute() { |
1135 |
|
AsyncFib f = new AsyncFib(8); |
1136 |
|
f.quietlyInvoke(); |
1137 |
|
assertEquals(21, f.number); |
1145 |
|
*/ |
1146 |
|
public void testForkJoinSingleton() { |
1147 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1148 |
< |
public void realCompute() { |
1148 |
> |
protected void realCompute() { |
1149 |
|
AsyncFib f = new AsyncFib(8); |
1150 |
|
assertSame(f, f.fork()); |
1151 |
|
assertNull(f.join()); |
1160 |
|
*/ |
1161 |
|
public void testForkGetSingleton() { |
1162 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1163 |
< |
public void realCompute() throws Exception { |
1163 |
> |
protected void realCompute() throws Exception { |
1164 |
|
AsyncFib f = new AsyncFib(8); |
1165 |
|
assertSame(f, f.fork()); |
1166 |
|
assertNull(f.get()); |
1175 |
|
*/ |
1176 |
|
public void testForkTimedGetSingleton() { |
1177 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1178 |
< |
public void realCompute() throws Exception { |
1178 |
> |
protected void realCompute() throws Exception { |
1179 |
|
AsyncFib f = new AsyncFib(8); |
1180 |
|
assertSame(f, f.fork()); |
1181 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1190 |
|
*/ |
1191 |
|
public void testForkTimedGetNPESingleton() { |
1192 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1193 |
< |
public void realCompute() throws Exception { |
1193 |
> |
protected void realCompute() throws Exception { |
1194 |
|
AsyncFib f = new AsyncFib(8); |
1195 |
|
assertSame(f, f.fork()); |
1196 |
|
try { |
1206 |
|
*/ |
1207 |
|
public void testForkQuietlyJoinSingleton() { |
1208 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1209 |
< |
public void realCompute() { |
1209 |
> |
protected void realCompute() { |
1210 |
|
AsyncFib f = new AsyncFib(8); |
1211 |
|
assertSame(f, f.fork()); |
1212 |
|
f.quietlyJoin(); |
1216 |
|
testInvokeOnPool(singletonPool(), a); |
1217 |
|
} |
1218 |
|
|
1183 |
– |
|
1219 |
|
/** |
1220 |
|
* helpQuiesce returns when tasks are complete. |
1221 |
|
* getQueuedTaskCount returns 0 when quiescent |
1222 |
|
*/ |
1223 |
|
public void testForkHelpQuiesceSingleton() { |
1224 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1225 |
< |
public void realCompute() { |
1225 |
> |
protected void realCompute() { |
1226 |
|
AsyncFib f = new AsyncFib(8); |
1227 |
|
assertSame(f, f.fork()); |
1228 |
< |
f.helpQuiesce(); |
1228 |
> |
helpQuiesce(); |
1229 |
|
assertEquals(0, getQueuedTaskCount()); |
1230 |
|
assertEquals(21, f.number); |
1231 |
|
checkCompletedNormally(f); |
1233 |
|
testInvokeOnPool(singletonPool(), a); |
1234 |
|
} |
1235 |
|
|
1201 |
– |
|
1236 |
|
/** |
1237 |
|
* invoke task throws exception when task completes abnormally |
1238 |
|
*/ |
1239 |
|
public void testAbnormalInvokeSingleton() { |
1240 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1241 |
< |
public void realCompute() { |
1241 |
> |
protected void realCompute() { |
1242 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1243 |
|
try { |
1244 |
|
f.invoke(); |
1255 |
|
*/ |
1256 |
|
public void testAbnormalQuietlyInvokeSingleton() { |
1257 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1258 |
< |
public void realCompute() { |
1258 |
> |
protected void realCompute() { |
1259 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1260 |
|
f.quietlyInvoke(); |
1261 |
|
assertTrue(f.getException() instanceof FJException); |
1269 |
|
*/ |
1270 |
|
public void testAbnormalForkJoinSingleton() { |
1271 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1272 |
< |
public void realCompute() { |
1272 |
> |
protected void realCompute() { |
1273 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1274 |
|
assertSame(f, f.fork()); |
1275 |
|
try { |
1287 |
|
*/ |
1288 |
|
public void testAbnormalForkGetSingleton() { |
1289 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1290 |
< |
public void realCompute() throws Exception { |
1290 |
> |
protected void realCompute() throws Exception { |
1291 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1292 |
|
assertSame(f, f.fork()); |
1293 |
|
try { |
1307 |
|
*/ |
1308 |
|
public void testAbnormalForkTimedGetSingleton() { |
1309 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1310 |
< |
public void realCompute() throws Exception { |
1310 |
> |
protected void realCompute() throws Exception { |
1311 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1312 |
|
assertSame(f, f.fork()); |
1313 |
|
try { |
1327 |
|
*/ |
1328 |
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1329 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1330 |
< |
public void realCompute() { |
1330 |
> |
protected void realCompute() { |
1331 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1332 |
|
assertSame(f, f.fork()); |
1333 |
|
f.quietlyJoin(); |
1342 |
|
*/ |
1343 |
|
public void testCancelledInvokeSingleton() { |
1344 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1345 |
< |
public void realCompute() { |
1345 |
> |
protected void realCompute() { |
1346 |
|
AsyncFib f = new AsyncFib(8); |
1347 |
|
assertTrue(f.cancel(true)); |
1348 |
|
try { |
1360 |
|
*/ |
1361 |
|
public void testCancelledForkJoinSingleton() { |
1362 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1363 |
< |
public void realCompute() { |
1363 |
> |
protected void realCompute() { |
1364 |
|
AsyncFib f = new AsyncFib(8); |
1365 |
|
assertTrue(f.cancel(true)); |
1366 |
|
assertSame(f, f.fork()); |
1379 |
|
*/ |
1380 |
|
public void testCancelledForkGetSingleton() { |
1381 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1382 |
< |
public void realCompute() throws Exception { |
1382 |
> |
protected void realCompute() throws Exception { |
1383 |
|
AsyncFib f = new AsyncFib(8); |
1384 |
|
assertTrue(f.cancel(true)); |
1385 |
|
assertSame(f, f.fork()); |
1398 |
|
*/ |
1399 |
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1400 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1401 |
< |
public void realCompute() throws Exception { |
1401 |
> |
protected void realCompute() throws Exception { |
1402 |
|
AsyncFib f = new AsyncFib(8); |
1403 |
|
assertTrue(f.cancel(true)); |
1404 |
|
assertSame(f, f.fork()); |
1417 |
|
*/ |
1418 |
|
public void testCancelledForkQuietlyJoinSingleton() { |
1419 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1420 |
< |
public void realCompute() { |
1420 |
> |
protected void realCompute() { |
1421 |
|
AsyncFib f = new AsyncFib(8); |
1422 |
|
assertTrue(f.cancel(true)); |
1423 |
|
assertSame(f, f.fork()); |
1432 |
|
*/ |
1433 |
|
public void testCompleteExceptionallySingleton() { |
1434 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1435 |
< |
public void realCompute() { |
1435 |
> |
protected void realCompute() { |
1436 |
|
AsyncFib f = new AsyncFib(8); |
1437 |
|
f.completeExceptionally(new FJException()); |
1438 |
|
try { |
1450 |
|
*/ |
1451 |
|
public void testInvokeAll2Singleton() { |
1452 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1453 |
< |
public void realCompute() { |
1453 |
> |
protected void realCompute() { |
1454 |
|
AsyncFib f = new AsyncFib(8); |
1455 |
|
AsyncFib g = new AsyncFib(9); |
1456 |
|
invokeAll(f, g); |
1467 |
|
*/ |
1468 |
|
public void testInvokeAll1Singleton() { |
1469 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1470 |
< |
public void realCompute() { |
1470 |
> |
protected void realCompute() { |
1471 |
|
AsyncFib f = new AsyncFib(8); |
1472 |
|
invokeAll(f); |
1473 |
|
checkCompletedNormally(f); |
1481 |
|
*/ |
1482 |
|
public void testInvokeAll3Singleton() { |
1483 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1484 |
< |
public void realCompute() { |
1484 |
> |
protected void realCompute() { |
1485 |
|
AsyncFib f = new AsyncFib(8); |
1486 |
|
AsyncFib g = new AsyncFib(9); |
1487 |
|
AsyncFib h = new AsyncFib(7); |
1501 |
|
*/ |
1502 |
|
public void testInvokeAllCollectionSingleton() { |
1503 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1504 |
< |
public void realCompute() { |
1504 |
> |
protected void realCompute() { |
1505 |
|
AsyncFib f = new AsyncFib(8); |
1506 |
|
AsyncFib g = new AsyncFib(9); |
1507 |
|
AsyncFib h = new AsyncFib(7); |
1520 |
|
testInvokeOnPool(singletonPool(), a); |
1521 |
|
} |
1522 |
|
|
1489 |
– |
|
1523 |
|
/** |
1524 |
|
* invokeAll(tasks) with any null task throws NPE |
1525 |
|
*/ |
1526 |
|
public void testInvokeAllNPESingleton() { |
1527 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1528 |
< |
public void realCompute() { |
1528 |
> |
protected void realCompute() { |
1529 |
|
AsyncFib f = new AsyncFib(8); |
1530 |
|
AsyncFib g = new AsyncFib(9); |
1531 |
|
AsyncFib h = null; |
1542 |
|
*/ |
1543 |
|
public void testAbnormalInvokeAll2Singleton() { |
1544 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1545 |
< |
public void realCompute() { |
1545 |
> |
protected void realCompute() { |
1546 |
|
AsyncFib f = new AsyncFib(8); |
1547 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1548 |
+ |
ForkJoinTask[] tasks = { f, g }; |
1549 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1550 |
|
try { |
1551 |
< |
invokeAll(f, g); |
1551 |
> |
invokeAll(tasks); |
1552 |
|
shouldThrow(); |
1553 |
|
} catch (FJException success) { |
1554 |
|
checkCompletedAbnormally(g, success); |
1562 |
|
*/ |
1563 |
|
public void testAbnormalInvokeAll1Singleton() { |
1564 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1565 |
< |
public void realCompute() { |
1565 |
> |
protected void realCompute() { |
1566 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1567 |
|
try { |
1568 |
|
invokeAll(g); |
1579 |
|
*/ |
1580 |
|
public void testAbnormalInvokeAll3Singleton() { |
1581 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1582 |
< |
public void realCompute() { |
1582 |
> |
protected void realCompute() { |
1583 |
|
AsyncFib f = new AsyncFib(8); |
1584 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1585 |
|
AsyncFib h = new AsyncFib(7); |
1586 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
1587 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
1588 |
|
try { |
1589 |
< |
invokeAll(f, g, h); |
1589 |
> |
invokeAll(tasks); |
1590 |
|
shouldThrow(); |
1591 |
|
} catch (FJException success) { |
1592 |
|
checkCompletedAbnormally(g, success); |
1596 |
|
} |
1597 |
|
|
1598 |
|
/** |
1599 |
< |
* invokeAll(collection) throws exception if any task does |
1599 |
> |
* invokeAll(collection) throws exception if any task does |
1600 |
|
*/ |
1601 |
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1602 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1603 |
< |
public void realCompute() { |
1603 |
> |
protected void realCompute() { |
1604 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
1605 |
|
AsyncFib g = new AsyncFib(9); |
1606 |
|
AsyncFib h = new AsyncFib(7); |
1607 |
< |
HashSet set = new HashSet(); |
1608 |
< |
set.add(f); |
1609 |
< |
set.add(g); |
1573 |
< |
set.add(h); |
1607 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
1608 |
> |
List taskList = Arrays.asList(tasks); |
1609 |
> |
Collections.shuffle(taskList); |
1610 |
|
try { |
1611 |
< |
invokeAll(set); |
1611 |
> |
invokeAll(taskList); |
1612 |
|
shouldThrow(); |
1613 |
|
} catch (FJException success) { |
1614 |
|
checkCompletedAbnormally(f, success); |
1617 |
|
testInvokeOnPool(singletonPool(), a); |
1618 |
|
} |
1619 |
|
|
1620 |
+ |
/** |
1621 |
+ |
* ForkJoinTask.quietlyComplete returns when task completes |
1622 |
+ |
* normally without setting a value. The most recent value |
1623 |
+ |
* established by setRawResult(V) (or null by default) is returned |
1624 |
+ |
* from invoke. |
1625 |
+ |
*/ |
1626 |
+ |
public void testQuietlyComplete() { |
1627 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1628 |
+ |
protected void realCompute() { |
1629 |
+ |
AsyncFib f = new AsyncFib(8); |
1630 |
+ |
f.quietlyComplete(); |
1631 |
+ |
assertEquals(8, f.number); |
1632 |
+ |
checkCompletedNormally(f); |
1633 |
+ |
}}; |
1634 |
+ |
testInvokeOnPool(mainPool(), a); |
1635 |
+ |
} |
1636 |
+ |
|
1637 |
|
} |