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.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.TimeUnit; |
17 |
+ |
import java.util.concurrent.TimeoutException; |
18 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
+ |
import java.util.Arrays; |
20 |
|
import java.util.HashSet; |
21 |
|
|
22 |
|
public class RecursiveActionTest extends JSR166TestCase { |
45 |
|
|
46 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
47 |
|
try { |
48 |
< |
assertFalse(a.isDone()); |
43 |
< |
assertFalse(a.isCompletedNormally()); |
44 |
< |
assertFalse(a.isCompletedAbnormally()); |
45 |
< |
assertFalse(a.isCancelled()); |
46 |
< |
assertNull(a.getException()); |
48 |
> |
checkNotDone(a); |
49 |
|
|
50 |
|
assertNull(pool.invoke(a)); |
51 |
|
|
52 |
< |
assertTrue(a.isDone()); |
51 |
< |
assertTrue(a.isCompletedNormally()); |
52 |
< |
assertFalse(a.isCompletedAbnormally()); |
53 |
< |
assertFalse(a.isCancelled()); |
54 |
< |
assertNull(a.getException()); |
52 |
> |
checkCompletedNormally(a); |
53 |
|
} finally { |
54 |
|
joinPool(pool); |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
< |
static final class FJException extends RuntimeException { |
59 |
< |
FJException() { super(); } |
58 |
> |
void checkNotDone(RecursiveAction a) { |
59 |
> |
assertFalse(a.isDone()); |
60 |
> |
assertFalse(a.isCompletedNormally()); |
61 |
> |
assertFalse(a.isCompletedAbnormally()); |
62 |
> |
assertFalse(a.isCancelled()); |
63 |
> |
assertNull(a.getException()); |
64 |
> |
assertNull(a.getRawResult()); |
65 |
> |
|
66 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
67 |
> |
Thread.currentThread().interrupt(); |
68 |
> |
try { |
69 |
> |
a.get(); |
70 |
> |
shouldThrow(); |
71 |
> |
} catch (InterruptedException success) { |
72 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
73 |
> |
|
74 |
> |
Thread.currentThread().interrupt(); |
75 |
> |
try { |
76 |
> |
a.get(5L, SECONDS); |
77 |
> |
shouldThrow(); |
78 |
> |
} catch (InterruptedException success) { |
79 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
80 |
> |
} |
81 |
> |
|
82 |
> |
try { |
83 |
> |
a.get(0L, SECONDS); |
84 |
> |
shouldThrow(); |
85 |
> |
} catch (TimeoutException success) { |
86 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
87 |
> |
} |
88 |
> |
|
89 |
> |
void checkCompletedNormally(RecursiveAction a) { |
90 |
> |
assertTrue(a.isDone()); |
91 |
> |
assertFalse(a.isCancelled()); |
92 |
> |
assertTrue(a.isCompletedNormally()); |
93 |
> |
assertFalse(a.isCompletedAbnormally()); |
94 |
> |
assertNull(a.getException()); |
95 |
> |
assertNull(a.getRawResult()); |
96 |
> |
assertNull(a.join()); |
97 |
> |
assertFalse(a.cancel(false)); |
98 |
> |
assertFalse(a.cancel(true)); |
99 |
> |
try { |
100 |
> |
assertNull(a.get()); |
101 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
102 |
> |
try { |
103 |
> |
assertNull(a.get(5L, SECONDS)); |
104 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
105 |
> |
} |
106 |
> |
|
107 |
> |
void checkCancelled(RecursiveAction a) { |
108 |
> |
assertTrue(a.isDone()); |
109 |
> |
assertTrue(a.isCancelled()); |
110 |
> |
assertFalse(a.isCompletedNormally()); |
111 |
> |
assertTrue(a.isCompletedAbnormally()); |
112 |
> |
assertTrue(a.getException() instanceof CancellationException); |
113 |
> |
assertNull(a.getRawResult()); |
114 |
> |
|
115 |
> |
try { |
116 |
> |
a.join(); |
117 |
> |
shouldThrow(); |
118 |
> |
} catch (CancellationException success) { |
119 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
120 |
> |
|
121 |
> |
try { |
122 |
> |
a.get(); |
123 |
> |
shouldThrow(); |
124 |
> |
} catch (CancellationException success) { |
125 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
126 |
> |
|
127 |
> |
try { |
128 |
> |
a.get(5L, SECONDS); |
129 |
> |
shouldThrow(); |
130 |
> |
} catch (CancellationException success) { |
131 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
132 |
> |
} |
133 |
> |
|
134 |
> |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
135 |
> |
assertTrue(a.isDone()); |
136 |
> |
assertFalse(a.isCancelled()); |
137 |
> |
assertFalse(a.isCompletedNormally()); |
138 |
> |
assertTrue(a.isCompletedAbnormally()); |
139 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
140 |
> |
assertNull(a.getRawResult()); |
141 |
> |
assertFalse(a.cancel(false)); |
142 |
> |
assertFalse(a.cancel(true)); |
143 |
> |
|
144 |
> |
try { |
145 |
> |
a.join(); |
146 |
> |
shouldThrow(); |
147 |
> |
} catch (Throwable expected) { |
148 |
> |
assertSame(expected.getClass(), t.getClass()); |
149 |
> |
} |
150 |
> |
|
151 |
> |
try { |
152 |
> |
a.get(); |
153 |
> |
shouldThrow(); |
154 |
> |
} catch (ExecutionException success) { |
155 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
156 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
157 |
> |
|
158 |
> |
try { |
159 |
> |
a.get(5L, SECONDS); |
160 |
> |
shouldThrow(); |
161 |
> |
} catch (ExecutionException success) { |
162 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
163 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
> |
} |
165 |
> |
|
166 |
> |
public static final class FJException extends RuntimeException { |
167 |
> |
public FJException() { super(); } |
168 |
> |
public FJException(Throwable cause) { super(cause); } |
169 |
|
} |
170 |
|
|
171 |
|
// A simple recursive action for testing |
215 |
|
FibAction f = new FibAction(8); |
216 |
|
assertNull(f.invoke()); |
217 |
|
assertEquals(21, f.result); |
218 |
< |
assertTrue(f.isDone()); |
112 |
< |
assertFalse(f.isCancelled()); |
113 |
< |
assertFalse(f.isCompletedAbnormally()); |
114 |
< |
assertNull(f.getRawResult()); |
218 |
> |
checkCompletedNormally(f); |
219 |
|
}}; |
220 |
|
testInvokeOnPool(mainPool(), a); |
221 |
|
} |
231 |
|
FibAction f = new FibAction(8); |
232 |
|
f.quietlyInvoke(); |
233 |
|
assertEquals(21, f.result); |
234 |
< |
assertTrue(f.isDone()); |
131 |
< |
assertFalse(f.isCancelled()); |
132 |
< |
assertFalse(f.isCompletedAbnormally()); |
133 |
< |
assertNull(f.getRawResult()); |
234 |
> |
checkCompletedNormally(f); |
235 |
|
}}; |
236 |
|
testInvokeOnPool(mainPool(), a); |
237 |
|
} |
246 |
|
assertSame(f, f.fork()); |
247 |
|
assertNull(f.join()); |
248 |
|
assertEquals(21, f.result); |
249 |
< |
assertTrue(f.isDone()); |
250 |
< |
assertNull(f.getRawResult()); |
249 |
> |
checkCompletedNormally(f); |
250 |
> |
}}; |
251 |
> |
testInvokeOnPool(mainPool(), a); |
252 |
> |
} |
253 |
> |
|
254 |
> |
/** |
255 |
> |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
256 |
> |
*/ |
257 |
> |
public void testJoinIgnoresInterrupts() { |
258 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
259 |
> |
public void realCompute() { |
260 |
> |
FibAction f = new FibAction(8); |
261 |
> |
final Thread myself = Thread.currentThread(); |
262 |
> |
|
263 |
> |
// test join() |
264 |
> |
assertSame(f, f.fork()); |
265 |
> |
myself.interrupt(); |
266 |
> |
assertTrue(myself.isInterrupted()); |
267 |
> |
assertNull(f.join()); |
268 |
> |
Thread.interrupted(); |
269 |
> |
assertEquals(21, f.result); |
270 |
> |
checkCompletedNormally(f); |
271 |
> |
|
272 |
> |
f = new FibAction(8); |
273 |
> |
f.cancel(true); |
274 |
> |
assertSame(f, f.fork()); |
275 |
> |
myself.interrupt(); |
276 |
> |
assertTrue(myself.isInterrupted()); |
277 |
> |
try { |
278 |
> |
f.join(); |
279 |
> |
shouldThrow(); |
280 |
> |
} catch (CancellationException success) { |
281 |
> |
Thread.interrupted(); |
282 |
> |
checkCancelled(f); |
283 |
> |
} |
284 |
> |
|
285 |
> |
f = new FibAction(8); |
286 |
> |
f.completeExceptionally(new FJException()); |
287 |
> |
assertSame(f, f.fork()); |
288 |
> |
myself.interrupt(); |
289 |
> |
assertTrue(myself.isInterrupted()); |
290 |
> |
try { |
291 |
> |
f.join(); |
292 |
> |
shouldThrow(); |
293 |
> |
} catch (FJException success) { |
294 |
> |
Thread.interrupted(); |
295 |
> |
checkCompletedAbnormally(f, success); |
296 |
> |
} |
297 |
> |
|
298 |
> |
// test quietlyJoin() |
299 |
> |
f = new FibAction(8); |
300 |
> |
assertSame(f, f.fork()); |
301 |
> |
myself.interrupt(); |
302 |
> |
assertTrue(myself.isInterrupted()); |
303 |
> |
f.quietlyJoin(); |
304 |
> |
Thread.interrupted(); |
305 |
> |
assertEquals(21, f.result); |
306 |
> |
checkCompletedNormally(f); |
307 |
> |
|
308 |
> |
f = new FibAction(8); |
309 |
> |
f.cancel(true); |
310 |
> |
assertSame(f, f.fork()); |
311 |
> |
myself.interrupt(); |
312 |
> |
assertTrue(myself.isInterrupted()); |
313 |
> |
f.quietlyJoin(); |
314 |
> |
Thread.interrupted(); |
315 |
> |
checkCancelled(f); |
316 |
> |
|
317 |
> |
f = new FibAction(8); |
318 |
> |
f.completeExceptionally(new FJException()); |
319 |
> |
assertSame(f, f.fork()); |
320 |
> |
myself.interrupt(); |
321 |
> |
assertTrue(myself.isInterrupted()); |
322 |
> |
f.quietlyJoin(); |
323 |
> |
Thread.interrupted(); |
324 |
> |
checkCompletedAbnormally(f, f.getException()); |
325 |
> |
}}; |
326 |
> |
testInvokeOnPool(mainPool(), a); |
327 |
> |
a.reinitialize(); |
328 |
> |
testInvokeOnPool(singletonPool(), a); |
329 |
> |
} |
330 |
> |
|
331 |
> |
/** |
332 |
> |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
333 |
> |
* succeeds in the presence of interrupts |
334 |
> |
*/ |
335 |
> |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
336 |
> |
final SynchronousQueue<FibAction[]> sq = |
337 |
> |
new SynchronousQueue<FibAction[]>(); |
338 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
339 |
> |
public void realCompute() throws InterruptedException { |
340 |
> |
FibAction[] fibActions = new FibAction[6]; |
341 |
> |
for (int i = 0; i < fibActions.length; i++) |
342 |
> |
fibActions[i] = new FibAction(8); |
343 |
> |
|
344 |
> |
fibActions[1].cancel(false); |
345 |
> |
fibActions[2].completeExceptionally(new FJException()); |
346 |
> |
fibActions[4].cancel(true); |
347 |
> |
fibActions[5].completeExceptionally(new FJException()); |
348 |
> |
|
349 |
> |
for (int i = 0; i < fibActions.length; i++) |
350 |
> |
fibActions[i].fork(); |
351 |
> |
|
352 |
> |
sq.put(fibActions); |
353 |
> |
|
354 |
> |
helpQuiesce(); |
355 |
> |
}}; |
356 |
> |
|
357 |
> |
Runnable r = new CheckedRunnable() { |
358 |
> |
public void realRun() throws InterruptedException { |
359 |
> |
FibAction[] fibActions = sq.take(); |
360 |
> |
FibAction f; |
361 |
> |
final Thread myself = Thread.currentThread(); |
362 |
> |
|
363 |
> |
// test join() ------------ |
364 |
> |
|
365 |
> |
f = fibActions[0]; |
366 |
> |
assertFalse(ForkJoinTask.inForkJoinPool()); |
367 |
> |
myself.interrupt(); |
368 |
> |
assertTrue(myself.isInterrupted()); |
369 |
> |
assertNull(f.join()); |
370 |
> |
assertTrue(Thread.interrupted()); |
371 |
> |
assertEquals(21, f.result); |
372 |
> |
checkCompletedNormally(f); |
373 |
> |
|
374 |
> |
f = fibActions[1]; |
375 |
> |
myself.interrupt(); |
376 |
> |
assertTrue(myself.isInterrupted()); |
377 |
> |
try { |
378 |
> |
f.join(); |
379 |
> |
shouldThrow(); |
380 |
> |
} catch (CancellationException success) { |
381 |
> |
assertTrue(Thread.interrupted()); |
382 |
> |
checkCancelled(f); |
383 |
> |
} |
384 |
> |
|
385 |
> |
f = fibActions[2]; |
386 |
> |
myself.interrupt(); |
387 |
> |
assertTrue(myself.isInterrupted()); |
388 |
> |
try { |
389 |
> |
f.join(); |
390 |
> |
shouldThrow(); |
391 |
> |
} catch (FJException success) { |
392 |
> |
assertTrue(Thread.interrupted()); |
393 |
> |
checkCompletedAbnormally(f, success); |
394 |
> |
} |
395 |
> |
|
396 |
> |
// test quietlyJoin() --------- |
397 |
> |
|
398 |
> |
f = fibActions[3]; |
399 |
> |
myself.interrupt(); |
400 |
> |
assertTrue(myself.isInterrupted()); |
401 |
> |
f.quietlyJoin(); |
402 |
> |
assertTrue(Thread.interrupted()); |
403 |
> |
assertEquals(21, f.result); |
404 |
> |
checkCompletedNormally(f); |
405 |
> |
|
406 |
> |
f = fibActions[4]; |
407 |
> |
myself.interrupt(); |
408 |
> |
assertTrue(myself.isInterrupted()); |
409 |
> |
f.quietlyJoin(); |
410 |
> |
assertTrue(Thread.interrupted()); |
411 |
> |
checkCancelled(f); |
412 |
> |
|
413 |
> |
f = fibActions[5]; |
414 |
> |
myself.interrupt(); |
415 |
> |
assertTrue(myself.isInterrupted()); |
416 |
> |
f.quietlyJoin(); |
417 |
> |
assertTrue(Thread.interrupted()); |
418 |
> |
assertTrue(f.getException() instanceof FJException); |
419 |
> |
checkCompletedAbnormally(f, f.getException()); |
420 |
|
}}; |
421 |
+ |
|
422 |
+ |
Thread t; |
423 |
+ |
|
424 |
+ |
t = newStartedThread(r); |
425 |
|
testInvokeOnPool(mainPool(), a); |
426 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
427 |
+ |
|
428 |
+ |
a.reinitialize(); |
429 |
+ |
t = newStartedThread(r); |
430 |
+ |
testInvokeOnPool(singletonPool(), a); |
431 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
441 |
|
assertSame(f, f.fork()); |
442 |
|
assertNull(f.get()); |
443 |
|
assertEquals(21, f.result); |
444 |
< |
assertTrue(f.isDone()); |
444 |
> |
checkCompletedNormally(f); |
445 |
|
}}; |
446 |
|
testInvokeOnPool(mainPool(), a); |
447 |
|
} |
454 |
|
public void realCompute() throws Exception { |
455 |
|
FibAction f = new FibAction(8); |
456 |
|
assertSame(f, f.fork()); |
457 |
< |
assertNull(f.get(5L, TimeUnit.SECONDS)); |
457 |
> |
assertNull(f.get(5L, SECONDS)); |
458 |
|
assertEquals(21, f.result); |
459 |
< |
assertTrue(f.isDone()); |
459 |
> |
checkCompletedNormally(f); |
460 |
|
}}; |
461 |
|
testInvokeOnPool(mainPool(), a); |
462 |
|
} |
487 |
|
assertSame(f, f.fork()); |
488 |
|
f.quietlyJoin(); |
489 |
|
assertEquals(21, f.result); |
490 |
< |
assertTrue(f.isDone()); |
490 |
> |
checkCompletedNormally(f); |
491 |
|
}}; |
492 |
|
testInvokeOnPool(mainPool(), a); |
493 |
|
} |
494 |
|
|
215 |
– |
|
495 |
|
/** |
496 |
|
* helpQuiesce returns when tasks are complete. |
497 |
|
* getQueuedTaskCount returns 0 when quiescent |
501 |
|
public void realCompute() { |
502 |
|
FibAction f = new FibAction(8); |
503 |
|
assertSame(f, f.fork()); |
504 |
< |
f.helpQuiesce(); |
504 |
> |
helpQuiesce(); |
505 |
|
assertEquals(21, f.result); |
227 |
– |
assertTrue(f.isDone()); |
506 |
|
assertEquals(0, getQueuedTaskCount()); |
507 |
+ |
checkCompletedNormally(f); |
508 |
|
}}; |
509 |
|
testInvokeOnPool(mainPool(), a); |
510 |
|
} |
511 |
|
|
233 |
– |
|
512 |
|
/** |
513 |
|
* invoke task throws exception when task completes abnormally |
514 |
|
*/ |
519 |
|
try { |
520 |
|
f.invoke(); |
521 |
|
shouldThrow(); |
522 |
< |
} catch (FJException success) {} |
522 |
> |
} catch (FJException success) { |
523 |
> |
checkCompletedAbnormally(f, success); |
524 |
> |
} |
525 |
|
}}; |
526 |
|
testInvokeOnPool(mainPool(), a); |
527 |
|
} |
534 |
|
public void realCompute() { |
535 |
|
FailingFibAction f = new FailingFibAction(8); |
536 |
|
f.quietlyInvoke(); |
537 |
< |
assertTrue(f.isDone()); |
537 |
> |
assertTrue(f.getException() instanceof FJException); |
538 |
> |
checkCompletedAbnormally(f, f.getException()); |
539 |
|
}}; |
540 |
|
testInvokeOnPool(mainPool(), a); |
541 |
|
} |
551 |
|
try { |
552 |
|
f.join(); |
553 |
|
shouldThrow(); |
554 |
< |
} catch (FJException success) {} |
554 |
> |
} catch (FJException success) { |
555 |
> |
checkCompletedAbnormally(f, success); |
556 |
> |
} |
557 |
|
}}; |
558 |
|
testInvokeOnPool(mainPool(), a); |
559 |
|
} |
569 |
|
try { |
570 |
|
f.get(); |
571 |
|
shouldThrow(); |
572 |
< |
} catch (ExecutionException success) {} |
572 |
> |
} catch (ExecutionException success) { |
573 |
> |
Throwable cause = success.getCause(); |
574 |
> |
assertTrue(cause instanceof FJException); |
575 |
> |
checkCompletedAbnormally(f, cause); |
576 |
> |
} |
577 |
|
}}; |
578 |
|
testInvokeOnPool(mainPool(), a); |
579 |
|
} |
589 |
|
try { |
590 |
|
f.get(5L, TimeUnit.SECONDS); |
591 |
|
shouldThrow(); |
592 |
< |
} catch (ExecutionException success) {} |
592 |
> |
} catch (ExecutionException success) { |
593 |
> |
Throwable cause = success.getCause(); |
594 |
> |
assertTrue(cause instanceof FJException); |
595 |
> |
checkCompletedAbnormally(f, cause); |
596 |
> |
} |
597 |
|
}}; |
598 |
|
testInvokeOnPool(mainPool(), a); |
599 |
|
} |
607 |
|
FailingFibAction f = new FailingFibAction(8); |
608 |
|
assertSame(f, f.fork()); |
609 |
|
f.quietlyJoin(); |
319 |
– |
assertTrue(f.isDone()); |
320 |
– |
assertTrue(f.isCompletedAbnormally()); |
610 |
|
assertTrue(f.getException() instanceof FJException); |
611 |
+ |
checkCompletedAbnormally(f, f.getException()); |
612 |
|
}}; |
613 |
|
testInvokeOnPool(mainPool(), a); |
614 |
|
} |
624 |
|
try { |
625 |
|
f.invoke(); |
626 |
|
shouldThrow(); |
627 |
< |
} catch (CancellationException success) {} |
627 |
> |
} catch (CancellationException success) { |
628 |
> |
checkCancelled(f); |
629 |
> |
} |
630 |
|
}}; |
631 |
|
testInvokeOnPool(mainPool(), a); |
632 |
|
} |
643 |
|
try { |
644 |
|
f.join(); |
645 |
|
shouldThrow(); |
646 |
< |
} catch (CancellationException success) {} |
646 |
> |
} catch (CancellationException success) { |
647 |
> |
checkCancelled(f); |
648 |
> |
} |
649 |
|
}}; |
650 |
|
testInvokeOnPool(mainPool(), a); |
651 |
|
} |
662 |
|
try { |
663 |
|
f.get(); |
664 |
|
shouldThrow(); |
665 |
< |
} catch (CancellationException success) {} |
665 |
> |
} catch (CancellationException success) { |
666 |
> |
checkCancelled(f); |
667 |
> |
} |
668 |
|
}}; |
669 |
|
testInvokeOnPool(mainPool(), a); |
670 |
|
} |
679 |
|
assertTrue(f.cancel(true)); |
680 |
|
assertSame(f, f.fork()); |
681 |
|
try { |
682 |
< |
f.get(5L, TimeUnit.SECONDS); |
682 |
> |
f.get(5L, SECONDS); |
683 |
|
shouldThrow(); |
684 |
< |
} catch (CancellationException success) {} |
684 |
> |
} catch (CancellationException success) { |
685 |
> |
checkCancelled(f); |
686 |
> |
} |
687 |
|
}}; |
688 |
|
testInvokeOnPool(mainPool(), a); |
689 |
|
} |
698 |
|
assertTrue(f.cancel(true)); |
699 |
|
assertSame(f, f.fork()); |
700 |
|
f.quietlyJoin(); |
701 |
< |
assertTrue(f.isDone()); |
404 |
< |
assertTrue(f.isCompletedAbnormally()); |
405 |
< |
assertTrue(f.isCancelled()); |
406 |
< |
assertTrue(f.getException() instanceof CancellationException); |
701 |
> |
checkCancelled(f); |
702 |
|
}}; |
703 |
|
testInvokeOnPool(mainPool(), a); |
704 |
|
} |
770 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
771 |
|
public void realCompute() { |
772 |
|
ForkJoinWorkerThread w = |
773 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
774 |
< |
int idx = w.getPoolIndex(); |
775 |
< |
assertTrue(idx >= 0); |
776 |
< |
assertTrue(idx < mainPool.getPoolSize()); |
773 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
774 |
> |
assertTrue(w.getPoolIndex() >= 0); |
775 |
> |
// pool size can shrink after assigning index, so cannot check |
776 |
> |
// assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
777 |
|
}}; |
778 |
|
testInvokeOnPool(mainPool, a); |
779 |
|
} |
780 |
|
|
486 |
– |
|
781 |
|
/** |
782 |
|
* setRawResult(null) succeeds |
783 |
|
*/ |
785 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
786 |
|
public void realCompute() { |
787 |
|
setRawResult(null); |
788 |
+ |
assertNull(getRawResult()); |
789 |
|
}}; |
790 |
|
assertNull(a.invoke()); |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* A reinitialized task may be re-invoked |
794 |
> |
* A reinitialized normally completed task may be re-invoked |
795 |
|
*/ |
796 |
|
public void testReinitialize() { |
797 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
798 |
|
public void realCompute() { |
799 |
|
FibAction f = new FibAction(8); |
800 |
< |
assertNull(f.invoke()); |
801 |
< |
assertEquals(21, f.result); |
802 |
< |
assertTrue(f.isDone()); |
803 |
< |
assertFalse(f.isCancelled()); |
804 |
< |
assertFalse(f.isCompletedAbnormally()); |
805 |
< |
f.reinitialize(); |
806 |
< |
assertNull(f.invoke()); |
807 |
< |
assertEquals(21, f.result); |
800 |
> |
checkNotDone(f); |
801 |
> |
|
802 |
> |
for (int i = 0; i < 3; i++) { |
803 |
> |
assertNull(f.invoke()); |
804 |
> |
assertEquals(21, f.result); |
805 |
> |
checkCompletedNormally(f); |
806 |
> |
f.reinitialize(); |
807 |
> |
checkNotDone(f); |
808 |
> |
} |
809 |
> |
}}; |
810 |
> |
testInvokeOnPool(mainPool(), a); |
811 |
> |
} |
812 |
> |
|
813 |
> |
/** |
814 |
> |
* A reinitialized abnormally completed task may be re-invoked |
815 |
> |
*/ |
816 |
> |
public void testReinitializeAbnormal() { |
817 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
818 |
> |
public void realCompute() { |
819 |
> |
FailingFibAction f = new FailingFibAction(8); |
820 |
> |
checkNotDone(f); |
821 |
> |
|
822 |
> |
for (int i = 0; i < 3; i++) { |
823 |
> |
try { |
824 |
> |
f.invoke(); |
825 |
> |
shouldThrow(); |
826 |
> |
} catch (FJException success) { |
827 |
> |
checkCompletedAbnormally(f, success); |
828 |
> |
} |
829 |
> |
f.reinitialize(); |
830 |
> |
checkNotDone(f); |
831 |
> |
} |
832 |
|
}}; |
833 |
|
testInvokeOnPool(mainPool(), a); |
834 |
|
} |
844 |
|
try { |
845 |
|
f.invoke(); |
846 |
|
shouldThrow(); |
847 |
< |
} catch (FJException success) {} |
847 |
> |
} catch (FJException success) { |
848 |
> |
checkCompletedAbnormally(f, success); |
849 |
> |
} |
850 |
|
}}; |
851 |
|
testInvokeOnPool(mainPool(), a); |
852 |
|
} |
860 |
|
FibAction f = new FibAction(8); |
861 |
|
f.complete(null); |
862 |
|
assertNull(f.invoke()); |
542 |
– |
assertTrue(f.isDone()); |
863 |
|
assertEquals(0, f.result); |
864 |
+ |
checkCompletedNormally(f); |
865 |
|
}}; |
866 |
|
testInvokeOnPool(mainPool(), a); |
867 |
|
} |
875 |
|
FibAction f = new FibAction(8); |
876 |
|
FibAction g = new FibAction(9); |
877 |
|
invokeAll(f, g); |
878 |
< |
assertTrue(f.isDone()); |
878 |
> |
checkCompletedNormally(f); |
879 |
|
assertEquals(21, f.result); |
880 |
< |
assertTrue(g.isDone()); |
880 |
> |
checkCompletedNormally(g); |
881 |
|
assertEquals(34, g.result); |
882 |
|
}}; |
883 |
|
testInvokeOnPool(mainPool(), a); |
891 |
|
public void realCompute() { |
892 |
|
FibAction f = new FibAction(8); |
893 |
|
invokeAll(f); |
894 |
< |
assertTrue(f.isDone()); |
894 |
> |
checkCompletedNormally(f); |
895 |
|
assertEquals(21, f.result); |
896 |
|
}}; |
897 |
|
testInvokeOnPool(mainPool(), a); |
908 |
|
FibAction h = new FibAction(7); |
909 |
|
invokeAll(f, g, h); |
910 |
|
assertTrue(f.isDone()); |
590 |
– |
assertEquals(21, f.result); |
911 |
|
assertTrue(g.isDone()); |
592 |
– |
assertEquals(34, g.result); |
912 |
|
assertTrue(h.isDone()); |
913 |
+ |
checkCompletedNormally(f); |
914 |
+ |
assertEquals(21, f.result); |
915 |
+ |
checkCompletedNormally(g); |
916 |
+ |
assertEquals(34, g.result); |
917 |
+ |
checkCompletedNormally(g); |
918 |
|
assertEquals(13, h.result); |
919 |
|
}}; |
920 |
|
testInvokeOnPool(mainPool(), a); |
935 |
|
set.add(h); |
936 |
|
invokeAll(set); |
937 |
|
assertTrue(f.isDone()); |
614 |
– |
assertEquals(21, f.result); |
938 |
|
assertTrue(g.isDone()); |
616 |
– |
assertEquals(34, g.result); |
939 |
|
assertTrue(h.isDone()); |
940 |
+ |
checkCompletedNormally(f); |
941 |
+ |
assertEquals(21, f.result); |
942 |
+ |
checkCompletedNormally(g); |
943 |
+ |
assertEquals(34, g.result); |
944 |
+ |
checkCompletedNormally(g); |
945 |
|
assertEquals(13, h.result); |
946 |
|
}}; |
947 |
|
testInvokeOnPool(mainPool(), a); |
948 |
|
} |
949 |
|
|
623 |
– |
|
950 |
|
/** |
951 |
|
* invokeAll(tasks) with any null task throws NPE |
952 |
|
*/ |
975 |
|
try { |
976 |
|
invokeAll(f, g); |
977 |
|
shouldThrow(); |
978 |
< |
} catch (FJException success) {} |
978 |
> |
} catch (FJException success) { |
979 |
> |
checkCompletedAbnormally(g, success); |
980 |
> |
} |
981 |
|
}}; |
982 |
|
testInvokeOnPool(mainPool(), a); |
983 |
|
} |
992 |
|
try { |
993 |
|
invokeAll(g); |
994 |
|
shouldThrow(); |
995 |
< |
} catch (FJException success) {} |
995 |
> |
} catch (FJException success) { |
996 |
> |
checkCompletedAbnormally(g, success); |
997 |
> |
} |
998 |
|
}}; |
999 |
|
testInvokeOnPool(mainPool(), a); |
1000 |
|
} |
1011 |
|
try { |
1012 |
|
invokeAll(f, g, h); |
1013 |
|
shouldThrow(); |
1014 |
< |
} catch (FJException success) {} |
1014 |
> |
} catch (FJException success) { |
1015 |
> |
checkCompletedAbnormally(g, success); |
1016 |
> |
} |
1017 |
|
}}; |
1018 |
|
testInvokeOnPool(mainPool(), a); |
1019 |
|
} |
1034 |
|
try { |
1035 |
|
invokeAll(set); |
1036 |
|
shouldThrow(); |
1037 |
< |
} catch (FJException success) {} |
1037 |
> |
} catch (FJException success) { |
1038 |
> |
checkCompletedAbnormally(f, success); |
1039 |
> |
} |
1040 |
|
}}; |
1041 |
|
testInvokeOnPool(mainPool(), a); |
1042 |
|
} |
1054 |
|
assertSame(f, f.fork()); |
1055 |
|
assertTrue(f.tryUnfork()); |
1056 |
|
helpQuiesce(); |
1057 |
< |
assertFalse(f.isDone()); |
1058 |
< |
assertTrue(g.isDone()); |
1057 |
> |
checkNotDone(f); |
1058 |
> |
checkCompletedNormally(g); |
1059 |
|
}}; |
1060 |
|
testInvokeOnPool(singletonPool(), a); |
1061 |
|
} |
1075 |
|
assertSame(f, f.fork()); |
1076 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1077 |
|
helpQuiesce(); |
1078 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
1079 |
+ |
checkCompletedNormally(f); |
1080 |
+ |
checkCompletedNormally(g); |
1081 |
+ |
checkCompletedNormally(h); |
1082 |
|
}}; |
1083 |
|
testInvokeOnPool(singletonPool(), a); |
1084 |
|
} |
1095 |
|
assertSame(f, f.fork()); |
1096 |
|
assertSame(f, peekNextLocalTask()); |
1097 |
|
assertNull(f.join()); |
1098 |
< |
assertTrue(f.isDone()); |
1098 |
> |
checkCompletedNormally(f); |
1099 |
|
helpQuiesce(); |
1100 |
+ |
checkCompletedNormally(f); |
1101 |
+ |
checkCompletedNormally(g); |
1102 |
|
}}; |
1103 |
|
testInvokeOnPool(singletonPool(), a); |
1104 |
|
} |
1116 |
|
assertSame(f, f.fork()); |
1117 |
|
assertSame(f, pollNextLocalTask()); |
1118 |
|
helpQuiesce(); |
1119 |
< |
assertFalse(f.isDone()); |
1119 |
> |
checkNotDone(f); |
1120 |
> |
checkCompletedNormally(g); |
1121 |
|
}}; |
1122 |
|
testInvokeOnPool(singletonPool(), a); |
1123 |
|
} |
1124 |
|
|
1125 |
|
/** |
1126 |
< |
* pollTask returns an unexecuted task |
786 |
< |
* without executing it |
1126 |
> |
* pollTask returns an unexecuted task without executing it |
1127 |
|
*/ |
1128 |
|
public void testPollTask() { |
1129 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1134 |
|
assertSame(f, f.fork()); |
1135 |
|
assertSame(f, pollTask()); |
1136 |
|
helpQuiesce(); |
1137 |
< |
assertFalse(f.isDone()); |
1138 |
< |
assertTrue(g.isDone()); |
1137 |
> |
checkNotDone(f); |
1138 |
> |
checkCompletedNormally(g); |
1139 |
|
}}; |
1140 |
|
testInvokeOnPool(singletonPool(), a); |
1141 |
|
} |
1153 |
|
assertSame(g, peekNextLocalTask()); |
1154 |
|
assertNull(f.join()); |
1155 |
|
helpQuiesce(); |
1156 |
< |
assertTrue(f.isDone()); |
1156 |
> |
checkCompletedNormally(f); |
1157 |
> |
checkCompletedNormally(g); |
1158 |
|
}}; |
1159 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1160 |
|
} |
1161 |
|
|
1162 |
|
/** |
1163 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1164 |
< |
* without executing it, in async mode |
1163 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1164 |
> |
* executing it, in async mode |
1165 |
|
*/ |
1166 |
|
public void testPollNextLocalTaskAsync() { |
1167 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1172 |
|
assertSame(f, f.fork()); |
1173 |
|
assertSame(g, pollNextLocalTask()); |
1174 |
|
helpQuiesce(); |
1175 |
< |
assertTrue(f.isDone()); |
1176 |
< |
assertFalse(g.isDone()); |
1175 |
> |
checkCompletedNormally(f); |
1176 |
> |
checkNotDone(g); |
1177 |
|
}}; |
1178 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1179 |
|
} |
1180 |
|
|
1181 |
|
/** |
1182 |
< |
* pollTask returns an unexecuted task |
1183 |
< |
* without executing it, in async mode |
1182 |
> |
* pollTask returns an unexecuted task without executing it, in |
1183 |
> |
* async mode |
1184 |
|
*/ |
1185 |
|
public void testPollTaskAsync() { |
1186 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
1191 |
|
assertSame(f, f.fork()); |
1192 |
|
assertSame(g, pollTask()); |
1193 |
|
helpQuiesce(); |
1194 |
< |
assertTrue(f.isDone()); |
1195 |
< |
assertFalse(g.isDone()); |
1194 |
> |
checkCompletedNormally(f); |
1195 |
> |
checkNotDone(g); |
1196 |
|
}}; |
1197 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1198 |
|
} |
1199 |
|
|
1200 |
+ |
/** Demo from RecursiveAction javadoc */ |
1201 |
+ |
static class SortTask extends RecursiveAction { |
1202 |
+ |
final long[] array; final int lo, hi; |
1203 |
+ |
SortTask(long[] array, int lo, int hi) { |
1204 |
+ |
this.array = array; this.lo = lo; this.hi = hi; |
1205 |
+ |
} |
1206 |
+ |
SortTask(long[] array) { this(array, 0, array.length); } |
1207 |
+ |
protected void compute() { |
1208 |
+ |
if (hi - lo < THRESHOLD) |
1209 |
+ |
sortSequentially(lo, hi); |
1210 |
+ |
else { |
1211 |
+ |
int mid = (lo + hi) >>> 1; |
1212 |
+ |
invokeAll(new SortTask(array, lo, mid), |
1213 |
+ |
new SortTask(array, mid, hi)); |
1214 |
+ |
merge(lo, mid, hi); |
1215 |
+ |
} |
1216 |
+ |
} |
1217 |
+ |
// implementation details follow: |
1218 |
+ |
final static int THRESHOLD = 100; |
1219 |
+ |
void sortSequentially(int lo, int hi) { |
1220 |
+ |
Arrays.sort(array, lo, hi); |
1221 |
+ |
} |
1222 |
+ |
void merge(int lo, int mid, int hi) { |
1223 |
+ |
long[] buf = Arrays.copyOfRange(array, lo, mid); |
1224 |
+ |
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
1225 |
+ |
array[j] = (k == hi || buf[i] < array[k]) ? |
1226 |
+ |
buf[i++] : array[k++]; |
1227 |
+ |
} |
1228 |
+ |
} |
1229 |
+ |
|
1230 |
+ |
/** |
1231 |
+ |
* SortTask demo works as advertised |
1232 |
+ |
*/ |
1233 |
+ |
public void testSortTaskDemo() { |
1234 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1235 |
+ |
long[] array = new long[1007]; |
1236 |
+ |
for (int i = 0; i < array.length; i++) |
1237 |
+ |
array[i] = rnd.nextLong(); |
1238 |
+ |
long[] arrayClone = array.clone(); |
1239 |
+ |
testInvokeOnPool(mainPool(), new SortTask(array)); |
1240 |
+ |
Arrays.sort(arrayClone); |
1241 |
+ |
assertTrue(Arrays.equals(array, arrayClone)); |
1242 |
+ |
} |
1243 |
|
} |