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