5 |
|
*/ |
6 |
|
|
7 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
|
|
9 |
< |
import java.util.HashSet; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.concurrent.CountDownLatch; |
11 |
|
import java.util.concurrent.ExecutionException; |
12 |
|
import java.util.concurrent.ForkJoinPool; |
13 |
|
import java.util.concurrent.ForkJoinTask; |
14 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
15 |
|
import java.util.concurrent.RecursiveAction; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
|
|
35 |
|
static final short EXCEPTION_STATE = 1; |
36 |
|
|
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
|
41 |
|
public static Test suite() { |
60 |
|
null, true); |
61 |
|
} |
62 |
|
|
63 |
+ |
// Compute fib naively and efficiently |
64 |
+ |
final int[] fib; |
65 |
+ |
{ |
66 |
+ |
int[] fib = new int[10]; |
67 |
+ |
fib[0] = 0; |
68 |
+ |
fib[1] = 1; |
69 |
+ |
for (int i = 2; i < fib.length; i++) |
70 |
+ |
fib[i] = fib[i - 1] + fib[i - 2]; |
71 |
+ |
this.fib = fib; |
72 |
+ |
} |
73 |
+ |
|
74 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
75 |
< |
try { |
75 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
76 |
|
assertFalse(a.isDone()); |
77 |
|
assertFalse(a.isCompletedNormally()); |
78 |
|
assertFalse(a.isCompletedAbnormally()); |
88 |
|
assertFalse(a.isCancelled()); |
89 |
|
assertNull(a.getException()); |
90 |
|
assertNull(a.getRawResult()); |
79 |
– |
} finally { |
80 |
– |
joinPool(pool); |
91 |
|
} |
92 |
|
} |
93 |
|
|
99 |
|
assertNull(a.getException()); |
100 |
|
assertNull(a.getRawResult()); |
101 |
|
if (a instanceof BinaryAsyncAction) |
102 |
< |
assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == INITIAL_STATE); |
102 |
> |
assertEquals(INITIAL_STATE, |
103 |
> |
((BinaryAsyncAction)a).getForkJoinTaskTag()); |
104 |
|
|
105 |
|
try { |
106 |
< |
a.get(0L, SECONDS); |
106 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
107 |
|
shouldThrow(); |
108 |
|
} catch (TimeoutException success) { |
109 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
|
assertNull(a.getException()); |
122 |
|
assertSame(expected, a.getRawResult()); |
123 |
|
if (a instanceof BinaryAsyncAction) |
124 |
< |
assertTrue(((BinaryAsyncAction)a).getForkJoinTaskTag() == COMPLETE_STATE); |
124 |
> |
assertEquals(COMPLETE_STATE, |
125 |
> |
((BinaryAsyncAction)a).getForkJoinTaskTag()); |
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) < LONG_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) < LONG_DELAY_MS); |
140 |
|
Thread.interrupted(); |
141 |
|
} |
142 |
|
|
144 |
|
assertFalse(a.cancel(true)); |
145 |
|
try { |
146 |
|
assertSame(expected, a.get()); |
147 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
148 |
< |
try { |
137 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
138 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
> |
assertSame(expected, a.get(randomTimeout(), randomTimeUnit())); |
148 |
> |
} catch (Exception fail) { threadUnexpectedException(fail); } |
149 |
|
} |
150 |
|
|
151 |
|
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
170 |
|
Thread.interrupted(); |
171 |
|
|
172 |
|
{ |
173 |
< |
long t0 = System.nanoTime(); |
173 |
> |
long startTime = System.nanoTime(); |
174 |
|
a.quietlyJoin(); // should be no-op |
175 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
175 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
176 |
|
} |
177 |
|
|
178 |
|
try { |
183 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
184 |
|
|
185 |
|
try { |
186 |
< |
a.get(5L, SECONDS); |
186 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
187 |
|
shouldThrow(); |
188 |
|
} catch (ExecutionException success) { |
189 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
196 |
|
|
197 |
|
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
198 |
|
|
199 |
< |
private BinaryAsyncAction parent; |
199 |
> |
private volatile BinaryAsyncAction parent; |
200 |
|
|
201 |
< |
private BinaryAsyncAction sibling; |
201 |
> |
private volatile BinaryAsyncAction sibling; |
202 |
|
|
203 |
|
protected BinaryAsyncAction() { |
204 |
|
setForkJoinTaskTag(INITIAL_STATE); |
241 |
|
super.completeExceptionally(ex); |
242 |
|
} |
243 |
|
|
244 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
245 |
+ |
if (super.cancel(mayInterruptIfRunning)) { |
246 |
+ |
completeExceptionally(new FJException()); |
247 |
+ |
return true; |
248 |
+ |
} |
249 |
+ |
return false; |
250 |
+ |
} |
251 |
+ |
|
252 |
|
public final void complete() { |
253 |
|
BinaryAsyncAction a = this; |
254 |
|
for (;;) { |
271 |
|
} |
272 |
|
|
273 |
|
public final void completeExceptionally(Throwable ex) { |
274 |
< |
BinaryAsyncAction a = this; |
257 |
< |
while (!a.isCompletedAbnormally()) { |
274 |
> |
for (BinaryAsyncAction a = this;;) { |
275 |
|
a.completeThisExceptionally(ex); |
276 |
|
BinaryAsyncAction s = a.sibling; |
277 |
< |
if (s != null) |
278 |
< |
s.cancel(false); |
279 |
< |
if (!a.onException() || (a = a.parent) == null) |
277 |
> |
if (s != null && !s.isDone()) |
278 |
> |
s.completeExceptionally(ex); |
279 |
> |
if ((a = a.parent) == null) |
280 |
|
break; |
281 |
|
} |
282 |
|
} |
296 |
|
|
297 |
|
} |
298 |
|
|
299 |
< |
static final class AsyncFib extends BinaryAsyncAction { |
299 |
> |
final class AsyncFib extends BinaryAsyncAction { |
300 |
|
int number; |
301 |
< |
public AsyncFib(int n) { |
302 |
< |
this.number = n; |
301 |
> |
int expectedResult; |
302 |
> |
public AsyncFib(int number) { |
303 |
> |
this.number = number; |
304 |
> |
this.expectedResult = fib[number]; |
305 |
|
} |
306 |
|
|
307 |
|
public final boolean exec() { |
308 |
|
try { |
309 |
|
AsyncFib f = this; |
310 |
|
int n = f.number; |
311 |
< |
if (n > 1) { |
312 |
< |
while (n > 1) { |
313 |
< |
AsyncFib p = f; |
314 |
< |
AsyncFib r = new AsyncFib(n - 2); |
315 |
< |
f = new AsyncFib(--n); |
316 |
< |
p.linkSubtasks(r, f); |
298 |
< |
r.fork(); |
299 |
< |
} |
300 |
< |
f.number = n; |
311 |
> |
while (n > 1) { |
312 |
> |
AsyncFib p = f; |
313 |
> |
AsyncFib r = new AsyncFib(n - 2); |
314 |
> |
f = new AsyncFib(--n); |
315 |
> |
p.linkSubtasks(r, f); |
316 |
> |
r.fork(); |
317 |
|
} |
318 |
|
f.complete(); |
319 |
|
} |
320 |
|
catch (Throwable ex) { |
321 |
|
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
322 |
|
} |
323 |
+ |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
324 |
+ |
throw new FJException(); |
325 |
|
return false; |
326 |
|
} |
327 |
|
|
329 |
|
number = ((AsyncFib)x).number + ((AsyncFib)y).number; |
330 |
|
super.onComplete(x, y); |
331 |
|
} |
332 |
+ |
|
333 |
+ |
public void checkCompletedNormally() { |
334 |
+ |
assertEquals(expectedResult, number); |
335 |
+ |
ForkJoinTask8Test.this.checkCompletedNormally(this); |
336 |
+ |
} |
337 |
|
} |
338 |
|
|
339 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
343 |
|
} |
344 |
|
|
345 |
|
public final boolean exec() { |
346 |
< |
FailingAsyncFib f = this; |
347 |
< |
int n = f.number; |
348 |
< |
if (n > 1) { |
346 |
> |
try { |
347 |
> |
FailingAsyncFib f = this; |
348 |
> |
int n = f.number; |
349 |
|
while (n > 1) { |
350 |
|
FailingAsyncFib p = f; |
351 |
|
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
353 |
|
p.linkSubtasks(r, f); |
354 |
|
r.fork(); |
355 |
|
} |
356 |
< |
f.number = n; |
356 |
> |
f.complete(); |
357 |
> |
} |
358 |
> |
catch (Throwable ex) { |
359 |
> |
compareAndSetForkJoinTaskTag(INITIAL_STATE, EXCEPTION_STATE); |
360 |
|
} |
361 |
< |
f.complete(); |
361 |
> |
if (getForkJoinTaskTag() == EXCEPTION_STATE) |
362 |
> |
throw new FJException(); |
363 |
|
return false; |
364 |
|
} |
365 |
|
|
374 |
|
* completed tasks; getRawResult returns null. |
375 |
|
*/ |
376 |
|
public void testInvoke() { |
377 |
+ |
testInvoke(mainPool()); |
378 |
+ |
} |
379 |
+ |
public void testInvoke_Singleton() { |
380 |
+ |
testInvoke(singletonPool()); |
381 |
+ |
} |
382 |
+ |
public void testInvoke(ForkJoinPool pool) { |
383 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
384 |
|
protected void realCompute() { |
385 |
|
AsyncFib f = new AsyncFib(8); |
386 |
|
assertNull(f.invoke()); |
387 |
< |
assertEquals(21, f.number); |
355 |
< |
checkCompletedNormally(f); |
387 |
> |
f.checkCompletedNormally(); |
388 |
|
}}; |
389 |
< |
testInvokeOnPool(mainPool(), a); |
389 |
> |
testInvokeOnPool(pool, a); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
395 |
|
* completed tasks |
396 |
|
*/ |
397 |
|
public void testQuietlyInvoke() { |
398 |
+ |
testQuietlyInvoke(mainPool()); |
399 |
+ |
} |
400 |
+ |
public void testQuietlyInvoke_Singleton() { |
401 |
+ |
testQuietlyInvoke(singletonPool()); |
402 |
+ |
} |
403 |
+ |
public void testQuietlyInvoke(ForkJoinPool pool) { |
404 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
405 |
|
protected void realCompute() { |
406 |
|
AsyncFib f = new AsyncFib(8); |
407 |
|
f.quietlyInvoke(); |
408 |
< |
assertEquals(21, f.number); |
371 |
< |
checkCompletedNormally(f); |
408 |
> |
f.checkCompletedNormally(); |
409 |
|
}}; |
410 |
< |
testInvokeOnPool(mainPool(), a); |
410 |
> |
testInvokeOnPool(pool, a); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
|
* join of a forked task returns when task completes |
415 |
|
*/ |
416 |
|
public void testForkJoin() { |
417 |
+ |
testForkJoin(mainPool()); |
418 |
+ |
} |
419 |
+ |
public void testForkJoin_Singleton() { |
420 |
+ |
testForkJoin(singletonPool()); |
421 |
+ |
} |
422 |
+ |
public void testForkJoin(ForkJoinPool pool) { |
423 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
424 |
|
protected void realCompute() { |
425 |
|
AsyncFib f = new AsyncFib(8); |
426 |
|
assertSame(f, f.fork()); |
427 |
|
assertNull(f.join()); |
428 |
< |
assertEquals(21, f.number); |
386 |
< |
checkCompletedNormally(f); |
428 |
> |
f.checkCompletedNormally(); |
429 |
|
}}; |
430 |
< |
testInvokeOnPool(mainPool(), a); |
430 |
> |
testInvokeOnPool(pool, a); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
|
* get of a forked task returns when task completes |
435 |
|
*/ |
436 |
|
public void testForkGet() { |
437 |
+ |
testForkGet(mainPool()); |
438 |
+ |
} |
439 |
+ |
public void testForkGet_Singleton() { |
440 |
+ |
testForkGet(singletonPool()); |
441 |
+ |
} |
442 |
+ |
public void testForkGet(ForkJoinPool pool) { |
443 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
444 |
|
protected void realCompute() throws Exception { |
445 |
|
AsyncFib f = new AsyncFib(8); |
446 |
|
assertSame(f, f.fork()); |
447 |
|
assertNull(f.get()); |
448 |
< |
assertEquals(21, f.number); |
401 |
< |
checkCompletedNormally(f); |
448 |
> |
f.checkCompletedNormally(); |
449 |
|
}}; |
450 |
< |
testInvokeOnPool(mainPool(), a); |
450 |
> |
testInvokeOnPool(pool, a); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
|
* timed get of a forked task returns when task completes |
455 |
|
*/ |
456 |
|
public void testForkTimedGet() { |
457 |
+ |
testForkTimedGet(mainPool()); |
458 |
+ |
} |
459 |
+ |
public void testForkTimedGet_Singleton() { |
460 |
+ |
testForkTimedGet(singletonPool()); |
461 |
+ |
} |
462 |
+ |
public void testForkTimedGet(ForkJoinPool pool) { |
463 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
464 |
|
protected void realCompute() throws Exception { |
465 |
|
AsyncFib f = new AsyncFib(8); |
466 |
|
assertSame(f, f.fork()); |
467 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
468 |
< |
assertEquals(21, f.number); |
416 |
< |
checkCompletedNormally(f); |
468 |
> |
f.checkCompletedNormally(); |
469 |
|
}}; |
470 |
< |
testInvokeOnPool(mainPool(), a); |
470 |
> |
testInvokeOnPool(pool, a); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* timed get with null time unit throws NPE |
474 |
> |
* timed get with null time unit throws NullPointerException |
475 |
|
*/ |
476 |
< |
public void testForkTimedGetNPE() { |
476 |
> |
public void testForkTimedGetNullTimeUnit() { |
477 |
> |
testForkTimedGetNullTimeUnit(mainPool()); |
478 |
> |
} |
479 |
> |
public void testForkTimedGetNullTimeUnit_Singleton() { |
480 |
> |
testForkTimedGet(singletonPool()); |
481 |
> |
} |
482 |
> |
public void testForkTimedGetNullTimeUnit(ForkJoinPool pool) { |
483 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
484 |
|
protected void realCompute() throws Exception { |
485 |
|
AsyncFib f = new AsyncFib(8); |
486 |
|
assertSame(f, f.fork()); |
487 |
|
try { |
488 |
< |
f.get(5L, null); |
488 |
> |
f.get(randomTimeout(), null); |
489 |
|
shouldThrow(); |
490 |
|
} catch (NullPointerException success) {} |
491 |
|
}}; |
492 |
< |
testInvokeOnPool(mainPool(), a); |
492 |
> |
testInvokeOnPool(pool, a); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
496 |
|
* quietlyJoin of a forked task returns when task completes |
497 |
|
*/ |
498 |
|
public void testForkQuietlyJoin() { |
499 |
+ |
testForkQuietlyJoin(mainPool()); |
500 |
+ |
} |
501 |
+ |
public void testForkQuietlyJoin_Singleton() { |
502 |
+ |
testForkQuietlyJoin(singletonPool()); |
503 |
+ |
} |
504 |
+ |
public void testForkQuietlyJoin(ForkJoinPool pool) { |
505 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
506 |
|
protected void realCompute() { |
507 |
|
AsyncFib f = new AsyncFib(8); |
508 |
|
assertSame(f, f.fork()); |
509 |
|
f.quietlyJoin(); |
510 |
< |
assertEquals(21, f.number); |
447 |
< |
checkCompletedNormally(f); |
510 |
> |
f.checkCompletedNormally(); |
511 |
|
}}; |
512 |
< |
testInvokeOnPool(mainPool(), a); |
512 |
> |
testInvokeOnPool(pool, a); |
513 |
|
} |
514 |
|
|
515 |
|
/** |
517 |
|
* getQueuedTaskCount returns 0 when quiescent |
518 |
|
*/ |
519 |
|
public void testForkHelpQuiesce() { |
520 |
+ |
testForkHelpQuiesce(mainPool()); |
521 |
+ |
} |
522 |
+ |
public void testForkHelpQuiesce_Singleton() { |
523 |
+ |
testForkHelpQuiesce(singletonPool()); |
524 |
+ |
} |
525 |
+ |
public void testForkHelpQuiesce(ForkJoinPool pool) { |
526 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
527 |
|
protected void realCompute() { |
528 |
|
AsyncFib f = new AsyncFib(8); |
529 |
|
assertSame(f, f.fork()); |
530 |
|
helpQuiesce(); |
462 |
– |
assertEquals(21, f.number); |
531 |
|
assertEquals(0, getQueuedTaskCount()); |
532 |
< |
checkCompletedNormally(f); |
532 |
> |
f.checkCompletedNormally(); |
533 |
|
}}; |
534 |
< |
testInvokeOnPool(mainPool(), a); |
534 |
> |
testInvokeOnPool(pool, a); |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
|
* invoke task throws exception when task completes abnormally |
539 |
|
*/ |
540 |
|
public void testAbnormalInvoke() { |
541 |
+ |
testAbnormalInvoke(mainPool()); |
542 |
+ |
} |
543 |
+ |
public void testAbnormalInvoke_Singleton() { |
544 |
+ |
testAbnormalInvoke(singletonPool()); |
545 |
+ |
} |
546 |
+ |
public void testAbnormalInvoke(ForkJoinPool pool) { |
547 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
548 |
|
protected void realCompute() { |
549 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
554 |
|
checkCompletedAbnormally(f, success); |
555 |
|
} |
556 |
|
}}; |
557 |
< |
testInvokeOnPool(mainPool(), a); |
557 |
> |
testInvokeOnPool(pool, a); |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
|
* quietlyInvoke task returns when task completes abnormally |
562 |
|
*/ |
563 |
|
public void testAbnormalQuietlyInvoke() { |
564 |
+ |
testAbnormalQuietlyInvoke(mainPool()); |
565 |
+ |
} |
566 |
+ |
public void testAbnormalQuietlyInvoke_Singleton() { |
567 |
+ |
testAbnormalQuietlyInvoke(singletonPool()); |
568 |
+ |
} |
569 |
+ |
public void testAbnormalQuietlyInvoke(ForkJoinPool pool) { |
570 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
571 |
|
protected void realCompute() { |
572 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
574 |
|
assertTrue(f.getException() instanceof FJException); |
575 |
|
checkCompletedAbnormally(f, f.getException()); |
576 |
|
}}; |
577 |
< |
testInvokeOnPool(mainPool(), a); |
577 |
> |
testInvokeOnPool(pool, a); |
578 |
|
} |
579 |
|
|
580 |
|
/** |
581 |
|
* join of a forked task throws exception when task completes abnormally |
582 |
|
*/ |
583 |
|
public void testAbnormalForkJoin() { |
584 |
+ |
testAbnormalForkJoin(mainPool()); |
585 |
+ |
} |
586 |
+ |
public void testAbnormalForkJoin_Singleton() { |
587 |
+ |
testAbnormalForkJoin(singletonPool()); |
588 |
+ |
} |
589 |
+ |
public void testAbnormalForkJoin(ForkJoinPool pool) { |
590 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
591 |
|
protected void realCompute() { |
592 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
598 |
|
checkCompletedAbnormally(f, success); |
599 |
|
} |
600 |
|
}}; |
601 |
< |
testInvokeOnPool(mainPool(), a); |
601 |
> |
testInvokeOnPool(pool, a); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
605 |
|
* get of a forked task throws exception when task completes abnormally |
606 |
|
*/ |
607 |
|
public void testAbnormalForkGet() { |
608 |
+ |
testAbnormalForkGet(mainPool()); |
609 |
+ |
} |
610 |
+ |
public void testAbnormalForkGet_Singleton() { |
611 |
+ |
testAbnormalForkJoin(singletonPool()); |
612 |
+ |
} |
613 |
+ |
public void testAbnormalForkGet(ForkJoinPool pool) { |
614 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
615 |
|
protected void realCompute() throws Exception { |
616 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
624 |
|
checkCompletedAbnormally(f, cause); |
625 |
|
} |
626 |
|
}}; |
627 |
< |
testInvokeOnPool(mainPool(), a); |
627 |
> |
testInvokeOnPool(pool, a); |
628 |
|
} |
629 |
|
|
630 |
|
/** |
631 |
|
* timed get of a forked task throws exception when task completes abnormally |
632 |
|
*/ |
633 |
|
public void testAbnormalForkTimedGet() { |
634 |
+ |
testAbnormalForkTimedGet(mainPool()); |
635 |
+ |
} |
636 |
+ |
public void testAbnormalForkTimedGet_Singleton() { |
637 |
+ |
testAbnormalForkTimedGet(singletonPool()); |
638 |
+ |
} |
639 |
+ |
public void testAbnormalForkTimedGet(ForkJoinPool pool) { |
640 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
641 |
|
protected void realCompute() throws Exception { |
642 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
650 |
|
checkCompletedAbnormally(f, cause); |
651 |
|
} |
652 |
|
}}; |
653 |
< |
testInvokeOnPool(mainPool(), a); |
653 |
> |
testInvokeOnPool(pool, a); |
654 |
|
} |
655 |
|
|
656 |
|
/** |
657 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
658 |
|
*/ |
659 |
|
public void testAbnormalForkQuietlyJoin() { |
660 |
+ |
testAbnormalForkQuietlyJoin(mainPool()); |
661 |
+ |
} |
662 |
+ |
public void testAbnormalForkQuietlyJoin_Singleton() { |
663 |
+ |
testAbnormalForkQuietlyJoin(singletonPool()); |
664 |
+ |
} |
665 |
+ |
public void testAbnormalForkQuietlyJoin(ForkJoinPool pool) { |
666 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
667 |
|
protected void realCompute() { |
668 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
671 |
|
assertTrue(f.getException() instanceof FJException); |
672 |
|
checkCompletedAbnormally(f, f.getException()); |
673 |
|
}}; |
674 |
< |
testInvokeOnPool(mainPool(), a); |
674 |
> |
testInvokeOnPool(pool, a); |
675 |
|
} |
676 |
|
|
677 |
|
/** |
678 |
|
* getPool of executing task returns its pool |
679 |
|
*/ |
680 |
|
public void testGetPool() { |
681 |
< |
final ForkJoinPool mainPool = mainPool(); |
681 |
> |
testGetPool(mainPool()); |
682 |
> |
} |
683 |
> |
public void testGetPool_Singleton() { |
684 |
> |
testGetPool(singletonPool()); |
685 |
> |
} |
686 |
> |
public void testGetPool(ForkJoinPool pool) { |
687 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
688 |
|
protected void realCompute() { |
689 |
< |
assertSame(mainPool, getPool()); |
689 |
> |
assertSame(pool, getPool()); |
690 |
|
}}; |
691 |
< |
testInvokeOnPool(mainPool, a); |
691 |
> |
testInvokeOnPool(pool, a); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
706 |
|
* inForkJoinPool of executing task returns true |
707 |
|
*/ |
708 |
|
public void testInForkJoinPool() { |
709 |
+ |
testInForkJoinPool(mainPool()); |
710 |
+ |
} |
711 |
+ |
public void testInForkJoinPool_Singleton() { |
712 |
+ |
testInForkJoinPool(singletonPool()); |
713 |
+ |
} |
714 |
+ |
public void testInForkJoinPool(ForkJoinPool pool) { |
715 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
716 |
|
protected void realCompute() { |
717 |
|
assertTrue(inForkJoinPool()); |
718 |
|
}}; |
719 |
< |
testInvokeOnPool(mainPool(), a); |
719 |
> |
testInvokeOnPool(pool, a); |
720 |
|
} |
721 |
|
|
722 |
|
/** |
746 |
|
* invoke task throws exception after invoking completeExceptionally |
747 |
|
*/ |
748 |
|
public void testCompleteExceptionally() { |
749 |
+ |
testCompleteExceptionally(mainPool()); |
750 |
+ |
} |
751 |
+ |
public void testCompleteExceptionally_Singleton() { |
752 |
+ |
testCompleteExceptionally(singletonPool()); |
753 |
+ |
} |
754 |
+ |
public void testCompleteExceptionally(ForkJoinPool pool) { |
755 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
756 |
|
protected void realCompute() { |
757 |
|
AsyncFib f = new AsyncFib(8); |
763 |
|
checkCompletedAbnormally(f, success); |
764 |
|
} |
765 |
|
}}; |
766 |
< |
testInvokeOnPool(mainPool(), a); |
766 |
> |
testInvokeOnPool(pool, a); |
767 |
|
} |
768 |
|
|
769 |
|
/** |
770 |
< |
* invokeAll(t1, t2) invokes all task arguments |
770 |
> |
* invokeAll(tasks) with 1 argument invokes task |
771 |
|
*/ |
772 |
< |
public void testInvokeAll2() { |
772 |
> |
public void testInvokeAll1() { |
773 |
> |
testInvokeAll1(mainPool()); |
774 |
> |
} |
775 |
> |
public void testInvokeAll1_Singleton() { |
776 |
> |
testInvokeAll1(singletonPool()); |
777 |
> |
} |
778 |
> |
public void testInvokeAll1(ForkJoinPool pool) { |
779 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
780 |
|
protected void realCompute() { |
781 |
|
AsyncFib f = new AsyncFib(8); |
782 |
< |
AsyncFib g = new AsyncFib(9); |
783 |
< |
invokeAll(f, g); |
657 |
< |
assertEquals(21, f.number); |
658 |
< |
assertEquals(34, g.number); |
659 |
< |
checkCompletedNormally(f); |
660 |
< |
checkCompletedNormally(g); |
782 |
> |
invokeAll(f); |
783 |
> |
f.checkCompletedNormally(); |
784 |
|
}}; |
785 |
< |
testInvokeOnPool(mainPool(), a); |
785 |
> |
testInvokeOnPool(pool, a); |
786 |
|
} |
787 |
|
|
788 |
|
/** |
789 |
< |
* invokeAll(tasks) with 1 argument invokes task |
789 |
> |
* invokeAll(t1, t2) invokes all task arguments |
790 |
|
*/ |
791 |
< |
public void testInvokeAll1() { |
791 |
> |
public void testInvokeAll2() { |
792 |
> |
testInvokeAll2(mainPool()); |
793 |
> |
} |
794 |
> |
public void testInvokeAll2_Singleton() { |
795 |
> |
testInvokeAll2(singletonPool()); |
796 |
> |
} |
797 |
> |
public void testInvokeAll2(ForkJoinPool pool) { |
798 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
799 |
|
protected void realCompute() { |
800 |
< |
AsyncFib f = new AsyncFib(8); |
801 |
< |
invokeAll(f); |
802 |
< |
checkCompletedNormally(f); |
803 |
< |
assertEquals(21, f.number); |
800 |
> |
AsyncFib[] tasks = { |
801 |
> |
new AsyncFib(8), |
802 |
> |
new AsyncFib(9), |
803 |
> |
}; |
804 |
> |
invokeAll(tasks[0], tasks[1]); |
805 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
806 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
807 |
|
}}; |
808 |
< |
testInvokeOnPool(mainPool(), a); |
808 |
> |
testInvokeOnPool(pool, a); |
809 |
|
} |
810 |
|
|
811 |
|
/** |
812 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
813 |
|
*/ |
814 |
|
public void testInvokeAll3() { |
815 |
+ |
testInvokeAll3(mainPool()); |
816 |
+ |
} |
817 |
+ |
public void testInvokeAll3_Singleton() { |
818 |
+ |
testInvokeAll3(singletonPool()); |
819 |
+ |
} |
820 |
+ |
public void testInvokeAll3(ForkJoinPool pool) { |
821 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
822 |
|
protected void realCompute() { |
823 |
< |
AsyncFib f = new AsyncFib(8); |
824 |
< |
AsyncFib g = new AsyncFib(9); |
825 |
< |
AsyncFib h = new AsyncFib(7); |
826 |
< |
invokeAll(f, g, h); |
827 |
< |
assertEquals(21, f.number); |
828 |
< |
assertEquals(34, g.number); |
829 |
< |
assertEquals(13, h.number); |
830 |
< |
checkCompletedNormally(f); |
693 |
< |
checkCompletedNormally(g); |
694 |
< |
checkCompletedNormally(h); |
823 |
> |
AsyncFib[] tasks = { |
824 |
> |
new AsyncFib(8), |
825 |
> |
new AsyncFib(9), |
826 |
> |
new AsyncFib(7), |
827 |
> |
}; |
828 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
829 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
830 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
831 |
|
}}; |
832 |
< |
testInvokeOnPool(mainPool(), a); |
832 |
> |
testInvokeOnPool(pool, a); |
833 |
|
} |
834 |
|
|
835 |
|
/** |
836 |
|
* invokeAll(collection) invokes all tasks in the collection |
837 |
|
*/ |
838 |
|
public void testInvokeAllCollection() { |
839 |
+ |
testInvokeAllCollection(mainPool()); |
840 |
+ |
} |
841 |
+ |
public void testInvokeAllCollection_Singleton() { |
842 |
+ |
testInvokeAllCollection(singletonPool()); |
843 |
+ |
} |
844 |
+ |
public void testInvokeAllCollection(ForkJoinPool pool) { |
845 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
846 |
|
protected void realCompute() { |
847 |
< |
AsyncFib f = new AsyncFib(8); |
848 |
< |
AsyncFib g = new AsyncFib(9); |
849 |
< |
AsyncFib h = new AsyncFib(7); |
850 |
< |
HashSet set = new HashSet(); |
851 |
< |
set.add(f); |
852 |
< |
set.add(g); |
853 |
< |
set.add(h); |
854 |
< |
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); |
847 |
> |
AsyncFib[] tasks = { |
848 |
> |
new AsyncFib(8), |
849 |
> |
new AsyncFib(9), |
850 |
> |
new AsyncFib(7), |
851 |
> |
}; |
852 |
> |
invokeAll(Arrays.asList(tasks)); |
853 |
> |
for (AsyncFib task : tasks) assertTrue(task.isDone()); |
854 |
> |
for (AsyncFib task : tasks) task.checkCompletedNormally(); |
855 |
|
}}; |
856 |
< |
testInvokeOnPool(mainPool(), a); |
856 |
> |
testInvokeOnPool(pool, a); |
857 |
|
} |
858 |
|
|
859 |
|
/** |
860 |
|
* invokeAll(tasks) with any null task throws NullPointerException |
861 |
|
*/ |
862 |
|
public void testInvokeAllNullTask() { |
863 |
+ |
testInvokeAllNullTask(mainPool()); |
864 |
+ |
} |
865 |
+ |
public void testInvokeAllNullTask_Singleton() { |
866 |
+ |
testInvokeAllNullTask(singletonPool()); |
867 |
+ |
} |
868 |
+ |
public void testInvokeAllNullTask(ForkJoinPool pool) { |
869 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
870 |
|
protected void realCompute() { |
729 |
– |
AsyncFib f = new AsyncFib(8); |
730 |
– |
AsyncFib g = new AsyncFib(9); |
871 |
|
AsyncFib nul = null; |
872 |
|
Runnable[] throwingActions = { |
873 |
|
() -> invokeAll(nul), |
874 |
|
() -> invokeAll(nul, nul), |
875 |
< |
() -> invokeAll(f, g, nul), |
876 |
< |
() -> invokeAll(f, nul, g), |
877 |
< |
() -> invokeAll(nul, f, g), |
875 |
> |
() -> invokeAll(new AsyncFib(8), new AsyncFib(9), nul), |
876 |
> |
() -> invokeAll(new AsyncFib(8), nul, new AsyncFib(9)), |
877 |
> |
() -> invokeAll(nul, new AsyncFib(8), new AsyncFib(9)), |
878 |
|
}; |
879 |
|
assertThrows(NullPointerException.class, throwingActions); |
880 |
|
}}; |
881 |
< |
testInvokeOnPool(mainPool(), a); |
881 |
> |
testInvokeOnPool(pool, a); |
882 |
|
} |
883 |
|
|
884 |
|
/** |
885 |
< |
* invokeAll(t1, t2) throw exception if any task does |
885 |
> |
* invokeAll(tasks) with 1 argument throws exception if task does |
886 |
|
*/ |
887 |
< |
public void testAbnormalInvokeAll2() { |
887 |
> |
public void testAbnormalInvokeAll1() { |
888 |
> |
testAbnormalInvokeAll1(mainPool()); |
889 |
> |
} |
890 |
> |
public void testAbnormalInvokeAll1_Singleton() { |
891 |
> |
testAbnormalInvokeAll1(singletonPool()); |
892 |
> |
} |
893 |
> |
public void testAbnormalInvokeAll1(ForkJoinPool pool) { |
894 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
895 |
|
protected void realCompute() { |
750 |
– |
AsyncFib f = new AsyncFib(8); |
896 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
897 |
|
try { |
898 |
< |
invokeAll(f, g); |
898 |
> |
invokeAll(g); |
899 |
|
shouldThrow(); |
900 |
|
} catch (FJException success) { |
901 |
|
checkCompletedAbnormally(g, success); |
902 |
|
} |
903 |
|
}}; |
904 |
< |
testInvokeOnPool(mainPool(), a); |
904 |
> |
testInvokeOnPool(pool, a); |
905 |
|
} |
906 |
|
|
907 |
|
/** |
908 |
< |
* invokeAll(tasks) with 1 argument throws exception if task does |
908 |
> |
* invokeAll(t1, t2) throw exception if any task does |
909 |
|
*/ |
910 |
< |
public void testAbnormalInvokeAll1() { |
910 |
> |
public void testAbnormalInvokeAll2() { |
911 |
> |
testAbnormalInvokeAll2(mainPool()); |
912 |
> |
} |
913 |
> |
public void testAbnormalInvokeAll2_Singleton() { |
914 |
> |
testAbnormalInvokeAll2(singletonPool()); |
915 |
> |
} |
916 |
> |
public void testAbnormalInvokeAll2(ForkJoinPool pool) { |
917 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
918 |
|
protected void realCompute() { |
919 |
+ |
AsyncFib f = new AsyncFib(8); |
920 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
921 |
+ |
ForkJoinTask[] tasks = { f, g }; |
922 |
+ |
shuffle(tasks); |
923 |
|
try { |
924 |
< |
invokeAll(g); |
924 |
> |
invokeAll(tasks[0], tasks[1]); |
925 |
|
shouldThrow(); |
926 |
|
} catch (FJException success) { |
927 |
|
checkCompletedAbnormally(g, success); |
928 |
|
} |
929 |
|
}}; |
930 |
< |
testInvokeOnPool(mainPool(), a); |
930 |
> |
testInvokeOnPool(pool, a); |
931 |
|
} |
932 |
|
|
933 |
|
/** |
934 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
935 |
|
*/ |
936 |
|
public void testAbnormalInvokeAll3() { |
937 |
+ |
testAbnormalInvokeAll3(mainPool()); |
938 |
+ |
} |
939 |
+ |
public void testAbnormalInvokeAll3_Singleton() { |
940 |
+ |
testAbnormalInvokeAll3(singletonPool()); |
941 |
+ |
} |
942 |
+ |
public void testAbnormalInvokeAll3(ForkJoinPool pool) { |
943 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
944 |
|
protected void realCompute() { |
945 |
|
AsyncFib f = new AsyncFib(8); |
946 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
947 |
|
AsyncFib h = new AsyncFib(7); |
948 |
+ |
ForkJoinTask[] tasks = { f, g, h }; |
949 |
+ |
shuffle(tasks); |
950 |
|
try { |
951 |
< |
invokeAll(f, g, h); |
951 |
> |
invokeAll(tasks[0], tasks[1], tasks[2]); |
952 |
|
shouldThrow(); |
953 |
|
} catch (FJException success) { |
954 |
|
checkCompletedAbnormally(g, success); |
955 |
|
} |
956 |
|
}}; |
957 |
< |
testInvokeOnPool(mainPool(), a); |
957 |
> |
testInvokeOnPool(pool, a); |
958 |
|
} |
959 |
|
|
960 |
|
/** |
961 |
|
* invokeAll(collection) throws exception if any task does |
962 |
|
*/ |
963 |
|
public void testAbnormalInvokeAllCollection() { |
964 |
+ |
testAbnormalInvokeAllCollection(mainPool()); |
965 |
+ |
} |
966 |
+ |
public void testAbnormalInvokeAllCollection_Singleton() { |
967 |
+ |
testAbnormalInvokeAllCollection(singletonPool()); |
968 |
+ |
} |
969 |
+ |
public void testAbnormalInvokeAllCollection(ForkJoinPool pool) { |
970 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
971 |
|
protected void realCompute() { |
972 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
973 |
|
AsyncFib g = new AsyncFib(9); |
974 |
|
AsyncFib h = new AsyncFib(7); |
975 |
< |
HashSet set = new HashSet(); |
976 |
< |
set.add(f); |
809 |
< |
set.add(g); |
810 |
< |
set.add(h); |
975 |
> |
ForkJoinTask[] tasks = { f, g, h }; |
976 |
> |
shuffle(tasks); |
977 |
|
try { |
978 |
< |
invokeAll(set); |
978 |
> |
invokeAll(Arrays.asList(tasks)); |
979 |
|
shouldThrow(); |
980 |
|
} catch (FJException success) { |
981 |
|
checkCompletedAbnormally(f, success); |
982 |
|
} |
983 |
|
}}; |
984 |
< |
testInvokeOnPool(mainPool(), a); |
984 |
> |
testInvokeOnPool(pool, a); |
985 |
|
} |
986 |
|
|
987 |
|
/** |
998 |
|
assertTrue(f.tryUnfork()); |
999 |
|
helpQuiesce(); |
1000 |
|
checkNotDone(f); |
1001 |
< |
checkCompletedNormally(g); |
1001 |
> |
g.checkCompletedNormally(); |
1002 |
|
}}; |
1003 |
|
testInvokeOnPool(singletonPool(), a); |
1004 |
|
} |
1019 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1020 |
|
helpQuiesce(); |
1021 |
|
assertEquals(0, getSurplusQueuedTaskCount()); |
1022 |
< |
checkCompletedNormally(f); |
1023 |
< |
checkCompletedNormally(g); |
1024 |
< |
checkCompletedNormally(h); |
1022 |
> |
f.checkCompletedNormally(); |
1023 |
> |
g.checkCompletedNormally(); |
1024 |
> |
h.checkCompletedNormally(); |
1025 |
|
}}; |
1026 |
|
testInvokeOnPool(singletonPool(), a); |
1027 |
|
} |
1038 |
|
assertSame(f, f.fork()); |
1039 |
|
assertSame(f, peekNextLocalTask()); |
1040 |
|
assertNull(f.join()); |
1041 |
< |
checkCompletedNormally(f); |
1041 |
> |
f.checkCompletedNormally(); |
1042 |
|
helpQuiesce(); |
1043 |
< |
checkCompletedNormally(g); |
1043 |
> |
g.checkCompletedNormally(); |
1044 |
|
}}; |
1045 |
|
testInvokeOnPool(singletonPool(), a); |
1046 |
|
} |
1059 |
|
assertSame(f, pollNextLocalTask()); |
1060 |
|
helpQuiesce(); |
1061 |
|
checkNotDone(f); |
1062 |
< |
assertEquals(34, g.number); |
897 |
< |
checkCompletedNormally(g); |
1062 |
> |
g.checkCompletedNormally(); |
1063 |
|
}}; |
1064 |
|
testInvokeOnPool(singletonPool(), a); |
1065 |
|
} |
1077 |
|
assertSame(f, pollTask()); |
1078 |
|
helpQuiesce(); |
1079 |
|
checkNotDone(f); |
1080 |
< |
checkCompletedNormally(g); |
1080 |
> |
g.checkCompletedNormally(); |
1081 |
|
}}; |
1082 |
|
testInvokeOnPool(singletonPool(), a); |
1083 |
|
} |
1095 |
|
assertSame(g, peekNextLocalTask()); |
1096 |
|
assertNull(f.join()); |
1097 |
|
helpQuiesce(); |
1098 |
< |
checkCompletedNormally(f); |
1099 |
< |
assertEquals(34, g.number); |
935 |
< |
checkCompletedNormally(g); |
1098 |
> |
f.checkCompletedNormally(); |
1099 |
> |
g.checkCompletedNormally(); |
1100 |
|
}}; |
1101 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1102 |
|
} |
1114 |
|
assertSame(f, f.fork()); |
1115 |
|
assertSame(g, pollNextLocalTask()); |
1116 |
|
helpQuiesce(); |
1117 |
< |
assertEquals(21, f.number); |
954 |
< |
checkCompletedNormally(f); |
1117 |
> |
f.checkCompletedNormally(); |
1118 |
|
checkNotDone(g); |
1119 |
|
}}; |
1120 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1133 |
|
assertSame(f, f.fork()); |
1134 |
|
assertSame(g, pollTask()); |
1135 |
|
helpQuiesce(); |
1136 |
< |
assertEquals(21, f.number); |
974 |
< |
checkCompletedNormally(f); |
1136 |
> |
f.checkCompletedNormally(); |
1137 |
|
checkNotDone(g); |
1138 |
|
}}; |
1139 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1140 |
|
} |
1141 |
|
|
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 NullPointerException |
1306 |
– |
*/ |
1307 |
– |
public void testInvokeAllNullTaskSingleton() { |
1308 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1309 |
– |
protected void realCompute() { |
1310 |
– |
AsyncFib f = new AsyncFib(8); |
1311 |
– |
AsyncFib g = new AsyncFib(9); |
1312 |
– |
AsyncFib nul = null; |
1313 |
– |
Runnable[] throwingActions = { |
1314 |
– |
() -> invokeAll(nul), |
1315 |
– |
() -> invokeAll(nul, nul), |
1316 |
– |
() -> invokeAll(f, g, nul), |
1317 |
– |
() -> invokeAll(f, nul, g), |
1318 |
– |
() -> invokeAll(nul, f, g), |
1319 |
– |
}; |
1320 |
– |
assertThrows(NullPointerException.class, throwingActions); |
1321 |
– |
}}; |
1322 |
– |
testInvokeOnPool(singletonPool(), a); |
1323 |
– |
} |
1324 |
– |
|
1325 |
– |
/** |
1326 |
– |
* invokeAll(t1, t2) throw exception if any task does |
1327 |
– |
*/ |
1328 |
– |
public void testAbnormalInvokeAll2Singleton() { |
1329 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1330 |
– |
protected void realCompute() { |
1331 |
– |
AsyncFib f = new AsyncFib(8); |
1332 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1333 |
– |
try { |
1334 |
– |
invokeAll(f, g); |
1335 |
– |
shouldThrow(); |
1336 |
– |
} catch (FJException success) { |
1337 |
– |
checkCompletedAbnormally(g, success); |
1338 |
– |
} |
1339 |
– |
}}; |
1340 |
– |
testInvokeOnPool(singletonPool(), a); |
1341 |
– |
} |
1342 |
– |
|
1343 |
– |
/** |
1344 |
– |
* invokeAll(tasks) with 1 argument throws exception if task does |
1345 |
– |
*/ |
1346 |
– |
public void testAbnormalInvokeAll1Singleton() { |
1347 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1348 |
– |
protected void realCompute() { |
1349 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1350 |
– |
try { |
1351 |
– |
invokeAll(g); |
1352 |
– |
shouldThrow(); |
1353 |
– |
} catch (FJException success) { |
1354 |
– |
checkCompletedAbnormally(g, success); |
1355 |
– |
} |
1356 |
– |
}}; |
1357 |
– |
testInvokeOnPool(singletonPool(), a); |
1358 |
– |
} |
1359 |
– |
|
1360 |
– |
/** |
1361 |
– |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1362 |
– |
*/ |
1363 |
– |
public void testAbnormalInvokeAll3Singleton() { |
1364 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1365 |
– |
protected void realCompute() { |
1366 |
– |
AsyncFib f = new AsyncFib(8); |
1367 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
1368 |
– |
AsyncFib h = new AsyncFib(7); |
1369 |
– |
try { |
1370 |
– |
invokeAll(f, g, h); |
1371 |
– |
shouldThrow(); |
1372 |
– |
} catch (FJException success) { |
1373 |
– |
checkCompletedAbnormally(g, success); |
1374 |
– |
} |
1375 |
– |
}}; |
1376 |
– |
testInvokeOnPool(singletonPool(), a); |
1377 |
– |
} |
1378 |
– |
|
1379 |
– |
/** |
1380 |
– |
* invokeAll(collection) throws exception if any task does |
1381 |
– |
*/ |
1382 |
– |
public void testAbnormalInvokeAllCollectionSingleton() { |
1383 |
– |
RecursiveAction a = new CheckedRecursiveAction() { |
1384 |
– |
protected void realCompute() { |
1385 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
1386 |
– |
AsyncFib g = new AsyncFib(9); |
1387 |
– |
AsyncFib h = new AsyncFib(7); |
1388 |
– |
HashSet set = new HashSet(); |
1389 |
– |
set.add(f); |
1390 |
– |
set.add(g); |
1391 |
– |
set.add(h); |
1392 |
– |
try { |
1393 |
– |
invokeAll(set); |
1394 |
– |
shouldThrow(); |
1395 |
– |
} catch (FJException success) { |
1396 |
– |
checkCompletedAbnormally(f, success); |
1397 |
– |
} |
1398 |
– |
}}; |
1399 |
– |
testInvokeOnPool(singletonPool(), a); |
1400 |
– |
} |
1401 |
– |
|
1142 |
|
/** |
1143 |
|
* ForkJoinTask.quietlyComplete returns when task completes |
1144 |
|
* normally without setting a value. The most recent value |
1160 |
|
testInvokeOnPool(mainPool(), a); |
1161 |
|
} |
1162 |
|
|
1163 |
+ |
// jdk9 |
1164 |
+ |
|
1165 |
+ |
/** |
1166 |
+ |
* pollSubmission returns unexecuted submitted task, if present |
1167 |
+ |
*/ |
1168 |
+ |
public void testPollSubmission() { |
1169 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1170 |
+ |
final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done)); |
1171 |
+ |
final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done)); |
1172 |
+ |
final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done)); |
1173 |
+ |
final ForkJoinPool p = singletonPool(); |
1174 |
+ |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1175 |
+ |
Thread external = new Thread(new CheckedRunnable() { |
1176 |
+ |
public void realRun() { |
1177 |
+ |
p.execute(a); |
1178 |
+ |
p.execute(b); |
1179 |
+ |
p.execute(c); |
1180 |
+ |
}}); |
1181 |
+ |
RecursiveAction s = new CheckedRecursiveAction() { |
1182 |
+ |
protected void realCompute() { |
1183 |
+ |
external.start(); |
1184 |
+ |
try { |
1185 |
+ |
external.join(); |
1186 |
+ |
} catch (Exception ex) { |
1187 |
+ |
threadUnexpectedException(ex); |
1188 |
+ |
} |
1189 |
+ |
assertTrue(p.hasQueuedSubmissions()); |
1190 |
+ |
assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread); |
1191 |
+ |
ForkJoinTask r = ForkJoinTask.pollSubmission(); |
1192 |
+ |
assertTrue(r == a || r == b || r == c); |
1193 |
+ |
assertFalse(r.isDone()); |
1194 |
+ |
}}; |
1195 |
+ |
p.invoke(s); |
1196 |
+ |
} |
1197 |
+ |
} |
1198 |
+ |
|
1199 |
|
} |