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