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