7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
|
|
10 |
< |
import java.util.HashSet; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collections; |
12 |
> |
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
16 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
17 |
|
import java.util.concurrent.RecursiveAction; |
18 |
|
import java.util.concurrent.TimeoutException; |
19 |
|
|
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 { |
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); |
291 |
|
|
292 |
|
} |
293 |
|
|
294 |
< |
static final class AsyncFib extends BinaryAsyncAction { |
294 |
> |
final class AsyncFib extends BinaryAsyncAction { |
295 |
|
int number; |
296 |
< |
public AsyncFib(int n) { |
297 |
< |
this.number = n; |
296 |
> |
int expectedResult; |
297 |
> |
public AsyncFib(int number) { |
298 |
> |
this.number = number; |
299 |
> |
this.expectedResult = fib[number]; |
300 |
|
} |
301 |
|
|
302 |
|
public final boolean exec() { |
325 |
|
number = ((AsyncFib)x).number + ((AsyncFib)y).number; |
326 |
|
super.onComplete(x, y); |
327 |
|
} |
328 |
+ |
|
329 |
+ |
public void checkCompletedNormally() { |
330 |
+ |
assertEquals(expectedResult, number); |
331 |
+ |
ForkJoinTask8Test.this.checkCompletedNormally(this); |
332 |
+ |
} |
333 |
|
} |
334 |
|
|
335 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
366 |
|
* completed tasks; getRawResult returns null. |
367 |
|
*/ |
368 |
|
public void testInvoke() { |
369 |
+ |
testInvoke(mainPool()); |
370 |
+ |
} |
371 |
+ |
public void testInvoke_Singleton() { |
372 |
+ |
testInvoke(singletonPool()); |
373 |
+ |
} |
374 |
+ |
public void testInvoke(ForkJoinPool pool) { |
375 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
376 |
|
protected void realCompute() { |
377 |
|
AsyncFib f = new AsyncFib(8); |
378 |
|
assertNull(f.invoke()); |
379 |
< |
assertEquals(21, f.number); |
355 |
< |
checkCompletedNormally(f); |
379 |
> |
f.checkCompletedNormally(); |
380 |
|
}}; |
381 |
< |
testInvokeOnPool(mainPool(), a); |
381 |
> |
testInvokeOnPool(pool, a); |
382 |
|
} |
383 |
|
|
384 |
|
/** |
387 |
|
* completed tasks |
388 |
|
*/ |
389 |
|
public void testQuietlyInvoke() { |
390 |
+ |
testQuietlyInvoke(mainPool()); |
391 |
+ |
} |
392 |
+ |
public void testQuietlyInvoke_Singleton() { |
393 |
+ |
testQuietlyInvoke(singletonPool()); |
394 |
+ |
} |
395 |
+ |
public void testQuietlyInvoke(ForkJoinPool pool) { |
396 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
397 |
|
protected void realCompute() { |
398 |
|
AsyncFib f = new AsyncFib(8); |
399 |
|
f.quietlyInvoke(); |
400 |
< |
assertEquals(21, f.number); |
371 |
< |
checkCompletedNormally(f); |
400 |
> |
f.checkCompletedNormally(); |
401 |
|
}}; |
402 |
< |
testInvokeOnPool(mainPool(), a); |
402 |
> |
testInvokeOnPool(pool, a); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
|
* join of a forked task returns when task completes |
407 |
|
*/ |
408 |
|
public void testForkJoin() { |
409 |
+ |
testForkJoin(mainPool()); |
410 |
+ |
} |
411 |
+ |
public void testForkJoin_Singleton() { |
412 |
+ |
testForkJoin(singletonPool()); |
413 |
+ |
} |
414 |
+ |
public void testForkJoin(ForkJoinPool pool) { |
415 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
416 |
|
protected void realCompute() { |
417 |
|
AsyncFib f = new AsyncFib(8); |
418 |
|
assertSame(f, f.fork()); |
419 |
|
assertNull(f.join()); |
420 |
< |
assertEquals(21, f.number); |
386 |
< |
checkCompletedNormally(f); |
420 |
> |
f.checkCompletedNormally(); |
421 |
|
}}; |
422 |
< |
testInvokeOnPool(mainPool(), a); |
422 |
> |
testInvokeOnPool(pool, a); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* get of a forked task returns when task completes |
427 |
|
*/ |
428 |
|
public void testForkGet() { |
429 |
+ |
testForkGet(mainPool()); |
430 |
+ |
} |
431 |
+ |
public void testForkGet_Singleton() { |
432 |
+ |
testForkGet(singletonPool()); |
433 |
+ |
} |
434 |
+ |
public void testForkGet(ForkJoinPool pool) { |
435 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
436 |
|
protected void realCompute() throws Exception { |
437 |
|
AsyncFib f = new AsyncFib(8); |
438 |
|
assertSame(f, f.fork()); |
439 |
|
assertNull(f.get()); |
440 |
< |
assertEquals(21, f.number); |
401 |
< |
checkCompletedNormally(f); |
440 |
> |
f.checkCompletedNormally(); |
441 |
|
}}; |
442 |
< |
testInvokeOnPool(mainPool(), a); |
442 |
> |
testInvokeOnPool(pool, a); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
|
* timed get of a forked task returns when task completes |
447 |
|
*/ |
448 |
|
public void testForkTimedGet() { |
449 |
+ |
testForkTimedGet(mainPool()); |
450 |
+ |
} |
451 |
+ |
public void testForkTimedGet_Singleton() { |
452 |
+ |
testForkTimedGet(singletonPool()); |
453 |
+ |
} |
454 |
+ |
public void testForkTimedGet(ForkJoinPool pool) { |
455 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
456 |
|
protected void realCompute() throws Exception { |
457 |
|
AsyncFib f = new AsyncFib(8); |
458 |
|
assertSame(f, f.fork()); |
459 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
460 |
< |
assertEquals(21, f.number); |
416 |
< |
checkCompletedNormally(f); |
460 |
> |
f.checkCompletedNormally(); |
461 |
|
}}; |
462 |
< |
testInvokeOnPool(mainPool(), a); |
462 |
> |
testInvokeOnPool(pool, a); |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
< |
* timed get with null time unit throws NPE |
466 |
> |
* timed get with null time unit throws NullPointerException |
467 |
|
*/ |
468 |
< |
public void testForkTimedGetNPE() { |
468 |
> |
public void testForkTimedGetNullTimeUnit() { |
469 |
> |
testForkTimedGetNullTimeUnit(mainPool()); |
470 |
> |
} |
471 |
> |
public void testForkTimedGetNullTimeUnit_Singleton() { |
472 |
> |
testForkTimedGet(singletonPool()); |
473 |
> |
} |
474 |
> |
public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) { |
475 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
476 |
|
protected void realCompute() throws Exception { |
477 |
|
AsyncFib f = new AsyncFib(8); |
481 |
|
shouldThrow(); |
482 |
|
} catch (NullPointerException success) {} |
483 |
|
}}; |
484 |
< |
testInvokeOnPool(mainPool(), a); |
484 |
> |
testInvokeOnPool(pool, a); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
|
* quietlyJoin of a forked task returns when task completes |
489 |
|
*/ |
490 |
|
public void testForkQuietlyJoin() { |
491 |
+ |
testForkQuietlyJoin(mainPool()); |
492 |
+ |
} |
493 |
+ |
public void testForkQuietlyJoin_Singleton() { |
494 |
+ |
testForkQuietlyJoin(singletonPool()); |
495 |
+ |
} |
496 |
+ |
public void testForkQuietlyJoin(ForkJoinPool pool) { |
497 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
498 |
|
protected void realCompute() { |
499 |
|
AsyncFib f = new AsyncFib(8); |
500 |
|
assertSame(f, f.fork()); |
501 |
|
f.quietlyJoin(); |
502 |
< |
assertEquals(21, f.number); |
447 |
< |
checkCompletedNormally(f); |
502 |
> |
f.checkCompletedNormally(); |
503 |
|
}}; |
504 |
< |
testInvokeOnPool(mainPool(), a); |
504 |
> |
testInvokeOnPool(pool, a); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
509 |
|
* getQueuedTaskCount returns 0 when quiescent |
510 |
|
*/ |
511 |
|
public void testForkHelpQuiesce() { |
512 |
+ |
testForkHelpQuiesce(mainPool()); |
513 |
+ |
} |
514 |
+ |
public void testForkHelpQuiesce_Singleton() { |
515 |
+ |
testForkHelpQuiesce(singletonPool()); |
516 |
+ |
} |
517 |
+ |
public void testForkHelpQuiesce(ForkJoinPool pool) { |
518 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
519 |
|
protected void realCompute() { |
520 |
|
AsyncFib f = new AsyncFib(8); |
521 |
|
assertSame(f, f.fork()); |
522 |
|
helpQuiesce(); |
462 |
– |
assertEquals(21, f.number); |
523 |
|
assertEquals(0, getQueuedTaskCount()); |
524 |
< |
checkCompletedNormally(f); |
524 |
> |
f.checkCompletedNormally(); |
525 |
|
}}; |
526 |
< |
testInvokeOnPool(mainPool(), a); |
526 |
> |
testInvokeOnPool(pool, a); |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
|
* invoke task throws exception when task completes abnormally |
531 |
|
*/ |
532 |
|
public void testAbnormalInvoke() { |
533 |
+ |
testAbnormalInvoke(mainPool()); |
534 |
+ |
} |
535 |
+ |
public void testAbnormalInvoke_Singleton() { |
536 |
+ |
testAbnormalInvoke(singletonPool()); |
537 |
+ |
} |
538 |
+ |
public void testAbnormalInvoke(ForkJoinPool pool) { |
539 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
540 |
|
protected void realCompute() { |
541 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
546 |
|
checkCompletedAbnormally(f, success); |
547 |
|
} |
548 |
|
}}; |
549 |
< |
testInvokeOnPool(mainPool(), a); |
549 |
> |
testInvokeOnPool(pool, a); |
550 |
|
} |
551 |
|
|
552 |
|
/** |
553 |
|
* quietlyInvoke task returns when task completes abnormally |
554 |
|
*/ |
555 |
|
public void testAbnormalQuietlyInvoke() { |
556 |
+ |
testAbnormalQuietlyInvoke(mainPool()); |
557 |
+ |
} |
558 |
+ |
public void testAbnormalQuietlyInvoke_Singleton() { |
559 |
+ |
testAbnormalQuietlyInvoke(singletonPool()); |
560 |
+ |
} |
561 |
+ |
public void testAbnormalQuietlyInvoke(ForkJoinPool pool) { |
562 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
563 |
|
protected void realCompute() { |
564 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
566 |
|
assertTrue(f.getException() instanceof FJException); |
567 |
|
checkCompletedAbnormally(f, f.getException()); |
568 |
|
}}; |
569 |
< |
testInvokeOnPool(mainPool(), a); |
569 |
> |
testInvokeOnPool(pool, a); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
|
* join of a forked task throws exception when task completes abnormally |
574 |
|
*/ |
575 |
|
public void testAbnormalForkJoin() { |
576 |
+ |
testAbnormalForkJoin(mainPool()); |
577 |
+ |
} |
578 |
+ |
public void testAbnormalForkJoin_Singleton() { |
579 |
+ |
testAbnormalForkJoin(singletonPool()); |
580 |
+ |
} |
581 |
+ |
public void testAbnormalForkJoin(ForkJoinPool pool) { |
582 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
583 |
|
protected void realCompute() { |
584 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
590 |
|
checkCompletedAbnormally(f, success); |
591 |
|
} |
592 |
|
}}; |
593 |
< |
testInvokeOnPool(mainPool(), a); |
593 |
> |
testInvokeOnPool(pool, a); |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
|
* get of a forked task throws exception when task completes abnormally |
598 |
|
*/ |
599 |
|
public void testAbnormalForkGet() { |
600 |
+ |
testAbnormalForkGet(mainPool()); |
601 |
+ |
} |
602 |
+ |
public void testAbnormalForkGet_Singleton() { |
603 |
+ |
testAbnormalForkJoin(singletonPool()); |
604 |
+ |
} |
605 |
+ |
public void testAbnormalForkGet(ForkJoinPool pool) { |
606 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
607 |
|
protected void realCompute() throws Exception { |
608 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
616 |
|
checkCompletedAbnormally(f, cause); |
617 |
|
} |
618 |
|
}}; |
619 |
< |
testInvokeOnPool(mainPool(), a); |
619 |
> |
testInvokeOnPool(pool, a); |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
|
* timed get of a forked task throws exception when task completes abnormally |
624 |
|
*/ |
625 |
|
public void testAbnormalForkTimedGet() { |
626 |
+ |
testAbnormalForkTimedGet(mainPool()); |
627 |
+ |
} |
628 |
+ |
public void testAbnormalForkTimedGet_Singleton() { |
629 |
+ |
testAbnormalForkTimedGet(singletonPool()); |
630 |
+ |
} |
631 |
+ |
public void testAbnormalForkTimedGet(ForkJoinPool pool) { |
632 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
633 |
|
protected void realCompute() throws Exception { |
634 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
642 |
|
checkCompletedAbnormally(f, cause); |
643 |
|
} |
644 |
|
}}; |
645 |
< |
testInvokeOnPool(mainPool(), a); |
645 |
> |
testInvokeOnPool(pool, a); |
646 |
|
} |
647 |
|
|
648 |
|
/** |
649 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
650 |
|
*/ |
651 |
|
public void testAbnormalForkQuietlyJoin() { |
652 |
+ |
testAbnormalForkQuietlyJoin(mainPool()); |
653 |
+ |
} |
654 |
+ |
public void testAbnormalForkQuietlyJoin_Singleton() { |
655 |
+ |
testAbnormalForkQuietlyJoin(singletonPool()); |
656 |
+ |
} |
657 |
+ |
public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) { |
658 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
659 |
|
protected void realCompute() { |
660 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
663 |
|
assertTrue(f.getException() instanceof FJException); |
664 |
|
checkCompletedAbnormally(f, f.getException()); |
665 |
|
}}; |
666 |
< |
testInvokeOnPool(mainPool(), a); |
666 |
> |
testInvokeOnPool(pool, a); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
|
* getPool of executing task returns its pool |
671 |
|
*/ |
672 |
|
public void testGetPool() { |
673 |
< |
final ForkJoinPool mainPool = mainPool(); |
673 |
> |
testGetPool(mainPool()); |
674 |
> |
} |
675 |
> |
public void testGetPool_Singleton() { |
676 |
> |
testGetPool(singletonPool()); |
677 |
> |
} |
678 |
> |
public void testGetPool(ForkJoinPool pool) { |
679 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
680 |
|
protected void realCompute() { |
681 |
< |
assertSame(mainPool, getPool()); |
681 |
> |
assertSame(pool, getPool()); |
682 |
|
}}; |
683 |
< |
testInvokeOnPool(mainPool, a); |
683 |
> |
testInvokeOnPool(pool, a); |
684 |
|
} |
685 |
|
|
686 |
|
/** |
698 |
|
* inForkJoinPool of executing task returns true |
699 |
|
*/ |
700 |
|
public void testInForkJoinPool() { |
701 |
+ |
testInForkJoinPool(mainPool()); |
702 |
+ |
} |
703 |
+ |
public void testInForkJoinPool_Singleton() { |
704 |
+ |
testInForkJoinPool(singletonPool()); |
705 |
+ |
} |
706 |
+ |
public void testInForkJoinPool(ForkJoinPool pool) { |
707 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
708 |
|
protected void realCompute() { |
709 |
|
assertTrue(inForkJoinPool()); |
710 |
|
}}; |
711 |
< |
testInvokeOnPool(mainPool(), a); |
711 |
> |
testInvokeOnPool(pool, a); |
712 |
|
} |
713 |
|
|
714 |
|
/** |
738 |
|
* invoke task throws exception after invoking completeExceptionally |
739 |
|
*/ |
740 |
|
public void testCompleteExceptionally() { |
741 |
+ |
testCompleteExceptionally(mainPool()); |
742 |
+ |
} |
743 |
+ |
public void testCompleteExceptionally_Singleton() { |
744 |
+ |
testCompleteExceptionally(singletonPool()); |
745 |
+ |
} |
746 |
+ |
public void testCompleteExceptionally(ForkJoinPool pool) { |
747 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
748 |
|
protected void realCompute() { |
749 |
|
AsyncFib f = new AsyncFib(8); |
755 |
|
checkCompletedAbnormally(f, success); |
756 |
|
} |
757 |
|
}}; |
758 |
< |
testInvokeOnPool(mainPool(), a); |
758 |
> |
testInvokeOnPool(pool, a); |
759 |
|
} |
760 |
|
|
761 |
|
/** |
762 |
< |
* invokeAll(t1, t2) invokes all task arguments |
762 |
> |
* invokeAll(tasks) with 1 argument invokes task |
763 |
|
*/ |
764 |
< |
public void testInvokeAll2() { |
764 |
> |
public void testInvokeAll1() { |
765 |
> |
testInvokeAll1(mainPool()); |
766 |
> |
} |
767 |
> |
public void testInvokeAll1_Singleton() { |
768 |
> |
testInvokeAll1(singletonPool()); |
769 |
> |
} |
770 |
> |
public void testInvokeAll1(ForkJoinPool pool) { |
771 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
772 |
|
protected void realCompute() { |
773 |
|
AsyncFib f = new AsyncFib(8); |
774 |
< |
AsyncFib g = new AsyncFib(9); |
775 |
< |
invokeAll(f, g); |
657 |
< |
assertEquals(21, f.number); |
658 |
< |
assertEquals(34, g.number); |
659 |
< |
checkCompletedNormally(f); |
660 |
< |
checkCompletedNormally(g); |
774 |
> |
invokeAll(f); |
775 |
> |
f.checkCompletedNormally(); |
776 |
|
}}; |
777 |
< |
testInvokeOnPool(mainPool(), a); |
777 |
> |
testInvokeOnPool(pool, a); |
778 |
|
} |
779 |
|
|
780 |
|
/** |
781 |
< |
* invokeAll(tasks) with 1 argument invokes task |
781 |
> |
* invokeAll(t1, t2) invokes all task arguments |
782 |
|
*/ |
783 |
< |
public void testInvokeAll1() { |
783 |
> |
public void testInvokeAll2() { |
784 |
> |
testInvokeAll2(mainPool()); |
785 |
> |
} |
786 |
> |
public void testInvokeAll2_Singleton() { |
787 |
> |
testInvokeAll2(singletonPool()); |
788 |
> |
} |
789 |
> |
public void testInvokeAll2(ForkJoinPool pool) { |
790 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
791 |
|
protected void realCompute() { |
792 |
< |
AsyncFib f = new AsyncFib(8); |
793 |
< |
invokeAll(f); |
794 |
< |
checkCompletedNormally(f); |
795 |
< |
assertEquals(21, f.number); |
792 |
> |
AsyncFib[] tasks = { |
793 |
> |
new AsyncFib(8), |
794 |
> |
new AsyncFib(9), |
795 |
> |
}; |
796 |
> |
invokeAll(tasks[0], tasks[1]); |
797 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
798 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
799 |
|
}}; |
800 |
< |
testInvokeOnPool(mainPool(), a); |
800 |
> |
testInvokeOnPool(pool, a); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
805 |
|
*/ |
806 |
|
public void testInvokeAll3() { |
807 |
+ |
testInvokeAll3(mainPool()); |
808 |
+ |
} |
809 |
+ |
public void testInvokeAll3_Singleton() { |
810 |
+ |
testInvokeAll3(singletonPool()); |
811 |
+ |
} |
812 |
+ |
public void testInvokeAll3(ForkJoinPool pool) { |
813 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
814 |
|
protected void realCompute() { |
815 |
< |
AsyncFib f = new AsyncFib(8); |
816 |
< |
AsyncFib g = new AsyncFib(9); |
817 |
< |
AsyncFib h = new AsyncFib(7); |
818 |
< |
invokeAll(f, g, h); |
819 |
< |
assertEquals(21, f.number); |
820 |
< |
assertEquals(34, g.number); |
821 |
< |
assertEquals(13, h.number); |
822 |
< |
checkCompletedNormally(f); |
693 |
< |
checkCompletedNormally(g); |
694 |
< |
checkCompletedNormally(h); |
815 |
> |
AsyncFib[] tasks = { |
816 |
> |
new AsyncFib(8), |
817 |
> |
new AsyncFib(9), |
818 |
> |
new AsyncFib(7), |
819 |
> |
}; |
820 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
821 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
822 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
823 |
|
}}; |
824 |
< |
testInvokeOnPool(mainPool(), a); |
824 |
> |
testInvokeOnPool(pool, a); |
825 |
|
} |
826 |
|
|
827 |
|
/** |
828 |
|
* invokeAll(collection) invokes all tasks in the collection |
829 |
|
*/ |
830 |
|
public void testInvokeAllCollection() { |
831 |
+ |
testInvokeAllCollection(mainPool()); |
832 |
+ |
} |
833 |
+ |
public void testInvokeAllCollection_Singleton() { |
834 |
+ |
testInvokeAllCollection(singletonPool()); |
835 |
+ |
} |
836 |
+ |
public void testInvokeAllCollection(ForkJoinPool pool) { |
837 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
838 |
|
protected void realCompute() { |
839 |
< |
AsyncFib f = new AsyncFib(8); |
840 |
< |
AsyncFib g = new AsyncFib(9); |
841 |
< |
AsyncFib h = new AsyncFib(7); |
842 |
< |
HashSet set = new HashSet(); |
843 |
< |
set.add(f); |
844 |
< |
set.add(g); |
845 |
< |
set.add(h); |
846 |
< |
invokeAll(set); |
713 |
< |
assertEquals(21, f.number); |
714 |
< |
assertEquals(34, g.number); |
715 |
< |
assertEquals(13, h.number); |
716 |
< |
checkCompletedNormally(f); |
717 |
< |
checkCompletedNormally(g); |
718 |
< |
checkCompletedNormally(h); |
839 |
> |
AsyncFib[] tasks = { |
840 |
> |
new AsyncFib(8), |
841 |
> |
new AsyncFib(9), |
842 |
> |
new AsyncFib(7), |
843 |
> |
}; |
844 |
> |
invokeAll(Arrays.asList(tasks)); |
845 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
846 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
847 |
|
}}; |
848 |
< |
testInvokeOnPool(mainPool(), a); |
848 |
> |
testInvokeOnPool(pool, a); |
849 |
|
} |
850 |
|
|
851 |
|
/** |
852 |
|
* invokeAll(tasks) with any null task throws NullPointerException |
853 |
|
*/ |
854 |
|
public void testInvokeAllNullTask() { |
855 |
+ |
testInvokeAllNullTask(mainPool()); |
856 |
+ |
} |
857 |
+ |
public void testInvokeAllNullTask_Singleton() { |
858 |
+ |
testInvokeAllNullTask(singletonPool()); |
859 |
+ |
} |
860 |
+ |
public void testInvokeAllNullTask(ForkJoinPool pool) { |
861 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
862 |
|
protected void realCompute() { |
729 |
– |
AsyncFib f = new AsyncFib(8); |
730 |
– |
AsyncFib g = new AsyncFib(9); |
863 |
|
AsyncFib nul = null; |
864 |
|
Runnable[] throwingActions = { |
865 |
|
() -> invokeAll(nul), |
866 |
|
() -> invokeAll(nul, nul), |
867 |
< |
() -> invokeAll(f, g, nul), |
868 |
< |
() -> invokeAll(f, nul, g), |
869 |
< |
() -> invokeAll(nul, f, g), |
867 |
> |
() -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul), |
868 |
> |
() -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)), |
869 |
> |
() -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)), |
870 |
|
}; |
871 |
|
assertThrows(NullPointerException.class, throwingActions); |
872 |
|
}}; |
873 |
< |
testInvokeOnPool(mainPool(), a); |
873 |
> |
testInvokeOnPool(pool, a); |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
< |
* invokeAll(t1, t2) throw exception if any task does |
877 |
> |
* invokeAll(tasks) with 1 argument throws exception if task does |
878 |
|
*/ |
879 |
< |
public void testAbnormalInvokeAll2() { |
879 |
> |
public void testAbnormalInvokeAll1() { |
880 |
> |
testAbnormalInvokeAll1(mainPool()); |
881 |
> |
} |
882 |
> |
public void testAbnormalInvokeAll1_Singleton() { |
883 |
> |
testAbnormalInvokeAll1(singletonPool()); |
884 |
> |
} |
885 |
> |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
886 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
887 |
|
protected void realCompute() { |
750 |
– |
AsyncFib f = new AsyncFib(8); |
888 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
889 |
|
try { |
890 |
< |
invokeAll(f, g); |
890 |
> |
invokeAll(g); |
891 |
|
shouldThrow(); |
892 |
|
} catch (FJException success) { |
893 |
|
checkCompletedAbnormally(g, success); |
894 |
|
} |
895 |
|
}}; |
896 |
< |
testInvokeOnPool(mainPool(), a); |
896 |
> |
testInvokeOnPool(pool, a); |
897 |
|
} |
898 |
|
|
899 |
|
/** |
900 |
< |
* invokeAll(tasks) with 1 argument throws exception if task does |
900 |
> |
* invokeAll(t1, t2) throw exception if any task does |
901 |
|
*/ |
902 |
< |
public void testAbnormalInvokeAll1() { |
902 |
> |
public void testAbnormalInvokeAll2() { |
903 |
> |
testAbnormalInvokeAll2(mainPool()); |
904 |
> |
} |
905 |
> |
public void testAbnormalInvokeAll2_Singleton() { |
906 |
> |
testAbnormalInvokeAll2(singletonPool()); |
907 |
> |
} |
908 |
> |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
909 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
910 |
|
protected void realCompute() { |
911 |
+ |
AsyncFib f = new AsyncFib(8); |
912 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
913 |
+ |
ForkJoinTask[] tasks = { f, g }; |
914 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
915 |
|
try { |
916 |
< |
invokeAll(g); |
916 |
> |
invokeAll(tasks[0], tasks[1]); |
917 |
|
shouldThrow(); |
918 |
|
} catch (FJException success) { |
919 |
|
checkCompletedAbnormally(g, success); |
920 |
|
} |
921 |
|
}}; |
922 |
< |
testInvokeOnPool(mainPool(), a); |
922 |
> |
testInvokeOnPool(pool, a); |
923 |
|
} |
924 |
|
|
925 |
|
/** |
926 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
927 |
|
*/ |
928 |
|
public void testAbnormalInvokeAll3() { |
929 |
+ |
testAbnormalInvokeAll3(mainPool()); |
930 |
+ |
} |
931 |
+ |
public void testAbnormalInvokeAll3_Singleton() { |
932 |
+ |
testAbnormalInvokeAll3(singletonPool()); |
933 |
+ |
} |
934 |
+ |
public void testAbnormalInvokeAll3(ForkJoinPool pool) { |
935 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
936 |
|
protected void realCompute() { |
937 |
|
AsyncFib f = new AsyncFib(8); |
938 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
939 |
|
AsyncFib h = new AsyncFib(7); |
940 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
941 |
+ |
Collections.shuffle(Arrays.asList(tasks)); |
942 |
|
try { |
943 |
< |
invokeAll(f, g, h); |
943 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
944 |
|
shouldThrow(); |
945 |
|
} catch (FJException success) { |
946 |
|
checkCompletedAbnormally(g, success); |
947 |
|
} |
948 |
|
}}; |
949 |
< |
testInvokeOnPool(mainPool(), a); |
949 |
> |
testInvokeOnPool(pool, a); |
950 |
|
} |
951 |
|
|
952 |
|
/** |
953 |
|
* invokeAll(collection) throws exception if any task does |
954 |
|
*/ |
955 |
|
public void testAbnormalInvokeAllCollection() { |
956 |
+ |
testAbnormalInvokeAllCollection(mainPool()); |
957 |
+ |
} |
958 |
+ |
public void testAbnormalInvokeAllCollection_Singleton() { |
959 |
+ |
testAbnormalInvokeAllCollection(singletonPool()); |
960 |
+ |
} |
961 |
+ |
public void testAbnormalInvokeAllCollection(ForkJoinPool pool) { |
962 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
963 |
|
protected void realCompute() { |
964 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
965 |
|
AsyncFib g = new AsyncFib(9); |
966 |
|
AsyncFib h = new AsyncFib(7); |
967 |
< |
HashSet set = new HashSet(); |
968 |
< |
set.add(f); |
809 |
< |
set.add(g); |
810 |
< |
set.add(h); |
967 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
968 |
> |
Collections.shuffle(Arrays.asList(tasks)); |
969 |
|
try { |
970 |
< |
invokeAll(set); |
970 |
> |
invokeAll(Arrays.asList(tasks)); |
971 |
|
shouldThrow(); |
972 |
|
} catch (FJException success) { |
973 |
|
checkCompletedAbnormally(f, success); |
974 |
|
} |
975 |
|
}}; |
976 |
< |
testInvokeOnPool(mainPool(), a); |
976 |
> |
testInvokeOnPool(pool, a); |
977 |
|
} |
978 |
|
|
979 |
|
/** |
990 |
|
assertTrue(f.tryUnfork()); |
991 |
|
helpQuiesce(); |
992 |
|
checkNotDone(f); |
993 |
< |
checkCompletedNormally(g); |
993 |
> |
g.checkCompletedNormally(); |
994 |
|
}}; |
995 |
|
testInvokeOnPool(singletonPool(), a); |
996 |
|
} |
1011 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1012 |
|
helpQuiesce(); |
1013 |
|
assertEquals(0, getSurplusQueuedTaskCount()); |
1014 |
< |
checkCompletedNormally(f); |
1015 |
< |
checkCompletedNormally(g); |
1016 |
< |
checkCompletedNormally(h); |
1014 |
> |
f.checkCompletedNormally(); |
1015 |
> |
g.checkCompletedNormally(); |
1016 |
> |
h.checkCompletedNormally(); |
1017 |
|
}}; |
1018 |
|
testInvokeOnPool(singletonPool(), a); |
1019 |
|
} |
1030 |
|
assertSame(f, f.fork()); |
1031 |
|
assertSame(f, peekNextLocalTask()); |
1032 |
|
assertNull(f.join()); |
1033 |
< |
checkCompletedNormally(f); |
1033 |
> |
f.checkCompletedNormally(); |
1034 |
|
helpQuiesce(); |
1035 |
< |
checkCompletedNormally(g); |
1035 |
> |
g.checkCompletedNormally(); |
1036 |
|
}}; |
1037 |
|
testInvokeOnPool(singletonPool(), a); |
1038 |
|
} |
1051 |
|
assertSame(f, pollNextLocalTask()); |
1052 |
|
helpQuiesce(); |
1053 |
|
checkNotDone(f); |
1054 |
< |
assertEquals(34, g.number); |
897 |
< |
checkCompletedNormally(g); |
1054 |
> |
g.checkCompletedNormally(); |
1055 |
|
}}; |
1056 |
|
testInvokeOnPool(singletonPool(), a); |
1057 |
|
} |
1069 |
|
assertSame(f, pollTask()); |
1070 |
|
helpQuiesce(); |
1071 |
|
checkNotDone(f); |
1072 |
< |
checkCompletedNormally(g); |
1072 |
> |
g.checkCompletedNormally(); |
1073 |
|
}}; |
1074 |
|
testInvokeOnPool(singletonPool(), a); |
1075 |
|
} |
1087 |
|
assertSame(g, peekNextLocalTask()); |
1088 |
|
assertNull(f.join()); |
1089 |
|
helpQuiesce(); |
1090 |
< |
checkCompletedNormally(f); |
1091 |
< |
assertEquals(34, g.number); |
935 |
< |
checkCompletedNormally(g); |
1090 |
> |
f.checkCompletedNormally(); |
1091 |
> |
g.checkCompletedNormally(); |
1092 |
|
}}; |
1093 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1094 |
|
} |
1106 |
|
assertSame(f, f.fork()); |
1107 |
|
assertSame(g, pollNextLocalTask()); |
1108 |
|
helpQuiesce(); |
1109 |
< |
assertEquals(21, f.number); |
954 |
< |
checkCompletedNormally(f); |
1109 |
> |
f.checkCompletedNormally(); |
1110 |
|
checkNotDone(g); |
1111 |
|
}}; |
1112 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1125 |
|
assertSame(f, f.fork()); |
1126 |
|
assertSame(g, pollTask()); |
1127 |
|
helpQuiesce(); |
1128 |
< |
assertEquals(21, f.number); |
974 |
< |
checkCompletedNormally(f); |
1128 |
> |
f.checkCompletedNormally(); |
1129 |
|
checkNotDone(g); |
1130 |
|
}}; |
1131 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1132 |
|
} |
1133 |
|
|
980 |
– |
// versions for singleton pools |
981 |
– |
|
982 |
– |
/** |
983 |
– |
* invoke returns when task completes normally. |
984 |
– |
* isCompletedAbnormally and isCancelled return false for normally |
985 |
– |
* completed tasks; getRawResult returns null. |
986 |
– |
*/ |
987 |
– |
public void testInvokeSingleton() { |
988 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
989 |
– |
protected void realCompute() { |
990 |
– |
AsyncFib f = new AsyncFib(8); |
991 |
– |
assertNull(f.invoke()); |
992 |
– |
assertEquals(21, f.number); |
993 |
– |
checkCompletedNormally(f); |
994 |
– |
}}; |
995 |
– |
testInvokeOnPool(singletonPool(), a); |
996 |
– |
} |
997 |
– |
|
998 |
– |
/** |
999 |
– |
* quietlyInvoke task returns when task completes normally. |
1000 |
– |
* isCompletedAbnormally and isCancelled return false for normally |
1001 |
– |
* completed tasks |
1002 |
– |
*/ |
1003 |
– |
public void testQuietlyInvokeSingleton() { |
1004 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1005 |
– |
protected void realCompute() { |
1006 |
– |
AsyncFib f = new AsyncFib(8); |
1007 |
– |
f.quietlyInvoke(); |
1008 |
– |
assertEquals(21, f.number); |
1009 |
– |
checkCompletedNormally(f); |
1010 |
– |
}}; |
1011 |
– |
testInvokeOnPool(singletonPool(), a); |
1012 |
– |
} |
1013 |
– |
|
1014 |
– |
/** |
1015 |
– |
* join of a forked task returns when task completes |
1016 |
– |
*/ |
1017 |
– |
public void testForkJoinSingleton() { |
1018 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1019 |
– |
protected void realCompute() { |
1020 |
– |
AsyncFib f = new AsyncFib(8); |
1021 |
– |
assertSame(f, f.fork()); |
1022 |
– |
assertNull(f.join()); |
1023 |
– |
assertEquals(21, f.number); |
1024 |
– |
checkCompletedNormally(f); |
1025 |
– |
}}; |
1026 |
– |
testInvokeOnPool(singletonPool(), a); |
1027 |
– |
} |
1028 |
– |
|
1029 |
– |
/** |
1030 |
– |
* get of a forked task returns when task completes |
1031 |
– |
*/ |
1032 |
– |
public void testForkGetSingleton() { |
1033 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1034 |
– |
protected void realCompute() throws Exception { |
1035 |
– |
AsyncFib f = new AsyncFib(8); |
1036 |
– |
assertSame(f, f.fork()); |
1037 |
– |
assertNull(f.get()); |
1038 |
– |
assertEquals(21, f.number); |
1039 |
– |
checkCompletedNormally(f); |
1040 |
– |
}}; |
1041 |
– |
testInvokeOnPool(singletonPool(), a); |
1042 |
– |
} |
1043 |
– |
|
1044 |
– |
/** |
1045 |
– |
* timed get of a forked task returns when task completes |
1046 |
– |
*/ |
1047 |
– |
public void testForkTimedGetSingleton() { |
1048 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1049 |
– |
protected void realCompute() throws Exception { |
1050 |
– |
AsyncFib f = new AsyncFib(8); |
1051 |
– |
assertSame(f, f.fork()); |
1052 |
– |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1053 |
– |
assertEquals(21, f.number); |
1054 |
– |
checkCompletedNormally(f); |
1055 |
– |
}}; |
1056 |
– |
testInvokeOnPool(singletonPool(), a); |
1057 |
– |
} |
1058 |
– |
|
1059 |
– |
/** |
1060 |
– |
* timed get with null time unit throws NPE |
1061 |
– |
*/ |
1062 |
– |
public void testForkTimedGetNPESingleton() { |
1063 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1064 |
– |
protected void realCompute() throws Exception { |
1065 |
– |
AsyncFib f = new AsyncFib(8); |
1066 |
– |
assertSame(f, f.fork()); |
1067 |
– |
try { |
1068 |
– |
f.get(5L, null); |
1069 |
– |
shouldThrow(); |
1070 |
– |
} catch (NullPointerException success) {} |
1071 |
– |
}}; |
1072 |
– |
testInvokeOnPool(singletonPool(), a); |
1073 |
– |
} |
1074 |
– |
|
1075 |
– |
/** |
1076 |
– |
* quietlyJoin of a forked task returns when task completes |
1077 |
– |
*/ |
1078 |
– |
public void testForkQuietlyJoinSingleton() { |
1079 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1080 |
– |
protected void realCompute() { |
1081 |
– |
AsyncFib f = new AsyncFib(8); |
1082 |
– |
assertSame(f, f.fork()); |
1083 |
– |
f.quietlyJoin(); |
1084 |
– |
assertEquals(21, f.number); |
1085 |
– |
checkCompletedNormally(f); |
1086 |
– |
}}; |
1087 |
– |
testInvokeOnPool(singletonPool(), a); |
1088 |
– |
} |
1089 |
– |
|
1090 |
– |
/** |
1091 |
– |
* helpQuiesce returns when tasks are complete. |
1092 |
– |
* getQueuedTaskCount returns 0 when quiescent |
1093 |
– |
*/ |
1094 |
– |
public void testForkHelpQuiesceSingleton() { |
1095 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1096 |
– |
protected void realCompute() { |
1097 |
– |
AsyncFib f = new AsyncFib(8); |
1098 |
– |
assertSame(f, f.fork()); |
1099 |
– |
helpQuiesce(); |
1100 |
– |
assertEquals(0, getQueuedTaskCount()); |
1101 |
– |
assertEquals(21, f.number); |
1102 |
– |
checkCompletedNormally(f); |
1103 |
– |
}}; |
1104 |
– |
testInvokeOnPool(singletonPool(), a); |
1105 |
– |
} |
1106 |
– |
|
1107 |
– |
/** |
1108 |
– |
* invoke task throws exception when task completes abnormally |
1109 |
– |
*/ |
1110 |
– |
public void testAbnormalInvokeSingleton() { |
1111 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1112 |
– |
protected void realCompute() { |
1113 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1114 |
– |
try { |
1115 |
– |
f.invoke(); |
1116 |
– |
shouldThrow(); |
1117 |
– |
} catch (FJException success) { |
1118 |
– |
checkCompletedAbnormally(f, success); |
1119 |
– |
} |
1120 |
– |
}}; |
1121 |
– |
testInvokeOnPool(singletonPool(), a); |
1122 |
– |
} |
1123 |
– |
|
1124 |
– |
/** |
1125 |
– |
* quietlyInvoke task returns when task completes abnormally |
1126 |
– |
*/ |
1127 |
– |
public void testAbnormalQuietlyInvokeSingleton() { |
1128 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1129 |
– |
protected void realCompute() { |
1130 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1131 |
– |
f.quietlyInvoke(); |
1132 |
– |
assertTrue(f.getException() instanceof FJException); |
1133 |
– |
checkCompletedAbnormally(f, f.getException()); |
1134 |
– |
}}; |
1135 |
– |
testInvokeOnPool(singletonPool(), a); |
1136 |
– |
} |
1137 |
– |
|
1138 |
– |
/** |
1139 |
– |
* join of a forked task throws exception when task completes abnormally |
1140 |
– |
*/ |
1141 |
– |
public void testAbnormalForkJoinSingleton() { |
1142 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1143 |
– |
protected void realCompute() { |
1144 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1145 |
– |
assertSame(f, f.fork()); |
1146 |
– |
try { |
1147 |
– |
f.join(); |
1148 |
– |
shouldThrow(); |
1149 |
– |
} catch (FJException success) { |
1150 |
– |
checkCompletedAbnormally(f, success); |
1151 |
– |
} |
1152 |
– |
}}; |
1153 |
– |
testInvokeOnPool(singletonPool(), a); |
1154 |
– |
} |
1155 |
– |
|
1156 |
– |
/** |
1157 |
– |
* get of a forked task throws exception when task completes abnormally |
1158 |
– |
*/ |
1159 |
– |
public void testAbnormalForkGetSingleton() { |
1160 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1161 |
– |
protected void realCompute() throws Exception { |
1162 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1163 |
– |
assertSame(f, f.fork()); |
1164 |
– |
try { |
1165 |
– |
f.get(); |
1166 |
– |
shouldThrow(); |
1167 |
– |
} catch (ExecutionException success) { |
1168 |
– |
Throwable cause = success.getCause(); |
1169 |
– |
assertTrue(cause instanceof FJException); |
1170 |
– |
checkCompletedAbnormally(f, cause); |
1171 |
– |
} |
1172 |
– |
}}; |
1173 |
– |
testInvokeOnPool(singletonPool(), a); |
1174 |
– |
} |
1175 |
– |
|
1176 |
– |
/** |
1177 |
– |
* timed get of a forked task throws exception when task completes abnormally |
1178 |
– |
*/ |
1179 |
– |
public void testAbnormalForkTimedGetSingleton() { |
1180 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1181 |
– |
protected void realCompute() throws Exception { |
1182 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1183 |
– |
assertSame(f, f.fork()); |
1184 |
– |
try { |
1185 |
– |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1186 |
– |
shouldThrow(); |
1187 |
– |
} catch (ExecutionException success) { |
1188 |
– |
Throwable cause = success.getCause(); |
1189 |
– |
assertTrue(cause instanceof FJException); |
1190 |
– |
checkCompletedAbnormally(f, cause); |
1191 |
– |
} |
1192 |
– |
}}; |
1193 |
– |
testInvokeOnPool(singletonPool(), a); |
1194 |
– |
} |
1195 |
– |
|
1196 |
– |
/** |
1197 |
– |
* quietlyJoin of a forked task returns when task completes abnormally |
1198 |
– |
*/ |
1199 |
– |
public void testAbnormalForkQuietlyJoinSingleton() { |
1200 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1201 |
– |
protected void realCompute() { |
1202 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1203 |
– |
assertSame(f, f.fork()); |
1204 |
– |
f.quietlyJoin(); |
1205 |
– |
assertTrue(f.getException() instanceof FJException); |
1206 |
– |
checkCompletedAbnormally(f, f.getException()); |
1207 |
– |
}}; |
1208 |
– |
testInvokeOnPool(singletonPool(), a); |
1209 |
– |
} |
1210 |
– |
|
1211 |
– |
/** |
1212 |
– |
* invoke task throws exception after invoking completeExceptionally |
1213 |
– |
*/ |
1214 |
– |
public void testCompleteExceptionallySingleton() { |
1215 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1216 |
– |
protected void realCompute() { |
1217 |
– |
AsyncFib f = new AsyncFib(8); |
1218 |
– |
f.completeExceptionally(new FJException()); |
1219 |
– |
try { |
1220 |
– |
f.invoke(); |
1221 |
– |
shouldThrow(); |
1222 |
– |
} catch (FJException success) { |
1223 |
– |
checkCompletedAbnormally(f, success); |
1224 |
– |
} |
1225 |
– |
}}; |
1226 |
– |
testInvokeOnPool(singletonPool(), a); |
1227 |
– |
} |
1228 |
– |
|
1229 |
– |
/** |
1230 |
– |
* invokeAll(t1, t2) invokes all task arguments |
1231 |
– |
*/ |
1232 |
– |
public void testInvokeAll2Singleton() { |
1233 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1234 |
– |
protected void realCompute() { |
1235 |
– |
AsyncFib f = new AsyncFib(8); |
1236 |
– |
AsyncFib g = new AsyncFib(9); |
1237 |
– |
invokeAll(f, g); |
1238 |
– |
assertEquals(21, f.number); |
1239 |
– |
assertEquals(34, g.number); |
1240 |
– |
checkCompletedNormally(f); |
1241 |
– |
checkCompletedNormally(g); |
1242 |
– |
}}; |
1243 |
– |
testInvokeOnPool(singletonPool(), a); |
1244 |
– |
} |
1245 |
– |
|
1246 |
– |
/** |
1247 |
– |
* invokeAll(tasks) with 1 argument invokes task |
1248 |
– |
*/ |
1249 |
– |
public void testInvokeAll1Singleton() { |
1250 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1251 |
– |
protected void realCompute() { |
1252 |
– |
AsyncFib f = new AsyncFib(8); |
1253 |
– |
invokeAll(f); |
1254 |
– |
checkCompletedNormally(f); |
1255 |
– |
assertEquals(21, f.number); |
1256 |
– |
}}; |
1257 |
– |
testInvokeOnPool(singletonPool(), a); |
1258 |
– |
} |
1259 |
– |
|
1260 |
– |
/** |
1261 |
– |
* invokeAll(tasks) with > 2 argument invokes tasks |
1262 |
– |
*/ |
1263 |
– |
public void testInvokeAll3Singleton() { |
1264 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1265 |
– |
protected void realCompute() { |
1266 |
– |
AsyncFib f = new AsyncFib(8); |
1267 |
– |
AsyncFib g = new AsyncFib(9); |
1268 |
– |
AsyncFib h = new AsyncFib(7); |
1269 |
– |
invokeAll(f, g, h); |
1270 |
– |
assertEquals(21, f.number); |
1271 |
– |
assertEquals(34, g.number); |
1272 |
– |
assertEquals(13, h.number); |
1273 |
– |
checkCompletedNormally(f); |
1274 |
– |
checkCompletedNormally(g); |
1275 |
– |
checkCompletedNormally(h); |
1276 |
– |
}}; |
1277 |
– |
testInvokeOnPool(singletonPool(), a); |
1278 |
– |
} |
1279 |
– |
|
1280 |
– |
/** |
1281 |
– |
* invokeAll(collection) invokes all tasks in the collection |
1282 |
– |
*/ |
1283 |
– |
public void testInvokeAllCollectionSingleton() { |
1284 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1285 |
– |
protected void realCompute() { |
1286 |
– |
AsyncFib f = new AsyncFib(8); |
1287 |
– |
AsyncFib g = new AsyncFib(9); |
1288 |
– |
AsyncFib h = new AsyncFib(7); |
1289 |
– |
HashSet set = new HashSet(); |
1290 |
– |
set.add(f); |
1291 |
– |
set.add(g); |
1292 |
– |
set.add(h); |
1293 |
– |
invokeAll(set); |
1294 |
– |
assertEquals(21, f.number); |
1295 |
– |
assertEquals(34, g.number); |
1296 |
– |
assertEquals(13, h.number); |
1297 |
– |
checkCompletedNormally(f); |
1298 |
– |
checkCompletedNormally(g); |
1299 |
– |
checkCompletedNormally(h); |
1300 |
– |
}}; |
1301 |
– |
testInvokeOnPool(singletonPool(), a); |
1302 |
– |
} |
1303 |
– |
|
1304 |
– |
/** |
1305 |
– |
* invokeAll(tasks) with any null task throws NPE |
1306 |
– |
*/ |
1307 |
– |
public void testInvokeAllNPESingleton() { |
1308 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1309 |
– |
protected void realCompute() { |
1310 |
– |
AsyncFib f = new AsyncFib(8); |
1311 |
– |
AsyncFib g = new AsyncFib(9); |
1312 |
– |
AsyncFib h = null; |
1313 |
– |
try { |
1314 |
– |
invokeAll(f, g, h); |
1315 |
– |
shouldThrow(); |
1316 |
– |
} catch (NullPointerException success) {} |
1317 |
– |
}}; |
1318 |
– |
testInvokeOnPool(singletonPool(), a); |
1319 |
– |
} |
1320 |
– |
|
1321 |
– |
/** |
1322 |
– |
* invokeAll(t1, t2) throw exception if any task does |
1323 |
– |
*/ |
1324 |
– |
public void testAbnormalInvokeAll2Singleton() { |
1325 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1326 |
– |
protected void realCompute() { |
1327 |
– |
AsyncFib f = new AsyncFib(8); |
1328 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1329 |
– |
try { |
1330 |
– |
invokeAll(f, g); |
1331 |
– |
shouldThrow(); |
1332 |
– |
} catch (FJException success) { |
1333 |
– |
checkCompletedAbnormally(g, success); |
1334 |
– |
} |
1335 |
– |
}}; |
1336 |
– |
testInvokeOnPool(singletonPool(), a); |
1337 |
– |
} |
1338 |
– |
|
1339 |
– |
/** |
1340 |
– |
* invokeAll(tasks) with 1 argument throws exception if task does |
1341 |
– |
*/ |
1342 |
– |
public void testAbnormalInvokeAll1Singleton() { |
1343 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1344 |
– |
protected void realCompute() { |
1345 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1346 |
– |
try { |
1347 |
– |
invokeAll(g); |
1348 |
– |
shouldThrow(); |
1349 |
– |
} catch (FJException success) { |
1350 |
– |
checkCompletedAbnormally(g, success); |
1351 |
– |
} |
1352 |
– |
}}; |
1353 |
– |
testInvokeOnPool(singletonPool(), a); |
1354 |
– |
} |
1355 |
– |
|
1356 |
– |
/** |
1357 |
– |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1358 |
– |
*/ |
1359 |
– |
public void testAbnormalInvokeAll3Singleton() { |
1360 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1361 |
– |
protected void realCompute() { |
1362 |
– |
AsyncFib f = new AsyncFib(8); |
1363 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1364 |
– |
AsyncFib h = new AsyncFib(7); |
1365 |
– |
try { |
1366 |
– |
invokeAll(f, g, h); |
1367 |
– |
shouldThrow(); |
1368 |
– |
} catch (FJException success) { |
1369 |
– |
checkCompletedAbnormally(g, success); |
1370 |
– |
} |
1371 |
– |
}}; |
1372 |
– |
testInvokeOnPool(singletonPool(), a); |
1373 |
– |
} |
1374 |
– |
|
1375 |
– |
/** |
1376 |
– |
* invokeAll(collection) throws exception if any task does |
1377 |
– |
*/ |
1378 |
– |
public void testAbnormalInvokeAllCollectionSingleton() { |
1379 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1380 |
– |
protected void realCompute() { |
1381 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1382 |
– |
AsyncFib g = new AsyncFib(9); |
1383 |
– |
AsyncFib h = new AsyncFib(7); |
1384 |
– |
HashSet set = new HashSet(); |
1385 |
– |
set.add(f); |
1386 |
– |
set.add(g); |
1387 |
– |
set.add(h); |
1388 |
– |
try { |
1389 |
– |
invokeAll(set); |
1390 |
– |
shouldThrow(); |
1391 |
– |
} catch (FJException success) { |
1392 |
– |
checkCompletedAbnormally(f, success); |
1393 |
– |
} |
1394 |
– |
}}; |
1395 |
– |
testInvokeOnPool(singletonPool(), a); |
1396 |
– |
} |
1397 |
– |
|
1134 |
|
/** |
1135 |
|
* ForkJoinTask.quietlyComplete returns when task completes |
1136 |
|
* normally without setting a value. The most recent value |
1152 |
|
testInvokeOnPool(mainPool(), a); |
1153 |
|
} |
1154 |
|
|
1155 |
+ |
// jdk9 |
1156 |
+ |
|
1157 |
+ |
/** |
1158 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1159 |
+ |
*/ |
1160 |
+ |
public void testPollSubmission() { |
1161 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1162 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1163 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1164 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1165 |
+ |
final ForkJoinPool p = singletonPool(); |
1166 |
+ |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1167 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1168 |
+ |
public void realRun() { |
1169 |
+ |
p.execute(a); |
1170 |
+ |
p.execute(b); |
1171 |
+ |
p.execute(c); |
1172 |
+ |
}}); |
1173 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1174 |
+ |
protected void realCompute() { |
1175 |
+ |
external.start(); |
1176 |
+ |
try { |
1177 |
+ |
external.join(); |
1178 |
+ |
} catch (Exception ex) { |
1179 |
+ |
threadUnexpectedException(ex); |
1180 |
+ |
} |
1181 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1182 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1183 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1184 |
+ |
assertTrue(r == a || r == b || r == c); |
1185 |
+ |
assertFalse(r.isDone()); |
1186 |
+ |
}}; |
1187 |
+ |
p.invoke(s); |
1188 |
+ |
} |
1189 |
+ |
} |
1190 |
+ |
|
1191 |
|
} |