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 |
< |
/** |
263 |
> |
/** |
264 |
|
* quietelyInvoke task returns when task completes abnormally |
265 |
|
*/ |
266 |
|
public void testAbnormalQuietlyInvoke() { |
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 (Exception success) { |
311 |
|
} |
312 |
|
return NoResult; |
313 |
|
} |
315 |
|
mainPool.invoke(a); |
316 |
|
} |
317 |
|
|
318 |
< |
/** |
318 |
> |
/** |
319 |
|
* timed get of a forked task throws exception when task completes abnormally |
320 |
|
*/ |
321 |
|
public void testAbnormalForkTimedGet() { |
327 |
|
Integer r = f.get(5L, TimeUnit.SECONDS); |
328 |
|
shouldThrow(); |
329 |
|
return r; |
330 |
< |
} catch(Exception success) { |
330 |
> |
} catch (Exception success) { |
331 |
|
} |
332 |
|
return NoResult; |
333 |
|
} |
335 |
|
mainPool.invoke(a); |
336 |
|
} |
337 |
|
|
338 |
< |
/** |
338 |
> |
/** |
339 |
|
* join of a forked task throws exception when task completes abnormally |
340 |
|
*/ |
341 |
|
public void testAbnormalForkHelpJoin() { |
347 |
|
Integer r = f.helpJoin(); |
348 |
|
shouldThrow(); |
349 |
|
return r; |
350 |
< |
} catch(FJException success) { |
350 |
> |
} catch (FJException success) { |
351 |
|
} |
352 |
|
return NoResult; |
353 |
|
} |
355 |
|
mainPool.invoke(a); |
356 |
|
} |
357 |
|
|
358 |
< |
/** |
358 |
> |
/** |
359 |
|
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
360 |
|
* getException of failed task returns its exception. |
361 |
|
* isCompletedAbnormally of a failed task returns true. |
377 |
|
mainPool.invoke(a); |
378 |
|
} |
379 |
|
|
380 |
< |
/** |
380 |
> |
/** |
381 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
382 |
|
*/ |
383 |
|
public void testAbnormalForkQuietlyJoin() { |
395 |
|
mainPool.invoke(a); |
396 |
|
} |
397 |
|
|
398 |
< |
/** |
398 |
> |
/** |
399 |
|
* invoke task throws exception when task cancelled |
400 |
|
*/ |
401 |
|
public void testCancelledInvoke() { |
407 |
|
Integer r = f.invoke(); |
408 |
|
shouldThrow(); |
409 |
|
return r; |
410 |
< |
} catch(CancellationException success) { |
410 |
> |
} catch (CancellationException success) { |
411 |
|
} |
412 |
|
return NoResult; |
413 |
|
} |
415 |
|
mainPool.invoke(a); |
416 |
|
} |
417 |
|
|
418 |
< |
/** |
418 |
> |
/** |
419 |
|
* join of a forked task throws exception when task cancelled |
420 |
|
*/ |
421 |
|
public void testCancelledForkJoin() { |
428 |
|
Integer r = f.join(); |
429 |
|
shouldThrow(); |
430 |
|
return r; |
431 |
< |
} catch(CancellationException success) { |
431 |
> |
} catch (CancellationException success) { |
432 |
|
} |
433 |
|
return NoResult; |
434 |
|
} |
436 |
|
mainPool.invoke(a); |
437 |
|
} |
438 |
|
|
439 |
< |
/** |
439 |
> |
/** |
440 |
|
* get of a forked task throws exception when task cancelled |
441 |
|
*/ |
442 |
|
public void testCancelledForkGet() { |
449 |
|
Integer r = f.get(); |
450 |
|
shouldThrow(); |
451 |
|
return r; |
452 |
< |
} catch(Exception success) { |
452 |
> |
} catch (Exception success) { |
453 |
|
} |
454 |
|
return NoResult; |
455 |
|
} |
457 |
|
mainPool.invoke(a); |
458 |
|
} |
459 |
|
|
460 |
< |
/** |
460 |
> |
/** |
461 |
|
* timed get of a forked task throws exception when task cancelled |
462 |
|
*/ |
463 |
|
public void testCancelledForkTimedGet() { |
470 |
|
Integer r = f.get(5L, TimeUnit.SECONDS); |
471 |
|
shouldThrow(); |
472 |
|
return r; |
473 |
< |
} catch(Exception success) { |
473 |
> |
} catch (Exception success) { |
474 |
|
} |
475 |
|
return NoResult; |
476 |
|
} |
478 |
|
mainPool.invoke(a); |
479 |
|
} |
480 |
|
|
481 |
< |
/** |
481 |
> |
/** |
482 |
|
* join of a forked task throws exception when task cancelled |
483 |
|
*/ |
484 |
|
public void testCancelledForkHelpJoin() { |
491 |
|
Integer r = f.helpJoin(); |
492 |
|
shouldThrow(); |
493 |
|
return r; |
494 |
< |
} catch(CancellationException success) { |
494 |
> |
} catch (CancellationException success) { |
495 |
|
} |
496 |
|
return NoResult; |
497 |
|
} |
499 |
|
mainPool.invoke(a); |
500 |
|
} |
501 |
|
|
502 |
< |
/** |
502 |
> |
/** |
503 |
|
* quietlyHelpJoin of a forked task returns when task cancelled. |
504 |
|
* getException of cancelled task returns its exception |
505 |
|
* isCompletedAbnormally of a cancelled task returns true. |
522 |
|
mainPool.invoke(a); |
523 |
|
} |
524 |
|
|
525 |
< |
/** |
525 |
> |
/** |
526 |
|
* quietlyJoin of a forked task returns when task cancelled |
527 |
|
*/ |
528 |
|
public void testCancelledForkQuietlyJoin() { |
566 |
|
}; |
567 |
|
a.invoke(); |
568 |
|
} |
569 |
< |
|
569 |
> |
|
570 |
|
/** |
571 |
|
* inForkJoinPool of executing task returns true |
572 |
|
*/ |
606 |
|
assertEquals(a.invoke(), NoResult); |
607 |
|
} |
608 |
|
|
609 |
< |
/** |
609 |
> |
/** |
610 |
|
* A reinitialized task may be re-invoked |
611 |
|
*/ |
612 |
|
public void testReinitialize() { |
627 |
|
mainPool.invoke(a); |
628 |
|
} |
629 |
|
|
630 |
< |
/** |
630 |
> |
/** |
631 |
|
* invoke task throws exception after invoking completeExceptionally |
632 |
|
*/ |
633 |
|
public void testCompleteExceptionally() { |
639 |
|
Integer r = f.invoke(); |
640 |
|
shouldThrow(); |
641 |
|
return r; |
642 |
< |
} catch(FJException success) { |
642 |
> |
} catch (FJException success) { |
643 |
|
} |
644 |
|
return NoResult; |
645 |
|
} |
647 |
|
mainPool.invoke(a); |
648 |
|
} |
649 |
|
|
650 |
< |
/** |
650 |
> |
/** |
651 |
|
* invoke task suppresses execution invoking complete |
652 |
|
*/ |
653 |
|
public void testComplete() { |
664 |
|
mainPool.invoke(a); |
665 |
|
} |
666 |
|
|
667 |
< |
/** |
667 |
> |
/** |
668 |
|
* invokeAll(t1, t2) invokes all task arguments |
669 |
|
*/ |
670 |
|
public void testInvokeAll2() { |
683 |
|
mainPool.invoke(a); |
684 |
|
} |
685 |
|
|
686 |
< |
/** |
686 |
> |
/** |
687 |
|
* invokeAll(tasks) with 1 argument invokes task |
688 |
|
*/ |
689 |
|
public void testInvokeAll1() { |
699 |
|
mainPool.invoke(a); |
700 |
|
} |
701 |
|
|
702 |
< |
/** |
702 |
> |
/** |
703 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
704 |
|
*/ |
705 |
|
public void testInvokeAll3() { |
721 |
|
mainPool.invoke(a); |
722 |
|
} |
723 |
|
|
724 |
< |
/** |
724 |
> |
/** |
725 |
|
* invokeAll(collection) invokes all tasks in the collection |
726 |
|
*/ |
727 |
|
public void testInvokeAllCollection() { |
748 |
|
} |
749 |
|
|
750 |
|
|
751 |
< |
/** |
751 |
> |
/** |
752 |
|
* invokeAll(t1, t2) throw exception if any task does |
753 |
|
*/ |
754 |
|
public void testAbnormalInvokeAll2() { |
760 |
|
invokeAll(f, g); |
761 |
|
shouldThrow(); |
762 |
|
return NoResult; |
763 |
< |
} catch(FJException success) { |
763 |
> |
} catch (FJException success) { |
764 |
|
} |
765 |
|
return NoResult; |
766 |
|
} |
768 |
|
mainPool.invoke(a); |
769 |
|
} |
770 |
|
|
771 |
< |
/** |
771 |
> |
/** |
772 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
773 |
|
*/ |
774 |
|
public void testAbnormalInvokeAll1() { |
779 |
|
invokeAll(g); |
780 |
|
shouldThrow(); |
781 |
|
return NoResult; |
782 |
< |
} catch(FJException success) { |
782 |
> |
} catch (FJException success) { |
783 |
|
} |
784 |
|
return NoResult; |
785 |
|
} |
787 |
|
mainPool.invoke(a); |
788 |
|
} |
789 |
|
|
790 |
< |
/** |
790 |
> |
/** |
791 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
792 |
|
*/ |
793 |
|
public void testAbnormalInvokeAll3() { |
800 |
|
invokeAll(f, g, h); |
801 |
|
shouldThrow(); |
802 |
|
return NoResult; |
803 |
< |
} catch(FJException success) { |
803 |
> |
} catch (FJException success) { |
804 |
|
} |
805 |
|
return NoResult; |
806 |
|
} |
808 |
|
mainPool.invoke(a); |
809 |
|
} |
810 |
|
|
811 |
< |
/** |
811 |
> |
/** |
812 |
|
* invokeAll(collection) throws exception if any task does |
813 |
|
*/ |
814 |
|
public void testAbnormalInvokeAllCollection() { |
825 |
|
invokeAll(set); |
826 |
|
shouldThrow(); |
827 |
|
return NoResult; |
828 |
< |
} catch(FJException success) { |
828 |
> |
} catch (FJException success) { |
829 |
|
} |
830 |
|
return NoResult; |
831 |
|
} |
833 |
|
mainPool.invoke(a); |
834 |
|
} |
835 |
|
|
836 |
< |
/** |
836 |
> |
/** |
837 |
|
* tryUnfork returns true for most recent unexecuted task, |
838 |
|
* and suppresses execution |
839 |
|
*/ |
854 |
|
singletonPool.invoke(a); |
855 |
|
} |
856 |
|
|
857 |
< |
/** |
857 |
> |
/** |
858 |
|
* getSurplusQueuedTaskCount returns > 0 when |
859 |
|
* there are more tasks than threads |
860 |
|
*/ |
875 |
|
singletonPool.invoke(a); |
876 |
|
} |
877 |
|
|
878 |
< |
/** |
878 |
> |
/** |
879 |
|
* peekNextLocalTask returns most recent unexecuted task. |
880 |
|
*/ |
881 |
|
public void testPeekNextLocalTask() { |
895 |
|
singletonPool.invoke(a); |
896 |
|
} |
897 |
|
|
898 |
< |
/** |
898 |
> |
/** |
899 |
|
* pollNextLocalTask returns most recent unexecuted task |
900 |
|
* without executing it |
901 |
|
*/ |
915 |
|
singletonPool.invoke(a); |
916 |
|
} |
917 |
|
|
918 |
< |
/** |
918 |
> |
/** |
919 |
|
* pollTask returns an unexecuted task |
920 |
|
* without executing it |
921 |
|
*/ |
936 |
|
singletonPool.invoke(a); |
937 |
|
} |
938 |
|
|
939 |
< |
/** |
939 |
> |
/** |
940 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
941 |
|
*/ |
942 |
|
public void testPeekNextLocalTaskAsync() { |
956 |
|
asyncSingletonPool.invoke(a); |
957 |
|
} |
958 |
|
|
959 |
< |
/** |
959 |
> |
/** |
960 |
|
* pollNextLocalTask returns least recent unexecuted task |
961 |
|
* without executing it, in async mode |
962 |
|
*/ |
977 |
|
asyncSingletonPool.invoke(a); |
978 |
|
} |
979 |
|
|
980 |
< |
/** |
980 |
> |
/** |
981 |
|
* pollTask returns an unexecuted task |
982 |
|
* without executing it, in async mode |
983 |
|
*/ |