11 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
12 |
|
|
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(RecursiveTaskTest.class); |
32 |
|
static final Integer NoResult = Integer.valueOf(-17); |
33 |
|
|
34 |
|
// A simple recursive task for testing |
35 |
< |
static final class FibTask extends RecursiveTask<Integer> { |
35 |
> |
static final class FibTask extends RecursiveTask<Integer> { |
36 |
|
final int number; |
37 |
|
FibTask(int n) { number = n; } |
38 |
|
public Integer compute() { |
46 |
|
} |
47 |
|
|
48 |
|
// A recursive action failing in base case |
49 |
< |
static final class FailingFibTask extends RecursiveTask<Integer> { |
49 |
> |
static final class FailingFibTask extends RecursiveTask<Integer> { |
50 |
|
final int number; |
51 |
|
int result; |
52 |
|
FailingFibTask(int n) { number = n; } |
60 |
|
} |
61 |
|
} |
62 |
|
|
63 |
< |
/** |
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 |
< |
* |
68 |
> |
* |
69 |
|
*/ |
70 |
|
public void testInvoke() { |
71 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
83 |
|
assertTrue(mainPool.invoke(a) == 21); |
84 |
|
} |
85 |
|
|
86 |
< |
/** |
86 |
> |
/** |
87 |
|
* quietlyInvoke task returns when task completes normally. |
88 |
|
* isCompletedAbnormally and isCancelled return false for normally |
89 |
|
* completed tasks |
104 |
|
assertTrue(mainPool.invoke(a) == 21); |
105 |
|
} |
106 |
|
|
107 |
< |
/** |
107 |
> |
/** |
108 |
|
* join of a forked task returns when task completes |
109 |
|
*/ |
110 |
|
public void testForkJoin() { |
121 |
|
assertTrue(mainPool.invoke(a) == 21); |
122 |
|
} |
123 |
|
|
124 |
< |
/** |
124 |
> |
/** |
125 |
|
* get of a forked task returns when task completes |
126 |
|
*/ |
127 |
|
public void testForkGet() { |
134 |
|
threadAssertTrue(r == 21); |
135 |
|
threadAssertTrue(f.isDone()); |
136 |
|
return r; |
137 |
< |
} catch(Exception ex) { |
137 |
> |
} catch (Exception ex) { |
138 |
|
unexpectedException(); |
139 |
|
} |
140 |
|
return NoResult; |
143 |
|
assertTrue(mainPool.invoke(a) == 21); |
144 |
|
} |
145 |
|
|
146 |
< |
/** |
146 |
> |
/** |
147 |
|
* timed get of a forked task returns when task completes |
148 |
|
*/ |
149 |
|
public void testForkTimedGet() { |
156 |
|
threadAssertTrue(r == 21); |
157 |
|
threadAssertTrue(f.isDone()); |
158 |
|
return r; |
159 |
< |
} catch(Exception ex) { |
159 |
> |
} catch (Exception ex) { |
160 |
|
unexpectedException(); |
161 |
|
} |
162 |
|
return NoResult; |
165 |
|
assertTrue(mainPool.invoke(a) == 21); |
166 |
|
} |
167 |
|
|
168 |
< |
/** |
168 |
> |
/** |
169 |
|
* helpJoin of a forked task returns when task completes |
170 |
|
*/ |
171 |
|
public void testForkHelpJoin() { |
182 |
|
assertTrue(mainPool.invoke(a) == 21); |
183 |
|
} |
184 |
|
|
185 |
< |
/** |
185 |
> |
/** |
186 |
|
* quietlyJoin of a forked task returns when task completes |
187 |
|
*/ |
188 |
|
public void testForkQuietlyJoin() { |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
< |
/** |
204 |
> |
/** |
205 |
|
* quietlyHelpJoin of a forked task returns when task completes |
206 |
|
*/ |
207 |
|
public void testForkQuietlyHelpJoin() { |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
< |
/** |
223 |
> |
/** |
224 |
|
* helpQuiesce returns when tasks are complete. |
225 |
|
* getQueuedTaskCount returns 0 when quiescent |
226 |
|
*/ |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
< |
/** |
244 |
> |
/** |
245 |
|
* invoke task throws exception when task completes abnormally |
246 |
|
*/ |
247 |
|
public void testAbnormalInvoke() { |
252 |
|
f.invoke(); |
253 |
|
shouldThrow(); |
254 |
|
return NoResult; |
255 |
< |
} catch(FJException success) { |
255 |
> |
} catch (FJException success) { |
256 |
|
} |
257 |
|
return NoResult; |
258 |
|
} |
260 |
|
mainPool.invoke(a); |
261 |
|
} |
262 |
|
|
263 |
< |
/** |
264 |
< |
* quietelyInvoke task returns when task completes abnormally |
263 |
> |
/** |
264 |
> |
* quietlyInvoke task returns when task completes abnormally |
265 |
|
*/ |
266 |
|
public void testAbnormalQuietlyInvoke() { |
267 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
275 |
|
mainPool.invoke(a); |
276 |
|
} |
277 |
|
|
278 |
< |
/** |
278 |
> |
/** |
279 |
|
* join of a forked task throws exception when task completes abnormally |
280 |
|
*/ |
281 |
|
public void testAbnormalForkJoin() { |
287 |
|
Integer r = f.join(); |
288 |
|
shouldThrow(); |
289 |
|
return r; |
290 |
< |
} catch(FJException success) { |
290 |
> |
} catch (FJException success) { |
291 |
|
} |
292 |
|
return NoResult; |
293 |
|
} |
295 |
|
mainPool.invoke(a); |
296 |
|
} |
297 |
|
|
298 |
< |
/** |
298 |
> |
/** |
299 |
|
* get of a forked task throws exception when task completes abnormally |
300 |
|
*/ |
301 |
|
public void testAbnormalForkGet() { |
307 |
|
Integer r = f.get(); |
308 |
|
shouldThrow(); |
309 |
|
return r; |
310 |
< |
} catch(Exception success) { |
310 |
> |
} catch (ExecutionException success) { |
311 |
> |
} catch (Exception ex) { |
312 |
> |
unexpectedException(ex); |
313 |
|
} |
314 |
|
return NoResult; |
315 |
|
} |
317 |
|
mainPool.invoke(a); |
318 |
|
} |
319 |
|
|
320 |
< |
/** |
320 |
> |
/** |
321 |
|
* timed get of a forked task throws exception when task completes abnormally |
322 |
|
*/ |
323 |
|
public void testAbnormalForkTimedGet() { |
329 |
|
Integer r = f.get(5L, TimeUnit.SECONDS); |
330 |
|
shouldThrow(); |
331 |
|
return r; |
332 |
< |
} catch(Exception success) { |
332 |
> |
} catch (ExecutionException success) { |
333 |
> |
} catch (Exception ex) { |
334 |
> |
unexpectedException(ex); |
335 |
|
} |
336 |
|
return NoResult; |
337 |
|
} |
339 |
|
mainPool.invoke(a); |
340 |
|
} |
341 |
|
|
342 |
< |
/** |
342 |
> |
/** |
343 |
|
* join of a forked task throws exception when task completes abnormally |
344 |
|
*/ |
345 |
|
public void testAbnormalForkHelpJoin() { |
351 |
|
Integer r = f.helpJoin(); |
352 |
|
shouldThrow(); |
353 |
|
return r; |
354 |
< |
} catch(FJException success) { |
354 |
> |
} catch (FJException success) { |
355 |
|
} |
356 |
|
return NoResult; |
357 |
|
} |
359 |
|
mainPool.invoke(a); |
360 |
|
} |
361 |
|
|
362 |
< |
/** |
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. |
381 |
|
mainPool.invoke(a); |
382 |
|
} |
383 |
|
|
384 |
< |
/** |
384 |
> |
/** |
385 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
386 |
|
*/ |
387 |
|
public void testAbnormalForkQuietlyJoin() { |
399 |
|
mainPool.invoke(a); |
400 |
|
} |
401 |
|
|
402 |
< |
/** |
402 |
> |
/** |
403 |
|
* invoke task throws exception when task cancelled |
404 |
|
*/ |
405 |
|
public void testCancelledInvoke() { |
411 |
|
Integer r = f.invoke(); |
412 |
|
shouldThrow(); |
413 |
|
return r; |
414 |
< |
} catch(CancellationException success) { |
414 |
> |
} catch (CancellationException success) { |
415 |
|
} |
416 |
|
return NoResult; |
417 |
|
} |
419 |
|
mainPool.invoke(a); |
420 |
|
} |
421 |
|
|
422 |
< |
/** |
422 |
> |
/** |
423 |
|
* join of a forked task throws exception when task cancelled |
424 |
|
*/ |
425 |
|
public void testCancelledForkJoin() { |
432 |
|
Integer r = f.join(); |
433 |
|
shouldThrow(); |
434 |
|
return r; |
435 |
< |
} catch(CancellationException success) { |
435 |
> |
} catch (CancellationException success) { |
436 |
|
} |
437 |
|
return NoResult; |
438 |
|
} |
440 |
|
mainPool.invoke(a); |
441 |
|
} |
442 |
|
|
443 |
< |
/** |
443 |
> |
/** |
444 |
|
* get of a forked task throws exception when task cancelled |
445 |
|
*/ |
446 |
|
public void testCancelledForkGet() { |
453 |
|
Integer r = f.get(); |
454 |
|
shouldThrow(); |
455 |
|
return r; |
456 |
< |
} catch(Exception success) { |
456 |
> |
} catch (CancellationException success) { |
457 |
> |
} catch (Exception ex) { |
458 |
> |
unexpectedException(ex); |
459 |
|
} |
460 |
|
return NoResult; |
461 |
|
} |
463 |
|
mainPool.invoke(a); |
464 |
|
} |
465 |
|
|
466 |
< |
/** |
466 |
> |
/** |
467 |
|
* timed get of a forked task throws exception when task cancelled |
468 |
|
*/ |
469 |
|
public void testCancelledForkTimedGet() { |
476 |
|
Integer r = f.get(5L, TimeUnit.SECONDS); |
477 |
|
shouldThrow(); |
478 |
|
return r; |
479 |
< |
} catch(Exception success) { |
479 |
> |
} catch (CancellationException success) { |
480 |
> |
} catch (Exception ex) { |
481 |
> |
unexpectedException(ex); |
482 |
|
} |
483 |
|
return NoResult; |
484 |
|
} |
486 |
|
mainPool.invoke(a); |
487 |
|
} |
488 |
|
|
489 |
< |
/** |
489 |
> |
/** |
490 |
|
* join of a forked task throws exception when task cancelled |
491 |
|
*/ |
492 |
|
public void testCancelledForkHelpJoin() { |
499 |
|
Integer r = f.helpJoin(); |
500 |
|
shouldThrow(); |
501 |
|
return r; |
502 |
< |
} catch(CancellationException success) { |
502 |
> |
} catch (CancellationException success) { |
503 |
|
} |
504 |
|
return NoResult; |
505 |
|
} |
507 |
|
mainPool.invoke(a); |
508 |
|
} |
509 |
|
|
510 |
< |
/** |
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. |
530 |
|
mainPool.invoke(a); |
531 |
|
} |
532 |
|
|
533 |
< |
/** |
533 |
> |
/** |
534 |
|
* quietlyJoin of a forked task returns when task cancelled |
535 |
|
*/ |
536 |
|
public void testCancelledForkQuietlyJoin() { |
574 |
|
}; |
575 |
|
a.invoke(); |
576 |
|
} |
577 |
< |
|
577 |
> |
|
578 |
|
/** |
579 |
|
* inForkJoinPool of executing task returns true |
580 |
|
*/ |
614 |
|
assertEquals(a.invoke(), NoResult); |
615 |
|
} |
616 |
|
|
617 |
< |
/** |
617 |
> |
/** |
618 |
|
* A reinitialized task may be re-invoked |
619 |
|
*/ |
620 |
|
public void testReinitialize() { |
635 |
|
mainPool.invoke(a); |
636 |
|
} |
637 |
|
|
638 |
< |
/** |
638 |
> |
/** |
639 |
|
* invoke task throws exception after invoking completeExceptionally |
640 |
|
*/ |
641 |
|
public void testCompleteExceptionally() { |
647 |
|
Integer r = f.invoke(); |
648 |
|
shouldThrow(); |
649 |
|
return r; |
650 |
< |
} catch(FJException success) { |
650 |
> |
} catch (FJException success) { |
651 |
|
} |
652 |
|
return NoResult; |
653 |
|
} |
655 |
|
mainPool.invoke(a); |
656 |
|
} |
657 |
|
|
658 |
< |
/** |
658 |
> |
/** |
659 |
|
* invoke task suppresses execution invoking complete |
660 |
|
*/ |
661 |
|
public void testComplete() { |
672 |
|
mainPool.invoke(a); |
673 |
|
} |
674 |
|
|
675 |
< |
/** |
675 |
> |
/** |
676 |
|
* invokeAll(t1, t2) invokes all task arguments |
677 |
|
*/ |
678 |
|
public void testInvokeAll2() { |
691 |
|
mainPool.invoke(a); |
692 |
|
} |
693 |
|
|
694 |
< |
/** |
694 |
> |
/** |
695 |
|
* invokeAll(tasks) with 1 argument invokes task |
696 |
|
*/ |
697 |
|
public void testInvokeAll1() { |
707 |
|
mainPool.invoke(a); |
708 |
|
} |
709 |
|
|
710 |
< |
/** |
710 |
> |
/** |
711 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
712 |
|
*/ |
713 |
|
public void testInvokeAll3() { |
729 |
|
mainPool.invoke(a); |
730 |
|
} |
731 |
|
|
732 |
< |
/** |
732 |
> |
/** |
733 |
|
* invokeAll(collection) invokes all tasks in the collection |
734 |
|
*/ |
735 |
|
public void testInvokeAllCollection() { |
756 |
|
} |
757 |
|
|
758 |
|
|
759 |
< |
/** |
759 |
> |
/** |
760 |
|
* invokeAll(t1, t2) throw exception if any task does |
761 |
|
*/ |
762 |
|
public void testAbnormalInvokeAll2() { |
768 |
|
invokeAll(f, g); |
769 |
|
shouldThrow(); |
770 |
|
return NoResult; |
771 |
< |
} catch(FJException success) { |
771 |
> |
} catch (FJException success) { |
772 |
|
} |
773 |
|
return NoResult; |
774 |
|
} |
776 |
|
mainPool.invoke(a); |
777 |
|
} |
778 |
|
|
779 |
< |
/** |
779 |
> |
/** |
780 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
781 |
|
*/ |
782 |
|
public void testAbnormalInvokeAll1() { |
787 |
|
invokeAll(g); |
788 |
|
shouldThrow(); |
789 |
|
return NoResult; |
790 |
< |
} catch(FJException success) { |
790 |
> |
} catch (FJException success) { |
791 |
|
} |
792 |
|
return NoResult; |
793 |
|
} |
795 |
|
mainPool.invoke(a); |
796 |
|
} |
797 |
|
|
798 |
< |
/** |
798 |
> |
/** |
799 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
800 |
|
*/ |
801 |
|
public void testAbnormalInvokeAll3() { |
808 |
|
invokeAll(f, g, h); |
809 |
|
shouldThrow(); |
810 |
|
return NoResult; |
811 |
< |
} catch(FJException success) { |
811 |
> |
} catch (FJException success) { |
812 |
|
} |
813 |
|
return NoResult; |
814 |
|
} |
816 |
|
mainPool.invoke(a); |
817 |
|
} |
818 |
|
|
819 |
< |
/** |
820 |
< |
* invokeAll(collection) throws exception if any task does |
819 |
> |
/** |
820 |
> |
* invokeAll(collection) throws exception if any task does |
821 |
|
*/ |
822 |
|
public void testAbnormalInvokeAllCollection() { |
823 |
|
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
833 |
|
invokeAll(set); |
834 |
|
shouldThrow(); |
835 |
|
return NoResult; |
836 |
< |
} catch(FJException success) { |
836 |
> |
} catch (FJException success) { |
837 |
|
} |
838 |
|
return NoResult; |
839 |
|
} |
841 |
|
mainPool.invoke(a); |
842 |
|
} |
843 |
|
|
844 |
< |
/** |
844 |
> |
/** |
845 |
|
* tryUnfork returns true for most recent unexecuted task, |
846 |
|
* and suppresses execution |
847 |
|
*/ |
862 |
|
singletonPool.invoke(a); |
863 |
|
} |
864 |
|
|
865 |
< |
/** |
865 |
> |
/** |
866 |
|
* getSurplusQueuedTaskCount returns > 0 when |
867 |
|
* there are more tasks than threads |
868 |
|
*/ |
883 |
|
singletonPool.invoke(a); |
884 |
|
} |
885 |
|
|
886 |
< |
/** |
886 |
> |
/** |
887 |
|
* peekNextLocalTask returns most recent unexecuted task. |
888 |
|
*/ |
889 |
|
public void testPeekNextLocalTask() { |
903 |
|
singletonPool.invoke(a); |
904 |
|
} |
905 |
|
|
906 |
< |
/** |
906 |
> |
/** |
907 |
|
* pollNextLocalTask returns most recent unexecuted task |
908 |
|
* without executing it |
909 |
|
*/ |
923 |
|
singletonPool.invoke(a); |
924 |
|
} |
925 |
|
|
926 |
< |
/** |
926 |
> |
/** |
927 |
|
* pollTask returns an unexecuted task |
928 |
|
* without executing it |
929 |
|
*/ |
944 |
|
singletonPool.invoke(a); |
945 |
|
} |
946 |
|
|
947 |
< |
/** |
947 |
> |
/** |
948 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
949 |
|
*/ |
950 |
|
public void testPeekNextLocalTaskAsync() { |
964 |
|
asyncSingletonPool.invoke(a); |
965 |
|
} |
966 |
|
|
967 |
< |
/** |
967 |
> |
/** |
968 |
|
* pollNextLocalTask returns least recent unexecuted task |
969 |
|
* without executing it, in async mode |
970 |
|
*/ |
985 |
|
asyncSingletonPool.invoke(a); |
986 |
|
} |
987 |
|
|
988 |
< |
/** |
988 |
> |
/** |
989 |
|
* pollTask returns an unexecuted task |
990 |
|
* without executing it, in async mode |
991 |
|
*/ |