1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
import java.util.concurrent.ExecutionException; |
7 |
import java.util.concurrent.CancellationException; |
8 |
import java.util.concurrent.ForkJoinPool; |
9 |
import java.util.concurrent.ForkJoinTask; |
10 |
import java.util.concurrent.CountedCompleter; |
11 |
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
import java.util.concurrent.RecursiveAction; |
13 |
import java.util.concurrent.TimeUnit; |
14 |
import java.util.concurrent.TimeoutException; |
15 |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
16 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
import java.util.HashSet; |
19 |
import junit.framework.*; |
20 |
|
21 |
public class CountedCompleterTest extends JSR166TestCase { |
22 |
|
23 |
public static void main(String[] args) { |
24 |
junit.textui.TestRunner.run(suite()); |
25 |
} |
26 |
|
27 |
public static Test suite() { |
28 |
return new TestSuite(CountedCompleterTest.class); |
29 |
} |
30 |
|
31 |
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1 |
32 |
static final int mainPoolSize = |
33 |
Math.max(2, Runtime.getRuntime().availableProcessors()); |
34 |
|
35 |
private static ForkJoinPool mainPool() { |
36 |
return new ForkJoinPool(mainPoolSize); |
37 |
} |
38 |
|
39 |
private static ForkJoinPool singletonPool() { |
40 |
return new ForkJoinPool(1); |
41 |
} |
42 |
|
43 |
private static ForkJoinPool asyncSingletonPool() { |
44 |
return new ForkJoinPool(1, |
45 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
46 |
null, true); |
47 |
} |
48 |
|
49 |
private void testInvokeOnPool(ForkJoinPool pool, ForkJoinTask a) { |
50 |
try { |
51 |
assertFalse(a.isDone()); |
52 |
assertFalse(a.isCompletedNormally()); |
53 |
assertFalse(a.isCompletedAbnormally()); |
54 |
assertFalse(a.isCancelled()); |
55 |
assertNull(a.getException()); |
56 |
assertNull(a.getRawResult()); |
57 |
|
58 |
assertNull(pool.invoke(a)); |
59 |
|
60 |
assertTrue(a.isDone()); |
61 |
assertTrue(a.isCompletedNormally()); |
62 |
assertFalse(a.isCompletedAbnormally()); |
63 |
assertFalse(a.isCancelled()); |
64 |
assertNull(a.getException()); |
65 |
assertNull(a.getRawResult()); |
66 |
} finally { |
67 |
joinPool(pool); |
68 |
} |
69 |
} |
70 |
|
71 |
void checkNotDone(CountedCompleter a) { |
72 |
assertFalse(a.isDone()); |
73 |
assertFalse(a.isCompletedNormally()); |
74 |
assertFalse(a.isCompletedAbnormally()); |
75 |
assertFalse(a.isCancelled()); |
76 |
assertNull(a.getException()); |
77 |
assertNull(a.getRawResult()); |
78 |
|
79 |
try { |
80 |
a.get(0L, SECONDS); |
81 |
shouldThrow(); |
82 |
} catch (TimeoutException success) { |
83 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
} |
85 |
|
86 |
<T> void checkCompletedNormally(CountedCompleter<T> a) { |
87 |
checkCompletedNormally(a, null); |
88 |
} |
89 |
|
90 |
<T> void checkCompletedNormally(CountedCompleter<T> a, T expected) { |
91 |
assertTrue(a.isDone()); |
92 |
assertFalse(a.isCancelled()); |
93 |
assertTrue(a.isCompletedNormally()); |
94 |
assertFalse(a.isCompletedAbnormally()); |
95 |
assertNull(a.getException()); |
96 |
assertSame(expected, a.getRawResult()); |
97 |
|
98 |
{ |
99 |
Thread.currentThread().interrupt(); |
100 |
long t0 = System.nanoTime(); |
101 |
assertSame(expected, a.join()); |
102 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
103 |
Thread.interrupted(); |
104 |
} |
105 |
|
106 |
{ |
107 |
Thread.currentThread().interrupt(); |
108 |
long t0 = System.nanoTime(); |
109 |
a.quietlyJoin(); // should be no-op |
110 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
111 |
Thread.interrupted(); |
112 |
} |
113 |
|
114 |
assertFalse(a.cancel(false)); |
115 |
assertFalse(a.cancel(true)); |
116 |
try { |
117 |
assertSame(expected, a.get()); |
118 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
119 |
try { |
120 |
assertSame(expected, a.get(5L, SECONDS)); |
121 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
} |
123 |
|
124 |
void checkCancelled(CountedCompleter a) { |
125 |
assertTrue(a.isDone()); |
126 |
assertTrue(a.isCancelled()); |
127 |
assertFalse(a.isCompletedNormally()); |
128 |
assertTrue(a.isCompletedAbnormally()); |
129 |
assertTrue(a.getException() instanceof CancellationException); |
130 |
assertNull(a.getRawResult()); |
131 |
assertTrue(a.cancel(false)); |
132 |
assertTrue(a.cancel(true)); |
133 |
|
134 |
try { |
135 |
Thread.currentThread().interrupt(); |
136 |
a.join(); |
137 |
shouldThrow(); |
138 |
} catch (CancellationException success) { |
139 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
140 |
Thread.interrupted(); |
141 |
|
142 |
{ |
143 |
long t0 = System.nanoTime(); |
144 |
a.quietlyJoin(); // should be no-op |
145 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
146 |
} |
147 |
|
148 |
try { |
149 |
a.get(); |
150 |
shouldThrow(); |
151 |
} catch (CancellationException success) { |
152 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
|
154 |
try { |
155 |
a.get(5L, SECONDS); |
156 |
shouldThrow(); |
157 |
} catch (CancellationException success) { |
158 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
} |
160 |
|
161 |
void checkCompletedAbnormally(CountedCompleter a, Throwable t) { |
162 |
assertTrue(a.isDone()); |
163 |
assertFalse(a.isCancelled()); |
164 |
assertFalse(a.isCompletedNormally()); |
165 |
assertTrue(a.isCompletedAbnormally()); |
166 |
assertSame(t.getClass(), a.getException().getClass()); |
167 |
assertNull(a.getRawResult()); |
168 |
assertFalse(a.cancel(false)); |
169 |
assertFalse(a.cancel(true)); |
170 |
|
171 |
try { |
172 |
Thread.currentThread().interrupt(); |
173 |
a.join(); |
174 |
shouldThrow(); |
175 |
} catch (Throwable expected) { |
176 |
assertSame(t.getClass(), expected.getClass()); |
177 |
} |
178 |
Thread.interrupted(); |
179 |
|
180 |
{ |
181 |
long t0 = System.nanoTime(); |
182 |
a.quietlyJoin(); // should be no-op |
183 |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
184 |
} |
185 |
|
186 |
try { |
187 |
a.get(); |
188 |
shouldThrow(); |
189 |
} catch (ExecutionException success) { |
190 |
assertSame(t.getClass(), success.getCause().getClass()); |
191 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
192 |
|
193 |
try { |
194 |
a.get(5L, SECONDS); |
195 |
shouldThrow(); |
196 |
} catch (ExecutionException success) { |
197 |
assertSame(t.getClass(), success.getCause().getClass()); |
198 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
199 |
} |
200 |
|
201 |
public static final class FJException extends RuntimeException { |
202 |
FJException() { super(); } |
203 |
} |
204 |
|
205 |
static final class NoopCountedCompleter extends CountedCompleter { |
206 |
boolean post; // set true if onCompletion called |
207 |
NoopCountedCompleter() { super(); } |
208 |
NoopCountedCompleter(CountedCompleter p) { super(p); } |
209 |
public void compute() {} |
210 |
public final void onCompletion(CountedCompleter caller) { |
211 |
post = true; |
212 |
} |
213 |
} |
214 |
|
215 |
/** |
216 |
* A newly constructed CountedCompleter is not completed; |
217 |
* complete() causes completion. |
218 |
*/ |
219 |
public void testComplete() { |
220 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
221 |
assertFalse(a.isDone()); |
222 |
assertFalse(a.isCompletedNormally()); |
223 |
assertFalse(a.isCompletedAbnormally()); |
224 |
assertFalse(a.isCancelled()); |
225 |
assertNull(a.getException()); |
226 |
assertNull(a.getRawResult()); |
227 |
assertFalse(a.post); |
228 |
a.complete(null); |
229 |
assertTrue(a.post); |
230 |
assertTrue(a.isDone()); |
231 |
assertTrue(a.isCompletedNormally()); |
232 |
assertFalse(a.isCompletedAbnormally()); |
233 |
assertFalse(a.isCancelled()); |
234 |
assertNull(a.getException()); |
235 |
assertNull(a.getRawResult()); |
236 |
} |
237 |
|
238 |
/** |
239 |
* completeExceptionally completes exceptionally |
240 |
*/ |
241 |
public void testCompleteExceptionally() { |
242 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
243 |
assertFalse(a.isDone()); |
244 |
assertFalse(a.isCompletedNormally()); |
245 |
assertFalse(a.isCompletedAbnormally()); |
246 |
assertFalse(a.isCancelled()); |
247 |
assertNull(a.getException()); |
248 |
assertNull(a.getRawResult()); |
249 |
assertFalse(a.post); |
250 |
a.completeExceptionally(new FJException()); |
251 |
assertFalse(a.post); |
252 |
assertTrue(a.isDone()); |
253 |
assertFalse(a.isCompletedNormally()); |
254 |
assertTrue(a.isCompletedAbnormally()); |
255 |
assertFalse(a.isCancelled()); |
256 |
assertTrue(a.getException() instanceof FJException); |
257 |
assertNull(a.getRawResult()); |
258 |
} |
259 |
|
260 |
/** |
261 |
* setPendingCount sets the reported pending count |
262 |
*/ |
263 |
public void testSetPendingCount() { |
264 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
265 |
assertEquals(0, a.getPendingCount()); |
266 |
a.setPendingCount(1); |
267 |
assertEquals(1, a.getPendingCount()); |
268 |
a.setPendingCount(27); |
269 |
assertEquals(27, a.getPendingCount()); |
270 |
} |
271 |
|
272 |
/** |
273 |
* addToPendingCount adds to the reported pending count |
274 |
*/ |
275 |
public void testAddToPendingCount() { |
276 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
277 |
assertEquals(0, a.getPendingCount()); |
278 |
a.addToPendingCount(1); |
279 |
assertEquals(1, a.getPendingCount()); |
280 |
a.addToPendingCount(27); |
281 |
assertEquals(28, a.getPendingCount()); |
282 |
} |
283 |
|
284 |
/** |
285 |
* decrementPendingCountUnlessZero decrements reported pending |
286 |
* count unless zero |
287 |
*/ |
288 |
public void testDecrementPendingCount() { |
289 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
290 |
assertEquals(0, a.getPendingCount()); |
291 |
a.addToPendingCount(1); |
292 |
assertEquals(1, a.getPendingCount()); |
293 |
a.decrementPendingCountUnlessZero(); |
294 |
assertEquals(0, a.getPendingCount()); |
295 |
a.decrementPendingCountUnlessZero(); |
296 |
assertEquals(0, a.getPendingCount()); |
297 |
} |
298 |
|
299 |
/** |
300 |
* getCompleter returns parent or null if at root |
301 |
*/ |
302 |
public void testGetCompleter() { |
303 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
304 |
assertNull(a.getCompleter()); |
305 |
CountedCompleter b = new NoopCountedCompleter(a); |
306 |
assertEquals(a, b.getCompleter()); |
307 |
} |
308 |
|
309 |
/** |
310 |
* getRoot returns self if no parent, else parent's root |
311 |
*/ |
312 |
public void testGetRoot() { |
313 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
314 |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
315 |
assertEquals(a, a.getRoot()); |
316 |
assertEquals(a, b.getRoot()); |
317 |
} |
318 |
|
319 |
/** |
320 |
* tryComplete causes completion if pending count is zero |
321 |
*/ |
322 |
public void testTryComplete1() { |
323 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
324 |
assertEquals(0, a.getPendingCount()); |
325 |
a.tryComplete(); |
326 |
assertTrue(a.post); |
327 |
assertTrue(a.isDone()); |
328 |
} |
329 |
|
330 |
/** |
331 |
* propagateCompletion causes completion without invoking |
332 |
* onCompletion if pending count is zero |
333 |
*/ |
334 |
public void testPropagateCompletion() { |
335 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
336 |
assertEquals(0, a.getPendingCount()); |
337 |
a.propagateCompletion(); |
338 |
assertFalse(a.post); |
339 |
assertTrue(a.isDone()); |
340 |
} |
341 |
|
342 |
/** |
343 |
* tryComplete decrements pending count unless zero |
344 |
*/ |
345 |
public void testTryComplete2() { |
346 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
347 |
assertEquals(0, a.getPendingCount()); |
348 |
a.setPendingCount(1); |
349 |
a.tryComplete(); |
350 |
assertFalse(a.post); |
351 |
assertFalse(a.isDone()); |
352 |
assertEquals(0, a.getPendingCount()); |
353 |
a.tryComplete(); |
354 |
assertTrue(a.post); |
355 |
assertTrue(a.isDone()); |
356 |
} |
357 |
|
358 |
/** |
359 |
* firstComplete returns this if pending count is zero else null |
360 |
*/ |
361 |
public void testFirstComplete() { |
362 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
363 |
a.setPendingCount(1); |
364 |
assertNull(a.firstComplete()); |
365 |
assertEquals(a, a.firstComplete()); |
366 |
} |
367 |
|
368 |
/** |
369 |
* firstComplete.nextComplete returns parent if pending count is |
370 |
* zero else null |
371 |
*/ |
372 |
public void testNextComplete() { |
373 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
374 |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
375 |
a.setPendingCount(1); |
376 |
b.setPendingCount(1); |
377 |
assertNull(b.firstComplete()); |
378 |
CountedCompleter c = b.firstComplete(); |
379 |
assertEquals(b, c); |
380 |
CountedCompleter d = c.nextComplete(); |
381 |
assertNull(d); |
382 |
CountedCompleter e = c.nextComplete(); |
383 |
assertEquals(a, e); |
384 |
} |
385 |
|
386 |
/** |
387 |
* quietlyCompleteRoot completes root task |
388 |
*/ |
389 |
public void testQuietlyCompleteRoot() { |
390 |
NoopCountedCompleter a = new NoopCountedCompleter(); |
391 |
NoopCountedCompleter b = new NoopCountedCompleter(a); |
392 |
a.setPendingCount(1); |
393 |
b.setPendingCount(1); |
394 |
b.quietlyCompleteRoot(); |
395 |
assertTrue(a.isDone()); |
396 |
assertFalse(b.isDone()); |
397 |
} |
398 |
|
399 |
// Invocation tests use some interdependent task classes |
400 |
// to better test propagation etc |
401 |
|
402 |
|
403 |
// Version of Fibonacci with different classes for left vs right forks |
404 |
abstract static class CCF extends CountedCompleter { |
405 |
int number; |
406 |
int rnumber; |
407 |
|
408 |
public CCF(CountedCompleter parent, int n) { |
409 |
super(parent, 1); |
410 |
this.number = n; |
411 |
} |
412 |
|
413 |
public final void compute() { |
414 |
CountedCompleter p; |
415 |
CCF f = this; |
416 |
int n = number; |
417 |
while (n >= 2) { |
418 |
new RCCF(f, n - 2).fork(); |
419 |
f = new LCCF(f, --n); |
420 |
} |
421 |
f.number = n; |
422 |
f.onCompletion(f); |
423 |
if ((p = f.getCompleter()) != null) |
424 |
p.tryComplete(); |
425 |
else |
426 |
f.quietlyComplete(); |
427 |
} |
428 |
} |
429 |
|
430 |
static final class LCCF extends CCF { |
431 |
public LCCF(CountedCompleter parent, int n) { |
432 |
super(parent, n); |
433 |
} |
434 |
public final void onCompletion(CountedCompleter caller) { |
435 |
CCF p = (CCF)getCompleter(); |
436 |
int n = number + rnumber; |
437 |
if (p != null) |
438 |
p.number = n; |
439 |
else |
440 |
number = n; |
441 |
} |
442 |
} |
443 |
static final class RCCF extends CCF { |
444 |
public RCCF(CountedCompleter parent, int n) { |
445 |
super(parent, n); |
446 |
} |
447 |
public final void onCompletion(CountedCompleter caller) { |
448 |
CCF p = (CCF)getCompleter(); |
449 |
int n = number + rnumber; |
450 |
if (p != null) |
451 |
p.rnumber = n; |
452 |
else |
453 |
number = n; |
454 |
} |
455 |
} |
456 |
|
457 |
// Version of CCF with forced failure in left completions |
458 |
abstract static class FailingCCF extends CountedCompleter { |
459 |
int number; |
460 |
int rnumber; |
461 |
|
462 |
public FailingCCF(CountedCompleter parent, int n) { |
463 |
super(parent, 1); |
464 |
this.number = n; |
465 |
} |
466 |
|
467 |
public final void compute() { |
468 |
CountedCompleter p; |
469 |
FailingCCF f = this; |
470 |
int n = number; |
471 |
while (n >= 2) { |
472 |
new RFCCF(f, n - 2).fork(); |
473 |
f = new LFCCF(f, --n); |
474 |
} |
475 |
f.number = n; |
476 |
f.onCompletion(f); |
477 |
if ((p = f.getCompleter()) != null) |
478 |
p.tryComplete(); |
479 |
else |
480 |
f.quietlyComplete(); |
481 |
} |
482 |
} |
483 |
|
484 |
static final class LFCCF extends FailingCCF { |
485 |
public LFCCF(CountedCompleter parent, int n) { |
486 |
super(parent, n); |
487 |
} |
488 |
public final void onCompletion(CountedCompleter caller) { |
489 |
FailingCCF p = (FailingCCF)getCompleter(); |
490 |
int n = number + rnumber; |
491 |
if (p != null) |
492 |
p.number = n; |
493 |
else |
494 |
number = n; |
495 |
} |
496 |
} |
497 |
static final class RFCCF extends FailingCCF { |
498 |
public RFCCF(CountedCompleter parent, int n) { |
499 |
super(parent, n); |
500 |
} |
501 |
public final void onCompletion(CountedCompleter caller) { |
502 |
completeExceptionally(new FJException()); |
503 |
} |
504 |
} |
505 |
|
506 |
/** |
507 |
* invoke returns when task completes normally. |
508 |
* isCompletedAbnormally and isCancelled return false for normally |
509 |
* completed tasks; getRawResult returns null. |
510 |
*/ |
511 |
public void testInvoke() { |
512 |
ForkJoinTask a = new CheckedRecursiveAction() { |
513 |
protected void realCompute() { |
514 |
CCF f = new LCCF(null, 8); |
515 |
assertNull(f.invoke()); |
516 |
assertEquals(21, f.number); |
517 |
checkCompletedNormally(f); |
518 |
}}; |
519 |
testInvokeOnPool(mainPool(), a); |
520 |
} |
521 |
|
522 |
/** |
523 |
* quietlyInvoke task returns when task completes normally. |
524 |
* isCompletedAbnormally and isCancelled return false for normally |
525 |
* completed tasks |
526 |
*/ |
527 |
public void testQuietlyInvoke() { |
528 |
ForkJoinTask a = new CheckedRecursiveAction() { |
529 |
protected void realCompute() { |
530 |
CCF f = new LCCF(null, 8); |
531 |
f.quietlyInvoke(); |
532 |
assertEquals(21, f.number); |
533 |
checkCompletedNormally(f); |
534 |
}}; |
535 |
testInvokeOnPool(mainPool(), a); |
536 |
} |
537 |
|
538 |
/** |
539 |
* join of a forked task returns when task completes |
540 |
*/ |
541 |
public void testForkJoin() { |
542 |
ForkJoinTask a = new CheckedRecursiveAction() { |
543 |
protected void realCompute() { |
544 |
CCF f = new LCCF(null, 8); |
545 |
assertSame(f, f.fork()); |
546 |
assertNull(f.join()); |
547 |
assertEquals(21, f.number); |
548 |
checkCompletedNormally(f); |
549 |
}}; |
550 |
testInvokeOnPool(mainPool(), a); |
551 |
} |
552 |
|
553 |
/** |
554 |
* get of a forked task returns when task completes |
555 |
*/ |
556 |
public void testForkGet() { |
557 |
ForkJoinTask a = new CheckedRecursiveAction() { |
558 |
protected void realCompute() throws Exception { |
559 |
CCF f = new LCCF(null, 8); |
560 |
assertSame(f, f.fork()); |
561 |
assertNull(f.get()); |
562 |
assertEquals(21, f.number); |
563 |
checkCompletedNormally(f); |
564 |
}}; |
565 |
testInvokeOnPool(mainPool(), a); |
566 |
} |
567 |
|
568 |
/** |
569 |
* timed get of a forked task returns when task completes |
570 |
*/ |
571 |
public void testForkTimedGet() { |
572 |
ForkJoinTask a = new CheckedRecursiveAction() { |
573 |
protected void realCompute() throws Exception { |
574 |
CCF f = new LCCF(null, 8); |
575 |
assertSame(f, f.fork()); |
576 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
577 |
assertEquals(21, f.number); |
578 |
checkCompletedNormally(f); |
579 |
}}; |
580 |
testInvokeOnPool(mainPool(), a); |
581 |
} |
582 |
|
583 |
/** |
584 |
* timed get with null time unit throws NPE |
585 |
*/ |
586 |
public void testForkTimedGetNPE() { |
587 |
ForkJoinTask a = new CheckedRecursiveAction() { |
588 |
protected void realCompute() throws Exception { |
589 |
CCF f = new LCCF(null, 8); |
590 |
assertSame(f, f.fork()); |
591 |
try { |
592 |
f.get(5L, null); |
593 |
shouldThrow(); |
594 |
} catch (NullPointerException success) {} |
595 |
}}; |
596 |
testInvokeOnPool(mainPool(), a); |
597 |
} |
598 |
|
599 |
/** |
600 |
* quietlyJoin of a forked task returns when task completes |
601 |
*/ |
602 |
public void testForkQuietlyJoin() { |
603 |
ForkJoinTask a = new CheckedRecursiveAction() { |
604 |
protected void realCompute() { |
605 |
CCF f = new LCCF(null, 8); |
606 |
assertSame(f, f.fork()); |
607 |
f.quietlyJoin(); |
608 |
assertEquals(21, f.number); |
609 |
checkCompletedNormally(f); |
610 |
}}; |
611 |
testInvokeOnPool(mainPool(), a); |
612 |
} |
613 |
|
614 |
/** |
615 |
* helpQuiesce returns when tasks are complete. |
616 |
* getQueuedTaskCount returns 0 when quiescent |
617 |
*/ |
618 |
public void testForkHelpQuiesce() { |
619 |
ForkJoinTask a = new CheckedRecursiveAction() { |
620 |
protected void realCompute() { |
621 |
CCF f = new LCCF(null, 8); |
622 |
assertSame(f, f.fork()); |
623 |
helpQuiesce(); |
624 |
assertEquals(21, f.number); |
625 |
assertEquals(0, getQueuedTaskCount()); |
626 |
checkCompletedNormally(f); |
627 |
}}; |
628 |
testInvokeOnPool(mainPool(), a); |
629 |
} |
630 |
|
631 |
/** |
632 |
* invoke task throws exception when task completes abnormally |
633 |
*/ |
634 |
public void testAbnormalInvoke() { |
635 |
ForkJoinTask a = new CheckedRecursiveAction() { |
636 |
protected void realCompute() { |
637 |
FailingCCF f = new LFCCF(null, 8); |
638 |
try { |
639 |
f.invoke(); |
640 |
shouldThrow(); |
641 |
} catch (FJException success) { |
642 |
checkCompletedAbnormally(f, success); |
643 |
} |
644 |
}}; |
645 |
testInvokeOnPool(mainPool(), a); |
646 |
} |
647 |
|
648 |
/** |
649 |
* quietlyInvoke task returns when task completes abnormally |
650 |
*/ |
651 |
public void testAbnormalQuietlyInvoke() { |
652 |
ForkJoinTask a = new CheckedRecursiveAction() { |
653 |
protected void realCompute() { |
654 |
FailingCCF f = new LFCCF(null, 8); |
655 |
f.quietlyInvoke(); |
656 |
assertTrue(f.getException() instanceof FJException); |
657 |
checkCompletedAbnormally(f, f.getException()); |
658 |
}}; |
659 |
testInvokeOnPool(mainPool(), a); |
660 |
} |
661 |
|
662 |
/** |
663 |
* join of a forked task throws exception when task completes abnormally |
664 |
*/ |
665 |
public void testAbnormalForkJoin() { |
666 |
ForkJoinTask a = new CheckedRecursiveAction() { |
667 |
protected void realCompute() { |
668 |
FailingCCF f = new LFCCF(null, 8); |
669 |
assertSame(f, f.fork()); |
670 |
try { |
671 |
f.join(); |
672 |
shouldThrow(); |
673 |
} catch (FJException success) { |
674 |
checkCompletedAbnormally(f, success); |
675 |
} |
676 |
}}; |
677 |
testInvokeOnPool(mainPool(), a); |
678 |
} |
679 |
|
680 |
/** |
681 |
* get of a forked task throws exception when task completes abnormally |
682 |
*/ |
683 |
public void testAbnormalForkGet() { |
684 |
ForkJoinTask a = new CheckedRecursiveAction() { |
685 |
protected void realCompute() throws Exception { |
686 |
FailingCCF f = new LFCCF(null, 8); |
687 |
assertSame(f, f.fork()); |
688 |
try { |
689 |
f.get(); |
690 |
shouldThrow(); |
691 |
} catch (ExecutionException success) { |
692 |
Throwable cause = success.getCause(); |
693 |
assertTrue(cause instanceof FJException); |
694 |
checkCompletedAbnormally(f, cause); |
695 |
} |
696 |
}}; |
697 |
testInvokeOnPool(mainPool(), a); |
698 |
} |
699 |
|
700 |
/** |
701 |
* timed get of a forked task throws exception when task completes abnormally |
702 |
*/ |
703 |
public void testAbnormalForkTimedGet() { |
704 |
ForkJoinTask a = new CheckedRecursiveAction() { |
705 |
protected void realCompute() throws Exception { |
706 |
FailingCCF f = new LFCCF(null, 8); |
707 |
assertSame(f, f.fork()); |
708 |
try { |
709 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
710 |
shouldThrow(); |
711 |
} catch (ExecutionException success) { |
712 |
Throwable cause = success.getCause(); |
713 |
assertTrue(cause instanceof FJException); |
714 |
checkCompletedAbnormally(f, cause); |
715 |
} |
716 |
}}; |
717 |
testInvokeOnPool(mainPool(), a); |
718 |
} |
719 |
|
720 |
/** |
721 |
* quietlyJoin of a forked task returns when task completes abnormally |
722 |
*/ |
723 |
public void testAbnormalForkQuietlyJoin() { |
724 |
ForkJoinTask a = new CheckedRecursiveAction() { |
725 |
protected void realCompute() { |
726 |
FailingCCF f = new LFCCF(null, 8); |
727 |
assertSame(f, f.fork()); |
728 |
f.quietlyJoin(); |
729 |
assertTrue(f.getException() instanceof FJException); |
730 |
checkCompletedAbnormally(f, f.getException()); |
731 |
}}; |
732 |
testInvokeOnPool(mainPool(), a); |
733 |
} |
734 |
|
735 |
/** |
736 |
* invoke task throws exception when task cancelled |
737 |
*/ |
738 |
public void testCancelledInvoke() { |
739 |
ForkJoinTask a = new CheckedRecursiveAction() { |
740 |
protected void realCompute() { |
741 |
CCF f = new LCCF(null, 8); |
742 |
assertTrue(f.cancel(true)); |
743 |
try { |
744 |
f.invoke(); |
745 |
shouldThrow(); |
746 |
} catch (CancellationException success) { |
747 |
checkCancelled(f); |
748 |
} |
749 |
}}; |
750 |
testInvokeOnPool(mainPool(), a); |
751 |
} |
752 |
|
753 |
/** |
754 |
* join of a forked task throws exception when task cancelled |
755 |
*/ |
756 |
public void testCancelledForkJoin() { |
757 |
ForkJoinTask a = new CheckedRecursiveAction() { |
758 |
protected void realCompute() { |
759 |
CCF f = new LCCF(null, 8); |
760 |
assertTrue(f.cancel(true)); |
761 |
assertSame(f, f.fork()); |
762 |
try { |
763 |
f.join(); |
764 |
shouldThrow(); |
765 |
} catch (CancellationException success) { |
766 |
checkCancelled(f); |
767 |
} |
768 |
}}; |
769 |
testInvokeOnPool(mainPool(), a); |
770 |
} |
771 |
|
772 |
/** |
773 |
* get of a forked task throws exception when task cancelled |
774 |
*/ |
775 |
public void testCancelledForkGet() { |
776 |
ForkJoinTask a = new CheckedRecursiveAction() { |
777 |
protected void realCompute() throws Exception { |
778 |
CCF f = new LCCF(null, 8); |
779 |
assertTrue(f.cancel(true)); |
780 |
assertSame(f, f.fork()); |
781 |
try { |
782 |
f.get(); |
783 |
shouldThrow(); |
784 |
} catch (CancellationException success) { |
785 |
checkCancelled(f); |
786 |
} |
787 |
}}; |
788 |
testInvokeOnPool(mainPool(), a); |
789 |
} |
790 |
|
791 |
/** |
792 |
* timed get of a forked task throws exception when task cancelled |
793 |
*/ |
794 |
public void testCancelledForkTimedGet() throws Exception { |
795 |
ForkJoinTask a = new CheckedRecursiveAction() { |
796 |
protected void realCompute() throws Exception { |
797 |
CCF f = new LCCF(null, 8); |
798 |
assertTrue(f.cancel(true)); |
799 |
assertSame(f, f.fork()); |
800 |
try { |
801 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
802 |
shouldThrow(); |
803 |
} catch (CancellationException success) { |
804 |
checkCancelled(f); |
805 |
} |
806 |
}}; |
807 |
testInvokeOnPool(mainPool(), a); |
808 |
} |
809 |
|
810 |
/** |
811 |
* quietlyJoin of a forked task returns when task cancelled |
812 |
*/ |
813 |
public void testCancelledForkQuietlyJoin() { |
814 |
ForkJoinTask a = new CheckedRecursiveAction() { |
815 |
protected void realCompute() { |
816 |
CCF f = new LCCF(null, 8); |
817 |
assertTrue(f.cancel(true)); |
818 |
assertSame(f, f.fork()); |
819 |
f.quietlyJoin(); |
820 |
checkCancelled(f); |
821 |
}}; |
822 |
testInvokeOnPool(mainPool(), a); |
823 |
} |
824 |
|
825 |
/** |
826 |
* getPool of executing task returns its pool |
827 |
*/ |
828 |
public void testGetPool() { |
829 |
final ForkJoinPool mainPool = mainPool(); |
830 |
ForkJoinTask a = new CheckedRecursiveAction() { |
831 |
protected void realCompute() { |
832 |
assertSame(mainPool, getPool()); |
833 |
}}; |
834 |
testInvokeOnPool(mainPool, a); |
835 |
} |
836 |
|
837 |
/** |
838 |
* getPool of non-FJ task returns null |
839 |
*/ |
840 |
public void testGetPool2() { |
841 |
ForkJoinTask a = new CheckedRecursiveAction() { |
842 |
protected void realCompute() { |
843 |
assertNull(getPool()); |
844 |
}}; |
845 |
assertNull(a.invoke()); |
846 |
} |
847 |
|
848 |
/** |
849 |
* inForkJoinPool of executing task returns true |
850 |
*/ |
851 |
public void testInForkJoinPool() { |
852 |
ForkJoinTask a = new CheckedRecursiveAction() { |
853 |
protected void realCompute() { |
854 |
assertTrue(inForkJoinPool()); |
855 |
}}; |
856 |
testInvokeOnPool(mainPool(), a); |
857 |
} |
858 |
|
859 |
/** |
860 |
* inForkJoinPool of non-FJ task returns false |
861 |
*/ |
862 |
public void testInForkJoinPool2() { |
863 |
ForkJoinTask a = new CheckedRecursiveAction() { |
864 |
protected void realCompute() { |
865 |
assertFalse(inForkJoinPool()); |
866 |
}}; |
867 |
assertNull(a.invoke()); |
868 |
} |
869 |
|
870 |
/** |
871 |
* setRawResult(null) succeeds |
872 |
*/ |
873 |
public void testSetRawResult() { |
874 |
ForkJoinTask a = new CheckedRecursiveAction() { |
875 |
protected void realCompute() { |
876 |
setRawResult(null); |
877 |
assertNull(getRawResult()); |
878 |
}}; |
879 |
assertNull(a.invoke()); |
880 |
} |
881 |
|
882 |
/** |
883 |
* invoke task throws exception after invoking completeExceptionally |
884 |
*/ |
885 |
public void testCompleteExceptionally2() { |
886 |
ForkJoinTask a = new CheckedRecursiveAction() { |
887 |
protected void realCompute() { |
888 |
CCF f = new LCCF(null, 8); |
889 |
f.completeExceptionally(new FJException()); |
890 |
try { |
891 |
f.invoke(); |
892 |
shouldThrow(); |
893 |
} catch (FJException success) { |
894 |
checkCompletedAbnormally(f, success); |
895 |
} |
896 |
}}; |
897 |
testInvokeOnPool(mainPool(), a); |
898 |
} |
899 |
|
900 |
/** |
901 |
* invokeAll(t1, t2) invokes all task arguments |
902 |
*/ |
903 |
public void testInvokeAll2() { |
904 |
ForkJoinTask a = new CheckedRecursiveAction() { |
905 |
protected void realCompute() { |
906 |
CCF f = new LCCF(null, 8); |
907 |
CCF g = new LCCF(null, 9); |
908 |
invokeAll(f, g); |
909 |
assertEquals(21, f.number); |
910 |
assertEquals(34, g.number); |
911 |
checkCompletedNormally(f); |
912 |
checkCompletedNormally(g); |
913 |
}}; |
914 |
testInvokeOnPool(mainPool(), a); |
915 |
} |
916 |
|
917 |
/** |
918 |
* invokeAll(tasks) with 1 argument invokes task |
919 |
*/ |
920 |
public void testInvokeAll1() { |
921 |
ForkJoinTask a = new CheckedRecursiveAction() { |
922 |
protected void realCompute() { |
923 |
CCF f = new LCCF(null, 8); |
924 |
invokeAll(f); |
925 |
checkCompletedNormally(f); |
926 |
assertEquals(21, f.number); |
927 |
}}; |
928 |
testInvokeOnPool(mainPool(), a); |
929 |
} |
930 |
|
931 |
/** |
932 |
* invokeAll(tasks) with > 2 argument invokes tasks |
933 |
*/ |
934 |
public void testInvokeAll3() { |
935 |
ForkJoinTask a = new CheckedRecursiveAction() { |
936 |
protected void realCompute() { |
937 |
CCF f = new LCCF(null, 8); |
938 |
CCF g = new LCCF(null, 9); |
939 |
CCF h = new LCCF(null, 7); |
940 |
invokeAll(f, g, h); |
941 |
assertEquals(21, f.number); |
942 |
assertEquals(34, g.number); |
943 |
assertEquals(13, h.number); |
944 |
checkCompletedNormally(f); |
945 |
checkCompletedNormally(g); |
946 |
checkCompletedNormally(h); |
947 |
}}; |
948 |
testInvokeOnPool(mainPool(), a); |
949 |
} |
950 |
|
951 |
/** |
952 |
* invokeAll(collection) invokes all tasks in the collection |
953 |
*/ |
954 |
public void testInvokeAllCollection() { |
955 |
ForkJoinTask a = new CheckedRecursiveAction() { |
956 |
protected void realCompute() { |
957 |
CCF f = new LCCF(null, 8); |
958 |
CCF g = new LCCF(null, 9); |
959 |
CCF h = new LCCF(null, 7); |
960 |
HashSet set = new HashSet(); |
961 |
set.add(f); |
962 |
set.add(g); |
963 |
set.add(h); |
964 |
invokeAll(set); |
965 |
assertEquals(21, f.number); |
966 |
assertEquals(34, g.number); |
967 |
assertEquals(13, h.number); |
968 |
checkCompletedNormally(f); |
969 |
checkCompletedNormally(g); |
970 |
checkCompletedNormally(h); |
971 |
}}; |
972 |
testInvokeOnPool(mainPool(), a); |
973 |
} |
974 |
|
975 |
/** |
976 |
* invokeAll(tasks) with any null task throws NPE |
977 |
*/ |
978 |
public void testInvokeAllNPE() { |
979 |
ForkJoinTask a = new CheckedRecursiveAction() { |
980 |
protected void realCompute() { |
981 |
CCF f = new LCCF(null, 8); |
982 |
CCF g = new LCCF(null, 9); |
983 |
CCF h = null; |
984 |
try { |
985 |
invokeAll(f, g, h); |
986 |
shouldThrow(); |
987 |
} catch (NullPointerException success) {} |
988 |
}}; |
989 |
testInvokeOnPool(mainPool(), a); |
990 |
} |
991 |
|
992 |
/** |
993 |
* invokeAll(t1, t2) throw exception if any task does |
994 |
*/ |
995 |
public void testAbnormalInvokeAll2() { |
996 |
ForkJoinTask a = new CheckedRecursiveAction() { |
997 |
protected void realCompute() { |
998 |
CCF f = new LCCF(null, 8); |
999 |
FailingCCF g = new LFCCF(null, 9); |
1000 |
try { |
1001 |
invokeAll(f, g); |
1002 |
shouldThrow(); |
1003 |
} catch (FJException success) { |
1004 |
checkCompletedAbnormally(g, success); |
1005 |
} |
1006 |
}}; |
1007 |
testInvokeOnPool(mainPool(), a); |
1008 |
} |
1009 |
|
1010 |
/** |
1011 |
* invokeAll(tasks) with 1 argument throws exception if task does |
1012 |
*/ |
1013 |
public void testAbnormalInvokeAll1() { |
1014 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1015 |
protected void realCompute() { |
1016 |
FailingCCF g = new LFCCF(null, 9); |
1017 |
try { |
1018 |
invokeAll(g); |
1019 |
shouldThrow(); |
1020 |
} catch (FJException success) { |
1021 |
checkCompletedAbnormally(g, success); |
1022 |
} |
1023 |
}}; |
1024 |
testInvokeOnPool(mainPool(), a); |
1025 |
} |
1026 |
|
1027 |
/** |
1028 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1029 |
*/ |
1030 |
public void testAbnormalInvokeAll3() { |
1031 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1032 |
protected void realCompute() { |
1033 |
CCF f = new LCCF(null, 8); |
1034 |
FailingCCF g = new LFCCF(null, 9); |
1035 |
CCF h = new LCCF(null, 7); |
1036 |
try { |
1037 |
invokeAll(f, g, h); |
1038 |
shouldThrow(); |
1039 |
} catch (FJException success) { |
1040 |
checkCompletedAbnormally(g, success); |
1041 |
} |
1042 |
}}; |
1043 |
testInvokeOnPool(mainPool(), a); |
1044 |
} |
1045 |
|
1046 |
/** |
1047 |
* invokeAll(collection) throws exception if any task does |
1048 |
*/ |
1049 |
public void testAbnormalInvokeAllCollection() { |
1050 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1051 |
protected void realCompute() { |
1052 |
FailingCCF f = new LFCCF(null, 8); |
1053 |
CCF g = new LCCF(null, 9); |
1054 |
CCF h = new LCCF(null, 7); |
1055 |
HashSet set = new HashSet(); |
1056 |
set.add(f); |
1057 |
set.add(g); |
1058 |
set.add(h); |
1059 |
try { |
1060 |
invokeAll(set); |
1061 |
shouldThrow(); |
1062 |
} catch (FJException success) { |
1063 |
checkCompletedAbnormally(f, success); |
1064 |
} |
1065 |
}}; |
1066 |
testInvokeOnPool(mainPool(), a); |
1067 |
} |
1068 |
|
1069 |
/** |
1070 |
* tryUnfork returns true for most recent unexecuted task, |
1071 |
* and suppresses execution |
1072 |
*/ |
1073 |
public void testTryUnfork() { |
1074 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1075 |
protected void realCompute() { |
1076 |
CCF g = new LCCF(null, 9); |
1077 |
assertSame(g, g.fork()); |
1078 |
CCF f = new LCCF(null, 8); |
1079 |
assertSame(f, f.fork()); |
1080 |
assertTrue(f.tryUnfork()); |
1081 |
helpQuiesce(); |
1082 |
checkNotDone(f); |
1083 |
checkCompletedNormally(g); |
1084 |
}}; |
1085 |
testInvokeOnPool(singletonPool(), a); |
1086 |
} |
1087 |
|
1088 |
/** |
1089 |
* getSurplusQueuedTaskCount returns > 0 when |
1090 |
* there are more tasks than threads |
1091 |
*/ |
1092 |
public void testGetSurplusQueuedTaskCount() { |
1093 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1094 |
protected void realCompute() { |
1095 |
CCF h = new LCCF(null, 7); |
1096 |
assertSame(h, h.fork()); |
1097 |
CCF g = new LCCF(null, 9); |
1098 |
assertSame(g, g.fork()); |
1099 |
CCF f = new LCCF(null, 8); |
1100 |
assertSame(f, f.fork()); |
1101 |
assertTrue(getSurplusQueuedTaskCount() > 0); |
1102 |
helpQuiesce(); |
1103 |
assertEquals(0, getSurplusQueuedTaskCount()); |
1104 |
checkCompletedNormally(f); |
1105 |
checkCompletedNormally(g); |
1106 |
checkCompletedNormally(h); |
1107 |
}}; |
1108 |
testInvokeOnPool(singletonPool(), a); |
1109 |
} |
1110 |
|
1111 |
/** |
1112 |
* peekNextLocalTask returns most recent unexecuted task. |
1113 |
*/ |
1114 |
public void testPeekNextLocalTask() { |
1115 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1116 |
protected void realCompute() { |
1117 |
CCF g = new LCCF(null, 9); |
1118 |
assertSame(g, g.fork()); |
1119 |
CCF f = new LCCF(null, 8); |
1120 |
assertSame(f, f.fork()); |
1121 |
assertSame(f, peekNextLocalTask()); |
1122 |
assertNull(f.join()); |
1123 |
checkCompletedNormally(f); |
1124 |
helpQuiesce(); |
1125 |
checkCompletedNormally(g); |
1126 |
}}; |
1127 |
testInvokeOnPool(singletonPool(), a); |
1128 |
} |
1129 |
|
1130 |
/** |
1131 |
* pollNextLocalTask returns most recent unexecuted task without |
1132 |
* executing it |
1133 |
*/ |
1134 |
public void testPollNextLocalTask() { |
1135 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1136 |
protected void realCompute() { |
1137 |
CCF g = new LCCF(null, 9); |
1138 |
assertSame(g, g.fork()); |
1139 |
CCF f = new LCCF(null, 8); |
1140 |
assertSame(f, f.fork()); |
1141 |
assertSame(f, pollNextLocalTask()); |
1142 |
helpQuiesce(); |
1143 |
checkNotDone(f); |
1144 |
assertEquals(34, g.number); |
1145 |
checkCompletedNormally(g); |
1146 |
}}; |
1147 |
testInvokeOnPool(singletonPool(), a); |
1148 |
} |
1149 |
|
1150 |
/** |
1151 |
* pollTask returns an unexecuted task without executing it |
1152 |
*/ |
1153 |
public void testPollTask() { |
1154 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1155 |
protected void realCompute() { |
1156 |
CCF g = new LCCF(null, 9); |
1157 |
assertSame(g, g.fork()); |
1158 |
CCF f = new LCCF(null, 8); |
1159 |
assertSame(f, f.fork()); |
1160 |
assertSame(f, pollTask()); |
1161 |
helpQuiesce(); |
1162 |
checkNotDone(f); |
1163 |
checkCompletedNormally(g); |
1164 |
}}; |
1165 |
testInvokeOnPool(singletonPool(), a); |
1166 |
} |
1167 |
|
1168 |
/** |
1169 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
1170 |
*/ |
1171 |
public void testPeekNextLocalTaskAsync() { |
1172 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1173 |
protected void realCompute() { |
1174 |
CCF g = new LCCF(null, 9); |
1175 |
assertSame(g, g.fork()); |
1176 |
CCF f = new LCCF(null, 8); |
1177 |
assertSame(f, f.fork()); |
1178 |
assertSame(g, peekNextLocalTask()); |
1179 |
assertNull(f.join()); |
1180 |
helpQuiesce(); |
1181 |
checkCompletedNormally(f); |
1182 |
assertEquals(34, g.number); |
1183 |
checkCompletedNormally(g); |
1184 |
}}; |
1185 |
testInvokeOnPool(asyncSingletonPool(), a); |
1186 |
} |
1187 |
|
1188 |
/** |
1189 |
* pollNextLocalTask returns least recent unexecuted task without |
1190 |
* executing it, in async mode |
1191 |
*/ |
1192 |
public void testPollNextLocalTaskAsync() { |
1193 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1194 |
protected void realCompute() { |
1195 |
CCF g = new LCCF(null, 9); |
1196 |
assertSame(g, g.fork()); |
1197 |
CCF f = new LCCF(null, 8); |
1198 |
assertSame(f, f.fork()); |
1199 |
assertSame(g, pollNextLocalTask()); |
1200 |
helpQuiesce(); |
1201 |
assertEquals(21, f.number); |
1202 |
checkCompletedNormally(f); |
1203 |
checkNotDone(g); |
1204 |
}}; |
1205 |
testInvokeOnPool(asyncSingletonPool(), a); |
1206 |
} |
1207 |
|
1208 |
/** |
1209 |
* pollTask returns an unexecuted task without executing it, in |
1210 |
* async mode |
1211 |
*/ |
1212 |
public void testPollTaskAsync() { |
1213 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1214 |
protected void realCompute() { |
1215 |
CCF g = new LCCF(null, 9); |
1216 |
assertSame(g, g.fork()); |
1217 |
CCF f = new LCCF(null, 8); |
1218 |
assertSame(f, f.fork()); |
1219 |
assertSame(g, pollTask()); |
1220 |
helpQuiesce(); |
1221 |
assertEquals(21, f.number); |
1222 |
checkCompletedNormally(f); |
1223 |
checkNotDone(g); |
1224 |
}}; |
1225 |
testInvokeOnPool(asyncSingletonPool(), a); |
1226 |
} |
1227 |
|
1228 |
// versions for singleton pools |
1229 |
|
1230 |
/** |
1231 |
* invoke returns when task completes normally. |
1232 |
* isCompletedAbnormally and isCancelled return false for normally |
1233 |
* completed tasks; getRawResult returns null. |
1234 |
*/ |
1235 |
public void testInvokeSingleton() { |
1236 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1237 |
protected void realCompute() { |
1238 |
CCF f = new LCCF(null, 8); |
1239 |
assertNull(f.invoke()); |
1240 |
assertEquals(21, f.number); |
1241 |
checkCompletedNormally(f); |
1242 |
}}; |
1243 |
testInvokeOnPool(singletonPool(), a); |
1244 |
} |
1245 |
|
1246 |
/** |
1247 |
* quietlyInvoke task returns when task completes normally. |
1248 |
* isCompletedAbnormally and isCancelled return false for normally |
1249 |
* completed tasks |
1250 |
*/ |
1251 |
public void testQuietlyInvokeSingleton() { |
1252 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1253 |
protected void realCompute() { |
1254 |
CCF f = new LCCF(null, 8); |
1255 |
f.quietlyInvoke(); |
1256 |
assertEquals(21, f.number); |
1257 |
checkCompletedNormally(f); |
1258 |
}}; |
1259 |
testInvokeOnPool(singletonPool(), a); |
1260 |
} |
1261 |
|
1262 |
/** |
1263 |
* join of a forked task returns when task completes |
1264 |
*/ |
1265 |
public void testForkJoinSingleton() { |
1266 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1267 |
protected void realCompute() { |
1268 |
CCF f = new LCCF(null, 8); |
1269 |
assertSame(f, f.fork()); |
1270 |
assertNull(f.join()); |
1271 |
assertEquals(21, f.number); |
1272 |
checkCompletedNormally(f); |
1273 |
}}; |
1274 |
testInvokeOnPool(singletonPool(), a); |
1275 |
} |
1276 |
|
1277 |
/** |
1278 |
* get of a forked task returns when task completes |
1279 |
*/ |
1280 |
public void testForkGetSingleton() { |
1281 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1282 |
protected void realCompute() throws Exception { |
1283 |
CCF f = new LCCF(null, 8); |
1284 |
assertSame(f, f.fork()); |
1285 |
assertNull(f.get()); |
1286 |
assertEquals(21, f.number); |
1287 |
checkCompletedNormally(f); |
1288 |
}}; |
1289 |
testInvokeOnPool(singletonPool(), a); |
1290 |
} |
1291 |
|
1292 |
/** |
1293 |
* timed get of a forked task returns when task completes |
1294 |
*/ |
1295 |
public void testForkTimedGetSingleton() { |
1296 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1297 |
protected void realCompute() throws Exception { |
1298 |
CCF f = new LCCF(null, 8); |
1299 |
assertSame(f, f.fork()); |
1300 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1301 |
assertEquals(21, f.number); |
1302 |
checkCompletedNormally(f); |
1303 |
}}; |
1304 |
testInvokeOnPool(singletonPool(), a); |
1305 |
} |
1306 |
|
1307 |
/** |
1308 |
* timed get with null time unit throws NPE |
1309 |
*/ |
1310 |
public void testForkTimedGetNPESingleton() { |
1311 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1312 |
protected void realCompute() throws Exception { |
1313 |
CCF f = new LCCF(null, 8); |
1314 |
assertSame(f, f.fork()); |
1315 |
try { |
1316 |
f.get(5L, null); |
1317 |
shouldThrow(); |
1318 |
} catch (NullPointerException success) {} |
1319 |
}}; |
1320 |
testInvokeOnPool(singletonPool(), a); |
1321 |
} |
1322 |
|
1323 |
/** |
1324 |
* quietlyJoin of a forked task returns when task completes |
1325 |
*/ |
1326 |
public void testForkQuietlyJoinSingleton() { |
1327 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1328 |
protected void realCompute() { |
1329 |
CCF f = new LCCF(null, 8); |
1330 |
assertSame(f, f.fork()); |
1331 |
f.quietlyJoin(); |
1332 |
assertEquals(21, f.number); |
1333 |
checkCompletedNormally(f); |
1334 |
}}; |
1335 |
testInvokeOnPool(singletonPool(), a); |
1336 |
} |
1337 |
|
1338 |
/** |
1339 |
* helpQuiesce returns when tasks are complete. |
1340 |
* getQueuedTaskCount returns 0 when quiescent |
1341 |
*/ |
1342 |
public void testForkHelpQuiesceSingleton() { |
1343 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1344 |
protected void realCompute() { |
1345 |
CCF f = new LCCF(null, 8); |
1346 |
assertSame(f, f.fork()); |
1347 |
helpQuiesce(); |
1348 |
assertEquals(0, getQueuedTaskCount()); |
1349 |
assertEquals(21, f.number); |
1350 |
checkCompletedNormally(f); |
1351 |
}}; |
1352 |
testInvokeOnPool(singletonPool(), a); |
1353 |
} |
1354 |
|
1355 |
/** |
1356 |
* invoke task throws exception when task completes abnormally |
1357 |
*/ |
1358 |
public void testAbnormalInvokeSingleton() { |
1359 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1360 |
protected void realCompute() { |
1361 |
FailingCCF f = new LFCCF(null, 8); |
1362 |
try { |
1363 |
f.invoke(); |
1364 |
shouldThrow(); |
1365 |
} catch (FJException success) { |
1366 |
checkCompletedAbnormally(f, success); |
1367 |
} |
1368 |
}}; |
1369 |
testInvokeOnPool(singletonPool(), a); |
1370 |
} |
1371 |
|
1372 |
/** |
1373 |
* quietlyInvoke task returns when task completes abnormally |
1374 |
*/ |
1375 |
public void testAbnormalQuietlyInvokeSingleton() { |
1376 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1377 |
protected void realCompute() { |
1378 |
FailingCCF f = new LFCCF(null, 8); |
1379 |
f.quietlyInvoke(); |
1380 |
assertTrue(f.getException() instanceof FJException); |
1381 |
checkCompletedAbnormally(f, f.getException()); |
1382 |
}}; |
1383 |
testInvokeOnPool(singletonPool(), a); |
1384 |
} |
1385 |
|
1386 |
/** |
1387 |
* join of a forked task throws exception when task completes abnormally |
1388 |
*/ |
1389 |
public void testAbnormalForkJoinSingleton() { |
1390 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1391 |
protected void realCompute() { |
1392 |
FailingCCF f = new LFCCF(null, 8); |
1393 |
assertSame(f, f.fork()); |
1394 |
try { |
1395 |
f.join(); |
1396 |
shouldThrow(); |
1397 |
} catch (FJException success) { |
1398 |
checkCompletedAbnormally(f, success); |
1399 |
} |
1400 |
}}; |
1401 |
testInvokeOnPool(singletonPool(), a); |
1402 |
} |
1403 |
|
1404 |
/** |
1405 |
* get of a forked task throws exception when task completes abnormally |
1406 |
*/ |
1407 |
public void testAbnormalForkGetSingleton() { |
1408 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1409 |
protected void realCompute() throws Exception { |
1410 |
FailingCCF f = new LFCCF(null, 8); |
1411 |
assertSame(f, f.fork()); |
1412 |
try { |
1413 |
f.get(); |
1414 |
shouldThrow(); |
1415 |
} catch (ExecutionException success) { |
1416 |
Throwable cause = success.getCause(); |
1417 |
assertTrue(cause instanceof FJException); |
1418 |
checkCompletedAbnormally(f, cause); |
1419 |
} |
1420 |
}}; |
1421 |
testInvokeOnPool(singletonPool(), a); |
1422 |
} |
1423 |
|
1424 |
/** |
1425 |
* timed get of a forked task throws exception when task completes abnormally |
1426 |
*/ |
1427 |
public void testAbnormalForkTimedGetSingleton() { |
1428 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1429 |
protected void realCompute() throws Exception { |
1430 |
FailingCCF f = new LFCCF(null, 8); |
1431 |
assertSame(f, f.fork()); |
1432 |
try { |
1433 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1434 |
shouldThrow(); |
1435 |
} catch (ExecutionException success) { |
1436 |
Throwable cause = success.getCause(); |
1437 |
assertTrue(cause instanceof FJException); |
1438 |
checkCompletedAbnormally(f, cause); |
1439 |
} |
1440 |
}}; |
1441 |
testInvokeOnPool(singletonPool(), a); |
1442 |
} |
1443 |
|
1444 |
/** |
1445 |
* quietlyJoin of a forked task returns when task completes abnormally |
1446 |
*/ |
1447 |
public void testAbnormalForkQuietlyJoinSingleton() { |
1448 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1449 |
protected void realCompute() { |
1450 |
FailingCCF f = new LFCCF(null, 8); |
1451 |
assertSame(f, f.fork()); |
1452 |
f.quietlyJoin(); |
1453 |
assertTrue(f.getException() instanceof FJException); |
1454 |
checkCompletedAbnormally(f, f.getException()); |
1455 |
}}; |
1456 |
testInvokeOnPool(singletonPool(), a); |
1457 |
} |
1458 |
|
1459 |
/** |
1460 |
* invoke task throws exception when task cancelled |
1461 |
*/ |
1462 |
public void testCancelledInvokeSingleton() { |
1463 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1464 |
protected void realCompute() { |
1465 |
CCF f = new LCCF(null, 8); |
1466 |
assertTrue(f.cancel(true)); |
1467 |
try { |
1468 |
f.invoke(); |
1469 |
shouldThrow(); |
1470 |
} catch (CancellationException success) { |
1471 |
checkCancelled(f); |
1472 |
} |
1473 |
}}; |
1474 |
testInvokeOnPool(singletonPool(), a); |
1475 |
} |
1476 |
|
1477 |
/** |
1478 |
* join of a forked task throws exception when task cancelled |
1479 |
*/ |
1480 |
public void testCancelledForkJoinSingleton() { |
1481 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1482 |
protected void realCompute() { |
1483 |
CCF f = new LCCF(null, 8); |
1484 |
assertTrue(f.cancel(true)); |
1485 |
assertSame(f, f.fork()); |
1486 |
try { |
1487 |
f.join(); |
1488 |
shouldThrow(); |
1489 |
} catch (CancellationException success) { |
1490 |
checkCancelled(f); |
1491 |
} |
1492 |
}}; |
1493 |
testInvokeOnPool(singletonPool(), a); |
1494 |
} |
1495 |
|
1496 |
/** |
1497 |
* get of a forked task throws exception when task cancelled |
1498 |
*/ |
1499 |
public void testCancelledForkGetSingleton() { |
1500 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1501 |
protected void realCompute() throws Exception { |
1502 |
CCF f = new LCCF(null, 8); |
1503 |
assertTrue(f.cancel(true)); |
1504 |
assertSame(f, f.fork()); |
1505 |
try { |
1506 |
f.get(); |
1507 |
shouldThrow(); |
1508 |
} catch (CancellationException success) { |
1509 |
checkCancelled(f); |
1510 |
} |
1511 |
}}; |
1512 |
testInvokeOnPool(singletonPool(), a); |
1513 |
} |
1514 |
|
1515 |
/** |
1516 |
* timed get of a forked task throws exception when task cancelled |
1517 |
*/ |
1518 |
public void testCancelledForkTimedGetSingleton() throws Exception { |
1519 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1520 |
protected void realCompute() throws Exception { |
1521 |
CCF f = new LCCF(null, 8); |
1522 |
assertTrue(f.cancel(true)); |
1523 |
assertSame(f, f.fork()); |
1524 |
try { |
1525 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
1526 |
shouldThrow(); |
1527 |
} catch (CancellationException success) { |
1528 |
checkCancelled(f); |
1529 |
} |
1530 |
}}; |
1531 |
testInvokeOnPool(singletonPool(), a); |
1532 |
} |
1533 |
|
1534 |
/** |
1535 |
* quietlyJoin of a forked task returns when task cancelled |
1536 |
*/ |
1537 |
public void testCancelledForkQuietlyJoinSingleton() { |
1538 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1539 |
protected void realCompute() { |
1540 |
CCF f = new LCCF(null, 8); |
1541 |
assertTrue(f.cancel(true)); |
1542 |
assertSame(f, f.fork()); |
1543 |
f.quietlyJoin(); |
1544 |
checkCancelled(f); |
1545 |
}}; |
1546 |
testInvokeOnPool(singletonPool(), a); |
1547 |
} |
1548 |
|
1549 |
/** |
1550 |
* invoke task throws exception after invoking completeExceptionally |
1551 |
*/ |
1552 |
public void testCompleteExceptionallySingleton() { |
1553 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1554 |
protected void realCompute() { |
1555 |
CCF f = new LCCF(null, 8); |
1556 |
f.completeExceptionally(new FJException()); |
1557 |
try { |
1558 |
f.invoke(); |
1559 |
shouldThrow(); |
1560 |
} catch (FJException success) { |
1561 |
checkCompletedAbnormally(f, success); |
1562 |
} |
1563 |
}}; |
1564 |
testInvokeOnPool(singletonPool(), a); |
1565 |
} |
1566 |
|
1567 |
/** |
1568 |
* invokeAll(t1, t2) invokes all task arguments |
1569 |
*/ |
1570 |
public void testInvokeAll2Singleton() { |
1571 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1572 |
protected void realCompute() { |
1573 |
CCF f = new LCCF(null, 8); |
1574 |
CCF g = new LCCF(null, 9); |
1575 |
invokeAll(f, g); |
1576 |
assertEquals(21, f.number); |
1577 |
assertEquals(34, g.number); |
1578 |
checkCompletedNormally(f); |
1579 |
checkCompletedNormally(g); |
1580 |
}}; |
1581 |
testInvokeOnPool(singletonPool(), a); |
1582 |
} |
1583 |
|
1584 |
/** |
1585 |
* invokeAll(tasks) with 1 argument invokes task |
1586 |
*/ |
1587 |
public void testInvokeAll1Singleton() { |
1588 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1589 |
protected void realCompute() { |
1590 |
CCF f = new LCCF(null, 8); |
1591 |
invokeAll(f); |
1592 |
checkCompletedNormally(f); |
1593 |
assertEquals(21, f.number); |
1594 |
}}; |
1595 |
testInvokeOnPool(singletonPool(), a); |
1596 |
} |
1597 |
|
1598 |
/** |
1599 |
* invokeAll(tasks) with > 2 argument invokes tasks |
1600 |
*/ |
1601 |
public void testInvokeAll3Singleton() { |
1602 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1603 |
protected void realCompute() { |
1604 |
CCF f = new LCCF(null, 8); |
1605 |
CCF g = new LCCF(null, 9); |
1606 |
CCF h = new LCCF(null, 7); |
1607 |
invokeAll(f, g, h); |
1608 |
assertEquals(21, f.number); |
1609 |
assertEquals(34, g.number); |
1610 |
assertEquals(13, h.number); |
1611 |
checkCompletedNormally(f); |
1612 |
checkCompletedNormally(g); |
1613 |
checkCompletedNormally(h); |
1614 |
}}; |
1615 |
testInvokeOnPool(singletonPool(), a); |
1616 |
} |
1617 |
|
1618 |
/** |
1619 |
* invokeAll(collection) invokes all tasks in the collection |
1620 |
*/ |
1621 |
public void testInvokeAllCollectionSingleton() { |
1622 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1623 |
protected void realCompute() { |
1624 |
CCF f = new LCCF(null, 8); |
1625 |
CCF g = new LCCF(null, 9); |
1626 |
CCF h = new LCCF(null, 7); |
1627 |
HashSet set = new HashSet(); |
1628 |
set.add(f); |
1629 |
set.add(g); |
1630 |
set.add(h); |
1631 |
invokeAll(set); |
1632 |
assertEquals(21, f.number); |
1633 |
assertEquals(34, g.number); |
1634 |
assertEquals(13, h.number); |
1635 |
checkCompletedNormally(f); |
1636 |
checkCompletedNormally(g); |
1637 |
checkCompletedNormally(h); |
1638 |
}}; |
1639 |
testInvokeOnPool(singletonPool(), a); |
1640 |
} |
1641 |
|
1642 |
/** |
1643 |
* invokeAll(tasks) with any null task throws NPE |
1644 |
*/ |
1645 |
public void testInvokeAllNPESingleton() { |
1646 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1647 |
protected void realCompute() { |
1648 |
CCF f = new LCCF(null, 8); |
1649 |
CCF g = new LCCF(null, 9); |
1650 |
CCF h = null; |
1651 |
try { |
1652 |
invokeAll(f, g, h); |
1653 |
shouldThrow(); |
1654 |
} catch (NullPointerException success) {} |
1655 |
}}; |
1656 |
testInvokeOnPool(singletonPool(), a); |
1657 |
} |
1658 |
|
1659 |
/** |
1660 |
* invokeAll(t1, t2) throw exception if any task does |
1661 |
*/ |
1662 |
public void testAbnormalInvokeAll2Singleton() { |
1663 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1664 |
protected void realCompute() { |
1665 |
CCF f = new LCCF(null, 8); |
1666 |
FailingCCF g = new LFCCF(null, 9); |
1667 |
try { |
1668 |
invokeAll(f, g); |
1669 |
shouldThrow(); |
1670 |
} catch (FJException success) { |
1671 |
checkCompletedAbnormally(g, success); |
1672 |
} |
1673 |
}}; |
1674 |
testInvokeOnPool(singletonPool(), a); |
1675 |
} |
1676 |
|
1677 |
/** |
1678 |
* invokeAll(tasks) with 1 argument throws exception if task does |
1679 |
*/ |
1680 |
public void testAbnormalInvokeAll1Singleton() { |
1681 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1682 |
protected void realCompute() { |
1683 |
FailingCCF g = new LFCCF(null, 9); |
1684 |
try { |
1685 |
invokeAll(g); |
1686 |
shouldThrow(); |
1687 |
} catch (FJException success) { |
1688 |
checkCompletedAbnormally(g, success); |
1689 |
} |
1690 |
}}; |
1691 |
testInvokeOnPool(singletonPool(), a); |
1692 |
} |
1693 |
|
1694 |
/** |
1695 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1696 |
*/ |
1697 |
public void testAbnormalInvokeAll3Singleton() { |
1698 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1699 |
protected void realCompute() { |
1700 |
CCF f = new LCCF(null, 8); |
1701 |
FailingCCF g = new LFCCF(null, 9); |
1702 |
CCF h = new LCCF(null, 7); |
1703 |
try { |
1704 |
invokeAll(f, g, h); |
1705 |
shouldThrow(); |
1706 |
} catch (FJException success) { |
1707 |
checkCompletedAbnormally(g, success); |
1708 |
} |
1709 |
}}; |
1710 |
testInvokeOnPool(singletonPool(), a); |
1711 |
} |
1712 |
|
1713 |
/** |
1714 |
* invokeAll(collection) throws exception if any task does |
1715 |
*/ |
1716 |
public void testAbnormalInvokeAllCollectionSingleton() { |
1717 |
ForkJoinTask a = new CheckedRecursiveAction() { |
1718 |
protected void realCompute() { |
1719 |
FailingCCF f = new LFCCF(null, 8); |
1720 |
CCF g = new LCCF(null, 9); |
1721 |
CCF h = new LCCF(null, 7); |
1722 |
HashSet set = new HashSet(); |
1723 |
set.add(f); |
1724 |
set.add(g); |
1725 |
set.add(h); |
1726 |
try { |
1727 |
invokeAll(set); |
1728 |
shouldThrow(); |
1729 |
} catch (FJException success) { |
1730 |
checkCompletedAbnormally(f, success); |
1731 |
} |
1732 |
}}; |
1733 |
testInvokeOnPool(singletonPool(), a); |
1734 |
} |
1735 |
|
1736 |
} |