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