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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.concurrent.CancellationException; |
9 |
+ |
import java.util.concurrent.SynchronousQueue; |
10 |
|
import java.util.concurrent.ExecutionException; |
11 |
|
import java.util.concurrent.ForkJoinPool; |
12 |
+ |
import java.util.concurrent.ForkJoinTask; |
13 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
14 |
|
import java.util.concurrent.RecursiveAction; |
15 |
|
import java.util.concurrent.TimeUnit; |
16 |
+ |
import java.util.concurrent.TimeoutException; |
17 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
|
import java.util.HashSet; |
19 |
|
|
20 |
|
public class RecursiveActionTest extends JSR166TestCase { |
43 |
|
|
44 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
45 |
|
try { |
46 |
< |
assertFalse(a.isDone()); |
43 |
< |
assertFalse(a.isCompletedNormally()); |
44 |
< |
assertFalse(a.isCompletedAbnormally()); |
45 |
< |
assertFalse(a.isCancelled()); |
46 |
< |
assertNull(a.getException()); |
46 |
> |
checkNotDone(a); |
47 |
|
|
48 |
|
assertNull(pool.invoke(a)); |
49 |
|
|
50 |
< |
assertTrue(a.isDone()); |
51 |
< |
assertTrue(a.isCompletedNormally()); |
52 |
< |
assertFalse(a.isCompletedAbnormally()); |
53 |
< |
assertFalse(a.isCancelled()); |
54 |
< |
assertNull(a.getException()); |
50 |
> |
checkCompletedNormally(a); |
51 |
|
} finally { |
52 |
|
joinPool(pool); |
53 |
|
} |
54 |
|
} |
55 |
|
|
56 |
< |
static final class FJException extends RuntimeException { |
57 |
< |
FJException() { super(); } |
56 |
> |
void checkNotDone(RecursiveAction a) { |
57 |
> |
assertFalse(a.isDone()); |
58 |
> |
assertFalse(a.isCompletedNormally()); |
59 |
> |
assertFalse(a.isCompletedAbnormally()); |
60 |
> |
assertFalse(a.isCancelled()); |
61 |
> |
assertNull(a.getException()); |
62 |
> |
assertNull(a.getRawResult()); |
63 |
> |
|
64 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
65 |
> |
Thread.currentThread().interrupt(); |
66 |
> |
try { |
67 |
> |
a.get(); |
68 |
> |
shouldThrow(); |
69 |
> |
} catch (InterruptedException success) { |
70 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
71 |
> |
|
72 |
> |
Thread.currentThread().interrupt(); |
73 |
> |
try { |
74 |
> |
a.get(5L, SECONDS); |
75 |
> |
shouldThrow(); |
76 |
> |
} catch (InterruptedException success) { |
77 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
78 |
> |
} |
79 |
> |
|
80 |
> |
try { |
81 |
> |
a.get(0L, SECONDS); |
82 |
> |
shouldThrow(); |
83 |
> |
} catch (TimeoutException success) { |
84 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
85 |
> |
} |
86 |
> |
|
87 |
> |
void checkCompletedNormally(RecursiveAction a) { |
88 |
> |
assertTrue(a.isDone()); |
89 |
> |
assertFalse(a.isCancelled()); |
90 |
> |
assertTrue(a.isCompletedNormally()); |
91 |
> |
assertFalse(a.isCompletedAbnormally()); |
92 |
> |
assertNull(a.getException()); |
93 |
> |
assertNull(a.getRawResult()); |
94 |
> |
assertNull(a.join()); |
95 |
> |
assertFalse(a.cancel(false)); |
96 |
> |
assertFalse(a.cancel(true)); |
97 |
> |
try { |
98 |
> |
assertNull(a.get()); |
99 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
100 |
> |
try { |
101 |
> |
assertNull(a.get(5L, SECONDS)); |
102 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
103 |
> |
} |
104 |
> |
|
105 |
> |
void checkCancelled(RecursiveAction a) { |
106 |
> |
assertTrue(a.isDone()); |
107 |
> |
assertTrue(a.isCancelled()); |
108 |
> |
assertFalse(a.isCompletedNormally()); |
109 |
> |
assertTrue(a.isCompletedAbnormally()); |
110 |
> |
assertTrue(a.getException() instanceof CancellationException); |
111 |
> |
assertNull(a.getRawResult()); |
112 |
> |
|
113 |
> |
try { |
114 |
> |
a.join(); |
115 |
> |
shouldThrow(); |
116 |
> |
} catch (CancellationException success) { |
117 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
> |
|
119 |
> |
try { |
120 |
> |
a.get(); |
121 |
> |
shouldThrow(); |
122 |
> |
} catch (CancellationException success) { |
123 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
> |
|
125 |
> |
try { |
126 |
> |
a.get(5L, SECONDS); |
127 |
> |
shouldThrow(); |
128 |
> |
} catch (CancellationException success) { |
129 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
> |
} |
131 |
> |
|
132 |
> |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
133 |
> |
assertTrue(a.isDone()); |
134 |
> |
assertFalse(a.isCancelled()); |
135 |
> |
assertFalse(a.isCompletedNormally()); |
136 |
> |
assertTrue(a.isCompletedAbnormally()); |
137 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
138 |
> |
assertNull(a.getRawResult()); |
139 |
> |
assertFalse(a.cancel(false)); |
140 |
> |
assertFalse(a.cancel(true)); |
141 |
> |
|
142 |
> |
try { |
143 |
> |
a.join(); |
144 |
> |
shouldThrow(); |
145 |
> |
} catch (Throwable expected) { |
146 |
> |
assertSame(expected.getClass(), t.getClass()); |
147 |
> |
} |
148 |
> |
|
149 |
> |
try { |
150 |
> |
a.get(); |
151 |
> |
shouldThrow(); |
152 |
> |
} catch (ExecutionException success) { |
153 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
154 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
> |
|
156 |
> |
try { |
157 |
> |
a.get(5L, SECONDS); |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (ExecutionException success) { |
160 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
161 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
162 |
> |
} |
163 |
> |
|
164 |
> |
public static final class FJException extends RuntimeException { |
165 |
> |
public FJException() { super(); } |
166 |
> |
public FJException(Throwable cause) { super(cause); } |
167 |
|
} |
168 |
|
|
169 |
|
// A simple recursive action for testing |
213 |
|
FibAction f = new FibAction(8); |
214 |
|
assertNull(f.invoke()); |
215 |
|
assertEquals(21, f.result); |
216 |
< |
assertTrue(f.isDone()); |
112 |
< |
assertFalse(f.isCancelled()); |
113 |
< |
assertFalse(f.isCompletedAbnormally()); |
114 |
< |
assertNull(f.getRawResult()); |
216 |
> |
checkCompletedNormally(f); |
217 |
|
}}; |
218 |
|
testInvokeOnPool(mainPool(), a); |
219 |
|
} |
229 |
|
FibAction f = new FibAction(8); |
230 |
|
f.quietlyInvoke(); |
231 |
|
assertEquals(21, f.result); |
232 |
< |
assertTrue(f.isDone()); |
131 |
< |
assertFalse(f.isCancelled()); |
132 |
< |
assertFalse(f.isCompletedAbnormally()); |
133 |
< |
assertNull(f.getRawResult()); |
232 |
> |
checkCompletedNormally(f); |
233 |
|
}}; |
234 |
|
testInvokeOnPool(mainPool(), a); |
235 |
|
} |
244 |
|
assertSame(f, f.fork()); |
245 |
|
assertNull(f.join()); |
246 |
|
assertEquals(21, f.result); |
247 |
< |
assertTrue(f.isDone()); |
149 |
< |
assertNull(f.getRawResult()); |
247 |
> |
checkCompletedNormally(f); |
248 |
|
}}; |
249 |
|
testInvokeOnPool(mainPool(), a); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
+ |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
254 |
+ |
*/ |
255 |
+ |
public void testJoinIgnoresInterrupts() { |
256 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
257 |
+ |
public void realCompute() { |
258 |
+ |
FibAction f = new FibAction(8); |
259 |
+ |
final Thread myself = Thread.currentThread(); |
260 |
+ |
|
261 |
+ |
// test join() |
262 |
+ |
assertSame(f, f.fork()); |
263 |
+ |
myself.interrupt(); |
264 |
+ |
assertTrue(myself.isInterrupted()); |
265 |
+ |
assertNull(f.join()); |
266 |
+ |
Thread.interrupted(); |
267 |
+ |
assertEquals(21, f.result); |
268 |
+ |
checkCompletedNormally(f); |
269 |
+ |
|
270 |
+ |
f.reinitialize(); |
271 |
+ |
f.cancel(true); |
272 |
+ |
assertSame(f, f.fork()); |
273 |
+ |
myself.interrupt(); |
274 |
+ |
assertTrue(myself.isInterrupted()); |
275 |
+ |
try { |
276 |
+ |
f.join(); |
277 |
+ |
shouldThrow(); |
278 |
+ |
} catch (CancellationException success) { |
279 |
+ |
Thread.interrupted(); |
280 |
+ |
checkCancelled(f); |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
f.reinitialize(); |
284 |
+ |
f.completeExceptionally(new FJException()); |
285 |
+ |
assertSame(f, f.fork()); |
286 |
+ |
myself.interrupt(); |
287 |
+ |
assertTrue(myself.isInterrupted()); |
288 |
+ |
try { |
289 |
+ |
f.join(); |
290 |
+ |
shouldThrow(); |
291 |
+ |
} catch (FJException success) { |
292 |
+ |
Thread.interrupted(); |
293 |
+ |
checkCompletedAbnormally(f, success); |
294 |
+ |
} |
295 |
+ |
|
296 |
+ |
// test quietlyJoin() |
297 |
+ |
f.reinitialize(); |
298 |
+ |
assertSame(f, f.fork()); |
299 |
+ |
myself.interrupt(); |
300 |
+ |
assertTrue(myself.isInterrupted()); |
301 |
+ |
f.quietlyJoin(); |
302 |
+ |
Thread.interrupted(); |
303 |
+ |
assertEquals(21, f.result); |
304 |
+ |
checkCompletedNormally(f); |
305 |
+ |
|
306 |
+ |
f.reinitialize(); |
307 |
+ |
f.cancel(true); |
308 |
+ |
assertSame(f, f.fork()); |
309 |
+ |
myself.interrupt(); |
310 |
+ |
assertTrue(myself.isInterrupted()); |
311 |
+ |
f.quietlyJoin(); |
312 |
+ |
Thread.interrupted(); |
313 |
+ |
checkCancelled(f); |
314 |
+ |
|
315 |
+ |
f.reinitialize(); |
316 |
+ |
f.completeExceptionally(new FJException()); |
317 |
+ |
assertSame(f, f.fork()); |
318 |
+ |
myself.interrupt(); |
319 |
+ |
assertTrue(myself.isInterrupted()); |
320 |
+ |
f.quietlyJoin(); |
321 |
+ |
Thread.interrupted(); |
322 |
+ |
checkCompletedAbnormally(f, f.getException()); |
323 |
+ |
}}; |
324 |
+ |
testInvokeOnPool(mainPool(), a); |
325 |
+ |
a.reinitialize(); |
326 |
+ |
testInvokeOnPool(singletonPool(), a); |
327 |
+ |
} |
328 |
+ |
|
329 |
+ |
/** |
330 |
+ |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
331 |
+ |
* succeeds in the presence of interrupts |
332 |
+ |
*/ |
333 |
+ |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
334 |
+ |
final SynchronousQueue<FibAction[]> sq = |
335 |
+ |
new SynchronousQueue<FibAction[]>(); |
336 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
337 |
+ |
public void realCompute() throws InterruptedException { |
338 |
+ |
FibAction[] fibActions = new FibAction[6]; |
339 |
+ |
for (int i = 0; i < fibActions.length; i++) |
340 |
+ |
fibActions[i] = new FibAction(8); |
341 |
+ |
|
342 |
+ |
fibActions[1].cancel(false); |
343 |
+ |
fibActions[2].completeExceptionally(new FJException()); |
344 |
+ |
fibActions[4].cancel(true); |
345 |
+ |
fibActions[5].completeExceptionally(new FJException()); |
346 |
+ |
|
347 |
+ |
for (int i = 0; i < fibActions.length; i++) |
348 |
+ |
fibActions[i].fork(); |
349 |
+ |
|
350 |
+ |
sq.put(fibActions); |
351 |
+ |
|
352 |
+ |
helpQuiesce(); |
353 |
+ |
}}; |
354 |
+ |
|
355 |
+ |
Runnable r = new CheckedRunnable() { |
356 |
+ |
public void realRun() throws InterruptedException { |
357 |
+ |
FibAction[] fibActions = sq.take(); |
358 |
+ |
FibAction f; |
359 |
+ |
final Thread myself = Thread.currentThread(); |
360 |
+ |
|
361 |
+ |
// test join() ------------ |
362 |
+ |
|
363 |
+ |
f = fibActions[0]; |
364 |
+ |
assertFalse(ForkJoinTask.inForkJoinPool()); |
365 |
+ |
myself.interrupt(); |
366 |
+ |
assertTrue(myself.isInterrupted()); |
367 |
+ |
assertNull(f.join()); |
368 |
+ |
assertTrue(Thread.interrupted()); |
369 |
+ |
assertEquals(21, f.result); |
370 |
+ |
checkCompletedNormally(f); |
371 |
+ |
|
372 |
+ |
f = fibActions[1]; |
373 |
+ |
myself.interrupt(); |
374 |
+ |
assertTrue(myself.isInterrupted()); |
375 |
+ |
try { |
376 |
+ |
f.join(); |
377 |
+ |
shouldThrow(); |
378 |
+ |
} catch (CancellationException success) { |
379 |
+ |
assertTrue(Thread.interrupted()); |
380 |
+ |
checkCancelled(f); |
381 |
+ |
} |
382 |
+ |
|
383 |
+ |
f = fibActions[2]; |
384 |
+ |
myself.interrupt(); |
385 |
+ |
assertTrue(myself.isInterrupted()); |
386 |
+ |
try { |
387 |
+ |
f.join(); |
388 |
+ |
shouldThrow(); |
389 |
+ |
} catch (FJException success) { |
390 |
+ |
assertTrue(Thread.interrupted()); |
391 |
+ |
checkCompletedAbnormally(f, success); |
392 |
+ |
} |
393 |
+ |
|
394 |
+ |
// test quietlyJoin() --------- |
395 |
+ |
|
396 |
+ |
f = fibActions[3]; |
397 |
+ |
myself.interrupt(); |
398 |
+ |
assertTrue(myself.isInterrupted()); |
399 |
+ |
f.quietlyJoin(); |
400 |
+ |
assertTrue(Thread.interrupted()); |
401 |
+ |
assertEquals(21, f.result); |
402 |
+ |
checkCompletedNormally(f); |
403 |
+ |
|
404 |
+ |
f = fibActions[4]; |
405 |
+ |
myself.interrupt(); |
406 |
+ |
assertTrue(myself.isInterrupted()); |
407 |
+ |
f.quietlyJoin(); |
408 |
+ |
assertTrue(Thread.interrupted()); |
409 |
+ |
checkCancelled(f); |
410 |
+ |
|
411 |
+ |
f = fibActions[5]; |
412 |
+ |
myself.interrupt(); |
413 |
+ |
assertTrue(myself.isInterrupted()); |
414 |
+ |
f.quietlyJoin(); |
415 |
+ |
assertTrue(Thread.interrupted()); |
416 |
+ |
assertTrue(f.getException() instanceof FJException); |
417 |
+ |
checkCompletedAbnormally(f, f.getException()); |
418 |
+ |
}}; |
419 |
+ |
|
420 |
+ |
Thread t; |
421 |
+ |
|
422 |
+ |
t = newStartedThread(r); |
423 |
+ |
testInvokeOnPool(mainPool(), a); |
424 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
425 |
+ |
|
426 |
+ |
a.reinitialize(); |
427 |
+ |
t = newStartedThread(r); |
428 |
+ |
testInvokeOnPool(singletonPool(), a); |
429 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
/** |
433 |
|
* get of a forked task returns when task completes |
434 |
|
*/ |
435 |
|
public void testForkGet() { |
439 |
|
assertSame(f, f.fork()); |
440 |
|
assertNull(f.get()); |
441 |
|
assertEquals(21, f.result); |
442 |
< |
assertTrue(f.isDone()); |
442 |
> |
checkCompletedNormally(f); |
443 |
|
}}; |
444 |
|
testInvokeOnPool(mainPool(), a); |
445 |
|
} |
452 |
|
public void realCompute() throws Exception { |
453 |
|
FibAction f = new FibAction(8); |
454 |
|
assertSame(f, f.fork()); |
455 |
< |
assertNull(f.get(5L, TimeUnit.SECONDS)); |
455 |
> |
assertNull(f.get(5L, SECONDS)); |
456 |
|
assertEquals(21, f.result); |
457 |
< |
assertTrue(f.isDone()); |
457 |
> |
checkCompletedNormally(f); |
458 |
|
}}; |
459 |
|
testInvokeOnPool(mainPool(), a); |
460 |
|
} |
485 |
|
assertSame(f, f.fork()); |
486 |
|
f.quietlyJoin(); |
487 |
|
assertEquals(21, f.result); |
488 |
< |
assertTrue(f.isDone()); |
488 |
> |
checkCompletedNormally(f); |
489 |
|
}}; |
490 |
|
testInvokeOnPool(mainPool(), a); |
491 |
|
} |
492 |
|
|
215 |
– |
|
493 |
|
/** |
494 |
|
* helpQuiesce returns when tasks are complete. |
495 |
|
* getQueuedTaskCount returns 0 when quiescent |
499 |
|
public void realCompute() { |
500 |
|
FibAction f = new FibAction(8); |
501 |
|
assertSame(f, f.fork()); |
502 |
< |
f.helpQuiesce(); |
502 |
> |
helpQuiesce(); |
503 |
|
assertEquals(21, f.result); |
227 |
– |
assertTrue(f.isDone()); |
504 |
|
assertEquals(0, getQueuedTaskCount()); |
505 |
+ |
checkCompletedNormally(f); |
506 |
|
}}; |
507 |
|
testInvokeOnPool(mainPool(), a); |
508 |
|
} |
509 |
|
|
233 |
– |
|
510 |
|
/** |
511 |
|
* invoke task throws exception when task completes abnormally |
512 |
|
*/ |
517 |
|
try { |
518 |
|
f.invoke(); |
519 |
|
shouldThrow(); |
520 |
< |
} catch (FJException success) {} |
520 |
> |
} catch (FJException success) { |
521 |
> |
checkCompletedAbnormally(f, success); |
522 |
> |
} |
523 |
|
}}; |
524 |
|
testInvokeOnPool(mainPool(), a); |
525 |
|
} |
532 |
|
public void realCompute() { |
533 |
|
FailingFibAction f = new FailingFibAction(8); |
534 |
|
f.quietlyInvoke(); |
535 |
< |
assertTrue(f.isDone()); |
535 |
> |
assertTrue(f.getException() instanceof FJException); |
536 |
> |
checkCompletedAbnormally(f, f.getException()); |
537 |
|
}}; |
538 |
|
testInvokeOnPool(mainPool(), a); |
539 |
|
} |
549 |
|
try { |
550 |
|
f.join(); |
551 |
|
shouldThrow(); |
552 |
< |
} catch (FJException success) {} |
552 |
> |
} catch (FJException success) { |
553 |
> |
checkCompletedAbnormally(f, success); |
554 |
> |
} |
555 |
|
}}; |
556 |
|
testInvokeOnPool(mainPool(), a); |
557 |
|
} |
567 |
|
try { |
568 |
|
f.get(); |
569 |
|
shouldThrow(); |
570 |
< |
} catch (ExecutionException success) {} |
570 |
> |
} catch (ExecutionException success) { |
571 |
> |
Throwable cause = success.getCause(); |
572 |
> |
assertTrue(cause instanceof FJException); |
573 |
> |
checkCompletedAbnormally(f, cause); |
574 |
> |
} |
575 |
|
}}; |
576 |
|
testInvokeOnPool(mainPool(), a); |
577 |
|
} |
587 |
|
try { |
588 |
|
f.get(5L, TimeUnit.SECONDS); |
589 |
|
shouldThrow(); |
590 |
< |
} catch (ExecutionException success) {} |
590 |
> |
} catch (ExecutionException success) { |
591 |
> |
Throwable cause = success.getCause(); |
592 |
> |
assertTrue(cause instanceof FJException); |
593 |
> |
checkCompletedAbnormally(f, cause); |
594 |
> |
} |
595 |
|
}}; |
596 |
|
testInvokeOnPool(mainPool(), a); |
597 |
|
} |
605 |
|
FailingFibAction f = new FailingFibAction(8); |
606 |
|
assertSame(f, f.fork()); |
607 |
|
f.quietlyJoin(); |
319 |
– |
assertTrue(f.isDone()); |
320 |
– |
assertTrue(f.isCompletedAbnormally()); |
608 |
|
assertTrue(f.getException() instanceof FJException); |
609 |
+ |
checkCompletedAbnormally(f, f.getException()); |
610 |
|
}}; |
611 |
|
testInvokeOnPool(mainPool(), a); |
612 |
|
} |
622 |
|
try { |
623 |
|
f.invoke(); |
624 |
|
shouldThrow(); |
625 |
< |
} catch (CancellationException success) {} |
625 |
> |
} catch (CancellationException success) { |
626 |
> |
checkCancelled(f); |
627 |
> |
} |
628 |
|
}}; |
629 |
|
testInvokeOnPool(mainPool(), a); |
630 |
|
} |
641 |
|
try { |
642 |
|
f.join(); |
643 |
|
shouldThrow(); |
644 |
< |
} catch (CancellationException success) {} |
644 |
> |
} catch (CancellationException success) { |
645 |
> |
checkCancelled(f); |
646 |
> |
} |
647 |
|
}}; |
648 |
|
testInvokeOnPool(mainPool(), a); |
649 |
|
} |
660 |
|
try { |
661 |
|
f.get(); |
662 |
|
shouldThrow(); |
663 |
< |
} catch (CancellationException success) {} |
663 |
> |
} catch (CancellationException success) { |
664 |
> |
checkCancelled(f); |
665 |
> |
} |
666 |
|
}}; |
667 |
|
testInvokeOnPool(mainPool(), a); |
668 |
|
} |
677 |
|
assertTrue(f.cancel(true)); |
678 |
|
assertSame(f, f.fork()); |
679 |
|
try { |
680 |
< |
f.get(5L, TimeUnit.SECONDS); |
680 |
> |
f.get(5L, SECONDS); |
681 |
|
shouldThrow(); |
682 |
< |
} catch (CancellationException success) {} |
682 |
> |
} catch (CancellationException success) { |
683 |
> |
checkCancelled(f); |
684 |
> |
} |
685 |
|
}}; |
686 |
|
testInvokeOnPool(mainPool(), a); |
687 |
|
} |
696 |
|
assertTrue(f.cancel(true)); |
697 |
|
assertSame(f, f.fork()); |
698 |
|
f.quietlyJoin(); |
699 |
< |
assertTrue(f.isDone()); |
404 |
< |
assertTrue(f.isCompletedAbnormally()); |
405 |
< |
assertTrue(f.isCancelled()); |
406 |
< |
assertTrue(f.getException() instanceof CancellationException); |
699 |
> |
checkCancelled(f); |
700 |
|
}}; |
701 |
|
testInvokeOnPool(mainPool(), a); |
702 |
|
} |
768 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
769 |
|
public void realCompute() { |
770 |
|
ForkJoinWorkerThread w = |
771 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
772 |
< |
int idx = w.getPoolIndex(); |
773 |
< |
assertTrue(idx >= 0); |
774 |
< |
assertTrue(idx < mainPool.getPoolSize()); |
771 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
772 |
> |
assertTrue(w.getPoolIndex() >= 0); |
773 |
> |
// pool size can shrink after assigning index, so cannot check |
774 |
> |
// assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
775 |
|
}}; |
776 |
|
testInvokeOnPool(mainPool, a); |
777 |
|
} |
778 |
|
|
486 |
– |
|
779 |
|
/** |
780 |
|
* setRawResult(null) succeeds |
781 |
|
*/ |
783 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
784 |
|
public void realCompute() { |
785 |
|
setRawResult(null); |
786 |
+ |
assertNull(getRawResult()); |
787 |
|
}}; |
788 |
|
assertNull(a.invoke()); |
789 |
|
} |
795 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
796 |
|
public void realCompute() { |
797 |
|
FibAction f = new FibAction(8); |
798 |
< |
assertNull(f.invoke()); |
799 |
< |
assertEquals(21, f.result); |
800 |
< |
assertTrue(f.isDone()); |
801 |
< |
assertFalse(f.isCancelled()); |
802 |
< |
assertFalse(f.isCompletedAbnormally()); |
803 |
< |
f.reinitialize(); |
804 |
< |
assertNull(f.invoke()); |
805 |
< |
assertEquals(21, f.result); |
798 |
> |
checkNotDone(f); |
799 |
> |
|
800 |
> |
for (int i = 0; i < 3; i++) { |
801 |
> |
assertNull(f.invoke()); |
802 |
> |
assertEquals(21, f.result); |
803 |
> |
checkCompletedNormally(f); |
804 |
> |
f.reinitialize(); |
805 |
> |
checkNotDone(f); |
806 |
> |
} |
807 |
|
}}; |
808 |
|
testInvokeOnPool(mainPool(), a); |
809 |
|
} |
819 |
|
try { |
820 |
|
f.invoke(); |
821 |
|
shouldThrow(); |
822 |
< |
} catch (FJException success) {} |
822 |
> |
} catch (FJException success) { |
823 |
> |
checkCompletedAbnormally(f, success); |
824 |
> |
} |
825 |
|
}}; |
826 |
|
testInvokeOnPool(mainPool(), a); |
827 |
|
} |
835 |
|
FibAction f = new FibAction(8); |
836 |
|
f.complete(null); |
837 |
|
assertNull(f.invoke()); |
542 |
– |
assertTrue(f.isDone()); |
838 |
|
assertEquals(0, f.result); |
839 |
+ |
checkCompletedNormally(f); |
840 |
|
}}; |
841 |
|
testInvokeOnPool(mainPool(), a); |
842 |
|
} |
850 |
|
FibAction f = new FibAction(8); |
851 |
|
FibAction g = new FibAction(9); |
852 |
|
invokeAll(f, g); |
853 |
< |
assertTrue(f.isDone()); |
853 |
> |
checkCompletedNormally(f); |
854 |
|
assertEquals(21, f.result); |
855 |
< |
assertTrue(g.isDone()); |
855 |
> |
checkCompletedNormally(g); |
856 |
|
assertEquals(34, g.result); |
857 |
|
}}; |
858 |
|
testInvokeOnPool(mainPool(), a); |
866 |
|
public void realCompute() { |
867 |
|
FibAction f = new FibAction(8); |
868 |
|
invokeAll(f); |
869 |
< |
assertTrue(f.isDone()); |
869 |
> |
checkCompletedNormally(f); |
870 |
|
assertEquals(21, f.result); |
871 |
|
}}; |
872 |
|
testInvokeOnPool(mainPool(), a); |
883 |
|
FibAction h = new FibAction(7); |
884 |
|
invokeAll(f, g, h); |
885 |
|
assertTrue(f.isDone()); |
590 |
– |
assertEquals(21, f.result); |
886 |
|
assertTrue(g.isDone()); |
592 |
– |
assertEquals(34, g.result); |
887 |
|
assertTrue(h.isDone()); |
888 |
+ |
checkCompletedNormally(f); |
889 |
+ |
assertEquals(21, f.result); |
890 |
+ |
checkCompletedNormally(g); |
891 |
+ |
assertEquals(34, g.result); |
892 |
+ |
checkCompletedNormally(g); |
893 |
|
assertEquals(13, h.result); |
894 |
|
}}; |
895 |
|
testInvokeOnPool(mainPool(), a); |
910 |
|
set.add(h); |
911 |
|
invokeAll(set); |
912 |
|
assertTrue(f.isDone()); |
614 |
– |
assertEquals(21, f.result); |
913 |
|
assertTrue(g.isDone()); |
616 |
– |
assertEquals(34, g.result); |
914 |
|
assertTrue(h.isDone()); |
915 |
+ |
checkCompletedNormally(f); |
916 |
+ |
assertEquals(21, f.result); |
917 |
+ |
checkCompletedNormally(g); |
918 |
+ |
assertEquals(34, g.result); |
919 |
+ |
checkCompletedNormally(g); |
920 |
|
assertEquals(13, h.result); |
921 |
|
}}; |
922 |
|
testInvokeOnPool(mainPool(), a); |
923 |
|
} |
924 |
|
|
623 |
– |
|
925 |
|
/** |
926 |
|
* invokeAll(tasks) with any null task throws NPE |
927 |
|
*/ |
950 |
|
try { |
951 |
|
invokeAll(f, g); |
952 |
|
shouldThrow(); |
953 |
< |
} catch (FJException success) {} |
953 |
> |
} catch (FJException success) { |
954 |
> |
checkCompletedAbnormally(g, success); |
955 |
> |
} |
956 |
|
}}; |
957 |
|
testInvokeOnPool(mainPool(), a); |
958 |
|
} |
967 |
|
try { |
968 |
|
invokeAll(g); |
969 |
|
shouldThrow(); |
970 |
< |
} catch (FJException success) {} |
970 |
> |
} catch (FJException success) { |
971 |
> |
checkCompletedAbnormally(g, success); |
972 |
> |
} |
973 |
|
}}; |
974 |
|
testInvokeOnPool(mainPool(), a); |
975 |
|
} |
986 |
|
try { |
987 |
|
invokeAll(f, g, h); |
988 |
|
shouldThrow(); |
989 |
< |
} catch (FJException success) {} |
989 |
> |
} catch (FJException success) { |
990 |
> |
checkCompletedAbnormally(g, success); |
991 |
> |
} |
992 |
|
}}; |
993 |
|
testInvokeOnPool(mainPool(), a); |
994 |
|
} |
1009 |
|
try { |
1010 |
|
invokeAll(set); |
1011 |
|
shouldThrow(); |
1012 |
< |
} catch (FJException success) {} |
1012 |
> |
} catch (FJException success) { |
1013 |
> |
checkCompletedAbnormally(f, success); |
1014 |
> |
} |
1015 |
|
}}; |
1016 |
|
testInvokeOnPool(mainPool(), a); |
1017 |
|
} |
1029 |
|
assertSame(f, f.fork()); |
1030 |
|
assertTrue(f.tryUnfork()); |
1031 |
|
helpQuiesce(); |
1032 |
< |
assertFalse(f.isDone()); |
1033 |
< |
assertTrue(g.isDone()); |
1032 |
> |
checkNotDone(f); |
1033 |
> |
checkCompletedNormally(g); |
1034 |
|
}}; |
1035 |
|
testInvokeOnPool(singletonPool(), a); |
1036 |
|
} |
1050 |
|
assertSame(f, f.fork()); |
1051 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1052 |
|
helpQuiesce(); |
1053 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
1054 |
+ |
checkCompletedNormally(f); |
1055 |
+ |
checkCompletedNormally(g); |
1056 |
+ |
checkCompletedNormally(h); |
1057 |
|
}}; |
1058 |
|
testInvokeOnPool(singletonPool(), a); |
1059 |
|
} |
1070 |
|
assertSame(f, f.fork()); |
1071 |
|
assertSame(f, peekNextLocalTask()); |
1072 |
|
assertNull(f.join()); |
1073 |
< |
assertTrue(f.isDone()); |
1073 |
> |
checkCompletedNormally(f); |
1074 |
|
helpQuiesce(); |
1075 |
+ |
checkCompletedNormally(f); |
1076 |
+ |
checkCompletedNormally(g); |
1077 |
|
}}; |
1078 |
|
testInvokeOnPool(singletonPool(), a); |
1079 |
|
} |
1091 |
|
assertSame(f, f.fork()); |
1092 |
|
assertSame(f, pollNextLocalTask()); |
1093 |
|
helpQuiesce(); |
1094 |
< |
assertFalse(f.isDone()); |
1094 |
> |
checkNotDone(f); |
1095 |
> |
checkCompletedNormally(g); |
1096 |
|
}}; |
1097 |
|
testInvokeOnPool(singletonPool(), a); |
1098 |
|
} |
1099 |
|
|
1100 |
|
/** |
1101 |
< |
* pollTask returns an unexecuted task |
786 |
< |
* without executing it |
1101 |
> |
* pollTask returns an unexecuted task without executing it |
1102 |
|
*/ |
1103 |
|
public void testPollTask() { |
1104 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1109 |
|
assertSame(f, f.fork()); |
1110 |
|
assertSame(f, pollTask()); |
1111 |
|
helpQuiesce(); |
1112 |
< |
assertFalse(f.isDone()); |
1113 |
< |
assertTrue(g.isDone()); |
1112 |
> |
checkNotDone(f); |
1113 |
> |
checkCompletedNormally(g); |
1114 |
|
}}; |
1115 |
|
testInvokeOnPool(singletonPool(), a); |
1116 |
|
} |
1128 |
|
assertSame(g, peekNextLocalTask()); |
1129 |
|
assertNull(f.join()); |
1130 |
|
helpQuiesce(); |
1131 |
< |
assertTrue(f.isDone()); |
1131 |
> |
checkCompletedNormally(f); |
1132 |
> |
checkCompletedNormally(g); |
1133 |
|
}}; |
1134 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1135 |
|
} |
1136 |
|
|
1137 |
|
/** |
1138 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1139 |
< |
* without executing it, in async mode |
1138 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1139 |
> |
* executing it, in async mode |
1140 |
|
*/ |
1141 |
|
public void testPollNextLocalTaskAsync() { |
1142 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1147 |
|
assertSame(f, f.fork()); |
1148 |
|
assertSame(g, pollNextLocalTask()); |
1149 |
|
helpQuiesce(); |
1150 |
< |
assertTrue(f.isDone()); |
1151 |
< |
assertFalse(g.isDone()); |
1150 |
> |
checkCompletedNormally(f); |
1151 |
> |
checkNotDone(g); |
1152 |
|
}}; |
1153 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1154 |
|
} |
1155 |
|
|
1156 |
|
/** |
1157 |
< |
* pollTask returns an unexecuted task |
1158 |
< |
* without executing it, in async mode |
1157 |
> |
* pollTask returns an unexecuted task without executing it, in |
1158 |
> |
* async mode |
1159 |
|
*/ |
1160 |
|
public void testPollTaskAsync() { |
1161 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1166 |
|
assertSame(f, f.fork()); |
1167 |
|
assertSame(g, pollTask()); |
1168 |
|
helpQuiesce(); |
1169 |
< |
assertTrue(f.isDone()); |
1170 |
< |
assertFalse(g.isDone()); |
1169 |
> |
checkCompletedNormally(f); |
1170 |
> |
checkNotDone(g); |
1171 |
|
}}; |
1172 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1173 |
|
} |