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 |
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 { |
21 |
|
22 |
public static void main(String[] args) { |
23 |
junit.textui.TestRunner.run(suite()); |
24 |
} |
25 |
|
26 |
public static Test suite() { |
27 |
return new TestSuite(RecursiveActionTest.class); |
28 |
} |
29 |
|
30 |
private static ForkJoinPool mainPool() { |
31 |
return new ForkJoinPool(); |
32 |
} |
33 |
|
34 |
private static ForkJoinPool singletonPool() { |
35 |
return new ForkJoinPool(1); |
36 |
} |
37 |
|
38 |
private static ForkJoinPool asyncSingletonPool() { |
39 |
return new ForkJoinPool(1, |
40 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
41 |
null, true); |
42 |
} |
43 |
|
44 |
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
45 |
try { |
46 |
checkNotDone(a); |
47 |
|
48 |
assertNull(pool.invoke(a)); |
49 |
|
50 |
checkCompletedNormally(a); |
51 |
} finally { |
52 |
joinPool(pool); |
53 |
} |
54 |
} |
55 |
|
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, a.getException()); |
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(t, expected); |
147 |
} |
148 |
|
149 |
try { |
150 |
a.get(); |
151 |
shouldThrow(); |
152 |
} catch (ExecutionException success) { |
153 |
assertSame(t, success.getCause()); |
154 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
|
156 |
try { |
157 |
a.get(5L, SECONDS); |
158 |
shouldThrow(); |
159 |
} catch (ExecutionException success) { |
160 |
assertSame(t, success.getCause()); |
161 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
162 |
} |
163 |
|
164 |
static final class FJException extends RuntimeException { |
165 |
FJException() { super(); } |
166 |
} |
167 |
|
168 |
// A simple recursive action for testing |
169 |
final class FibAction extends CheckedRecursiveAction { |
170 |
final int number; |
171 |
int result; |
172 |
FibAction(int n) { number = n; } |
173 |
public void realCompute() { |
174 |
int n = number; |
175 |
if (n <= 1) |
176 |
result = n; |
177 |
else { |
178 |
FibAction f1 = new FibAction(n - 1); |
179 |
FibAction f2 = new FibAction(n - 2); |
180 |
invokeAll(f1, f2); |
181 |
result = f1.result + f2.result; |
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
// A recursive action failing in base case |
187 |
static final class FailingFibAction extends RecursiveAction { |
188 |
final int number; |
189 |
int result; |
190 |
FailingFibAction(int n) { number = n; } |
191 |
public void compute() { |
192 |
int n = number; |
193 |
if (n <= 1) |
194 |
throw new FJException(); |
195 |
else { |
196 |
FailingFibAction f1 = new FailingFibAction(n - 1); |
197 |
FailingFibAction f2 = new FailingFibAction(n - 2); |
198 |
invokeAll(f1, f2); |
199 |
result = f1.result + f2.result; |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
/** |
205 |
* invoke returns when task completes normally. |
206 |
* isCompletedAbnormally and isCancelled return false for normally |
207 |
* completed tasks. getRawResult of a RecursiveAction returns null; |
208 |
*/ |
209 |
public void testInvoke() { |
210 |
RecursiveAction a = new CheckedRecursiveAction() { |
211 |
public void realCompute() { |
212 |
FibAction f = new FibAction(8); |
213 |
assertNull(f.invoke()); |
214 |
assertEquals(21, f.result); |
215 |
checkCompletedNormally(f); |
216 |
}}; |
217 |
testInvokeOnPool(mainPool(), a); |
218 |
} |
219 |
|
220 |
/** |
221 |
* quietlyInvoke task returns when task completes normally. |
222 |
* isCompletedAbnormally and isCancelled return false for normally |
223 |
* completed tasks |
224 |
*/ |
225 |
public void testQuietlyInvoke() { |
226 |
RecursiveAction a = new CheckedRecursiveAction() { |
227 |
public void realCompute() { |
228 |
FibAction f = new FibAction(8); |
229 |
f.quietlyInvoke(); |
230 |
assertEquals(21, f.result); |
231 |
checkCompletedNormally(f); |
232 |
}}; |
233 |
testInvokeOnPool(mainPool(), a); |
234 |
} |
235 |
|
236 |
/** |
237 |
* join of a forked task returns when task completes |
238 |
*/ |
239 |
public void testForkJoin() { |
240 |
RecursiveAction a = new CheckedRecursiveAction() { |
241 |
public void realCompute() { |
242 |
FibAction f = new FibAction(8); |
243 |
assertSame(f, f.fork()); |
244 |
assertNull(f.join()); |
245 |
assertEquals(21, f.result); |
246 |
checkCompletedNormally(f); |
247 |
}}; |
248 |
testInvokeOnPool(mainPool(), a); |
249 |
} |
250 |
|
251 |
/** |
252 |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
253 |
*/ |
254 |
public void testJoinIgnoresInterrupts() { |
255 |
RecursiveAction a = new CheckedRecursiveAction() { |
256 |
public void realCompute() { |
257 |
FibAction f = new FibAction(8); |
258 |
final Thread myself = Thread.currentThread(); |
259 |
|
260 |
// test join() |
261 |
assertSame(f, f.fork()); |
262 |
myself.interrupt(); |
263 |
assertTrue(myself.isInterrupted()); |
264 |
assertNull(f.join()); |
265 |
Thread.interrupted(); |
266 |
assertEquals(21, f.result); |
267 |
checkCompletedNormally(f); |
268 |
|
269 |
f.reinitialize(); |
270 |
f.cancel(true); |
271 |
assertSame(f, f.fork()); |
272 |
myself.interrupt(); |
273 |
assertTrue(myself.isInterrupted()); |
274 |
try { |
275 |
f.join(); |
276 |
shouldThrow(); |
277 |
} catch (CancellationException success) { |
278 |
Thread.interrupted(); |
279 |
checkCancelled(f); |
280 |
} |
281 |
|
282 |
f.reinitialize(); |
283 |
f.completeExceptionally(new FJException()); |
284 |
assertSame(f, f.fork()); |
285 |
myself.interrupt(); |
286 |
assertTrue(myself.isInterrupted()); |
287 |
try { |
288 |
f.join(); |
289 |
shouldThrow(); |
290 |
} catch (FJException success) { |
291 |
Thread.interrupted(); |
292 |
checkCompletedAbnormally(f, success); |
293 |
} |
294 |
|
295 |
// test quietlyJoin() |
296 |
f.reinitialize(); |
297 |
assertSame(f, f.fork()); |
298 |
myself.interrupt(); |
299 |
assertTrue(myself.isInterrupted()); |
300 |
f.quietlyJoin(); |
301 |
Thread.interrupted(); |
302 |
assertEquals(21, f.result); |
303 |
checkCompletedNormally(f); |
304 |
|
305 |
f.reinitialize(); |
306 |
f.cancel(true); |
307 |
assertSame(f, f.fork()); |
308 |
myself.interrupt(); |
309 |
assertTrue(myself.isInterrupted()); |
310 |
f.quietlyJoin(); |
311 |
Thread.interrupted(); |
312 |
checkCancelled(f); |
313 |
|
314 |
f.reinitialize(); |
315 |
f.completeExceptionally(new FJException()); |
316 |
assertSame(f, f.fork()); |
317 |
myself.interrupt(); |
318 |
assertTrue(myself.isInterrupted()); |
319 |
f.quietlyJoin(); |
320 |
Thread.interrupted(); |
321 |
checkCompletedAbnormally(f, f.getException()); |
322 |
}}; |
323 |
testInvokeOnPool(mainPool(), a); |
324 |
a.reinitialize(); |
325 |
testInvokeOnPool(singletonPool(), a); |
326 |
} |
327 |
|
328 |
/** |
329 |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
330 |
* succeeds in the presence of interrupts |
331 |
*/ |
332 |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
333 |
final SynchronousQueue<FibAction[]> sq = |
334 |
new SynchronousQueue<FibAction[]>(); |
335 |
RecursiveAction a = new CheckedRecursiveAction() { |
336 |
public void realCompute() throws InterruptedException { |
337 |
FibAction[] fibActions = new FibAction[6]; |
338 |
for (int i = 0; i < fibActions.length; i++) |
339 |
fibActions[i] = new FibAction(8); |
340 |
|
341 |
fibActions[1].cancel(false); |
342 |
fibActions[2].completeExceptionally(new FJException()); |
343 |
fibActions[4].cancel(true); |
344 |
fibActions[5].completeExceptionally(new FJException()); |
345 |
|
346 |
for (int i = 0; i < fibActions.length; i++) |
347 |
fibActions[i].fork(); |
348 |
|
349 |
sq.put(fibActions); |
350 |
|
351 |
helpQuiesce(); |
352 |
}}; |
353 |
|
354 |
Runnable r = new CheckedRunnable() { |
355 |
public void realRun() throws InterruptedException { |
356 |
FibAction[] fibActions = sq.take(); |
357 |
FibAction f; |
358 |
final Thread myself = Thread.currentThread(); |
359 |
|
360 |
// test join() ------------ |
361 |
|
362 |
f = fibActions[0]; |
363 |
assertFalse(ForkJoinTask.inForkJoinPool()); |
364 |
myself.interrupt(); |
365 |
assertTrue(myself.isInterrupted()); |
366 |
assertNull(f.join()); |
367 |
assertTrue(Thread.interrupted()); |
368 |
assertEquals(21, f.result); |
369 |
checkCompletedNormally(f); |
370 |
|
371 |
f = fibActions[1]; |
372 |
myself.interrupt(); |
373 |
assertTrue(myself.isInterrupted()); |
374 |
try { |
375 |
f.join(); |
376 |
shouldThrow(); |
377 |
} catch (CancellationException success) { |
378 |
assertTrue(Thread.interrupted()); |
379 |
checkCancelled(f); |
380 |
} |
381 |
|
382 |
f = fibActions[2]; |
383 |
myself.interrupt(); |
384 |
assertTrue(myself.isInterrupted()); |
385 |
try { |
386 |
f.join(); |
387 |
shouldThrow(); |
388 |
} catch (FJException success) { |
389 |
assertTrue(Thread.interrupted()); |
390 |
checkCompletedAbnormally(f, success); |
391 |
} |
392 |
|
393 |
// test quietlyJoin() --------- |
394 |
|
395 |
f = fibActions[3]; |
396 |
myself.interrupt(); |
397 |
assertTrue(myself.isInterrupted()); |
398 |
f.quietlyJoin(); |
399 |
assertTrue(Thread.interrupted()); |
400 |
assertEquals(21, f.result); |
401 |
checkCompletedNormally(f); |
402 |
|
403 |
f = fibActions[4]; |
404 |
myself.interrupt(); |
405 |
assertTrue(myself.isInterrupted()); |
406 |
f.quietlyJoin(); |
407 |
assertTrue(Thread.interrupted()); |
408 |
checkCancelled(f); |
409 |
|
410 |
f = fibActions[5]; |
411 |
myself.interrupt(); |
412 |
assertTrue(myself.isInterrupted()); |
413 |
f.quietlyJoin(); |
414 |
assertTrue(Thread.interrupted()); |
415 |
assertTrue(f.getException() instanceof FJException); |
416 |
checkCompletedAbnormally(f, f.getException()); |
417 |
}}; |
418 |
|
419 |
Thread t; |
420 |
|
421 |
t = newStartedThread(r); |
422 |
testInvokeOnPool(mainPool(), a); |
423 |
awaitTermination(t, LONG_DELAY_MS); |
424 |
|
425 |
a.reinitialize(); |
426 |
t = newStartedThread(r); |
427 |
testInvokeOnPool(singletonPool(), a); |
428 |
awaitTermination(t, LONG_DELAY_MS); |
429 |
} |
430 |
|
431 |
/** |
432 |
* get of a forked task returns when task completes |
433 |
*/ |
434 |
public void testForkGet() { |
435 |
RecursiveAction a = new CheckedRecursiveAction() { |
436 |
public void realCompute() throws Exception { |
437 |
FibAction f = new FibAction(8); |
438 |
assertSame(f, f.fork()); |
439 |
assertNull(f.get()); |
440 |
assertEquals(21, f.result); |
441 |
checkCompletedNormally(f); |
442 |
}}; |
443 |
testInvokeOnPool(mainPool(), a); |
444 |
} |
445 |
|
446 |
/** |
447 |
* timed get of a forked task returns when task completes |
448 |
*/ |
449 |
public void testForkTimedGet() { |
450 |
RecursiveAction a = new CheckedRecursiveAction() { |
451 |
public void realCompute() throws Exception { |
452 |
FibAction f = new FibAction(8); |
453 |
assertSame(f, f.fork()); |
454 |
assertNull(f.get(5L, SECONDS)); |
455 |
assertEquals(21, f.result); |
456 |
checkCompletedNormally(f); |
457 |
}}; |
458 |
testInvokeOnPool(mainPool(), a); |
459 |
} |
460 |
|
461 |
/** |
462 |
* timed get with null time unit throws NPE |
463 |
*/ |
464 |
public void testForkTimedGetNPE() { |
465 |
RecursiveAction a = new CheckedRecursiveAction() { |
466 |
public void realCompute() throws Exception { |
467 |
FibAction f = new FibAction(8); |
468 |
assertSame(f, f.fork()); |
469 |
try { |
470 |
f.get(5L, null); |
471 |
shouldThrow(); |
472 |
} catch (NullPointerException success) {} |
473 |
}}; |
474 |
testInvokeOnPool(mainPool(), a); |
475 |
} |
476 |
|
477 |
/** |
478 |
* quietlyJoin of a forked task returns when task completes |
479 |
*/ |
480 |
public void testForkQuietlyJoin() { |
481 |
RecursiveAction a = new CheckedRecursiveAction() { |
482 |
public void realCompute() { |
483 |
FibAction f = new FibAction(8); |
484 |
assertSame(f, f.fork()); |
485 |
f.quietlyJoin(); |
486 |
assertEquals(21, f.result); |
487 |
checkCompletedNormally(f); |
488 |
}}; |
489 |
testInvokeOnPool(mainPool(), a); |
490 |
} |
491 |
|
492 |
|
493 |
/** |
494 |
* helpQuiesce returns when tasks are complete. |
495 |
* getQueuedTaskCount returns 0 when quiescent |
496 |
*/ |
497 |
public void testForkHelpQuiesce() { |
498 |
RecursiveAction a = new CheckedRecursiveAction() { |
499 |
public void realCompute() { |
500 |
FibAction f = new FibAction(8); |
501 |
assertSame(f, f.fork()); |
502 |
f.helpQuiesce(); |
503 |
assertEquals(21, f.result); |
504 |
assertEquals(0, getQueuedTaskCount()); |
505 |
checkCompletedNormally(f); |
506 |
}}; |
507 |
testInvokeOnPool(mainPool(), a); |
508 |
} |
509 |
|
510 |
|
511 |
/** |
512 |
* invoke task throws exception when task completes abnormally |
513 |
*/ |
514 |
public void testAbnormalInvoke() { |
515 |
RecursiveAction a = new CheckedRecursiveAction() { |
516 |
public void realCompute() { |
517 |
FailingFibAction f = new FailingFibAction(8); |
518 |
try { |
519 |
f.invoke(); |
520 |
shouldThrow(); |
521 |
} catch (FJException success) { |
522 |
checkCompletedAbnormally(f, success); |
523 |
} |
524 |
}}; |
525 |
testInvokeOnPool(mainPool(), a); |
526 |
} |
527 |
|
528 |
/** |
529 |
* quietlyInvoke task returns when task completes abnormally |
530 |
*/ |
531 |
public void testAbnormalQuietlyInvoke() { |
532 |
RecursiveAction a = new CheckedRecursiveAction() { |
533 |
public void realCompute() { |
534 |
FailingFibAction f = new FailingFibAction(8); |
535 |
f.quietlyInvoke(); |
536 |
assertTrue(f.getException() instanceof FJException); |
537 |
checkCompletedAbnormally(f, f.getException()); |
538 |
}}; |
539 |
testInvokeOnPool(mainPool(), a); |
540 |
} |
541 |
|
542 |
/** |
543 |
* join of a forked task throws exception when task completes abnormally |
544 |
*/ |
545 |
public void testAbnormalForkJoin() { |
546 |
RecursiveAction a = new CheckedRecursiveAction() { |
547 |
public void realCompute() { |
548 |
FailingFibAction f = new FailingFibAction(8); |
549 |
assertSame(f, f.fork()); |
550 |
try { |
551 |
f.join(); |
552 |
shouldThrow(); |
553 |
} catch (FJException success) { |
554 |
checkCompletedAbnormally(f, success); |
555 |
} |
556 |
}}; |
557 |
testInvokeOnPool(mainPool(), a); |
558 |
} |
559 |
|
560 |
/** |
561 |
* get of a forked task throws exception when task completes abnormally |
562 |
*/ |
563 |
public void testAbnormalForkGet() { |
564 |
RecursiveAction a = new CheckedRecursiveAction() { |
565 |
public void realCompute() throws Exception { |
566 |
FailingFibAction f = new FailingFibAction(8); |
567 |
assertSame(f, f.fork()); |
568 |
try { |
569 |
f.get(); |
570 |
shouldThrow(); |
571 |
} catch (ExecutionException success) { |
572 |
Throwable cause = success.getCause(); |
573 |
assertTrue(cause instanceof FJException); |
574 |
checkCompletedAbnormally(f, cause); |
575 |
} |
576 |
}}; |
577 |
testInvokeOnPool(mainPool(), a); |
578 |
} |
579 |
|
580 |
/** |
581 |
* timed get of a forked task throws exception when task completes abnormally |
582 |
*/ |
583 |
public void testAbnormalForkTimedGet() { |
584 |
RecursiveAction a = new CheckedRecursiveAction() { |
585 |
public void realCompute() throws Exception { |
586 |
FailingFibAction f = new FailingFibAction(8); |
587 |
assertSame(f, f.fork()); |
588 |
try { |
589 |
f.get(5L, TimeUnit.SECONDS); |
590 |
shouldThrow(); |
591 |
} catch (ExecutionException success) { |
592 |
Throwable cause = success.getCause(); |
593 |
assertTrue(cause instanceof FJException); |
594 |
checkCompletedAbnormally(f, cause); |
595 |
} |
596 |
}}; |
597 |
testInvokeOnPool(mainPool(), a); |
598 |
} |
599 |
|
600 |
/** |
601 |
* quietlyJoin of a forked task returns when task completes abnormally |
602 |
*/ |
603 |
public void testAbnormalForkQuietlyJoin() { |
604 |
RecursiveAction a = new CheckedRecursiveAction() { |
605 |
public void realCompute() { |
606 |
FailingFibAction f = new FailingFibAction(8); |
607 |
assertSame(f, f.fork()); |
608 |
f.quietlyJoin(); |
609 |
assertTrue(f.getException() instanceof FJException); |
610 |
checkCompletedAbnormally(f, f.getException()); |
611 |
}}; |
612 |
testInvokeOnPool(mainPool(), a); |
613 |
} |
614 |
|
615 |
/** |
616 |
* invoke task throws exception when task cancelled |
617 |
*/ |
618 |
public void testCancelledInvoke() { |
619 |
RecursiveAction a = new CheckedRecursiveAction() { |
620 |
public void realCompute() { |
621 |
FibAction f = new FibAction(8); |
622 |
assertTrue(f.cancel(true)); |
623 |
try { |
624 |
f.invoke(); |
625 |
shouldThrow(); |
626 |
} catch (CancellationException success) { |
627 |
checkCancelled(f); |
628 |
} |
629 |
}}; |
630 |
testInvokeOnPool(mainPool(), a); |
631 |
} |
632 |
|
633 |
/** |
634 |
* join of a forked task throws exception when task cancelled |
635 |
*/ |
636 |
public void testCancelledForkJoin() { |
637 |
RecursiveAction a = new CheckedRecursiveAction() { |
638 |
public void realCompute() { |
639 |
FibAction f = new FibAction(8); |
640 |
assertTrue(f.cancel(true)); |
641 |
assertSame(f, f.fork()); |
642 |
try { |
643 |
f.join(); |
644 |
shouldThrow(); |
645 |
} catch (CancellationException success) { |
646 |
checkCancelled(f); |
647 |
} |
648 |
}}; |
649 |
testInvokeOnPool(mainPool(), a); |
650 |
} |
651 |
|
652 |
/** |
653 |
* get of a forked task throws exception when task cancelled |
654 |
*/ |
655 |
public void testCancelledForkGet() { |
656 |
RecursiveAction a = new CheckedRecursiveAction() { |
657 |
public void realCompute() throws Exception { |
658 |
FibAction f = new FibAction(8); |
659 |
assertTrue(f.cancel(true)); |
660 |
assertSame(f, f.fork()); |
661 |
try { |
662 |
f.get(); |
663 |
shouldThrow(); |
664 |
} catch (CancellationException success) { |
665 |
checkCancelled(f); |
666 |
} |
667 |
}}; |
668 |
testInvokeOnPool(mainPool(), a); |
669 |
} |
670 |
|
671 |
/** |
672 |
* timed get of a forked task throws exception when task cancelled |
673 |
*/ |
674 |
public void testCancelledForkTimedGet() { |
675 |
RecursiveAction a = new CheckedRecursiveAction() { |
676 |
public void realCompute() throws Exception { |
677 |
FibAction f = new FibAction(8); |
678 |
assertTrue(f.cancel(true)); |
679 |
assertSame(f, f.fork()); |
680 |
try { |
681 |
f.get(5L, SECONDS); |
682 |
shouldThrow(); |
683 |
} catch (CancellationException success) { |
684 |
checkCancelled(f); |
685 |
} |
686 |
}}; |
687 |
testInvokeOnPool(mainPool(), a); |
688 |
} |
689 |
|
690 |
/** |
691 |
* quietlyJoin of a forked task returns when task cancelled |
692 |
*/ |
693 |
public void testCancelledForkQuietlyJoin() { |
694 |
RecursiveAction a = new CheckedRecursiveAction() { |
695 |
public void realCompute() { |
696 |
FibAction f = new FibAction(8); |
697 |
assertTrue(f.cancel(true)); |
698 |
assertSame(f, f.fork()); |
699 |
f.quietlyJoin(); |
700 |
checkCancelled(f); |
701 |
}}; |
702 |
testInvokeOnPool(mainPool(), a); |
703 |
} |
704 |
|
705 |
/** |
706 |
* getPool of executing task returns its pool |
707 |
*/ |
708 |
public void testGetPool() { |
709 |
final ForkJoinPool mainPool = mainPool(); |
710 |
RecursiveAction a = new CheckedRecursiveAction() { |
711 |
public void realCompute() { |
712 |
assertSame(mainPool, getPool()); |
713 |
}}; |
714 |
testInvokeOnPool(mainPool, a); |
715 |
} |
716 |
|
717 |
/** |
718 |
* getPool of non-FJ task returns null |
719 |
*/ |
720 |
public void testGetPool2() { |
721 |
RecursiveAction a = new CheckedRecursiveAction() { |
722 |
public void realCompute() { |
723 |
assertNull(getPool()); |
724 |
}}; |
725 |
assertNull(a.invoke()); |
726 |
} |
727 |
|
728 |
/** |
729 |
* inForkJoinPool of executing task returns true |
730 |
*/ |
731 |
public void testInForkJoinPool() { |
732 |
RecursiveAction a = new CheckedRecursiveAction() { |
733 |
public void realCompute() { |
734 |
assertTrue(inForkJoinPool()); |
735 |
}}; |
736 |
testInvokeOnPool(mainPool(), a); |
737 |
} |
738 |
|
739 |
/** |
740 |
* inForkJoinPool of non-FJ task returns false |
741 |
*/ |
742 |
public void testInForkJoinPool2() { |
743 |
RecursiveAction a = new CheckedRecursiveAction() { |
744 |
public void realCompute() { |
745 |
assertFalse(inForkJoinPool()); |
746 |
}}; |
747 |
assertNull(a.invoke()); |
748 |
} |
749 |
|
750 |
/** |
751 |
* getPool of current thread in pool returns its pool |
752 |
*/ |
753 |
public void testWorkerGetPool() { |
754 |
final ForkJoinPool mainPool = mainPool(); |
755 |
RecursiveAction a = new CheckedRecursiveAction() { |
756 |
public void realCompute() { |
757 |
ForkJoinWorkerThread w = |
758 |
(ForkJoinWorkerThread) Thread.currentThread(); |
759 |
assertSame(mainPool, w.getPool()); |
760 |
}}; |
761 |
testInvokeOnPool(mainPool, a); |
762 |
} |
763 |
|
764 |
/** |
765 |
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
766 |
*/ |
767 |
public void testWorkerGetPoolIndex() { |
768 |
final ForkJoinPool mainPool = mainPool(); |
769 |
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
public void realCompute() { |
771 |
ForkJoinWorkerThread w = |
772 |
(ForkJoinWorkerThread) Thread.currentThread(); |
773 |
assertTrue(w.getPoolIndex() >= 0); |
774 |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
775 |
}}; |
776 |
testInvokeOnPool(mainPool, a); |
777 |
} |
778 |
|
779 |
|
780 |
/** |
781 |
* setRawResult(null) succeeds |
782 |
*/ |
783 |
public void testSetRawResult() { |
784 |
RecursiveAction a = new CheckedRecursiveAction() { |
785 |
public void realCompute() { |
786 |
setRawResult(null); |
787 |
assertNull(getRawResult()); |
788 |
}}; |
789 |
assertNull(a.invoke()); |
790 |
} |
791 |
|
792 |
/** |
793 |
* A reinitialized task may be re-invoked |
794 |
*/ |
795 |
public void testReinitialize() { |
796 |
RecursiveAction a = new CheckedRecursiveAction() { |
797 |
public void realCompute() { |
798 |
FibAction f = new FibAction(8); |
799 |
checkNotDone(f); |
800 |
|
801 |
for (int i = 0; i < 3; i++) { |
802 |
assertNull(f.invoke()); |
803 |
assertEquals(21, f.result); |
804 |
checkCompletedNormally(f); |
805 |
f.reinitialize(); |
806 |
checkNotDone(f); |
807 |
} |
808 |
}}; |
809 |
testInvokeOnPool(mainPool(), a); |
810 |
} |
811 |
|
812 |
/** |
813 |
* invoke task throws exception after invoking completeExceptionally |
814 |
*/ |
815 |
public void testCompleteExceptionally() { |
816 |
RecursiveAction a = new CheckedRecursiveAction() { |
817 |
public void realCompute() { |
818 |
FibAction f = new FibAction(8); |
819 |
f.completeExceptionally(new FJException()); |
820 |
try { |
821 |
f.invoke(); |
822 |
shouldThrow(); |
823 |
} catch (FJException success) { |
824 |
checkCompletedAbnormally(f, success); |
825 |
} |
826 |
}}; |
827 |
testInvokeOnPool(mainPool(), a); |
828 |
} |
829 |
|
830 |
/** |
831 |
* invoke task suppresses execution invoking complete |
832 |
*/ |
833 |
public void testComplete() { |
834 |
RecursiveAction a = new CheckedRecursiveAction() { |
835 |
public void realCompute() { |
836 |
FibAction f = new FibAction(8); |
837 |
f.complete(null); |
838 |
assertNull(f.invoke()); |
839 |
assertEquals(0, f.result); |
840 |
checkCompletedNormally(f); |
841 |
}}; |
842 |
testInvokeOnPool(mainPool(), a); |
843 |
} |
844 |
|
845 |
/** |
846 |
* invokeAll(t1, t2) invokes all task arguments |
847 |
*/ |
848 |
public void testInvokeAll2() { |
849 |
RecursiveAction a = new CheckedRecursiveAction() { |
850 |
public void realCompute() { |
851 |
FibAction f = new FibAction(8); |
852 |
FibAction g = new FibAction(9); |
853 |
invokeAll(f, g); |
854 |
checkCompletedNormally(f); |
855 |
assertEquals(21, f.result); |
856 |
checkCompletedNormally(g); |
857 |
assertEquals(34, g.result); |
858 |
}}; |
859 |
testInvokeOnPool(mainPool(), a); |
860 |
} |
861 |
|
862 |
/** |
863 |
* invokeAll(tasks) with 1 argument invokes task |
864 |
*/ |
865 |
public void testInvokeAll1() { |
866 |
RecursiveAction a = new CheckedRecursiveAction() { |
867 |
public void realCompute() { |
868 |
FibAction f = new FibAction(8); |
869 |
invokeAll(f); |
870 |
checkCompletedNormally(f); |
871 |
assertEquals(21, f.result); |
872 |
}}; |
873 |
testInvokeOnPool(mainPool(), a); |
874 |
} |
875 |
|
876 |
/** |
877 |
* invokeAll(tasks) with > 2 argument invokes tasks |
878 |
*/ |
879 |
public void testInvokeAll3() { |
880 |
RecursiveAction a = new CheckedRecursiveAction() { |
881 |
public void realCompute() { |
882 |
FibAction f = new FibAction(8); |
883 |
FibAction g = new FibAction(9); |
884 |
FibAction h = new FibAction(7); |
885 |
invokeAll(f, g, h); |
886 |
assertTrue(f.isDone()); |
887 |
assertTrue(g.isDone()); |
888 |
assertTrue(h.isDone()); |
889 |
checkCompletedNormally(f); |
890 |
assertEquals(21, f.result); |
891 |
checkCompletedNormally(g); |
892 |
assertEquals(34, g.result); |
893 |
checkCompletedNormally(g); |
894 |
assertEquals(13, h.result); |
895 |
}}; |
896 |
testInvokeOnPool(mainPool(), a); |
897 |
} |
898 |
|
899 |
/** |
900 |
* invokeAll(collection) invokes all tasks in the collection |
901 |
*/ |
902 |
public void testInvokeAllCollection() { |
903 |
RecursiveAction a = new CheckedRecursiveAction() { |
904 |
public void realCompute() { |
905 |
FibAction f = new FibAction(8); |
906 |
FibAction g = new FibAction(9); |
907 |
FibAction h = new FibAction(7); |
908 |
HashSet set = new HashSet(); |
909 |
set.add(f); |
910 |
set.add(g); |
911 |
set.add(h); |
912 |
invokeAll(set); |
913 |
assertTrue(f.isDone()); |
914 |
assertTrue(g.isDone()); |
915 |
assertTrue(h.isDone()); |
916 |
checkCompletedNormally(f); |
917 |
assertEquals(21, f.result); |
918 |
checkCompletedNormally(g); |
919 |
assertEquals(34, g.result); |
920 |
checkCompletedNormally(g); |
921 |
assertEquals(13, h.result); |
922 |
}}; |
923 |
testInvokeOnPool(mainPool(), a); |
924 |
} |
925 |
|
926 |
|
927 |
/** |
928 |
* invokeAll(tasks) with any null task throws NPE |
929 |
*/ |
930 |
public void testInvokeAllNPE() { |
931 |
RecursiveAction a = new CheckedRecursiveAction() { |
932 |
public void realCompute() { |
933 |
FibAction f = new FibAction(8); |
934 |
FibAction g = new FibAction(9); |
935 |
FibAction h = null; |
936 |
try { |
937 |
invokeAll(f, g, h); |
938 |
shouldThrow(); |
939 |
} catch (NullPointerException success) {} |
940 |
}}; |
941 |
testInvokeOnPool(mainPool(), a); |
942 |
} |
943 |
|
944 |
/** |
945 |
* invokeAll(t1, t2) throw exception if any task does |
946 |
*/ |
947 |
public void testAbnormalInvokeAll2() { |
948 |
RecursiveAction a = new CheckedRecursiveAction() { |
949 |
public void realCompute() { |
950 |
FibAction f = new FibAction(8); |
951 |
FailingFibAction g = new FailingFibAction(9); |
952 |
try { |
953 |
invokeAll(f, g); |
954 |
shouldThrow(); |
955 |
} catch (FJException success) { |
956 |
checkCompletedAbnormally(g, success); |
957 |
} |
958 |
}}; |
959 |
testInvokeOnPool(mainPool(), a); |
960 |
} |
961 |
|
962 |
/** |
963 |
* invokeAll(tasks) with 1 argument throws exception if task does |
964 |
*/ |
965 |
public void testAbnormalInvokeAll1() { |
966 |
RecursiveAction a = new CheckedRecursiveAction() { |
967 |
public void realCompute() { |
968 |
FailingFibAction g = new FailingFibAction(9); |
969 |
try { |
970 |
invokeAll(g); |
971 |
shouldThrow(); |
972 |
} catch (FJException success) { |
973 |
checkCompletedAbnormally(g, success); |
974 |
} |
975 |
}}; |
976 |
testInvokeOnPool(mainPool(), a); |
977 |
} |
978 |
|
979 |
/** |
980 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
981 |
*/ |
982 |
public void testAbnormalInvokeAll3() { |
983 |
RecursiveAction a = new CheckedRecursiveAction() { |
984 |
public void realCompute() { |
985 |
FibAction f = new FibAction(8); |
986 |
FailingFibAction g = new FailingFibAction(9); |
987 |
FibAction h = new FibAction(7); |
988 |
try { |
989 |
invokeAll(f, g, h); |
990 |
shouldThrow(); |
991 |
} catch (FJException success) { |
992 |
checkCompletedAbnormally(g, success); |
993 |
} |
994 |
}}; |
995 |
testInvokeOnPool(mainPool(), a); |
996 |
} |
997 |
|
998 |
/** |
999 |
* invokeAll(collection) throws exception if any task does |
1000 |
*/ |
1001 |
public void testAbnormalInvokeAllCollection() { |
1002 |
RecursiveAction a = new CheckedRecursiveAction() { |
1003 |
public void realCompute() { |
1004 |
FailingFibAction f = new FailingFibAction(8); |
1005 |
FibAction g = new FibAction(9); |
1006 |
FibAction h = new FibAction(7); |
1007 |
HashSet set = new HashSet(); |
1008 |
set.add(f); |
1009 |
set.add(g); |
1010 |
set.add(h); |
1011 |
try { |
1012 |
invokeAll(set); |
1013 |
shouldThrow(); |
1014 |
} catch (FJException success) { |
1015 |
checkCompletedAbnormally(f, success); |
1016 |
} |
1017 |
}}; |
1018 |
testInvokeOnPool(mainPool(), a); |
1019 |
} |
1020 |
|
1021 |
/** |
1022 |
* tryUnfork returns true for most recent unexecuted task, |
1023 |
* and suppresses execution |
1024 |
*/ |
1025 |
public void testTryUnfork() { |
1026 |
RecursiveAction a = new CheckedRecursiveAction() { |
1027 |
public void realCompute() { |
1028 |
FibAction g = new FibAction(9); |
1029 |
assertSame(g, g.fork()); |
1030 |
FibAction f = new FibAction(8); |
1031 |
assertSame(f, f.fork()); |
1032 |
assertTrue(f.tryUnfork()); |
1033 |
helpQuiesce(); |
1034 |
checkNotDone(f); |
1035 |
checkCompletedNormally(g); |
1036 |
}}; |
1037 |
testInvokeOnPool(singletonPool(), a); |
1038 |
} |
1039 |
|
1040 |
/** |
1041 |
* getSurplusQueuedTaskCount returns > 0 when |
1042 |
* there are more tasks than threads |
1043 |
*/ |
1044 |
public void testGetSurplusQueuedTaskCount() { |
1045 |
RecursiveAction a = new CheckedRecursiveAction() { |
1046 |
public void realCompute() { |
1047 |
FibAction h = new FibAction(7); |
1048 |
assertSame(h, h.fork()); |
1049 |
FibAction g = new FibAction(9); |
1050 |
assertSame(g, g.fork()); |
1051 |
FibAction f = new FibAction(8); |
1052 |
assertSame(f, f.fork()); |
1053 |
assertTrue(getSurplusQueuedTaskCount() > 0); |
1054 |
helpQuiesce(); |
1055 |
assertEquals(0, getSurplusQueuedTaskCount()); |
1056 |
checkCompletedNormally(f); |
1057 |
checkCompletedNormally(g); |
1058 |
checkCompletedNormally(h); |
1059 |
}}; |
1060 |
testInvokeOnPool(singletonPool(), a); |
1061 |
} |
1062 |
|
1063 |
/** |
1064 |
* peekNextLocalTask returns most recent unexecuted task. |
1065 |
*/ |
1066 |
public void testPeekNextLocalTask() { |
1067 |
RecursiveAction a = new CheckedRecursiveAction() { |
1068 |
public void realCompute() { |
1069 |
FibAction g = new FibAction(9); |
1070 |
assertSame(g, g.fork()); |
1071 |
FibAction f = new FibAction(8); |
1072 |
assertSame(f, f.fork()); |
1073 |
assertSame(f, peekNextLocalTask()); |
1074 |
assertNull(f.join()); |
1075 |
checkCompletedNormally(f); |
1076 |
helpQuiesce(); |
1077 |
checkCompletedNormally(f); |
1078 |
checkCompletedNormally(g); |
1079 |
}}; |
1080 |
testInvokeOnPool(singletonPool(), a); |
1081 |
} |
1082 |
|
1083 |
/** |
1084 |
* pollNextLocalTask returns most recent unexecuted task |
1085 |
* without executing it |
1086 |
*/ |
1087 |
public void testPollNextLocalTask() { |
1088 |
RecursiveAction a = new CheckedRecursiveAction() { |
1089 |
public void realCompute() { |
1090 |
FibAction g = new FibAction(9); |
1091 |
assertSame(g, g.fork()); |
1092 |
FibAction f = new FibAction(8); |
1093 |
assertSame(f, f.fork()); |
1094 |
assertSame(f, pollNextLocalTask()); |
1095 |
helpQuiesce(); |
1096 |
checkNotDone(f); |
1097 |
checkCompletedNormally(g); |
1098 |
}}; |
1099 |
testInvokeOnPool(singletonPool(), a); |
1100 |
} |
1101 |
|
1102 |
/** |
1103 |
* pollTask returns an unexecuted task without executing it |
1104 |
*/ |
1105 |
public void testPollTask() { |
1106 |
RecursiveAction a = new CheckedRecursiveAction() { |
1107 |
public void realCompute() { |
1108 |
FibAction g = new FibAction(9); |
1109 |
assertSame(g, g.fork()); |
1110 |
FibAction f = new FibAction(8); |
1111 |
assertSame(f, f.fork()); |
1112 |
assertSame(f, pollTask()); |
1113 |
helpQuiesce(); |
1114 |
checkNotDone(f); |
1115 |
checkCompletedNormally(g); |
1116 |
}}; |
1117 |
testInvokeOnPool(singletonPool(), a); |
1118 |
} |
1119 |
|
1120 |
/** |
1121 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
1122 |
*/ |
1123 |
public void testPeekNextLocalTaskAsync() { |
1124 |
RecursiveAction a = new CheckedRecursiveAction() { |
1125 |
public void realCompute() { |
1126 |
FibAction g = new FibAction(9); |
1127 |
assertSame(g, g.fork()); |
1128 |
FibAction f = new FibAction(8); |
1129 |
assertSame(f, f.fork()); |
1130 |
assertSame(g, peekNextLocalTask()); |
1131 |
assertNull(f.join()); |
1132 |
helpQuiesce(); |
1133 |
checkCompletedNormally(f); |
1134 |
checkCompletedNormally(g); |
1135 |
}}; |
1136 |
testInvokeOnPool(asyncSingletonPool(), a); |
1137 |
} |
1138 |
|
1139 |
/** |
1140 |
* pollNextLocalTask returns least recent unexecuted task without |
1141 |
* executing it, in async mode |
1142 |
*/ |
1143 |
public void testPollNextLocalTaskAsync() { |
1144 |
RecursiveAction a = new CheckedRecursiveAction() { |
1145 |
public void realCompute() { |
1146 |
FibAction g = new FibAction(9); |
1147 |
assertSame(g, g.fork()); |
1148 |
FibAction f = new FibAction(8); |
1149 |
assertSame(f, f.fork()); |
1150 |
assertSame(g, pollNextLocalTask()); |
1151 |
helpQuiesce(); |
1152 |
checkCompletedNormally(f); |
1153 |
checkNotDone(g); |
1154 |
}}; |
1155 |
testInvokeOnPool(asyncSingletonPool(), a); |
1156 |
} |
1157 |
|
1158 |
/** |
1159 |
* pollTask returns an unexecuted task without executing it, in |
1160 |
* async mode |
1161 |
*/ |
1162 |
public void testPollTaskAsync() { |
1163 |
RecursiveAction a = new CheckedRecursiveAction() { |
1164 |
public void realCompute() { |
1165 |
FibAction g = new FibAction(9); |
1166 |
assertSame(g, g.fork()); |
1167 |
FibAction f = new FibAction(8); |
1168 |
assertSame(f, f.fork()); |
1169 |
assertSame(g, pollTask()); |
1170 |
helpQuiesce(); |
1171 |
checkCompletedNormally(f); |
1172 |
checkNotDone(g); |
1173 |
}}; |
1174 |
testInvokeOnPool(asyncSingletonPool(), a); |
1175 |
} |
1176 |
|
1177 |
} |