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