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