3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
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.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.ExecutionException; |
7 |
– |
import java.util.concurrent.CancellationException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
16 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
17 |
|
import java.util.concurrent.RecursiveAction; |
12 |
– |
import java.util.concurrent.TimeUnit; |
18 |
|
import java.util.concurrent.TimeoutException; |
19 |
< |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
17 |
< |
import java.util.HashSet; |
18 |
< |
import junit.framework.*; |
19 |
> |
|
20 |
> |
import junit.framework.Test; |
21 |
> |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ForkJoinTask8Test extends JSR166TestCase { |
24 |
|
|
37 |
|
static final short EXCEPTION_STATE = 1; |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
62 |
|
null, true); |
63 |
|
} |
64 |
|
|
65 |
+ |
// Compute fib naively and efficiently |
66 |
+ |
final int[] fib; |
67 |
+ |
{ |
68 |
+ |
int[] fib = new int[10]; |
69 |
+ |
fib[0] = 0; |
70 |
+ |
fib[1] = 1; |
71 |
+ |
for (int i = 2; i < fib.length; i++) |
72 |
+ |
fib[i] = fib[i - 1] + fib[i - 2]; |
73 |
+ |
this.fib = fib; |
74 |
+ |
} |
75 |
+ |
|
76 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
77 |
< |
try { |
77 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
78 |
|
assertFalse(a.isDone()); |
79 |
|
assertFalse(a.isCompletedNormally()); |
80 |
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
assertFalse(a.isCancelled()); |
91 |
|
assertNull(a.getException()); |
92 |
|
assertNull(a.getRawResult()); |
79 |
– |
} finally { |
80 |
– |
joinPool(pool); |
93 |
|
} |
94 |
|
} |
95 |
|
|
126 |
|
|
127 |
|
{ |
128 |
|
Thread.currentThread().interrupt(); |
129 |
< |
long t0 = System.nanoTime(); |
129 |
> |
long startTime = System.nanoTime(); |
130 |
|
assertSame(expected, a.join()); |
131 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
131 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
132 |
|
Thread.interrupted(); |
133 |
|
} |
134 |
|
|
135 |
|
{ |
136 |
|
Thread.currentThread().interrupt(); |
137 |
< |
long t0 = System.nanoTime(); |
137 |
> |
long startTime = System.nanoTime(); |
138 |
|
a.quietlyJoin(); // should be no-op |
139 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
139 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
140 |
|
Thread.interrupted(); |
141 |
|
} |
142 |
|
|
172 |
|
Thread.interrupted(); |
173 |
|
|
174 |
|
{ |
175 |
< |
long t0 = System.nanoTime(); |
175 |
> |
long startTime = System.nanoTime(); |
176 |
|
a.quietlyJoin(); // should be no-op |
177 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
177 |
> |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
178 |
|
} |
179 |
|
|
180 |
|
try { |
192 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
193 |
|
} |
194 |
|
|
183 |
– |
|
195 |
|
public static final class FJException extends RuntimeException { |
196 |
|
FJException() { super(); } |
197 |
|
} |
198 |
|
|
199 |
|
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
200 |
|
|
201 |
< |
private BinaryAsyncAction parent; |
201 |
> |
private volatile BinaryAsyncAction parent; |
202 |
|
|
203 |
< |
private BinaryAsyncAction sibling; |
203 |
> |
private volatile BinaryAsyncAction sibling; |
204 |
|
|
205 |
|
protected BinaryAsyncAction() { |
206 |
|
setForkJoinTaskTag(INITIAL_STATE); |
243 |
|
super.completeExceptionally(ex); |
244 |
|
} |
245 |
|
|
246 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
247 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
248 |
+ |
completeExceptionally(new FJException()); |
249 |
+ |
return true; |
250 |
+ |
} |
251 |
+ |
return false; |
252 |
+ |
} |
253 |
+ |
|
254 |
|
public final void complete() { |
255 |
|
BinaryAsyncAction a = this; |
256 |
|
for (;;) { |
273 |
|
} |
274 |
|
|
275 |
|
public final void completeExceptionally(Throwable ex) { |
276 |
< |
BinaryAsyncAction a = this; |
258 |
< |
while (!a.isCompletedAbnormally()) { |
276 |
> |
for (BinaryAsyncAction a = this;;) { |
277 |
|
a.completeThisExceptionally(ex); |
278 |
|
BinaryAsyncAction s = a.sibling; |
279 |
< |
if (s != null) |
280 |
< |
s.cancel(false); |
281 |
< |
if (!a.onException() || (a = a.parent) == null) |
279 |
> |
if (s != null && !s.isDone()) |
280 |
> |
s.completeExceptionally(ex); |
281 |
> |
if ((a = a.parent) == null) |
282 |
|
break; |
283 |
|
} |
284 |
|
} |
298 |
|
|
299 |
|
} |
300 |
|
|
301 |
< |
static final class AsyncFib extends BinaryAsyncAction { |
301 |
> |
final class AsyncFib extends BinaryAsyncAction { |
302 |
|
int number; |
303 |
< |
public AsyncFib(int n) { |
304 |
< |
this.number = n; |
303 |
> |
int expectedResult; |
304 |
> |
public AsyncFib(int number) { |
305 |
> |
this.number = number; |
306 |
> |
this.expectedResult = fib[number]; |
307 |
|
} |
308 |
|
|
309 |
|
public final boolean exec() { |
325 |
|
catch (Throwable ex) { |
326 |
|
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
327 |
|
} |
328 |
+ |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
329 |
+ |
throw new FJException(); |
330 |
|
return false; |
331 |
|
} |
332 |
|
|
334 |
|
number = ((AsyncFib)x).number + ((AsyncFib)y).number; |
335 |
|
super.onComplete(x, y); |
336 |
|
} |
337 |
+ |
|
338 |
+ |
public void checkCompletedNormally() { |
339 |
+ |
assertEquals(expectedResult, number); |
340 |
+ |
ForkJoinTask8Test.this.checkCompletedNormally(this); |
341 |
+ |
} |
342 |
|
} |
343 |
|
|
344 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
348 |
|
} |
349 |
|
|
350 |
|
public final boolean exec() { |
351 |
< |
FailingAsyncFib f = this; |
352 |
< |
int n = f.number; |
353 |
< |
if (n > 1) { |
354 |
< |
while (n > 1) { |
355 |
< |
FailingAsyncFib p = f; |
356 |
< |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
357 |
< |
f = new FailingAsyncFib(--n); |
358 |
< |
p.linkSubtasks(r, f); |
359 |
< |
r.fork(); |
351 |
> |
try { |
352 |
> |
FailingAsyncFib f = this; |
353 |
> |
int n = f.number; |
354 |
> |
if (n > 1) { |
355 |
> |
while (n > 1) { |
356 |
> |
FailingAsyncFib p = f; |
357 |
> |
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
358 |
> |
f = new FailingAsyncFib(--n); |
359 |
> |
p.linkSubtasks(r, f); |
360 |
> |
r.fork(); |
361 |
> |
} |
362 |
> |
f.number = n; |
363 |
|
} |
364 |
< |
f.number = n; |
364 |
> |
f.complete(); |
365 |
> |
} |
366 |
> |
catch (Throwable ex) { |
367 |
> |
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
368 |
|
} |
369 |
< |
f.complete(); |
369 |
> |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
370 |
> |
throw new FJException(); |
371 |
|
return false; |
372 |
|
} |
373 |
|
|
382 |
|
* completed tasks; getRawResult returns null. |
383 |
|
*/ |
384 |
|
public void testInvoke() { |
385 |
+ |
testInvoke(mainPool()); |
386 |
+ |
} |
387 |
+ |
public void testInvoke_Singleton() { |
388 |
+ |
testInvoke(singletonPool()); |
389 |
+ |
} |
390 |
+ |
public void testInvoke(ForkJoinPool pool) { |
391 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
392 |
|
protected void realCompute() { |
393 |
|
AsyncFib f = new AsyncFib(8); |
394 |
|
assertNull(f.invoke()); |
395 |
< |
assertEquals(21, f.number); |
356 |
< |
checkCompletedNormally(f); |
395 |
> |
f.checkCompletedNormally(); |
396 |
|
}}; |
397 |
< |
testInvokeOnPool(mainPool(), a); |
397 |
> |
testInvokeOnPool(pool, a); |
398 |
|
} |
399 |
|
|
400 |
|
/** |
403 |
|
* completed tasks |
404 |
|
*/ |
405 |
|
public void testQuietlyInvoke() { |
406 |
+ |
testQuietlyInvoke(mainPool()); |
407 |
+ |
} |
408 |
+ |
public void testQuietlyInvoke_Singleton() { |
409 |
+ |
testQuietlyInvoke(singletonPool()); |
410 |
+ |
} |
411 |
+ |
public void testQuietlyInvoke(ForkJoinPool pool) { |
412 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
413 |
|
protected void realCompute() { |
414 |
|
AsyncFib f = new AsyncFib(8); |
415 |
|
f.quietlyInvoke(); |
416 |
< |
assertEquals(21, f.number); |
372 |
< |
checkCompletedNormally(f); |
416 |
> |
f.checkCompletedNormally(); |
417 |
|
}}; |
418 |
< |
testInvokeOnPool(mainPool(), a); |
418 |
> |
testInvokeOnPool(pool, a); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
422 |
|
* join of a forked task returns when task completes |
423 |
|
*/ |
424 |
|
public void testForkJoin() { |
425 |
+ |
testForkJoin(mainPool()); |
426 |
+ |
} |
427 |
+ |
public void testForkJoin_Singleton() { |
428 |
+ |
testForkJoin(singletonPool()); |
429 |
+ |
} |
430 |
+ |
public void testForkJoin(ForkJoinPool pool) { |
431 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
432 |
|
protected void realCompute() { |
433 |
|
AsyncFib f = new AsyncFib(8); |
434 |
|
assertSame(f, f.fork()); |
435 |
|
assertNull(f.join()); |
436 |
< |
assertEquals(21, f.number); |
387 |
< |
checkCompletedNormally(f); |
436 |
> |
f.checkCompletedNormally(); |
437 |
|
}}; |
438 |
< |
testInvokeOnPool(mainPool(), a); |
438 |
> |
testInvokeOnPool(pool, a); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
|
* get of a forked task returns when task completes |
443 |
|
*/ |
444 |
|
public void testForkGet() { |
445 |
+ |
testForkGet(mainPool()); |
446 |
+ |
} |
447 |
+ |
public void testForkGet_Singleton() { |
448 |
+ |
testForkGet(singletonPool()); |
449 |
+ |
} |
450 |
+ |
public void testForkGet(ForkJoinPool pool) { |
451 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
452 |
|
protected void realCompute() throws Exception { |
453 |
|
AsyncFib f = new AsyncFib(8); |
454 |
|
assertSame(f, f.fork()); |
455 |
|
assertNull(f.get()); |
456 |
< |
assertEquals(21, f.number); |
402 |
< |
checkCompletedNormally(f); |
456 |
> |
f.checkCompletedNormally(); |
457 |
|
}}; |
458 |
< |
testInvokeOnPool(mainPool(), a); |
458 |
> |
testInvokeOnPool(pool, a); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
|
* timed get of a forked task returns when task completes |
463 |
|
*/ |
464 |
|
public void testForkTimedGet() { |
465 |
+ |
testForkTimedGet(mainPool()); |
466 |
+ |
} |
467 |
+ |
public void testForkTimedGet_Singleton() { |
468 |
+ |
testForkTimedGet(singletonPool()); |
469 |
+ |
} |
470 |
+ |
public void testForkTimedGet(ForkJoinPool pool) { |
471 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
472 |
|
protected void realCompute() throws Exception { |
473 |
|
AsyncFib f = new AsyncFib(8); |
474 |
|
assertSame(f, f.fork()); |
475 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
476 |
< |
assertEquals(21, f.number); |
417 |
< |
checkCompletedNormally(f); |
476 |
> |
f.checkCompletedNormally(); |
477 |
|
}}; |
478 |
< |
testInvokeOnPool(mainPool(), a); |
478 |
> |
testInvokeOnPool(pool, a); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
< |
* timed get with null time unit throws NPE |
482 |
> |
* timed get with null time unit throws NullPointerException |
483 |
|
*/ |
484 |
< |
public void testForkTimedGetNPE() { |
484 |
> |
public void testForkTimedGetNullTimeUnit() { |
485 |
> |
testForkTimedGetNullTimeUnit(mainPool()); |
486 |
> |
} |
487 |
> |
public void testForkTimedGetNullTimeUnit_Singleton() { |
488 |
> |
testForkTimedGet(singletonPool()); |
489 |
> |
} |
490 |
> |
public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) { |
491 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
492 |
|
protected void realCompute() throws Exception { |
493 |
|
AsyncFib f = new AsyncFib(8); |
497 |
|
shouldThrow(); |
498 |
|
} catch (NullPointerException success) {} |
499 |
|
}}; |
500 |
< |
testInvokeOnPool(mainPool(), a); |
500 |
> |
testInvokeOnPool(pool, a); |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
|
* quietlyJoin of a forked task returns when task completes |
505 |
|
*/ |
506 |
|
public void testForkQuietlyJoin() { |
507 |
+ |
testForkQuietlyJoin(mainPool()); |
508 |
+ |
} |
509 |
+ |
public void testForkQuietlyJoin_Singleton() { |
510 |
+ |
testForkQuietlyJoin(singletonPool()); |
511 |
+ |
} |
512 |
+ |
public void testForkQuietlyJoin(ForkJoinPool pool) { |
513 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
514 |
|
protected void realCompute() { |
515 |
|
AsyncFib f = new AsyncFib(8); |
516 |
|
assertSame(f, f.fork()); |
517 |
|
f.quietlyJoin(); |
518 |
< |
assertEquals(21, f.number); |
448 |
< |
checkCompletedNormally(f); |
518 |
> |
f.checkCompletedNormally(); |
519 |
|
}}; |
520 |
< |
testInvokeOnPool(mainPool(), a); |
520 |
> |
testInvokeOnPool(pool, a); |
521 |
|
} |
522 |
|
|
523 |
|
/** |
525 |
|
* getQueuedTaskCount returns 0 when quiescent |
526 |
|
*/ |
527 |
|
public void testForkHelpQuiesce() { |
528 |
+ |
testForkHelpQuiesce(mainPool()); |
529 |
+ |
} |
530 |
+ |
public void testForkHelpQuiesce_Singleton() { |
531 |
+ |
testForkHelpQuiesce(singletonPool()); |
532 |
+ |
} |
533 |
+ |
public void testForkHelpQuiesce(ForkJoinPool pool) { |
534 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
535 |
|
protected void realCompute() { |
536 |
|
AsyncFib f = new AsyncFib(8); |
537 |
|
assertSame(f, f.fork()); |
538 |
|
helpQuiesce(); |
463 |
– |
assertEquals(21, f.number); |
539 |
|
assertEquals(0, getQueuedTaskCount()); |
540 |
< |
checkCompletedNormally(f); |
540 |
> |
f.checkCompletedNormally(); |
541 |
|
}}; |
542 |
< |
testInvokeOnPool(mainPool(), a); |
542 |
> |
testInvokeOnPool(pool, a); |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
|
* invoke task throws exception when task completes abnormally |
547 |
|
*/ |
548 |
|
public void testAbnormalInvoke() { |
549 |
+ |
testAbnormalInvoke(mainPool()); |
550 |
+ |
} |
551 |
+ |
public void testAbnormalInvoke_Singleton() { |
552 |
+ |
testAbnormalInvoke(singletonPool()); |
553 |
+ |
} |
554 |
+ |
public void testAbnormalInvoke(ForkJoinPool pool) { |
555 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
556 |
|
protected void realCompute() { |
557 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
562 |
|
checkCompletedAbnormally(f, success); |
563 |
|
} |
564 |
|
}}; |
565 |
< |
testInvokeOnPool(mainPool(), a); |
565 |
> |
testInvokeOnPool(pool, a); |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
|
* quietlyInvoke task returns when task completes abnormally |
570 |
|
*/ |
571 |
|
public void testAbnormalQuietlyInvoke() { |
572 |
+ |
testAbnormalQuietlyInvoke(mainPool()); |
573 |
+ |
} |
574 |
+ |
public void testAbnormalQuietlyInvoke_Singleton() { |
575 |
+ |
testAbnormalQuietlyInvoke(singletonPool()); |
576 |
+ |
} |
577 |
+ |
public void testAbnormalQuietlyInvoke(ForkJoinPool pool) { |
578 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
579 |
|
protected void realCompute() { |
580 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
582 |
|
assertTrue(f.getException() instanceof FJException); |
583 |
|
checkCompletedAbnormally(f, f.getException()); |
584 |
|
}}; |
585 |
< |
testInvokeOnPool(mainPool(), a); |
585 |
> |
testInvokeOnPool(pool, a); |
586 |
|
} |
587 |
|
|
588 |
|
/** |
589 |
|
* join of a forked task throws exception when task completes abnormally |
590 |
|
*/ |
591 |
|
public void testAbnormalForkJoin() { |
592 |
+ |
testAbnormalForkJoin(mainPool()); |
593 |
+ |
} |
594 |
+ |
public void testAbnormalForkJoin_Singleton() { |
595 |
+ |
testAbnormalForkJoin(singletonPool()); |
596 |
+ |
} |
597 |
+ |
public void testAbnormalForkJoin(ForkJoinPool pool) { |
598 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
599 |
|
protected void realCompute() { |
600 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
606 |
|
checkCompletedAbnormally(f, success); |
607 |
|
} |
608 |
|
}}; |
609 |
< |
testInvokeOnPool(mainPool(), a); |
609 |
> |
testInvokeOnPool(pool, a); |
610 |
|
} |
611 |
|
|
612 |
|
/** |
613 |
|
* get of a forked task throws exception when task completes abnormally |
614 |
|
*/ |
615 |
|
public void testAbnormalForkGet() { |
616 |
+ |
testAbnormalForkGet(mainPool()); |
617 |
+ |
} |
618 |
+ |
public void testAbnormalForkGet_Singleton() { |
619 |
+ |
testAbnormalForkJoin(singletonPool()); |
620 |
+ |
} |
621 |
+ |
public void testAbnormalForkGet(ForkJoinPool pool) { |
622 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
623 |
|
protected void realCompute() throws Exception { |
624 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
632 |
|
checkCompletedAbnormally(f, cause); |
633 |
|
} |
634 |
|
}}; |
635 |
< |
testInvokeOnPool(mainPool(), a); |
635 |
> |
testInvokeOnPool(pool, a); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* timed get of a forked task throws exception when task completes abnormally |
640 |
|
*/ |
641 |
|
public void testAbnormalForkTimedGet() { |
642 |
+ |
testAbnormalForkTimedGet(mainPool()); |
643 |
+ |
} |
644 |
+ |
public void testAbnormalForkTimedGet_Singleton() { |
645 |
+ |
testAbnormalForkTimedGet(singletonPool()); |
646 |
+ |
} |
647 |
+ |
public void testAbnormalForkTimedGet(ForkJoinPool pool) { |
648 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
649 |
|
protected void realCompute() throws Exception { |
650 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
658 |
|
checkCompletedAbnormally(f, cause); |
659 |
|
} |
660 |
|
}}; |
661 |
< |
testInvokeOnPool(mainPool(), a); |
661 |
> |
testInvokeOnPool(pool, a); |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
666 |
|
*/ |
667 |
|
public void testAbnormalForkQuietlyJoin() { |
668 |
+ |
testAbnormalForkQuietlyJoin(mainPool()); |
669 |
+ |
} |
670 |
+ |
public void testAbnormalForkQuietlyJoin_Singleton() { |
671 |
+ |
testAbnormalForkQuietlyJoin(singletonPool()); |
672 |
+ |
} |
673 |
+ |
public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) { |
674 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
675 |
|
protected void realCompute() { |
676 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
679 |
|
assertTrue(f.getException() instanceof FJException); |
680 |
|
checkCompletedAbnormally(f, f.getException()); |
681 |
|
}}; |
682 |
< |
testInvokeOnPool(mainPool(), a); |
682 |
> |
testInvokeOnPool(pool, a); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* getPool of executing task returns its pool |
687 |
|
*/ |
688 |
|
public void testGetPool() { |
689 |
< |
final ForkJoinPool mainPool = mainPool(); |
689 |
> |
testGetPool(mainPool()); |
690 |
> |
} |
691 |
> |
public void testGetPool_Singleton() { |
692 |
> |
testGetPool(singletonPool()); |
693 |
> |
} |
694 |
> |
public void testGetPool(ForkJoinPool pool) { |
695 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
696 |
|
protected void realCompute() { |
697 |
< |
assertSame(mainPool, getPool()); |
697 |
> |
assertSame(pool, getPool()); |
698 |
|
}}; |
699 |
< |
testInvokeOnPool(mainPool, a); |
699 |
> |
testInvokeOnPool(pool, a); |
700 |
|
} |
701 |
|
|
702 |
|
/** |
714 |
|
* inForkJoinPool of executing task returns true |
715 |
|
*/ |
716 |
|
public void testInForkJoinPool() { |
717 |
+ |
testInForkJoinPool(mainPool()); |
718 |
+ |
} |
719 |
+ |
public void testInForkJoinPool_Singleton() { |
720 |
+ |
testInForkJoinPool(singletonPool()); |
721 |
+ |
} |
722 |
+ |
public void testInForkJoinPool(ForkJoinPool pool) { |
723 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
724 |
|
protected void realCompute() { |
725 |
|
assertTrue(inForkJoinPool()); |
726 |
|
}}; |
727 |
< |
testInvokeOnPool(mainPool(), a); |
727 |
> |
testInvokeOnPool(pool, a); |
728 |
|
} |
729 |
|
|
730 |
|
/** |
754 |
|
* invoke task throws exception after invoking completeExceptionally |
755 |
|
*/ |
756 |
|
public void testCompleteExceptionally() { |
757 |
+ |
testCompleteExceptionally(mainPool()); |
758 |
+ |
} |
759 |
+ |
public void testCompleteExceptionally_Singleton() { |
760 |
+ |
testCompleteExceptionally(singletonPool()); |
761 |
+ |
} |
762 |
+ |
public void testCompleteExceptionally(ForkJoinPool pool) { |
763 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
764 |
|
protected void realCompute() { |
765 |
|
AsyncFib f = new AsyncFib(8); |
771 |
|
checkCompletedAbnormally(f, success); |
772 |
|
} |
773 |
|
}}; |
774 |
< |
testInvokeOnPool(mainPool(), a); |
774 |
> |
testInvokeOnPool(pool, a); |
775 |
|
} |
776 |
|
|
777 |
|
/** |
778 |
< |
* invokeAll(t1, t2) invokes all task arguments |
778 |
> |
* invokeAll(tasks) with 1 argument invokes task |
779 |
|
*/ |
780 |
< |
public void testInvokeAll2() { |
780 |
> |
public void testInvokeAll1() { |
781 |
> |
testInvokeAll1(mainPool()); |
782 |
> |
} |
783 |
> |
public void testInvokeAll1_Singleton() { |
784 |
> |
testInvokeAll1(singletonPool()); |
785 |
> |
} |
786 |
> |
public void testInvokeAll1(ForkJoinPool pool) { |
787 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
788 |
|
protected void realCompute() { |
789 |
|
AsyncFib f = new AsyncFib(8); |
790 |
< |
AsyncFib g = new AsyncFib(9); |
791 |
< |
invokeAll(f, g); |
658 |
< |
assertEquals(21, f.number); |
659 |
< |
assertEquals(34, g.number); |
660 |
< |
checkCompletedNormally(f); |
661 |
< |
checkCompletedNormally(g); |
790 |
> |
invokeAll(f); |
791 |
> |
f.checkCompletedNormally(); |
792 |
|
}}; |
793 |
< |
testInvokeOnPool(mainPool(), a); |
793 |
> |
testInvokeOnPool(pool, a); |
794 |
|
} |
795 |
|
|
796 |
|
/** |
797 |
< |
* invokeAll(tasks) with 1 argument invokes task |
797 |
> |
* invokeAll(t1, t2) invokes all task arguments |
798 |
|
*/ |
799 |
< |
public void testInvokeAll1() { |
799 |
> |
public void testInvokeAll2() { |
800 |
> |
testInvokeAll2(mainPool()); |
801 |
> |
} |
802 |
> |
public void testInvokeAll2_Singleton() { |
803 |
> |
testInvokeAll2(singletonPool()); |
804 |
> |
} |
805 |
> |
public void testInvokeAll2(ForkJoinPool pool) { |
806 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
807 |
|
protected void realCompute() { |
808 |
< |
AsyncFib f = new AsyncFib(8); |
809 |
< |
invokeAll(f); |
810 |
< |
checkCompletedNormally(f); |
811 |
< |
assertEquals(21, f.number); |
808 |
> |
AsyncFib[] tasks = { |
809 |
> |
new AsyncFib(8), |
810 |
> |
new AsyncFib(9), |
811 |
> |
}; |
812 |
> |
invokeAll(tasks[0], tasks[1]); |
813 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
814 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
815 |
|
}}; |
816 |
< |
testInvokeOnPool(mainPool(), a); |
816 |
> |
testInvokeOnPool(pool, a); |
817 |
|
} |
818 |
|
|
819 |
|
/** |
820 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
821 |
|
*/ |
822 |
|
public void testInvokeAll3() { |
823 |
+ |
testInvokeAll3(mainPool()); |
824 |
+ |
} |
825 |
+ |
public void testInvokeAll3_Singleton() { |
826 |
+ |
testInvokeAll3(singletonPool()); |
827 |
+ |
} |
828 |
+ |
public void testInvokeAll3(ForkJoinPool pool) { |
829 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
830 |
|
protected void realCompute() { |
831 |
< |
AsyncFib f = new AsyncFib(8); |
832 |
< |
AsyncFib g = new AsyncFib(9); |
833 |
< |
AsyncFib h = new AsyncFib(7); |
834 |
< |
invokeAll(f, g, h); |
835 |
< |
assertEquals(21, f.number); |
836 |
< |
assertEquals(34, g.number); |
837 |
< |
assertEquals(13, h.number); |
838 |
< |
checkCompletedNormally(f); |
694 |
< |
checkCompletedNormally(g); |
695 |
< |
checkCompletedNormally(h); |
831 |
> |
AsyncFib[] tasks = { |
832 |
> |
new AsyncFib(8), |
833 |
> |
new AsyncFib(9), |
834 |
> |
new AsyncFib(7), |
835 |
> |
}; |
836 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
837 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
838 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
839 |
|
}}; |
840 |
< |
testInvokeOnPool(mainPool(), a); |
840 |
> |
testInvokeOnPool(pool, a); |
841 |
|
} |
842 |
|
|
843 |
|
/** |
844 |
|
* invokeAll(collection) invokes all tasks in the collection |
845 |
|
*/ |
846 |
|
public void testInvokeAllCollection() { |
847 |
+ |
testInvokeAllCollection(mainPool()); |
848 |
+ |
} |
849 |
+ |
public void testInvokeAllCollection_Singleton() { |
850 |
+ |
testInvokeAllCollection(singletonPool()); |
851 |
+ |
} |
852 |
+ |
public void testInvokeAllCollection(ForkJoinPool pool) { |
853 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
854 |
|
protected void realCompute() { |
855 |
< |
AsyncFib f = new AsyncFib(8); |
856 |
< |
AsyncFib g = new AsyncFib(9); |
857 |
< |
AsyncFib h = new AsyncFib(7); |
858 |
< |
HashSet set = new HashSet(); |
859 |
< |
set.add(f); |
860 |
< |
set.add(g); |
861 |
< |
set.add(h); |
862 |
< |
invokeAll(set); |
714 |
< |
assertEquals(21, f.number); |
715 |
< |
assertEquals(34, g.number); |
716 |
< |
assertEquals(13, h.number); |
717 |
< |
checkCompletedNormally(f); |
718 |
< |
checkCompletedNormally(g); |
719 |
< |
checkCompletedNormally(h); |
855 |
> |
AsyncFib[] tasks = { |
856 |
> |
new AsyncFib(8), |
857 |
> |
new AsyncFib(9), |
858 |
> |
new AsyncFib(7), |
859 |
> |
}; |
860 |
> |
invokeAll(Arrays.asList(tasks)); |
861 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
862 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
863 |
|
}}; |
864 |
< |
testInvokeOnPool(mainPool(), a); |
864 |
> |
testInvokeOnPool(pool, a); |
865 |
|
} |
866 |
|
|
867 |
|
/** |
868 |
< |
* invokeAll(tasks) with any null task throws NPE |
868 |
> |
* invokeAll(tasks) with any null task throws NullPointerException |
869 |
|
*/ |
870 |
< |
public void testInvokeAllNPE() { |
870 |
> |
public void testInvokeAllNullTask() { |
871 |
> |
testInvokeAllNullTask(mainPool()); |
872 |
> |
} |
873 |
> |
public void testInvokeAllNullTask_Singleton() { |
874 |
> |
testInvokeAllNullTask(singletonPool()); |
875 |
> |
} |
876 |
> |
public void testInvokeAllNullTask(ForkJoinPool pool) { |
877 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
878 |
|
protected void realCompute() { |
879 |
< |
AsyncFib f = new AsyncFib(8); |
880 |
< |
AsyncFib g = new AsyncFib(9); |
881 |
< |
AsyncFib h = null; |
882 |
< |
try { |
883 |
< |
invokeAll(f, g, h); |
884 |
< |
shouldThrow(); |
885 |
< |
} catch (NullPointerException success) {} |
879 |
> |
AsyncFib nul = null; |
880 |
> |
Runnable[] throwingActions = { |
881 |
> |
() -> invokeAll(nul), |
882 |
> |
() -> invokeAll(nul, nul), |
883 |
> |
() -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul), |
884 |
> |
() -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)), |
885 |
> |
() -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)), |
886 |
> |
}; |
887 |
> |
assertThrows(NullPointerException.class, throwingActions); |
888 |
|
}}; |
889 |
< |
testInvokeOnPool(mainPool(), a); |
889 |
> |
testInvokeOnPool(pool, a); |
890 |
|
} |
891 |
|
|
892 |
|
/** |
893 |
< |
* invokeAll(t1, t2) throw exception if any task does |
893 |
> |
* invokeAll(tasks) with 1 argument throws exception if task does |
894 |
|
*/ |
895 |
< |
public void testAbnormalInvokeAll2() { |
895 |
> |
public void testAbnormalInvokeAll1() { |
896 |
> |
testAbnormalInvokeAll1(mainPool()); |
897 |
> |
} |
898 |
> |
public void testAbnormalInvokeAll1_Singleton() { |
899 |
> |
testAbnormalInvokeAll1(singletonPool()); |
900 |
> |
} |
901 |
> |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
902 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
903 |
|
protected void realCompute() { |
747 |
– |
AsyncFib f = new AsyncFib(8); |
904 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
905 |
|
try { |
906 |
< |
invokeAll(f, g); |
906 |
> |
invokeAll(g); |
907 |
|
shouldThrow(); |
908 |
|
} catch (FJException success) { |
909 |
|
checkCompletedAbnormally(g, success); |
910 |
|
} |
911 |
|
}}; |
912 |
< |
testInvokeOnPool(mainPool(), a); |
912 |
> |
testInvokeOnPool(pool, a); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
< |
* invokeAll(tasks) with 1 argument throws exception if task does |
916 |
> |
* invokeAll(t1, t2) throw exception if any task does |
917 |
|
*/ |
918 |
< |
public void testAbnormalInvokeAll1() { |
918 |
> |
public void testAbnormalInvokeAll2() { |
919 |
> |
testAbnormalInvokeAll2(mainPool()); |
920 |
> |
} |
921 |
> |
public void testAbnormalInvokeAll2_Singleton() { |
922 |
> |
testAbnormalInvokeAll2(singletonPool()); |
923 |
> |
} |
924 |
> |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
925 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
926 |
|
protected void realCompute() { |
927 |
+ |
AsyncFib f = new AsyncFib(8); |
928 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
929 |
+ |
ForkJoinTask[] tasks = { f, g }; |
930 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
931 |
|
try { |
932 |
< |
invokeAll(g); |
932 |
> |
invokeAll(tasks[0], tasks[1]); |
933 |
|
shouldThrow(); |
934 |
|
} catch (FJException success) { |
935 |
|
checkCompletedAbnormally(g, success); |
936 |
|
} |
937 |
|
}}; |
938 |
< |
testInvokeOnPool(mainPool(), a); |
938 |
> |
testInvokeOnPool(pool, a); |
939 |
|
} |
940 |
|
|
941 |
|
/** |
942 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
943 |
|
*/ |
944 |
|
public void testAbnormalInvokeAll3() { |
945 |
+ |
testAbnormalInvokeAll3(mainPool()); |
946 |
+ |
} |
947 |
+ |
public void testAbnormalInvokeAll3_Singleton() { |
948 |
+ |
testAbnormalInvokeAll3(singletonPool()); |
949 |
+ |
} |
950 |
+ |
public void testAbnormalInvokeAll3(ForkJoinPool pool) { |
951 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
952 |
|
protected void realCompute() { |
953 |
|
AsyncFib f = new AsyncFib(8); |
954 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
955 |
|
AsyncFib h = new AsyncFib(7); |
956 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
957 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
958 |
|
try { |
959 |
< |
invokeAll(f, g, h); |
959 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
960 |
|
shouldThrow(); |
961 |
|
} catch (FJException success) { |
962 |
|
checkCompletedAbnormally(g, success); |
963 |
|
} |
964 |
|
}}; |
965 |
< |
testInvokeOnPool(mainPool(), a); |
965 |
> |
testInvokeOnPool(pool, a); |
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
|
* invokeAll(collection) throws exception if any task does |
970 |
|
*/ |
971 |
|
public void testAbnormalInvokeAllCollection() { |
972 |
+ |
testAbnormalInvokeAllCollection(mainPool()); |
973 |
+ |
} |
974 |
+ |
public void testAbnormalInvokeAllCollection_Singleton() { |
975 |
+ |
testAbnormalInvokeAllCollection(singletonPool()); |
976 |
+ |
} |
977 |
+ |
public void testAbnormalInvokeAllCollection(ForkJoinPool pool) { |
978 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
979 |
|
protected void realCompute() { |
980 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
981 |
|
AsyncFib g = new AsyncFib(9); |
982 |
|
AsyncFib h = new AsyncFib(7); |
983 |
< |
HashSet set = new HashSet(); |
984 |
< |
set.add(f); |
806 |
< |
set.add(g); |
807 |
< |
set.add(h); |
983 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
984 |
> |
Collections.shuffle(Arrays.asList(tasks)); |
985 |
|
try { |
986 |
< |
invokeAll(set); |
986 |
> |
invokeAll(Arrays.asList(tasks)); |
987 |
|
shouldThrow(); |
988 |
|
} catch (FJException success) { |
989 |
|
checkCompletedAbnormally(f, success); |
990 |
|
} |
991 |
|
}}; |
992 |
< |
testInvokeOnPool(mainPool(), a); |
992 |
> |
testInvokeOnPool(pool, a); |
993 |
|
} |
994 |
|
|
995 |
|
/** |
1006 |
|
assertTrue(f.tryUnfork()); |
1007 |
|
helpQuiesce(); |
1008 |
|
checkNotDone(f); |
1009 |
< |
checkCompletedNormally(g); |
1009 |
> |
g.checkCompletedNormally(); |
1010 |
|
}}; |
1011 |
|
testInvokeOnPool(singletonPool(), a); |
1012 |
|
} |
1027 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1028 |
|
helpQuiesce(); |
1029 |
|
assertEquals(0, getSurplusQueuedTaskCount()); |
1030 |
< |
checkCompletedNormally(f); |
1031 |
< |
checkCompletedNormally(g); |
1032 |
< |
checkCompletedNormally(h); |
1030 |
> |
f.checkCompletedNormally(); |
1031 |
> |
g.checkCompletedNormally(); |
1032 |
> |
h.checkCompletedNormally(); |
1033 |
|
}}; |
1034 |
|
testInvokeOnPool(singletonPool(), a); |
1035 |
|
} |
1046 |
|
assertSame(f, f.fork()); |
1047 |
|
assertSame(f, peekNextLocalTask()); |
1048 |
|
assertNull(f.join()); |
1049 |
< |
checkCompletedNormally(f); |
1049 |
> |
f.checkCompletedNormally(); |
1050 |
|
helpQuiesce(); |
1051 |
< |
checkCompletedNormally(g); |
1051 |
> |
g.checkCompletedNormally(); |
1052 |
|
}}; |
1053 |
|
testInvokeOnPool(singletonPool(), a); |
1054 |
|
} |
1067 |
|
assertSame(f, pollNextLocalTask()); |
1068 |
|
helpQuiesce(); |
1069 |
|
checkNotDone(f); |
1070 |
< |
assertEquals(34, g.number); |
894 |
< |
checkCompletedNormally(g); |
1070 |
> |
g.checkCompletedNormally(); |
1071 |
|
}}; |
1072 |
|
testInvokeOnPool(singletonPool(), a); |
1073 |
|
} |
1085 |
|
assertSame(f, pollTask()); |
1086 |
|
helpQuiesce(); |
1087 |
|
checkNotDone(f); |
1088 |
< |
checkCompletedNormally(g); |
1088 |
> |
g.checkCompletedNormally(); |
1089 |
|
}}; |
1090 |
|
testInvokeOnPool(singletonPool(), a); |
1091 |
|
} |
1103 |
|
assertSame(g, peekNextLocalTask()); |
1104 |
|
assertNull(f.join()); |
1105 |
|
helpQuiesce(); |
1106 |
< |
checkCompletedNormally(f); |
1107 |
< |
assertEquals(34, g.number); |
932 |
< |
checkCompletedNormally(g); |
1106 |
> |
f.checkCompletedNormally(); |
1107 |
> |
g.checkCompletedNormally(); |
1108 |
|
}}; |
1109 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1110 |
|
} |
1122 |
|
assertSame(f, f.fork()); |
1123 |
|
assertSame(g, pollNextLocalTask()); |
1124 |
|
helpQuiesce(); |
1125 |
< |
assertEquals(21, f.number); |
951 |
< |
checkCompletedNormally(f); |
1125 |
> |
f.checkCompletedNormally(); |
1126 |
|
checkNotDone(g); |
1127 |
|
}}; |
1128 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1141 |
|
assertSame(f, f.fork()); |
1142 |
|
assertSame(g, pollTask()); |
1143 |
|
helpQuiesce(); |
1144 |
< |
assertEquals(21, f.number); |
971 |
< |
checkCompletedNormally(f); |
1144 |
> |
f.checkCompletedNormally(); |
1145 |
|
checkNotDone(g); |
1146 |
|
}}; |
1147 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1148 |
|
} |
1149 |
|
|
977 |
– |
// versions for singleton pools |
978 |
– |
|
979 |
– |
/** |
980 |
– |
* invoke returns when task completes normally. |
981 |
– |
* isCompletedAbnormally and isCancelled return false for normally |
982 |
– |
* completed tasks; getRawResult returns null. |
983 |
– |
*/ |
984 |
– |
public void testInvokeSingleton() { |
985 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
986 |
– |
protected void realCompute() { |
987 |
– |
AsyncFib f = new AsyncFib(8); |
988 |
– |
assertNull(f.invoke()); |
989 |
– |
assertEquals(21, f.number); |
990 |
– |
checkCompletedNormally(f); |
991 |
– |
}}; |
992 |
– |
testInvokeOnPool(singletonPool(), a); |
993 |
– |
} |
994 |
– |
|
995 |
– |
/** |
996 |
– |
* quietlyInvoke task returns when task completes normally. |
997 |
– |
* isCompletedAbnormally and isCancelled return false for normally |
998 |
– |
* completed tasks |
999 |
– |
*/ |
1000 |
– |
public void testQuietlyInvokeSingleton() { |
1001 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1002 |
– |
protected void realCompute() { |
1003 |
– |
AsyncFib f = new AsyncFib(8); |
1004 |
– |
f.quietlyInvoke(); |
1005 |
– |
assertEquals(21, f.number); |
1006 |
– |
checkCompletedNormally(f); |
1007 |
– |
}}; |
1008 |
– |
testInvokeOnPool(singletonPool(), a); |
1009 |
– |
} |
1010 |
– |
|
1011 |
– |
/** |
1012 |
– |
* join of a forked task returns when task completes |
1013 |
– |
*/ |
1014 |
– |
public void testForkJoinSingleton() { |
1015 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1016 |
– |
protected void realCompute() { |
1017 |
– |
AsyncFib f = new AsyncFib(8); |
1018 |
– |
assertSame(f, f.fork()); |
1019 |
– |
assertNull(f.join()); |
1020 |
– |
assertEquals(21, f.number); |
1021 |
– |
checkCompletedNormally(f); |
1022 |
– |
}}; |
1023 |
– |
testInvokeOnPool(singletonPool(), a); |
1024 |
– |
} |
1025 |
– |
|
1026 |
– |
/** |
1027 |
– |
* get of a forked task returns when task completes |
1028 |
– |
*/ |
1029 |
– |
public void testForkGetSingleton() { |
1030 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1031 |
– |
protected void realCompute() throws Exception { |
1032 |
– |
AsyncFib f = new AsyncFib(8); |
1033 |
– |
assertSame(f, f.fork()); |
1034 |
– |
assertNull(f.get()); |
1035 |
– |
assertEquals(21, f.number); |
1036 |
– |
checkCompletedNormally(f); |
1037 |
– |
}}; |
1038 |
– |
testInvokeOnPool(singletonPool(), a); |
1039 |
– |
} |
1040 |
– |
|
1041 |
– |
/** |
1042 |
– |
* timed get of a forked task returns when task completes |
1043 |
– |
*/ |
1044 |
– |
public void testForkTimedGetSingleton() { |
1045 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1046 |
– |
protected void realCompute() throws Exception { |
1047 |
– |
AsyncFib f = new AsyncFib(8); |
1048 |
– |
assertSame(f, f.fork()); |
1049 |
– |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1050 |
– |
assertEquals(21, f.number); |
1051 |
– |
checkCompletedNormally(f); |
1052 |
– |
}}; |
1053 |
– |
testInvokeOnPool(singletonPool(), a); |
1054 |
– |
} |
1055 |
– |
|
1056 |
– |
/** |
1057 |
– |
* timed get with null time unit throws NPE |
1058 |
– |
*/ |
1059 |
– |
public void testForkTimedGetNPESingleton() { |
1060 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1061 |
– |
protected void realCompute() throws Exception { |
1062 |
– |
AsyncFib f = new AsyncFib(8); |
1063 |
– |
assertSame(f, f.fork()); |
1064 |
– |
try { |
1065 |
– |
f.get(5L, null); |
1066 |
– |
shouldThrow(); |
1067 |
– |
} catch (NullPointerException success) {} |
1068 |
– |
}}; |
1069 |
– |
testInvokeOnPool(singletonPool(), a); |
1070 |
– |
} |
1071 |
– |
|
1072 |
– |
/** |
1073 |
– |
* quietlyJoin of a forked task returns when task completes |
1074 |
– |
*/ |
1075 |
– |
public void testForkQuietlyJoinSingleton() { |
1076 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1077 |
– |
protected void realCompute() { |
1078 |
– |
AsyncFib f = new AsyncFib(8); |
1079 |
– |
assertSame(f, f.fork()); |
1080 |
– |
f.quietlyJoin(); |
1081 |
– |
assertEquals(21, f.number); |
1082 |
– |
checkCompletedNormally(f); |
1083 |
– |
}}; |
1084 |
– |
testInvokeOnPool(singletonPool(), a); |
1085 |
– |
} |
1086 |
– |
|
1087 |
– |
/** |
1088 |
– |
* helpQuiesce returns when tasks are complete. |
1089 |
– |
* getQueuedTaskCount returns 0 when quiescent |
1090 |
– |
*/ |
1091 |
– |
public void testForkHelpQuiesceSingleton() { |
1092 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1093 |
– |
protected void realCompute() { |
1094 |
– |
AsyncFib f = new AsyncFib(8); |
1095 |
– |
assertSame(f, f.fork()); |
1096 |
– |
helpQuiesce(); |
1097 |
– |
assertEquals(0, getQueuedTaskCount()); |
1098 |
– |
assertEquals(21, f.number); |
1099 |
– |
checkCompletedNormally(f); |
1100 |
– |
}}; |
1101 |
– |
testInvokeOnPool(singletonPool(), a); |
1102 |
– |
} |
1103 |
– |
|
1104 |
– |
/** |
1105 |
– |
* invoke task throws exception when task completes abnormally |
1106 |
– |
*/ |
1107 |
– |
public void testAbnormalInvokeSingleton() { |
1108 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1109 |
– |
protected void realCompute() { |
1110 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1111 |
– |
try { |
1112 |
– |
f.invoke(); |
1113 |
– |
shouldThrow(); |
1114 |
– |
} catch (FJException success) { |
1115 |
– |
checkCompletedAbnormally(f, success); |
1116 |
– |
} |
1117 |
– |
}}; |
1118 |
– |
testInvokeOnPool(singletonPool(), a); |
1119 |
– |
} |
1120 |
– |
|
1121 |
– |
/** |
1122 |
– |
* quietlyInvoke task returns when task completes abnormally |
1123 |
– |
*/ |
1124 |
– |
public void testAbnormalQuietlyInvokeSingleton() { |
1125 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1126 |
– |
protected void realCompute() { |
1127 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1128 |
– |
f.quietlyInvoke(); |
1129 |
– |
assertTrue(f.getException() instanceof FJException); |
1130 |
– |
checkCompletedAbnormally(f, f.getException()); |
1131 |
– |
}}; |
1132 |
– |
testInvokeOnPool(singletonPool(), a); |
1133 |
– |
} |
1134 |
– |
|
1135 |
– |
/** |
1136 |
– |
* join of a forked task throws exception when task completes abnormally |
1137 |
– |
*/ |
1138 |
– |
public void testAbnormalForkJoinSingleton() { |
1139 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1140 |
– |
protected void realCompute() { |
1141 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1142 |
– |
assertSame(f, f.fork()); |
1143 |
– |
try { |
1144 |
– |
f.join(); |
1145 |
– |
shouldThrow(); |
1146 |
– |
} catch (FJException success) { |
1147 |
– |
checkCompletedAbnormally(f, success); |
1148 |
– |
} |
1149 |
– |
}}; |
1150 |
– |
testInvokeOnPool(singletonPool(), a); |
1151 |
– |
} |
1152 |
– |
|
1153 |
– |
/** |
1154 |
– |
* get of a forked task throws exception when task completes abnormally |
1155 |
– |
*/ |
1156 |
– |
public void testAbnormalForkGetSingleton() { |
1157 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1158 |
– |
protected void realCompute() throws Exception { |
1159 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1160 |
– |
assertSame(f, f.fork()); |
1161 |
– |
try { |
1162 |
– |
f.get(); |
1163 |
– |
shouldThrow(); |
1164 |
– |
} catch (ExecutionException success) { |
1165 |
– |
Throwable cause = success.getCause(); |
1166 |
– |
assertTrue(cause instanceof FJException); |
1167 |
– |
checkCompletedAbnormally(f, cause); |
1168 |
– |
} |
1169 |
– |
}}; |
1170 |
– |
testInvokeOnPool(singletonPool(), a); |
1171 |
– |
} |
1172 |
– |
|
1173 |
– |
/** |
1174 |
– |
* timed get of a forked task throws exception when task completes abnormally |
1175 |
– |
*/ |
1176 |
– |
public void testAbnormalForkTimedGetSingleton() { |
1177 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1178 |
– |
protected void realCompute() throws Exception { |
1179 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1180 |
– |
assertSame(f, f.fork()); |
1181 |
– |
try { |
1182 |
– |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1183 |
– |
shouldThrow(); |
1184 |
– |
} catch (ExecutionException success) { |
1185 |
– |
Throwable cause = success.getCause(); |
1186 |
– |
assertTrue(cause instanceof FJException); |
1187 |
– |
checkCompletedAbnormally(f, cause); |
1188 |
– |
} |
1189 |
– |
}}; |
1190 |
– |
testInvokeOnPool(singletonPool(), a); |
1191 |
– |
} |
1192 |
– |
|
1193 |
– |
/** |
1194 |
– |
* quietlyJoin of a forked task returns when task completes abnormally |
1195 |
– |
*/ |
1196 |
– |
public void testAbnormalForkQuietlyJoinSingleton() { |
1197 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1198 |
– |
protected void realCompute() { |
1199 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1200 |
– |
assertSame(f, f.fork()); |
1201 |
– |
f.quietlyJoin(); |
1202 |
– |
assertTrue(f.getException() instanceof FJException); |
1203 |
– |
checkCompletedAbnormally(f, f.getException()); |
1204 |
– |
}}; |
1205 |
– |
testInvokeOnPool(singletonPool(), a); |
1206 |
– |
} |
1207 |
– |
|
1208 |
– |
/** |
1209 |
– |
* invoke task throws exception after invoking completeExceptionally |
1210 |
– |
*/ |
1211 |
– |
public void testCompleteExceptionallySingleton() { |
1212 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1213 |
– |
protected void realCompute() { |
1214 |
– |
AsyncFib f = new AsyncFib(8); |
1215 |
– |
f.completeExceptionally(new FJException()); |
1216 |
– |
try { |
1217 |
– |
f.invoke(); |
1218 |
– |
shouldThrow(); |
1219 |
– |
} catch (FJException success) { |
1220 |
– |
checkCompletedAbnormally(f, success); |
1221 |
– |
} |
1222 |
– |
}}; |
1223 |
– |
testInvokeOnPool(singletonPool(), a); |
1224 |
– |
} |
1225 |
– |
|
1226 |
– |
/** |
1227 |
– |
* invokeAll(t1, t2) invokes all task arguments |
1228 |
– |
*/ |
1229 |
– |
public void testInvokeAll2Singleton() { |
1230 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1231 |
– |
protected void realCompute() { |
1232 |
– |
AsyncFib f = new AsyncFib(8); |
1233 |
– |
AsyncFib g = new AsyncFib(9); |
1234 |
– |
invokeAll(f, g); |
1235 |
– |
assertEquals(21, f.number); |
1236 |
– |
assertEquals(34, g.number); |
1237 |
– |
checkCompletedNormally(f); |
1238 |
– |
checkCompletedNormally(g); |
1239 |
– |
}}; |
1240 |
– |
testInvokeOnPool(singletonPool(), a); |
1241 |
– |
} |
1242 |
– |
|
1243 |
– |
/** |
1244 |
– |
* invokeAll(tasks) with 1 argument invokes task |
1245 |
– |
*/ |
1246 |
– |
public void testInvokeAll1Singleton() { |
1247 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1248 |
– |
protected void realCompute() { |
1249 |
– |
AsyncFib f = new AsyncFib(8); |
1250 |
– |
invokeAll(f); |
1251 |
– |
checkCompletedNormally(f); |
1252 |
– |
assertEquals(21, f.number); |
1253 |
– |
}}; |
1254 |
– |
testInvokeOnPool(singletonPool(), a); |
1255 |
– |
} |
1256 |
– |
|
1257 |
– |
/** |
1258 |
– |
* invokeAll(tasks) with > 2 argument invokes tasks |
1259 |
– |
*/ |
1260 |
– |
public void testInvokeAll3Singleton() { |
1261 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1262 |
– |
protected void realCompute() { |
1263 |
– |
AsyncFib f = new AsyncFib(8); |
1264 |
– |
AsyncFib g = new AsyncFib(9); |
1265 |
– |
AsyncFib h = new AsyncFib(7); |
1266 |
– |
invokeAll(f, g, h); |
1267 |
– |
assertEquals(21, f.number); |
1268 |
– |
assertEquals(34, g.number); |
1269 |
– |
assertEquals(13, h.number); |
1270 |
– |
checkCompletedNormally(f); |
1271 |
– |
checkCompletedNormally(g); |
1272 |
– |
checkCompletedNormally(h); |
1273 |
– |
}}; |
1274 |
– |
testInvokeOnPool(singletonPool(), a); |
1275 |
– |
} |
1276 |
– |
|
1277 |
– |
/** |
1278 |
– |
* invokeAll(collection) invokes all tasks in the collection |
1279 |
– |
*/ |
1280 |
– |
public void testInvokeAllCollectionSingleton() { |
1281 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1282 |
– |
protected void realCompute() { |
1283 |
– |
AsyncFib f = new AsyncFib(8); |
1284 |
– |
AsyncFib g = new AsyncFib(9); |
1285 |
– |
AsyncFib h = new AsyncFib(7); |
1286 |
– |
HashSet set = new HashSet(); |
1287 |
– |
set.add(f); |
1288 |
– |
set.add(g); |
1289 |
– |
set.add(h); |
1290 |
– |
invokeAll(set); |
1291 |
– |
assertEquals(21, f.number); |
1292 |
– |
assertEquals(34, g.number); |
1293 |
– |
assertEquals(13, h.number); |
1294 |
– |
checkCompletedNormally(f); |
1295 |
– |
checkCompletedNormally(g); |
1296 |
– |
checkCompletedNormally(h); |
1297 |
– |
}}; |
1298 |
– |
testInvokeOnPool(singletonPool(), a); |
1299 |
– |
} |
1300 |
– |
|
1301 |
– |
/** |
1302 |
– |
* invokeAll(tasks) with any null task throws NPE |
1303 |
– |
*/ |
1304 |
– |
public void testInvokeAllNPESingleton() { |
1305 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1306 |
– |
protected void realCompute() { |
1307 |
– |
AsyncFib f = new AsyncFib(8); |
1308 |
– |
AsyncFib g = new AsyncFib(9); |
1309 |
– |
AsyncFib h = null; |
1310 |
– |
try { |
1311 |
– |
invokeAll(f, g, h); |
1312 |
– |
shouldThrow(); |
1313 |
– |
} catch (NullPointerException success) {} |
1314 |
– |
}}; |
1315 |
– |
testInvokeOnPool(singletonPool(), a); |
1316 |
– |
} |
1317 |
– |
|
1318 |
– |
/** |
1319 |
– |
* invokeAll(t1, t2) throw exception if any task does |
1320 |
– |
*/ |
1321 |
– |
public void testAbnormalInvokeAll2Singleton() { |
1322 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1323 |
– |
protected void realCompute() { |
1324 |
– |
AsyncFib f = new AsyncFib(8); |
1325 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1326 |
– |
try { |
1327 |
– |
invokeAll(f, g); |
1328 |
– |
shouldThrow(); |
1329 |
– |
} catch (FJException success) { |
1330 |
– |
checkCompletedAbnormally(g, success); |
1331 |
– |
} |
1332 |
– |
}}; |
1333 |
– |
testInvokeOnPool(singletonPool(), a); |
1334 |
– |
} |
1335 |
– |
|
1336 |
– |
/** |
1337 |
– |
* invokeAll(tasks) with 1 argument throws exception if task does |
1338 |
– |
*/ |
1339 |
– |
public void testAbnormalInvokeAll1Singleton() { |
1340 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1341 |
– |
protected void realCompute() { |
1342 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1343 |
– |
try { |
1344 |
– |
invokeAll(g); |
1345 |
– |
shouldThrow(); |
1346 |
– |
} catch (FJException success) { |
1347 |
– |
checkCompletedAbnormally(g, success); |
1348 |
– |
} |
1349 |
– |
}}; |
1350 |
– |
testInvokeOnPool(singletonPool(), a); |
1351 |
– |
} |
1352 |
– |
|
1353 |
– |
/** |
1354 |
– |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1355 |
– |
*/ |
1356 |
– |
public void testAbnormalInvokeAll3Singleton() { |
1357 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1358 |
– |
protected void realCompute() { |
1359 |
– |
AsyncFib f = new AsyncFib(8); |
1360 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1361 |
– |
AsyncFib h = new AsyncFib(7); |
1362 |
– |
try { |
1363 |
– |
invokeAll(f, g, h); |
1364 |
– |
shouldThrow(); |
1365 |
– |
} catch (FJException success) { |
1366 |
– |
checkCompletedAbnormally(g, success); |
1367 |
– |
} |
1368 |
– |
}}; |
1369 |
– |
testInvokeOnPool(singletonPool(), a); |
1370 |
– |
} |
1371 |
– |
|
1372 |
– |
/** |
1373 |
– |
* invokeAll(collection) throws exception if any task does |
1374 |
– |
*/ |
1375 |
– |
public void testAbnormalInvokeAllCollectionSingleton() { |
1376 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1377 |
– |
protected void realCompute() { |
1378 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1379 |
– |
AsyncFib g = new AsyncFib(9); |
1380 |
– |
AsyncFib h = new AsyncFib(7); |
1381 |
– |
HashSet set = new HashSet(); |
1382 |
– |
set.add(f); |
1383 |
– |
set.add(g); |
1384 |
– |
set.add(h); |
1385 |
– |
try { |
1386 |
– |
invokeAll(set); |
1387 |
– |
shouldThrow(); |
1388 |
– |
} catch (FJException success) { |
1389 |
– |
checkCompletedAbnormally(f, success); |
1390 |
– |
} |
1391 |
– |
}}; |
1392 |
– |
testInvokeOnPool(singletonPool(), a); |
1393 |
– |
} |
1394 |
– |
|
1150 |
|
/** |
1151 |
|
* ForkJoinTask.quietlyComplete returns when task completes |
1152 |
|
* normally without setting a value. The most recent value |
1168 |
|
testInvokeOnPool(mainPool(), a); |
1169 |
|
} |
1170 |
|
|
1171 |
+ |
// jdk9 |
1172 |
+ |
|
1173 |
+ |
/** |
1174 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1175 |
+ |
*/ |
1176 |
+ |
public void testPollSubmission() { |
1177 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1178 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1179 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1180 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1181 |
+ |
final ForkJoinPool p = singletonPool(); |
1182 |
+ |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1183 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1184 |
+ |
public void realRun() { |
1185 |
+ |
p.execute(a); |
1186 |
+ |
p.execute(b); |
1187 |
+ |
p.execute(c); |
1188 |
+ |
}}); |
1189 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1190 |
+ |
protected void realCompute() { |
1191 |
+ |
external.start(); |
1192 |
+ |
try { |
1193 |
+ |
external.join(); |
1194 |
+ |
} catch (Exception ex) { |
1195 |
+ |
threadUnexpectedException(ex); |
1196 |
+ |
} |
1197 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1198 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1199 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1200 |
+ |
assertTrue(r == a || r == b || r == c); |
1201 |
+ |
assertFalse(r.isDone()); |
1202 |
+ |
}}; |
1203 |
+ |
p.invoke(s); |
1204 |
+ |
} |
1205 |
+ |
} |
1206 |
+ |
|
1207 |
|
} |