10 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
11 |
|
import java.util.concurrent.RecursiveAction; |
12 |
|
import java.util.concurrent.TimeUnit; |
13 |
+ |
import java.util.concurrent.TimeoutException; |
14 |
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
15 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
16 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
17 |
|
import java.util.HashSet; |
18 |
|
import junit.framework.*; |
19 |
|
|
27 |
|
return new TestSuite(ForkJoinTaskTest.class); |
28 |
|
} |
29 |
|
|
30 |
+ |
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1 |
31 |
+ |
static final int mainPoolSize = |
32 |
+ |
Math.max(2, Runtime.getRuntime().availableProcessors()); |
33 |
+ |
|
34 |
|
private static ForkJoinPool mainPool() { |
35 |
< |
return new ForkJoinPool(); |
35 |
> |
return new ForkJoinPool(mainPoolSize); |
36 |
|
} |
37 |
|
|
38 |
|
private static ForkJoinPool singletonPool() { |
47 |
|
|
48 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
49 |
|
try { |
50 |
< |
assertTrue(pool.invoke(a) == null); |
50 |
> |
assertFalse(a.isDone()); |
51 |
> |
assertFalse(a.isCompletedNormally()); |
52 |
> |
assertFalse(a.isCompletedAbnormally()); |
53 |
> |
assertFalse(a.isCancelled()); |
54 |
> |
assertNull(a.getException()); |
55 |
> |
assertNull(a.getRawResult()); |
56 |
> |
|
57 |
> |
assertNull(pool.invoke(a)); |
58 |
> |
|
59 |
> |
assertTrue(a.isDone()); |
60 |
> |
assertTrue(a.isCompletedNormally()); |
61 |
> |
assertFalse(a.isCompletedAbnormally()); |
62 |
> |
assertFalse(a.isCancelled()); |
63 |
> |
assertNull(a.getException()); |
64 |
> |
assertNull(a.getRawResult()); |
65 |
|
} finally { |
66 |
|
joinPool(pool); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
+ |
void checkNotDone(ForkJoinTask a) { |
71 |
+ |
assertFalse(a.isDone()); |
72 |
+ |
assertFalse(a.isCompletedNormally()); |
73 |
+ |
assertFalse(a.isCompletedAbnormally()); |
74 |
+ |
assertFalse(a.isCancelled()); |
75 |
+ |
assertNull(a.getException()); |
76 |
+ |
assertNull(a.getRawResult()); |
77 |
+ |
|
78 |
+ |
try { |
79 |
+ |
a.get(0L, SECONDS); |
80 |
+ |
shouldThrow(); |
81 |
+ |
} catch (TimeoutException success) { |
82 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
<T> void checkCompletedNormally(ForkJoinTask<T> a) { |
86 |
+ |
checkCompletedNormally(a, null); |
87 |
+ |
} |
88 |
+ |
|
89 |
+ |
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) { |
90 |
+ |
assertTrue(a.isDone()); |
91 |
+ |
assertFalse(a.isCancelled()); |
92 |
+ |
assertTrue(a.isCompletedNormally()); |
93 |
+ |
assertFalse(a.isCompletedAbnormally()); |
94 |
+ |
assertNull(a.getException()); |
95 |
+ |
assertSame(expected, a.getRawResult()); |
96 |
+ |
|
97 |
+ |
{ |
98 |
+ |
Thread.currentThread().interrupt(); |
99 |
+ |
long t0 = System.nanoTime(); |
100 |
+ |
assertSame(expected, a.join()); |
101 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
102 |
+ |
Thread.interrupted(); |
103 |
+ |
} |
104 |
+ |
|
105 |
+ |
{ |
106 |
+ |
Thread.currentThread().interrupt(); |
107 |
+ |
long t0 = System.nanoTime(); |
108 |
+ |
a.quietlyJoin(); // should be no-op |
109 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
110 |
+ |
Thread.interrupted(); |
111 |
+ |
} |
112 |
+ |
|
113 |
+ |
assertFalse(a.cancel(false)); |
114 |
+ |
assertFalse(a.cancel(true)); |
115 |
+ |
try { |
116 |
+ |
assertSame(expected, a.get()); |
117 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
+ |
try { |
119 |
+ |
assertSame(expected, a.get(5L, SECONDS)); |
120 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
+ |
} |
122 |
+ |
|
123 |
+ |
void checkCancelled(ForkJoinTask a) { |
124 |
+ |
assertTrue(a.isDone()); |
125 |
+ |
assertTrue(a.isCancelled()); |
126 |
+ |
assertFalse(a.isCompletedNormally()); |
127 |
+ |
assertTrue(a.isCompletedAbnormally()); |
128 |
+ |
assertTrue(a.getException() instanceof CancellationException); |
129 |
+ |
assertNull(a.getRawResult()); |
130 |
+ |
assertTrue(a.cancel(false)); |
131 |
+ |
assertTrue(a.cancel(true)); |
132 |
+ |
|
133 |
+ |
try { |
134 |
+ |
Thread.currentThread().interrupt(); |
135 |
+ |
a.join(); |
136 |
+ |
shouldThrow(); |
137 |
+ |
} catch (CancellationException success) { |
138 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
139 |
+ |
Thread.interrupted(); |
140 |
+ |
|
141 |
+ |
{ |
142 |
+ |
long t0 = System.nanoTime(); |
143 |
+ |
a.quietlyJoin(); // should be no-op |
144 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
try { |
148 |
+ |
a.get(); |
149 |
+ |
shouldThrow(); |
150 |
+ |
} catch (CancellationException success) { |
151 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
152 |
+ |
|
153 |
+ |
try { |
154 |
+ |
a.get(5L, SECONDS); |
155 |
+ |
shouldThrow(); |
156 |
+ |
} catch (CancellationException success) { |
157 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
161 |
+ |
assertTrue(a.isDone()); |
162 |
+ |
assertFalse(a.isCancelled()); |
163 |
+ |
assertFalse(a.isCompletedNormally()); |
164 |
+ |
assertTrue(a.isCompletedAbnormally()); |
165 |
+ |
assertSame(t, a.getException()); |
166 |
+ |
assertNull(a.getRawResult()); |
167 |
+ |
assertFalse(a.cancel(false)); |
168 |
+ |
assertFalse(a.cancel(true)); |
169 |
+ |
|
170 |
+ |
try { |
171 |
+ |
Thread.currentThread().interrupt(); |
172 |
+ |
a.join(); |
173 |
+ |
shouldThrow(); |
174 |
+ |
} catch (Throwable expected) { |
175 |
+ |
assertSame(t, expected); |
176 |
+ |
} |
177 |
+ |
Thread.interrupted(); |
178 |
+ |
|
179 |
+ |
{ |
180 |
+ |
long t0 = System.nanoTime(); |
181 |
+ |
a.quietlyJoin(); // should be no-op |
182 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
try { |
186 |
+ |
a.get(); |
187 |
+ |
shouldThrow(); |
188 |
+ |
} catch (ExecutionException success) { |
189 |
+ |
assertSame(t, success.getCause()); |
190 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
+ |
|
192 |
+ |
try { |
193 |
+ |
a.get(5L, SECONDS); |
194 |
+ |
shouldThrow(); |
195 |
+ |
} catch (ExecutionException success) { |
196 |
+ |
assertSame(t, success.getCause()); |
197 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
198 |
+ |
} |
199 |
+ |
|
200 |
|
/* |
201 |
|
* Testing coverage notes: |
202 |
|
* |
209 |
|
FJException() { super(); } |
210 |
|
} |
211 |
|
|
212 |
< |
static abstract class BinaryAsyncAction extends ForkJoinTask<Void> { |
212 |
> |
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
213 |
|
private volatile int controlState; |
214 |
|
|
215 |
|
static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater = |
381 |
|
/** |
382 |
|
* invoke returns when task completes normally. |
383 |
|
* isCompletedAbnormally and isCancelled return false for normally |
384 |
< |
* completed tasks. getRawResult of a RecursiveAction returns null; |
384 |
> |
* completed tasks; getRawResult returns null. |
385 |
|
*/ |
386 |
|
public void testInvoke() { |
387 |
< |
RecursiveAction a = new RecursiveAction() { |
388 |
< |
public void compute() { |
387 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
388 |
> |
public void realCompute() { |
389 |
|
AsyncFib f = new AsyncFib(8); |
390 |
< |
f.invoke(); |
391 |
< |
threadAssertTrue(f.number == 21); |
392 |
< |
threadAssertTrue(f.isDone()); |
242 |
< |
threadAssertFalse(f.isCancelled()); |
243 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
244 |
< |
threadAssertTrue(f.getRawResult() == null); |
390 |
> |
assertNull(f.invoke()); |
391 |
> |
assertEquals(21, f.number); |
392 |
> |
checkCompletedNormally(f); |
393 |
|
}}; |
394 |
|
testInvokeOnPool(mainPool(), a); |
395 |
|
} |
400 |
|
* completed tasks |
401 |
|
*/ |
402 |
|
public void testQuietlyInvoke() { |
403 |
< |
RecursiveAction a = new RecursiveAction() { |
404 |
< |
public void compute() { |
403 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
404 |
> |
public void realCompute() { |
405 |
|
AsyncFib f = new AsyncFib(8); |
406 |
|
f.quietlyInvoke(); |
407 |
< |
threadAssertTrue(f.number == 21); |
408 |
< |
threadAssertTrue(f.isDone()); |
261 |
< |
threadAssertFalse(f.isCancelled()); |
262 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
263 |
< |
threadAssertTrue(f.getRawResult() == null); |
407 |
> |
assertEquals(21, f.number); |
408 |
> |
checkCompletedNormally(f); |
409 |
|
}}; |
410 |
|
testInvokeOnPool(mainPool(), a); |
411 |
|
} |
414 |
|
* join of a forked task returns when task completes |
415 |
|
*/ |
416 |
|
public void testForkJoin() { |
417 |
< |
RecursiveAction a = new RecursiveAction() { |
418 |
< |
public void compute() { |
417 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
418 |
> |
public void realCompute() { |
419 |
|
AsyncFib f = new AsyncFib(8); |
420 |
< |
f.fork(); |
421 |
< |
f.join(); |
422 |
< |
threadAssertTrue(f.number == 21); |
423 |
< |
threadAssertTrue(f.isDone()); |
279 |
< |
threadAssertTrue(f.getRawResult() == null); |
420 |
> |
assertSame(f, f.fork()); |
421 |
> |
assertNull(f.join()); |
422 |
> |
assertEquals(21, f.number); |
423 |
> |
checkCompletedNormally(f); |
424 |
|
}}; |
425 |
|
testInvokeOnPool(mainPool(), a); |
426 |
|
} |
429 |
|
* get of a forked task returns when task completes |
430 |
|
*/ |
431 |
|
public void testForkGet() { |
432 |
< |
RecursiveAction a = new RecursiveAction() { |
433 |
< |
public void compute() { |
434 |
< |
try { |
435 |
< |
AsyncFib f = new AsyncFib(8); |
436 |
< |
f.fork(); |
437 |
< |
f.get(); |
438 |
< |
threadAssertTrue(f.number == 21); |
295 |
< |
threadAssertTrue(f.isDone()); |
296 |
< |
} catch (Exception ex) { |
297 |
< |
unexpectedException(ex); |
298 |
< |
} |
432 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
433 |
> |
public void realCompute() throws Exception { |
434 |
> |
AsyncFib f = new AsyncFib(8); |
435 |
> |
assertSame(f, f.fork()); |
436 |
> |
assertNull(f.get()); |
437 |
> |
assertEquals(21, f.number); |
438 |
> |
checkCompletedNormally(f); |
439 |
|
}}; |
440 |
|
testInvokeOnPool(mainPool(), a); |
441 |
|
} |
444 |
|
* timed get of a forked task returns when task completes |
445 |
|
*/ |
446 |
|
public void testForkTimedGet() { |
447 |
< |
RecursiveAction a = new RecursiveAction() { |
448 |
< |
public void compute() { |
449 |
< |
try { |
450 |
< |
AsyncFib f = new AsyncFib(8); |
451 |
< |
f.fork(); |
452 |
< |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
453 |
< |
threadAssertTrue(f.number == 21); |
314 |
< |
threadAssertTrue(f.isDone()); |
315 |
< |
} catch (Exception ex) { |
316 |
< |
unexpectedException(ex); |
317 |
< |
} |
447 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
448 |
> |
public void realCompute() throws Exception { |
449 |
> |
AsyncFib f = new AsyncFib(8); |
450 |
> |
assertSame(f, f.fork()); |
451 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
452 |
> |
assertEquals(21, f.number); |
453 |
> |
checkCompletedNormally(f); |
454 |
|
}}; |
455 |
|
testInvokeOnPool(mainPool(), a); |
456 |
|
} |
459 |
|
* timed get with null time unit throws NPE |
460 |
|
*/ |
461 |
|
public void testForkTimedGetNPE() { |
462 |
< |
RecursiveAction a = new RecursiveAction() { |
463 |
< |
public void compute() { |
462 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
463 |
> |
public void realCompute() throws Exception { |
464 |
> |
AsyncFib f = new AsyncFib(8); |
465 |
> |
assertSame(f, f.fork()); |
466 |
|
try { |
329 |
– |
AsyncFib f = new AsyncFib(8); |
330 |
– |
f.fork(); |
467 |
|
f.get(5L, null); |
468 |
|
shouldThrow(); |
469 |
< |
} catch (NullPointerException success) { |
334 |
< |
} catch (Exception ex) { |
335 |
< |
unexpectedException(ex); |
336 |
< |
} |
469 |
> |
} catch (NullPointerException success) {} |
470 |
|
}}; |
471 |
|
testInvokeOnPool(mainPool(), a); |
472 |
|
} |
475 |
|
* quietlyJoin of a forked task returns when task completes |
476 |
|
*/ |
477 |
|
public void testForkQuietlyJoin() { |
478 |
< |
RecursiveAction a = new RecursiveAction() { |
479 |
< |
public void compute() { |
478 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
479 |
> |
public void realCompute() { |
480 |
|
AsyncFib f = new AsyncFib(8); |
481 |
< |
f.fork(); |
481 |
> |
assertSame(f, f.fork()); |
482 |
|
f.quietlyJoin(); |
483 |
< |
threadAssertTrue(f.number == 21); |
484 |
< |
threadAssertTrue(f.isDone()); |
483 |
> |
assertEquals(21, f.number); |
484 |
> |
checkCompletedNormally(f); |
485 |
|
}}; |
486 |
|
testInvokeOnPool(mainPool(), a); |
487 |
|
} |
492 |
|
* getQueuedTaskCount returns 0 when quiescent |
493 |
|
*/ |
494 |
|
public void testForkHelpQuiesce() { |
495 |
< |
RecursiveAction a = new RecursiveAction() { |
496 |
< |
public void compute() { |
495 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
496 |
> |
public void realCompute() { |
497 |
|
AsyncFib f = new AsyncFib(8); |
498 |
< |
f.fork(); |
498 |
> |
assertSame(f, f.fork()); |
499 |
|
f.helpQuiesce(); |
500 |
< |
threadAssertTrue(f.number == 21); |
501 |
< |
threadAssertTrue(f.isDone()); |
502 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
500 |
> |
assertEquals(21, f.number); |
501 |
> |
assertEquals(0, getQueuedTaskCount()); |
502 |
> |
checkCompletedNormally(f); |
503 |
|
}}; |
504 |
|
testInvokeOnPool(mainPool(), a); |
505 |
|
} |
509 |
|
* invoke task throws exception when task completes abnormally |
510 |
|
*/ |
511 |
|
public void testAbnormalInvoke() { |
512 |
< |
RecursiveAction a = new RecursiveAction() { |
513 |
< |
public void compute() { |
512 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
513 |
> |
public void realCompute() { |
514 |
> |
FailingAsyncFib f = new FailingAsyncFib(8); |
515 |
|
try { |
382 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
516 |
|
f.invoke(); |
517 |
|
shouldThrow(); |
518 |
|
} catch (FJException success) { |
519 |
+ |
checkCompletedAbnormally(f, success); |
520 |
|
} |
521 |
|
}}; |
522 |
|
testInvokeOnPool(mainPool(), a); |
526 |
|
* quietlyInvoke task returns when task completes abnormally |
527 |
|
*/ |
528 |
|
public void testAbnormalQuietlyInvoke() { |
529 |
< |
RecursiveAction a = new RecursiveAction() { |
530 |
< |
public void compute() { |
529 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
530 |
> |
public void realCompute() { |
531 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
532 |
|
f.quietlyInvoke(); |
533 |
< |
threadAssertTrue(f.isDone()); |
533 |
> |
assertTrue(f.getException() instanceof FJException); |
534 |
> |
checkCompletedAbnormally(f, f.getException()); |
535 |
|
}}; |
536 |
|
testInvokeOnPool(mainPool(), a); |
537 |
|
} |
540 |
|
* join of a forked task throws exception when task completes abnormally |
541 |
|
*/ |
542 |
|
public void testAbnormalForkJoin() { |
543 |
< |
RecursiveAction a = new RecursiveAction() { |
544 |
< |
public void compute() { |
543 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
544 |
> |
public void realCompute() { |
545 |
> |
FailingAsyncFib f = new FailingAsyncFib(8); |
546 |
> |
assertSame(f, f.fork()); |
547 |
|
try { |
411 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
412 |
– |
f.fork(); |
548 |
|
f.join(); |
549 |
|
shouldThrow(); |
550 |
|
} catch (FJException success) { |
551 |
+ |
checkCompletedAbnormally(f, success); |
552 |
|
} |
553 |
|
}}; |
554 |
|
testInvokeOnPool(mainPool(), a); |
558 |
|
* get of a forked task throws exception when task completes abnormally |
559 |
|
*/ |
560 |
|
public void testAbnormalForkGet() { |
561 |
< |
RecursiveAction a = new RecursiveAction() { |
562 |
< |
public void compute() { |
561 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
562 |
> |
public void realCompute() throws Exception { |
563 |
> |
FailingAsyncFib f = new FailingAsyncFib(8); |
564 |
> |
assertSame(f, f.fork()); |
565 |
|
try { |
428 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
429 |
– |
f.fork(); |
566 |
|
f.get(); |
567 |
|
shouldThrow(); |
568 |
|
} catch (ExecutionException success) { |
569 |
< |
} catch (Exception ex) { |
570 |
< |
unexpectedException(ex); |
569 |
> |
Throwable cause = success.getCause(); |
570 |
> |
assertTrue(cause instanceof FJException); |
571 |
> |
checkCompletedAbnormally(f, cause); |
572 |
|
} |
573 |
|
}}; |
574 |
|
testInvokeOnPool(mainPool(), a); |
578 |
|
* timed get of a forked task throws exception when task completes abnormally |
579 |
|
*/ |
580 |
|
public void testAbnormalForkTimedGet() { |
581 |
< |
RecursiveAction a = new RecursiveAction() { |
582 |
< |
public void compute() { |
581 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
582 |
> |
public void realCompute() throws Exception { |
583 |
> |
FailingAsyncFib f = new FailingAsyncFib(8); |
584 |
> |
assertSame(f, f.fork()); |
585 |
|
try { |
586 |
< |
FailingAsyncFib f = new FailingAsyncFib(8); |
448 |
< |
f.fork(); |
449 |
< |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
586 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
587 |
|
shouldThrow(); |
588 |
|
} catch (ExecutionException success) { |
589 |
< |
} catch (Exception ex) { |
590 |
< |
unexpectedException(ex); |
589 |
> |
Throwable cause = success.getCause(); |
590 |
> |
assertTrue(cause instanceof FJException); |
591 |
> |
checkCompletedAbnormally(f, cause); |
592 |
|
} |
593 |
|
}}; |
594 |
|
testInvokeOnPool(mainPool(), a); |
598 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
599 |
|
*/ |
600 |
|
public void testAbnormalForkQuietlyJoin() { |
601 |
< |
RecursiveAction a = new RecursiveAction() { |
602 |
< |
public void compute() { |
601 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
602 |
> |
public void realCompute() { |
603 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
604 |
< |
f.fork(); |
604 |
> |
assertSame(f, f.fork()); |
605 |
|
f.quietlyJoin(); |
606 |
< |
threadAssertTrue(f.isDone()); |
607 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
470 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
606 |
> |
assertTrue(f.getException() instanceof FJException); |
607 |
> |
checkCompletedAbnormally(f, f.getException()); |
608 |
|
}}; |
609 |
|
testInvokeOnPool(mainPool(), a); |
610 |
|
} |
613 |
|
* invoke task throws exception when task cancelled |
614 |
|
*/ |
615 |
|
public void testCancelledInvoke() { |
616 |
< |
RecursiveAction a = new RecursiveAction() { |
617 |
< |
public void compute() { |
616 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
617 |
> |
public void realCompute() { |
618 |
> |
AsyncFib f = new AsyncFib(8); |
619 |
> |
assertTrue(f.cancel(true)); |
620 |
|
try { |
482 |
– |
AsyncFib f = new AsyncFib(8); |
483 |
– |
f.cancel(true); |
621 |
|
f.invoke(); |
622 |
|
shouldThrow(); |
623 |
|
} catch (CancellationException success) { |
624 |
+ |
checkCancelled(f); |
625 |
|
} |
626 |
|
}}; |
627 |
|
testInvokeOnPool(mainPool(), a); |
631 |
|
* join of a forked task throws exception when task cancelled |
632 |
|
*/ |
633 |
|
public void testCancelledForkJoin() { |
634 |
< |
RecursiveAction a = new RecursiveAction() { |
635 |
< |
public void compute() { |
634 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
635 |
> |
public void realCompute() { |
636 |
> |
AsyncFib f = new AsyncFib(8); |
637 |
> |
assertTrue(f.cancel(true)); |
638 |
> |
assertSame(f, f.fork()); |
639 |
|
try { |
499 |
– |
AsyncFib f = new AsyncFib(8); |
500 |
– |
f.cancel(true); |
501 |
– |
f.fork(); |
640 |
|
f.join(); |
641 |
|
shouldThrow(); |
642 |
|
} catch (CancellationException success) { |
643 |
+ |
checkCancelled(f); |
644 |
|
} |
645 |
|
}}; |
646 |
|
testInvokeOnPool(mainPool(), a); |
650 |
|
* get of a forked task throws exception when task cancelled |
651 |
|
*/ |
652 |
|
public void testCancelledForkGet() { |
653 |
< |
RecursiveAction a = new RecursiveAction() { |
654 |
< |
public void compute() { |
653 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
654 |
> |
public void realCompute() throws Exception { |
655 |
> |
AsyncFib f = new AsyncFib(8); |
656 |
> |
assertTrue(f.cancel(true)); |
657 |
> |
assertSame(f, f.fork()); |
658 |
|
try { |
517 |
– |
AsyncFib f = new AsyncFib(8); |
518 |
– |
f.cancel(true); |
519 |
– |
f.fork(); |
659 |
|
f.get(); |
660 |
|
shouldThrow(); |
661 |
|
} catch (CancellationException success) { |
662 |
< |
} catch (Exception ex) { |
524 |
< |
unexpectedException(ex); |
662 |
> |
checkCancelled(f); |
663 |
|
} |
664 |
|
}}; |
665 |
|
testInvokeOnPool(mainPool(), a); |
668 |
|
/** |
669 |
|
* timed get of a forked task throws exception when task cancelled |
670 |
|
*/ |
671 |
< |
public void testCancelledForkTimedGet() { |
672 |
< |
RecursiveAction a = new RecursiveAction() { |
673 |
< |
public void compute() { |
674 |
< |
try { |
675 |
< |
AsyncFib f = new AsyncFib(8); |
676 |
< |
f.cancel(true); |
677 |
< |
f.fork(); |
678 |
< |
f.get(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
671 |
> |
public void testCancelledForkTimedGet() throws Exception { |
672 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
673 |
> |
public void realCompute() throws Exception { |
674 |
> |
AsyncFib f = new AsyncFib(8); |
675 |
> |
assertTrue(f.cancel(true)); |
676 |
> |
assertSame(f, f.fork()); |
677 |
> |
try { |
678 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
679 |
|
shouldThrow(); |
680 |
|
} catch (CancellationException success) { |
681 |
< |
} catch (Exception ex) { |
544 |
< |
unexpectedException(ex); |
681 |
> |
checkCancelled(f); |
682 |
|
} |
683 |
|
}}; |
684 |
|
testInvokeOnPool(mainPool(), a); |
688 |
|
* quietlyJoin of a forked task returns when task cancelled |
689 |
|
*/ |
690 |
|
public void testCancelledForkQuietlyJoin() { |
691 |
< |
RecursiveAction a = new RecursiveAction() { |
692 |
< |
public void compute() { |
691 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
692 |
> |
public void realCompute() { |
693 |
|
AsyncFib f = new AsyncFib(8); |
694 |
< |
f.cancel(true); |
695 |
< |
f.fork(); |
694 |
> |
assertTrue(f.cancel(true)); |
695 |
> |
assertSame(f, f.fork()); |
696 |
|
f.quietlyJoin(); |
697 |
< |
threadAssertTrue(f.isDone()); |
561 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
562 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
697 |
> |
checkCancelled(f); |
698 |
|
}}; |
699 |
|
testInvokeOnPool(mainPool(), a); |
700 |
|
} |
704 |
|
*/ |
705 |
|
public void testGetPool() { |
706 |
|
final ForkJoinPool mainPool = mainPool(); |
707 |
< |
RecursiveAction a = new RecursiveAction() { |
708 |
< |
public void compute() { |
709 |
< |
threadAssertTrue(getPool() == mainPool); |
707 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
708 |
> |
public void realCompute() { |
709 |
> |
assertSame(mainPool, getPool()); |
710 |
|
}}; |
711 |
|
testInvokeOnPool(mainPool, a); |
712 |
|
} |
715 |
|
* getPool of non-FJ task returns null |
716 |
|
*/ |
717 |
|
public void testGetPool2() { |
718 |
< |
RecursiveAction a = new RecursiveAction() { |
719 |
< |
public void compute() { |
720 |
< |
threadAssertTrue(getPool() == null); |
718 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
719 |
> |
public void realCompute() { |
720 |
> |
assertNull(getPool()); |
721 |
|
}}; |
722 |
< |
a.invoke(); |
722 |
> |
assertNull(a.invoke()); |
723 |
|
} |
724 |
|
|
725 |
|
/** |
726 |
|
* inForkJoinPool of executing task returns true |
727 |
|
*/ |
728 |
|
public void testInForkJoinPool() { |
729 |
< |
RecursiveAction a = new RecursiveAction() { |
730 |
< |
public void compute() { |
731 |
< |
threadAssertTrue(inForkJoinPool()); |
729 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
730 |
> |
public void realCompute() { |
731 |
> |
assertTrue(inForkJoinPool()); |
732 |
|
}}; |
733 |
|
testInvokeOnPool(mainPool(), a); |
734 |
|
} |
737 |
|
* inForkJoinPool of non-FJ task returns false |
738 |
|
*/ |
739 |
|
public void testInForkJoinPool2() { |
740 |
< |
RecursiveAction a = new RecursiveAction() { |
741 |
< |
public void compute() { |
742 |
< |
threadAssertTrue(!inForkJoinPool()); |
740 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
741 |
> |
public void realCompute() { |
742 |
> |
assertFalse(inForkJoinPool()); |
743 |
|
}}; |
744 |
< |
a.invoke(); |
744 |
> |
assertNull(a.invoke()); |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* setRawResult(null) succeeds |
749 |
|
*/ |
750 |
|
public void testSetRawResult() { |
751 |
< |
RecursiveAction a = new RecursiveAction() { |
752 |
< |
public void compute() { |
751 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
752 |
> |
public void realCompute() { |
753 |
|
setRawResult(null); |
754 |
+ |
assertNull(getRawResult()); |
755 |
|
}}; |
756 |
< |
a.invoke(); |
756 |
> |
assertNull(a.invoke()); |
757 |
|
} |
758 |
|
|
759 |
|
/** |
760 |
|
* invoke task throws exception after invoking completeExceptionally |
761 |
|
*/ |
762 |
|
public void testCompleteExceptionally() { |
763 |
< |
RecursiveAction a = new RecursiveAction() { |
764 |
< |
public void compute() { |
763 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
764 |
> |
public void realCompute() { |
765 |
> |
AsyncFib f = new AsyncFib(8); |
766 |
> |
f.completeExceptionally(new FJException()); |
767 |
|
try { |
630 |
– |
AsyncFib f = new AsyncFib(8); |
631 |
– |
f.completeExceptionally(new FJException()); |
768 |
|
f.invoke(); |
769 |
|
shouldThrow(); |
770 |
|
} catch (FJException success) { |
771 |
+ |
checkCompletedAbnormally(f, success); |
772 |
|
} |
773 |
|
}}; |
774 |
|
testInvokeOnPool(mainPool(), a); |
778 |
|
* invokeAll(t1, t2) invokes all task arguments |
779 |
|
*/ |
780 |
|
public void testInvokeAll2() { |
781 |
< |
RecursiveAction a = new RecursiveAction() { |
782 |
< |
public void compute() { |
781 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
782 |
> |
public void realCompute() { |
783 |
|
AsyncFib f = new AsyncFib(8); |
784 |
|
AsyncFib g = new AsyncFib(9); |
785 |
|
invokeAll(f, g); |
786 |
< |
threadAssertTrue(f.isDone()); |
787 |
< |
threadAssertTrue(f.number == 21); |
788 |
< |
threadAssertTrue(g.isDone()); |
789 |
< |
threadAssertTrue(g.number == 34); |
786 |
> |
assertEquals(21, f.number); |
787 |
> |
assertEquals(34, g.number); |
788 |
> |
checkCompletedNormally(f); |
789 |
> |
checkCompletedNormally(g); |
790 |
|
}}; |
791 |
|
testInvokeOnPool(mainPool(), a); |
792 |
|
} |
795 |
|
* invokeAll(tasks) with 1 argument invokes task |
796 |
|
*/ |
797 |
|
public void testInvokeAll1() { |
798 |
< |
RecursiveAction a = new RecursiveAction() { |
799 |
< |
public void compute() { |
798 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
799 |
> |
public void realCompute() { |
800 |
|
AsyncFib f = new AsyncFib(8); |
801 |
|
invokeAll(f); |
802 |
< |
threadAssertTrue(f.isDone()); |
803 |
< |
threadAssertTrue(f.number == 21); |
802 |
> |
checkCompletedNormally(f); |
803 |
> |
assertEquals(21, f.number); |
804 |
|
}}; |
805 |
|
testInvokeOnPool(mainPool(), a); |
806 |
|
} |
809 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
810 |
|
*/ |
811 |
|
public void testInvokeAll3() { |
812 |
< |
RecursiveAction a = new RecursiveAction() { |
813 |
< |
public void compute() { |
812 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
813 |
> |
public void realCompute() { |
814 |
|
AsyncFib f = new AsyncFib(8); |
815 |
|
AsyncFib g = new AsyncFib(9); |
816 |
|
AsyncFib h = new AsyncFib(7); |
817 |
|
invokeAll(f, g, h); |
818 |
< |
threadAssertTrue(f.isDone()); |
819 |
< |
threadAssertTrue(f.number == 21); |
820 |
< |
threadAssertTrue(g.isDone()); |
821 |
< |
threadAssertTrue(g.number == 34); |
822 |
< |
threadAssertTrue(h.isDone()); |
823 |
< |
threadAssertTrue(h.number == 13); |
818 |
> |
assertEquals(21, f.number); |
819 |
> |
assertEquals(34, g.number); |
820 |
> |
assertEquals(13, h.number); |
821 |
> |
checkCompletedNormally(f); |
822 |
> |
checkCompletedNormally(g); |
823 |
> |
checkCompletedNormally(h); |
824 |
|
}}; |
825 |
|
testInvokeOnPool(mainPool(), a); |
826 |
|
} |
829 |
|
* invokeAll(collection) invokes all tasks in the collection |
830 |
|
*/ |
831 |
|
public void testInvokeAllCollection() { |
832 |
< |
RecursiveAction a = new RecursiveAction() { |
833 |
< |
public void compute() { |
832 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
833 |
> |
public void realCompute() { |
834 |
|
AsyncFib f = new AsyncFib(8); |
835 |
|
AsyncFib g = new AsyncFib(9); |
836 |
|
AsyncFib h = new AsyncFib(7); |
839 |
|
set.add(g); |
840 |
|
set.add(h); |
841 |
|
invokeAll(set); |
842 |
< |
threadAssertTrue(f.isDone()); |
843 |
< |
threadAssertTrue(f.number == 21); |
844 |
< |
threadAssertTrue(g.isDone()); |
845 |
< |
threadAssertTrue(g.number == 34); |
846 |
< |
threadAssertTrue(h.isDone()); |
847 |
< |
threadAssertTrue(h.number == 13); |
842 |
> |
assertEquals(21, f.number); |
843 |
> |
assertEquals(34, g.number); |
844 |
> |
assertEquals(13, h.number); |
845 |
> |
checkCompletedNormally(f); |
846 |
> |
checkCompletedNormally(g); |
847 |
> |
checkCompletedNormally(h); |
848 |
|
}}; |
849 |
|
testInvokeOnPool(mainPool(), a); |
850 |
|
} |
854 |
|
* invokeAll(tasks) with any null task throws NPE |
855 |
|
*/ |
856 |
|
public void testInvokeAllNPE() { |
857 |
< |
RecursiveAction a = new RecursiveAction() { |
858 |
< |
public void compute() { |
857 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
858 |
> |
public void realCompute() { |
859 |
> |
AsyncFib f = new AsyncFib(8); |
860 |
> |
AsyncFib g = new AsyncFib(9); |
861 |
> |
AsyncFib h = null; |
862 |
|
try { |
723 |
– |
AsyncFib f = new AsyncFib(8); |
724 |
– |
AsyncFib g = new AsyncFib(9); |
725 |
– |
AsyncFib h = null; |
863 |
|
invokeAll(f, g, h); |
864 |
|
shouldThrow(); |
865 |
< |
} catch (NullPointerException success) { |
729 |
< |
} |
865 |
> |
} catch (NullPointerException success) {} |
866 |
|
}}; |
867 |
|
testInvokeOnPool(mainPool(), a); |
868 |
|
} |
871 |
|
* invokeAll(t1, t2) throw exception if any task does |
872 |
|
*/ |
873 |
|
public void testAbnormalInvokeAll2() { |
874 |
< |
RecursiveAction a = new RecursiveAction() { |
875 |
< |
public void compute() { |
874 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
875 |
> |
public void realCompute() { |
876 |
> |
AsyncFib f = new AsyncFib(8); |
877 |
> |
FailingAsyncFib g = new FailingAsyncFib(9); |
878 |
|
try { |
741 |
– |
AsyncFib f = new AsyncFib(8); |
742 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
879 |
|
invokeAll(f, g); |
880 |
|
shouldThrow(); |
881 |
|
} catch (FJException success) { |
882 |
+ |
checkCompletedAbnormally(g, success); |
883 |
|
} |
884 |
|
}}; |
885 |
|
testInvokeOnPool(mainPool(), a); |
889 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
890 |
|
*/ |
891 |
|
public void testAbnormalInvokeAll1() { |
892 |
< |
RecursiveAction a = new RecursiveAction() { |
893 |
< |
public void compute() { |
892 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
893 |
> |
public void realCompute() { |
894 |
> |
FailingAsyncFib g = new FailingAsyncFib(9); |
895 |
|
try { |
758 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
896 |
|
invokeAll(g); |
897 |
|
shouldThrow(); |
898 |
|
} catch (FJException success) { |
899 |
+ |
checkCompletedAbnormally(g, success); |
900 |
|
} |
901 |
|
}}; |
902 |
|
testInvokeOnPool(mainPool(), a); |
906 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
907 |
|
*/ |
908 |
|
public void testAbnormalInvokeAll3() { |
909 |
< |
RecursiveAction a = new RecursiveAction() { |
910 |
< |
public void compute() { |
909 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
910 |
> |
public void realCompute() { |
911 |
> |
AsyncFib f = new AsyncFib(8); |
912 |
> |
FailingAsyncFib g = new FailingAsyncFib(9); |
913 |
> |
AsyncFib h = new AsyncFib(7); |
914 |
|
try { |
774 |
– |
AsyncFib f = new AsyncFib(8); |
775 |
– |
FailingAsyncFib g = new FailingAsyncFib(9); |
776 |
– |
AsyncFib h = new AsyncFib(7); |
915 |
|
invokeAll(f, g, h); |
916 |
|
shouldThrow(); |
917 |
|
} catch (FJException success) { |
918 |
+ |
checkCompletedAbnormally(g, success); |
919 |
|
} |
920 |
|
}}; |
921 |
|
testInvokeOnPool(mainPool(), a); |
925 |
|
* invokeAll(collection) throws exception if any task does |
926 |
|
*/ |
927 |
|
public void testAbnormalInvokeAllCollection() { |
928 |
< |
RecursiveAction a = new RecursiveAction() { |
929 |
< |
public void compute() { |
928 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
929 |
> |
public void realCompute() { |
930 |
> |
FailingAsyncFib f = new FailingAsyncFib(8); |
931 |
> |
AsyncFib g = new AsyncFib(9); |
932 |
> |
AsyncFib h = new AsyncFib(7); |
933 |
> |
HashSet set = new HashSet(); |
934 |
> |
set.add(f); |
935 |
> |
set.add(g); |
936 |
> |
set.add(h); |
937 |
|
try { |
792 |
– |
FailingAsyncFib f = new FailingAsyncFib(8); |
793 |
– |
AsyncFib g = new AsyncFib(9); |
794 |
– |
AsyncFib h = new AsyncFib(7); |
795 |
– |
HashSet set = new HashSet(); |
796 |
– |
set.add(f); |
797 |
– |
set.add(g); |
798 |
– |
set.add(h); |
938 |
|
invokeAll(set); |
939 |
|
shouldThrow(); |
940 |
|
} catch (FJException success) { |
941 |
+ |
checkCompletedAbnormally(f, success); |
942 |
|
} |
943 |
|
}}; |
944 |
|
testInvokeOnPool(mainPool(), a); |
949 |
|
* and suppresses execution |
950 |
|
*/ |
951 |
|
public void testTryUnfork() { |
952 |
< |
RecursiveAction a = new RecursiveAction() { |
953 |
< |
public void compute() { |
952 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
953 |
> |
public void realCompute() { |
954 |
|
AsyncFib g = new AsyncFib(9); |
955 |
< |
g.fork(); |
955 |
> |
assertSame(g, g.fork()); |
956 |
|
AsyncFib f = new AsyncFib(8); |
957 |
< |
f.fork(); |
958 |
< |
threadAssertTrue(f.tryUnfork()); |
957 |
> |
assertSame(f, f.fork()); |
958 |
> |
assertTrue(f.tryUnfork()); |
959 |
|
helpQuiesce(); |
960 |
< |
threadAssertFalse(f.isDone()); |
961 |
< |
threadAssertTrue(g.isDone()); |
960 |
> |
checkNotDone(f); |
961 |
> |
checkCompletedNormally(g); |
962 |
|
}}; |
963 |
|
testInvokeOnPool(singletonPool(), a); |
964 |
|
} |
968 |
|
* there are more tasks than threads |
969 |
|
*/ |
970 |
|
public void testGetSurplusQueuedTaskCount() { |
971 |
< |
RecursiveAction a = new RecursiveAction() { |
972 |
< |
public void compute() { |
971 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
972 |
> |
public void realCompute() { |
973 |
|
AsyncFib h = new AsyncFib(7); |
974 |
< |
h.fork(); |
974 |
> |
assertSame(h, h.fork()); |
975 |
|
AsyncFib g = new AsyncFib(9); |
976 |
< |
g.fork(); |
976 |
> |
assertSame(g, g.fork()); |
977 |
|
AsyncFib f = new AsyncFib(8); |
978 |
< |
f.fork(); |
979 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
978 |
> |
assertSame(f, f.fork()); |
979 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
980 |
|
helpQuiesce(); |
981 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
982 |
+ |
checkCompletedNormally(f); |
983 |
+ |
checkCompletedNormally(g); |
984 |
+ |
checkCompletedNormally(h); |
985 |
|
}}; |
986 |
|
testInvokeOnPool(singletonPool(), a); |
987 |
|
} |
990 |
|
* peekNextLocalTask returns most recent unexecuted task. |
991 |
|
*/ |
992 |
|
public void testPeekNextLocalTask() { |
993 |
< |
RecursiveAction a = new RecursiveAction() { |
994 |
< |
public void compute() { |
993 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
994 |
> |
public void realCompute() { |
995 |
|
AsyncFib g = new AsyncFib(9); |
996 |
< |
g.fork(); |
996 |
> |
assertSame(g, g.fork()); |
997 |
|
AsyncFib f = new AsyncFib(8); |
998 |
< |
f.fork(); |
999 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
1000 |
< |
f.join(); |
1001 |
< |
threadAssertTrue(f.isDone()); |
998 |
> |
assertSame(f, f.fork()); |
999 |
> |
assertSame(f, peekNextLocalTask()); |
1000 |
> |
assertNull(f.join()); |
1001 |
> |
checkCompletedNormally(f); |
1002 |
|
helpQuiesce(); |
1003 |
+ |
checkCompletedNormally(g); |
1004 |
|
}}; |
1005 |
|
testInvokeOnPool(singletonPool(), a); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1009 |
< |
* pollNextLocalTask returns most recent unexecuted task |
1010 |
< |
* without executing it |
1009 |
> |
* pollNextLocalTask returns most recent unexecuted task without |
1010 |
> |
* executing it |
1011 |
|
*/ |
1012 |
|
public void testPollNextLocalTask() { |
1013 |
< |
RecursiveAction a = new RecursiveAction() { |
1014 |
< |
public void compute() { |
1013 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1014 |
> |
public void realCompute() { |
1015 |
|
AsyncFib g = new AsyncFib(9); |
1016 |
< |
g.fork(); |
1016 |
> |
assertSame(g, g.fork()); |
1017 |
|
AsyncFib f = new AsyncFib(8); |
1018 |
< |
f.fork(); |
1019 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
1018 |
> |
assertSame(f, f.fork()); |
1019 |
> |
assertSame(f, pollNextLocalTask()); |
1020 |
|
helpQuiesce(); |
1021 |
< |
threadAssertFalse(f.isDone()); |
1021 |
> |
checkNotDone(f); |
1022 |
> |
assertEquals(34, g.number); |
1023 |
> |
checkCompletedNormally(g); |
1024 |
|
}}; |
1025 |
|
testInvokeOnPool(singletonPool(), a); |
1026 |
|
} |
1027 |
|
|
1028 |
|
/** |
1029 |
< |
* pollTask returns an unexecuted task |
883 |
< |
* without executing it |
1029 |
> |
* pollTask returns an unexecuted task without executing it |
1030 |
|
*/ |
1031 |
|
public void testPollTask() { |
1032 |
< |
RecursiveAction a = new RecursiveAction() { |
1033 |
< |
public void compute() { |
1032 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1033 |
> |
public void realCompute() { |
1034 |
|
AsyncFib g = new AsyncFib(9); |
1035 |
< |
g.fork(); |
1035 |
> |
assertSame(g, g.fork()); |
1036 |
|
AsyncFib f = new AsyncFib(8); |
1037 |
< |
f.fork(); |
1038 |
< |
threadAssertTrue(pollTask() == f); |
1037 |
> |
assertSame(f, f.fork()); |
1038 |
> |
assertSame(f, pollTask()); |
1039 |
|
helpQuiesce(); |
1040 |
< |
threadAssertFalse(f.isDone()); |
1041 |
< |
threadAssertTrue(g.isDone()); |
1040 |
> |
checkNotDone(f); |
1041 |
> |
checkCompletedNormally(g); |
1042 |
|
}}; |
1043 |
|
testInvokeOnPool(singletonPool(), a); |
1044 |
|
} |
1047 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1048 |
|
*/ |
1049 |
|
public void testPeekNextLocalTaskAsync() { |
1050 |
< |
RecursiveAction a = new RecursiveAction() { |
1051 |
< |
public void compute() { |
1050 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1051 |
> |
public void realCompute() { |
1052 |
|
AsyncFib g = new AsyncFib(9); |
1053 |
< |
g.fork(); |
1053 |
> |
assertSame(g, g.fork()); |
1054 |
|
AsyncFib f = new AsyncFib(8); |
1055 |
< |
f.fork(); |
1056 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
1057 |
< |
f.join(); |
1055 |
> |
assertSame(f, f.fork()); |
1056 |
> |
assertSame(g, peekNextLocalTask()); |
1057 |
> |
assertNull(f.join()); |
1058 |
|
helpQuiesce(); |
1059 |
< |
threadAssertTrue(f.isDone()); |
1059 |
> |
checkCompletedNormally(f); |
1060 |
> |
assertEquals(34, g.number); |
1061 |
> |
checkCompletedNormally(g); |
1062 |
|
}}; |
1063 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1064 |
|
} |
1065 |
|
|
1066 |
|
/** |
1067 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1068 |
< |
* without executing it, in async mode |
1067 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1068 |
> |
* executing it, in async mode |
1069 |
|
*/ |
1070 |
|
public void testPollNextLocalTaskAsync() { |
1071 |
< |
RecursiveAction a = new RecursiveAction() { |
1072 |
< |
public void compute() { |
1071 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1072 |
> |
public void realCompute() { |
1073 |
|
AsyncFib g = new AsyncFib(9); |
1074 |
< |
g.fork(); |
1074 |
> |
assertSame(g, g.fork()); |
1075 |
|
AsyncFib f = new AsyncFib(8); |
1076 |
< |
f.fork(); |
1077 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
1076 |
> |
assertSame(f, f.fork()); |
1077 |
> |
assertSame(g, pollNextLocalTask()); |
1078 |
|
helpQuiesce(); |
1079 |
< |
threadAssertTrue(f.isDone()); |
1080 |
< |
threadAssertFalse(g.isDone()); |
1079 |
> |
assertEquals(21, f.number); |
1080 |
> |
checkCompletedNormally(f); |
1081 |
> |
checkNotDone(g); |
1082 |
|
}}; |
1083 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1084 |
|
} |
1085 |
|
|
1086 |
|
/** |
1087 |
< |
* pollTask returns an unexecuted task |
1088 |
< |
* without executing it, in async mode |
1087 |
> |
* pollTask returns an unexecuted task without executing it, in |
1088 |
> |
* async mode |
1089 |
|
*/ |
1090 |
|
public void testPollTaskAsync() { |
1091 |
< |
RecursiveAction a = new RecursiveAction() { |
1092 |
< |
public void compute() { |
1091 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1092 |
> |
public void realCompute() { |
1093 |
|
AsyncFib g = new AsyncFib(9); |
1094 |
< |
g.fork(); |
1094 |
> |
assertSame(g, g.fork()); |
1095 |
|
AsyncFib f = new AsyncFib(8); |
1096 |
< |
f.fork(); |
1097 |
< |
threadAssertTrue(pollTask() == g); |
1096 |
> |
assertSame(f, f.fork()); |
1097 |
> |
assertSame(g, pollTask()); |
1098 |
|
helpQuiesce(); |
1099 |
< |
threadAssertTrue(f.isDone()); |
1100 |
< |
threadAssertFalse(g.isDone()); |
1099 |
> |
assertEquals(21, f.number); |
1100 |
> |
checkCompletedNormally(f); |
1101 |
> |
checkNotDone(g); |
1102 |
|
}}; |
1103 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1104 |
|
} |
1105 |
+ |
|
1106 |
+ |
// versions for singleton pools |
1107 |
+ |
|
1108 |
+ |
/** |
1109 |
+ |
* invoke returns when task completes normally. |
1110 |
+ |
* isCompletedAbnormally and isCancelled return false for normally |
1111 |
+ |
* completed tasks; getRawResult returns null. |
1112 |
+ |
*/ |
1113 |
+ |
public void testInvokeSingleton() { |
1114 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1115 |
+ |
public void realCompute() { |
1116 |
+ |
AsyncFib f = new AsyncFib(8); |
1117 |
+ |
assertNull(f.invoke()); |
1118 |
+ |
assertEquals(21, f.number); |
1119 |
+ |
checkCompletedNormally(f); |
1120 |
+ |
}}; |
1121 |
+ |
testInvokeOnPool(singletonPool(), a); |
1122 |
+ |
} |
1123 |
+ |
|
1124 |
+ |
/** |
1125 |
+ |
* quietlyInvoke task returns when task completes normally. |
1126 |
+ |
* isCompletedAbnormally and isCancelled return false for normally |
1127 |
+ |
* completed tasks |
1128 |
+ |
*/ |
1129 |
+ |
public void testQuietlyInvokeSingleton() { |
1130 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1131 |
+ |
public void realCompute() { |
1132 |
+ |
AsyncFib f = new AsyncFib(8); |
1133 |
+ |
f.quietlyInvoke(); |
1134 |
+ |
assertEquals(21, f.number); |
1135 |
+ |
checkCompletedNormally(f); |
1136 |
+ |
}}; |
1137 |
+ |
testInvokeOnPool(singletonPool(), a); |
1138 |
+ |
} |
1139 |
+ |
|
1140 |
+ |
/** |
1141 |
+ |
* join of a forked task returns when task completes |
1142 |
+ |
*/ |
1143 |
+ |
public void testForkJoinSingleton() { |
1144 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1145 |
+ |
public void realCompute() { |
1146 |
+ |
AsyncFib f = new AsyncFib(8); |
1147 |
+ |
assertSame(f, f.fork()); |
1148 |
+ |
assertNull(f.join()); |
1149 |
+ |
assertEquals(21, f.number); |
1150 |
+ |
checkCompletedNormally(f); |
1151 |
+ |
}}; |
1152 |
+ |
testInvokeOnPool(singletonPool(), a); |
1153 |
+ |
} |
1154 |
+ |
|
1155 |
+ |
/** |
1156 |
+ |
* get of a forked task returns when task completes |
1157 |
+ |
*/ |
1158 |
+ |
public void testForkGetSingleton() { |
1159 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1160 |
+ |
public void realCompute() throws Exception { |
1161 |
+ |
AsyncFib f = new AsyncFib(8); |
1162 |
+ |
assertSame(f, f.fork()); |
1163 |
+ |
assertNull(f.get()); |
1164 |
+ |
assertEquals(21, f.number); |
1165 |
+ |
checkCompletedNormally(f); |
1166 |
+ |
}}; |
1167 |
+ |
testInvokeOnPool(singletonPool(), a); |
1168 |
+ |
} |
1169 |
+ |
|
1170 |
+ |
/** |
1171 |
+ |
* timed get of a forked task returns when task completes |
1172 |
+ |
*/ |
1173 |
+ |
public void testForkTimedGetSingleton() { |
1174 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1175 |
+ |
public void realCompute() throws Exception { |
1176 |
+ |
AsyncFib f = new AsyncFib(8); |
1177 |
+ |
assertSame(f, f.fork()); |
1178 |
+ |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1179 |
+ |
assertEquals(21, f.number); |
1180 |
+ |
checkCompletedNormally(f); |
1181 |
+ |
}}; |
1182 |
+ |
testInvokeOnPool(singletonPool(), a); |
1183 |
+ |
} |
1184 |
+ |
|
1185 |
+ |
/** |
1186 |
+ |
* timed get with null time unit throws NPE |
1187 |
+ |
*/ |
1188 |
+ |
public void testForkTimedGetNPESingleton() { |
1189 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1190 |
+ |
public void realCompute() throws Exception { |
1191 |
+ |
AsyncFib f = new AsyncFib(8); |
1192 |
+ |
assertSame(f, f.fork()); |
1193 |
+ |
try { |
1194 |
+ |
f.get(5L, null); |
1195 |
+ |
shouldThrow(); |
1196 |
+ |
} catch (NullPointerException success) {} |
1197 |
+ |
}}; |
1198 |
+ |
testInvokeOnPool(singletonPool(), a); |
1199 |
+ |
} |
1200 |
+ |
|
1201 |
+ |
/** |
1202 |
+ |
* quietlyJoin of a forked task returns when task completes |
1203 |
+ |
*/ |
1204 |
+ |
public void testForkQuietlyJoinSingleton() { |
1205 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1206 |
+ |
public void realCompute() { |
1207 |
+ |
AsyncFib f = new AsyncFib(8); |
1208 |
+ |
assertSame(f, f.fork()); |
1209 |
+ |
f.quietlyJoin(); |
1210 |
+ |
assertEquals(21, f.number); |
1211 |
+ |
checkCompletedNormally(f); |
1212 |
+ |
}}; |
1213 |
+ |
testInvokeOnPool(singletonPool(), a); |
1214 |
+ |
} |
1215 |
+ |
|
1216 |
+ |
|
1217 |
+ |
/** |
1218 |
+ |
* helpQuiesce returns when tasks are complete. |
1219 |
+ |
* getQueuedTaskCount returns 0 when quiescent |
1220 |
+ |
*/ |
1221 |
+ |
public void testForkHelpQuiesceSingleton() { |
1222 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1223 |
+ |
public void realCompute() { |
1224 |
+ |
AsyncFib f = new AsyncFib(8); |
1225 |
+ |
assertSame(f, f.fork()); |
1226 |
+ |
f.helpQuiesce(); |
1227 |
+ |
assertEquals(0, getQueuedTaskCount()); |
1228 |
+ |
assertEquals(21, f.number); |
1229 |
+ |
checkCompletedNormally(f); |
1230 |
+ |
}}; |
1231 |
+ |
testInvokeOnPool(singletonPool(), a); |
1232 |
+ |
} |
1233 |
+ |
|
1234 |
+ |
|
1235 |
+ |
/** |
1236 |
+ |
* invoke task throws exception when task completes abnormally |
1237 |
+ |
*/ |
1238 |
+ |
public void testAbnormalInvokeSingleton() { |
1239 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1240 |
+ |
public void realCompute() { |
1241 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1242 |
+ |
try { |
1243 |
+ |
f.invoke(); |
1244 |
+ |
shouldThrow(); |
1245 |
+ |
} catch (FJException success) { |
1246 |
+ |
checkCompletedAbnormally(f, success); |
1247 |
+ |
} |
1248 |
+ |
}}; |
1249 |
+ |
testInvokeOnPool(singletonPool(), a); |
1250 |
+ |
} |
1251 |
+ |
|
1252 |
+ |
/** |
1253 |
+ |
* quietlyInvoke task returns when task completes abnormally |
1254 |
+ |
*/ |
1255 |
+ |
public void testAbnormalQuietlyInvokeSingleton() { |
1256 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1257 |
+ |
public void realCompute() { |
1258 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1259 |
+ |
f.quietlyInvoke(); |
1260 |
+ |
assertTrue(f.getException() instanceof FJException); |
1261 |
+ |
checkCompletedAbnormally(f, f.getException()); |
1262 |
+ |
}}; |
1263 |
+ |
testInvokeOnPool(singletonPool(), a); |
1264 |
+ |
} |
1265 |
+ |
|
1266 |
+ |
/** |
1267 |
+ |
* join of a forked task throws exception when task completes abnormally |
1268 |
+ |
*/ |
1269 |
+ |
public void testAbnormalForkJoinSingleton() { |
1270 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1271 |
+ |
public void realCompute() { |
1272 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1273 |
+ |
assertSame(f, f.fork()); |
1274 |
+ |
try { |
1275 |
+ |
f.join(); |
1276 |
+ |
shouldThrow(); |
1277 |
+ |
} catch (FJException success) { |
1278 |
+ |
checkCompletedAbnormally(f, success); |
1279 |
+ |
} |
1280 |
+ |
}}; |
1281 |
+ |
testInvokeOnPool(singletonPool(), a); |
1282 |
+ |
} |
1283 |
+ |
|
1284 |
+ |
/** |
1285 |
+ |
* get of a forked task throws exception when task completes abnormally |
1286 |
+ |
*/ |
1287 |
+ |
public void testAbnormalForkGetSingleton() { |
1288 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1289 |
+ |
public void realCompute() throws Exception { |
1290 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1291 |
+ |
assertSame(f, f.fork()); |
1292 |
+ |
try { |
1293 |
+ |
f.get(); |
1294 |
+ |
shouldThrow(); |
1295 |
+ |
} catch (ExecutionException success) { |
1296 |
+ |
Throwable cause = success.getCause(); |
1297 |
+ |
assertTrue(cause instanceof FJException); |
1298 |
+ |
checkCompletedAbnormally(f, cause); |
1299 |
+ |
} |
1300 |
+ |
}}; |
1301 |
+ |
testInvokeOnPool(singletonPool(), a); |
1302 |
+ |
} |
1303 |
+ |
|
1304 |
+ |
/** |
1305 |
+ |
* timed get of a forked task throws exception when task completes abnormally |
1306 |
+ |
*/ |
1307 |
+ |
public void testAbnormalForkTimedGetSingleton() { |
1308 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1309 |
+ |
public void realCompute() throws Exception { |
1310 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1311 |
+ |
assertSame(f, f.fork()); |
1312 |
+ |
try { |
1313 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1314 |
+ |
shouldThrow(); |
1315 |
+ |
} catch (ExecutionException success) { |
1316 |
+ |
Throwable cause = success.getCause(); |
1317 |
+ |
assertTrue(cause instanceof FJException); |
1318 |
+ |
checkCompletedAbnormally(f, cause); |
1319 |
+ |
} |
1320 |
+ |
}}; |
1321 |
+ |
testInvokeOnPool(singletonPool(), a); |
1322 |
+ |
} |
1323 |
+ |
|
1324 |
+ |
/** |
1325 |
+ |
* quietlyJoin of a forked task returns when task completes abnormally |
1326 |
+ |
*/ |
1327 |
+ |
public void testAbnormalForkQuietlyJoinSingleton() { |
1328 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1329 |
+ |
public void realCompute() { |
1330 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1331 |
+ |
assertSame(f, f.fork()); |
1332 |
+ |
f.quietlyJoin(); |
1333 |
+ |
assertTrue(f.getException() instanceof FJException); |
1334 |
+ |
checkCompletedAbnormally(f, f.getException()); |
1335 |
+ |
}}; |
1336 |
+ |
testInvokeOnPool(singletonPool(), a); |
1337 |
+ |
} |
1338 |
+ |
|
1339 |
+ |
/** |
1340 |
+ |
* invoke task throws exception when task cancelled |
1341 |
+ |
*/ |
1342 |
+ |
public void testCancelledInvokeSingleton() { |
1343 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1344 |
+ |
public void realCompute() { |
1345 |
+ |
AsyncFib f = new AsyncFib(8); |
1346 |
+ |
assertTrue(f.cancel(true)); |
1347 |
+ |
try { |
1348 |
+ |
f.invoke(); |
1349 |
+ |
shouldThrow(); |
1350 |
+ |
} catch (CancellationException success) { |
1351 |
+ |
checkCancelled(f); |
1352 |
+ |
} |
1353 |
+ |
}}; |
1354 |
+ |
testInvokeOnPool(singletonPool(), a); |
1355 |
+ |
} |
1356 |
+ |
|
1357 |
+ |
/** |
1358 |
+ |
* join of a forked task throws exception when task cancelled |
1359 |
+ |
*/ |
1360 |
+ |
public void testCancelledForkJoinSingleton() { |
1361 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1362 |
+ |
public void realCompute() { |
1363 |
+ |
AsyncFib f = new AsyncFib(8); |
1364 |
+ |
assertTrue(f.cancel(true)); |
1365 |
+ |
assertSame(f, f.fork()); |
1366 |
+ |
try { |
1367 |
+ |
f.join(); |
1368 |
+ |
shouldThrow(); |
1369 |
+ |
} catch (CancellationException success) { |
1370 |
+ |
checkCancelled(f); |
1371 |
+ |
} |
1372 |
+ |
}}; |
1373 |
+ |
testInvokeOnPool(singletonPool(), a); |
1374 |
+ |
} |
1375 |
+ |
|
1376 |
+ |
/** |
1377 |
+ |
* get of a forked task throws exception when task cancelled |
1378 |
+ |
*/ |
1379 |
+ |
public void testCancelledForkGetSingleton() { |
1380 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1381 |
+ |
public void realCompute() throws Exception { |
1382 |
+ |
AsyncFib f = new AsyncFib(8); |
1383 |
+ |
assertTrue(f.cancel(true)); |
1384 |
+ |
assertSame(f, f.fork()); |
1385 |
+ |
try { |
1386 |
+ |
f.get(); |
1387 |
+ |
shouldThrow(); |
1388 |
+ |
} catch (CancellationException success) { |
1389 |
+ |
checkCancelled(f); |
1390 |
+ |
} |
1391 |
+ |
}}; |
1392 |
+ |
testInvokeOnPool(singletonPool(), a); |
1393 |
+ |
} |
1394 |
+ |
|
1395 |
+ |
/** |
1396 |
+ |
* timed get of a forked task throws exception when task cancelled |
1397 |
+ |
*/ |
1398 |
+ |
public void testCancelledForkTimedGetSingleton() throws Exception { |
1399 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1400 |
+ |
public void realCompute() throws Exception { |
1401 |
+ |
AsyncFib f = new AsyncFib(8); |
1402 |
+ |
assertTrue(f.cancel(true)); |
1403 |
+ |
assertSame(f, f.fork()); |
1404 |
+ |
try { |
1405 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1406 |
+ |
shouldThrow(); |
1407 |
+ |
} catch (CancellationException success) { |
1408 |
+ |
checkCancelled(f); |
1409 |
+ |
} |
1410 |
+ |
}}; |
1411 |
+ |
testInvokeOnPool(singletonPool(), a); |
1412 |
+ |
} |
1413 |
+ |
|
1414 |
+ |
/** |
1415 |
+ |
* quietlyJoin of a forked task returns when task cancelled |
1416 |
+ |
*/ |
1417 |
+ |
public void testCancelledForkQuietlyJoinSingleton() { |
1418 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1419 |
+ |
public void realCompute() { |
1420 |
+ |
AsyncFib f = new AsyncFib(8); |
1421 |
+ |
assertTrue(f.cancel(true)); |
1422 |
+ |
assertSame(f, f.fork()); |
1423 |
+ |
f.quietlyJoin(); |
1424 |
+ |
checkCancelled(f); |
1425 |
+ |
}}; |
1426 |
+ |
testInvokeOnPool(singletonPool(), a); |
1427 |
+ |
} |
1428 |
+ |
|
1429 |
+ |
/** |
1430 |
+ |
* invoke task throws exception after invoking completeExceptionally |
1431 |
+ |
*/ |
1432 |
+ |
public void testCompleteExceptionallySingleton() { |
1433 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1434 |
+ |
public void realCompute() { |
1435 |
+ |
AsyncFib f = new AsyncFib(8); |
1436 |
+ |
f.completeExceptionally(new FJException()); |
1437 |
+ |
try { |
1438 |
+ |
f.invoke(); |
1439 |
+ |
shouldThrow(); |
1440 |
+ |
} catch (FJException success) { |
1441 |
+ |
checkCompletedAbnormally(f, success); |
1442 |
+ |
} |
1443 |
+ |
}}; |
1444 |
+ |
testInvokeOnPool(singletonPool(), a); |
1445 |
+ |
} |
1446 |
+ |
|
1447 |
+ |
/** |
1448 |
+ |
* invokeAll(t1, t2) invokes all task arguments |
1449 |
+ |
*/ |
1450 |
+ |
public void testInvokeAll2Singleton() { |
1451 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1452 |
+ |
public void realCompute() { |
1453 |
+ |
AsyncFib f = new AsyncFib(8); |
1454 |
+ |
AsyncFib g = new AsyncFib(9); |
1455 |
+ |
invokeAll(f, g); |
1456 |
+ |
assertEquals(21, f.number); |
1457 |
+ |
assertEquals(34, g.number); |
1458 |
+ |
checkCompletedNormally(f); |
1459 |
+ |
checkCompletedNormally(g); |
1460 |
+ |
}}; |
1461 |
+ |
testInvokeOnPool(singletonPool(), a); |
1462 |
+ |
} |
1463 |
+ |
|
1464 |
+ |
/** |
1465 |
+ |
* invokeAll(tasks) with 1 argument invokes task |
1466 |
+ |
*/ |
1467 |
+ |
public void testInvokeAll1Singleton() { |
1468 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1469 |
+ |
public void realCompute() { |
1470 |
+ |
AsyncFib f = new AsyncFib(8); |
1471 |
+ |
invokeAll(f); |
1472 |
+ |
checkCompletedNormally(f); |
1473 |
+ |
assertEquals(21, f.number); |
1474 |
+ |
}}; |
1475 |
+ |
testInvokeOnPool(singletonPool(), a); |
1476 |
+ |
} |
1477 |
+ |
|
1478 |
+ |
/** |
1479 |
+ |
* invokeAll(tasks) with > 2 argument invokes tasks |
1480 |
+ |
*/ |
1481 |
+ |
public void testInvokeAll3Singleton() { |
1482 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1483 |
+ |
public void realCompute() { |
1484 |
+ |
AsyncFib f = new AsyncFib(8); |
1485 |
+ |
AsyncFib g = new AsyncFib(9); |
1486 |
+ |
AsyncFib h = new AsyncFib(7); |
1487 |
+ |
invokeAll(f, g, h); |
1488 |
+ |
assertEquals(21, f.number); |
1489 |
+ |
assertEquals(34, g.number); |
1490 |
+ |
assertEquals(13, h.number); |
1491 |
+ |
checkCompletedNormally(f); |
1492 |
+ |
checkCompletedNormally(g); |
1493 |
+ |
checkCompletedNormally(h); |
1494 |
+ |
}}; |
1495 |
+ |
testInvokeOnPool(singletonPool(), a); |
1496 |
+ |
} |
1497 |
+ |
|
1498 |
+ |
/** |
1499 |
+ |
* invokeAll(collection) invokes all tasks in the collection |
1500 |
+ |
*/ |
1501 |
+ |
public void testInvokeAllCollectionSingleton() { |
1502 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1503 |
+ |
public void realCompute() { |
1504 |
+ |
AsyncFib f = new AsyncFib(8); |
1505 |
+ |
AsyncFib g = new AsyncFib(9); |
1506 |
+ |
AsyncFib h = new AsyncFib(7); |
1507 |
+ |
HashSet set = new HashSet(); |
1508 |
+ |
set.add(f); |
1509 |
+ |
set.add(g); |
1510 |
+ |
set.add(h); |
1511 |
+ |
invokeAll(set); |
1512 |
+ |
assertEquals(21, f.number); |
1513 |
+ |
assertEquals(34, g.number); |
1514 |
+ |
assertEquals(13, h.number); |
1515 |
+ |
checkCompletedNormally(f); |
1516 |
+ |
checkCompletedNormally(g); |
1517 |
+ |
checkCompletedNormally(h); |
1518 |
+ |
}}; |
1519 |
+ |
testInvokeOnPool(singletonPool(), a); |
1520 |
+ |
} |
1521 |
+ |
|
1522 |
+ |
|
1523 |
+ |
/** |
1524 |
+ |
* invokeAll(tasks) with any null task throws NPE |
1525 |
+ |
*/ |
1526 |
+ |
public void testInvokeAllNPESingleton() { |
1527 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1528 |
+ |
public void realCompute() { |
1529 |
+ |
AsyncFib f = new AsyncFib(8); |
1530 |
+ |
AsyncFib g = new AsyncFib(9); |
1531 |
+ |
AsyncFib h = null; |
1532 |
+ |
try { |
1533 |
+ |
invokeAll(f, g, h); |
1534 |
+ |
shouldThrow(); |
1535 |
+ |
} catch (NullPointerException success) {} |
1536 |
+ |
}}; |
1537 |
+ |
testInvokeOnPool(singletonPool(), a); |
1538 |
+ |
} |
1539 |
+ |
|
1540 |
+ |
/** |
1541 |
+ |
* invokeAll(t1, t2) throw exception if any task does |
1542 |
+ |
*/ |
1543 |
+ |
public void testAbnormalInvokeAll2Singleton() { |
1544 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1545 |
+ |
public void realCompute() { |
1546 |
+ |
AsyncFib f = new AsyncFib(8); |
1547 |
+ |
FailingAsyncFib g = new FailingAsyncFib(9); |
1548 |
+ |
try { |
1549 |
+ |
invokeAll(f, g); |
1550 |
+ |
shouldThrow(); |
1551 |
+ |
} catch (FJException success) { |
1552 |
+ |
checkCompletedAbnormally(g, success); |
1553 |
+ |
} |
1554 |
+ |
}}; |
1555 |
+ |
testInvokeOnPool(singletonPool(), a); |
1556 |
+ |
} |
1557 |
+ |
|
1558 |
+ |
/** |
1559 |
+ |
* invokeAll(tasks) with 1 argument throws exception if task does |
1560 |
+ |
*/ |
1561 |
+ |
public void testAbnormalInvokeAll1Singleton() { |
1562 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1563 |
+ |
public void realCompute() { |
1564 |
+ |
FailingAsyncFib g = new FailingAsyncFib(9); |
1565 |
+ |
try { |
1566 |
+ |
invokeAll(g); |
1567 |
+ |
shouldThrow(); |
1568 |
+ |
} catch (FJException success) { |
1569 |
+ |
checkCompletedAbnormally(g, success); |
1570 |
+ |
} |
1571 |
+ |
}}; |
1572 |
+ |
testInvokeOnPool(singletonPool(), a); |
1573 |
+ |
} |
1574 |
+ |
|
1575 |
+ |
/** |
1576 |
+ |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1577 |
+ |
*/ |
1578 |
+ |
public void testAbnormalInvokeAll3Singleton() { |
1579 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1580 |
+ |
public void realCompute() { |
1581 |
+ |
AsyncFib f = new AsyncFib(8); |
1582 |
+ |
FailingAsyncFib g = new FailingAsyncFib(9); |
1583 |
+ |
AsyncFib h = new AsyncFib(7); |
1584 |
+ |
try { |
1585 |
+ |
invokeAll(f, g, h); |
1586 |
+ |
shouldThrow(); |
1587 |
+ |
} catch (FJException success) { |
1588 |
+ |
checkCompletedAbnormally(g, success); |
1589 |
+ |
} |
1590 |
+ |
}}; |
1591 |
+ |
testInvokeOnPool(singletonPool(), a); |
1592 |
+ |
} |
1593 |
+ |
|
1594 |
+ |
/** |
1595 |
+ |
* invokeAll(collection) throws exception if any task does |
1596 |
+ |
*/ |
1597 |
+ |
public void testAbnormalInvokeAllCollectionSingleton() { |
1598 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
1599 |
+ |
public void realCompute() { |
1600 |
+ |
FailingAsyncFib f = new FailingAsyncFib(8); |
1601 |
+ |
AsyncFib g = new AsyncFib(9); |
1602 |
+ |
AsyncFib h = new AsyncFib(7); |
1603 |
+ |
HashSet set = new HashSet(); |
1604 |
+ |
set.add(f); |
1605 |
+ |
set.add(g); |
1606 |
+ |
set.add(h); |
1607 |
+ |
try { |
1608 |
+ |
invokeAll(set); |
1609 |
+ |
shouldThrow(); |
1610 |
+ |
} catch (FJException success) { |
1611 |
+ |
checkCompletedAbnormally(f, success); |
1612 |
+ |
} |
1613 |
+ |
}}; |
1614 |
+ |
testInvokeOnPool(singletonPool(), a); |
1615 |
+ |
} |
1616 |
+ |
|
1617 |
|
} |