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