11 |
|
public class RecursiveActionTest 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(RecursiveActionTest.class); |
29 |
|
} |
30 |
|
|
31 |
|
// A simple recursive action for testing |
32 |
< |
static final class FibAction extends RecursiveAction { |
32 |
> |
static final class FibAction extends RecursiveAction { |
33 |
|
final int number; |
34 |
|
int result; |
35 |
|
FibAction(int n) { number = n; } |
47 |
|
} |
48 |
|
|
49 |
|
// A recursive action failing in base case |
50 |
< |
static final class FailingFibAction extends RecursiveAction { |
50 |
> |
static final class FailingFibAction extends RecursiveAction { |
51 |
|
final int number; |
52 |
|
int result; |
53 |
|
FailingFibAction(int n) { number = n; } |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
< |
/** |
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 |
< |
* |
71 |
> |
* |
72 |
|
*/ |
73 |
|
public void testInvoke() { |
74 |
|
RecursiveAction a = new RecursiveAction() { |
85 |
|
mainPool.invoke(a); |
86 |
|
} |
87 |
|
|
88 |
< |
/** |
88 |
> |
/** |
89 |
|
* quietlyInvoke task returns when task completes normally. |
90 |
|
* isCompletedAbnormally and isCancelled return false for normally |
91 |
|
* completed tasks |
105 |
|
mainPool.invoke(a); |
106 |
|
} |
107 |
|
|
108 |
< |
/** |
108 |
> |
/** |
109 |
|
* join of a forked task returns when task completes |
110 |
|
*/ |
111 |
|
public void testForkJoin() { |
122 |
|
mainPool.invoke(a); |
123 |
|
} |
124 |
|
|
125 |
< |
/** |
125 |
> |
/** |
126 |
|
* get of a forked task returns when task completes |
127 |
|
*/ |
128 |
|
public void testForkGet() { |
134 |
|
f.get(); |
135 |
|
threadAssertTrue(f.result == 21); |
136 |
|
threadAssertTrue(f.isDone()); |
137 |
< |
} catch(Exception ex) { |
137 |
> |
} catch (Exception ex) { |
138 |
|
unexpectedException(); |
139 |
|
} |
140 |
|
} |
142 |
|
mainPool.invoke(a); |
143 |
|
} |
144 |
|
|
145 |
< |
/** |
145 |
> |
/** |
146 |
|
* timed get of a forked task returns when task completes |
147 |
|
*/ |
148 |
|
public void testForkTimedGet() { |
154 |
|
f.get(5L, TimeUnit.SECONDS); |
155 |
|
threadAssertTrue(f.result == 21); |
156 |
|
threadAssertTrue(f.isDone()); |
157 |
< |
} catch(Exception ex) { |
157 |
> |
} catch (Exception ex) { |
158 |
|
unexpectedException(); |
159 |
|
} |
160 |
|
} |
162 |
|
mainPool.invoke(a); |
163 |
|
} |
164 |
|
|
165 |
< |
/** |
165 |
> |
/** |
166 |
|
* timed get with null time unit throws NPE |
167 |
|
*/ |
168 |
|
public void testForkTimedGetNPE() { |
172 |
|
FibAction f = new FibAction(8); |
173 |
|
f.fork(); |
174 |
|
f.get(5L, null); |
175 |
< |
} catch(NullPointerException success) { |
176 |
< |
} catch(Exception ex) { |
175 |
> |
shouldThrow(); |
176 |
> |
} catch (NullPointerException success) { |
177 |
> |
} catch (Exception ex) { |
178 |
|
unexpectedException(); |
179 |
|
} |
180 |
|
} |
182 |
|
mainPool.invoke(a); |
183 |
|
} |
184 |
|
|
185 |
< |
/** |
185 |
> |
/** |
186 |
|
* helpJoin of a forked task returns when task completes |
187 |
|
*/ |
188 |
|
public void testForkHelpJoin() { |
198 |
|
mainPool.invoke(a); |
199 |
|
} |
200 |
|
|
201 |
< |
/** |
201 |
> |
/** |
202 |
|
* quietlyJoin of a forked task returns when task completes |
203 |
|
*/ |
204 |
|
public void testForkQuietlyJoin() { |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
< |
/** |
218 |
> |
/** |
219 |
|
* quietlyHelpJoin of a forked task returns when task completes |
220 |
|
*/ |
221 |
|
public void testForkQuietlyHelpJoin() { |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
< |
/** |
235 |
> |
/** |
236 |
|
* helpQuiesce returns when tasks are complete. |
237 |
|
* getQueuedTaskCount returns 0 when quiescent |
238 |
|
*/ |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
< |
/** |
254 |
> |
/** |
255 |
|
* invoke task throws exception when task completes abnormally |
256 |
|
*/ |
257 |
|
public void testAbnormalInvoke() { |
261 |
|
FailingFibAction f = new FailingFibAction(8); |
262 |
|
f.invoke(); |
263 |
|
shouldThrow(); |
264 |
< |
} catch(FJException success) { |
264 |
> |
} catch (FJException success) { |
265 |
|
} |
266 |
|
} |
267 |
|
}; |
268 |
|
mainPool.invoke(a); |
269 |
|
} |
270 |
|
|
271 |
< |
/** |
272 |
< |
* quietelyInvoke task returns when task completes abnormally |
271 |
> |
/** |
272 |
> |
* quietlyInvoke task returns when task completes abnormally |
273 |
|
*/ |
274 |
|
public void testAbnormalQuietlyInvoke() { |
275 |
|
RecursiveAction a = new RecursiveAction() { |
282 |
|
mainPool.invoke(a); |
283 |
|
} |
284 |
|
|
285 |
< |
/** |
285 |
> |
/** |
286 |
|
* join of a forked task throws exception when task completes abnormally |
287 |
|
*/ |
288 |
|
public void testAbnormalForkJoin() { |
293 |
|
f.fork(); |
294 |
|
f.join(); |
295 |
|
shouldThrow(); |
296 |
< |
} catch(FJException success) { |
296 |
> |
} catch (FJException success) { |
297 |
|
} |
298 |
|
} |
299 |
|
}; |
300 |
|
mainPool.invoke(a); |
301 |
|
} |
302 |
|
|
303 |
< |
/** |
303 |
> |
/** |
304 |
|
* get of a forked task throws exception when task completes abnormally |
305 |
|
*/ |
306 |
|
public void testAbnormalForkGet() { |
311 |
|
f.fork(); |
312 |
|
f.get(); |
313 |
|
shouldThrow(); |
314 |
< |
} catch(Exception success) { |
314 |
> |
} catch (ExecutionException success) { |
315 |
> |
} catch (Exception ex) { |
316 |
> |
unexpectedException(ex); |
317 |
|
} |
318 |
|
} |
319 |
|
}; |
320 |
|
mainPool.invoke(a); |
321 |
|
} |
322 |
|
|
323 |
< |
/** |
323 |
> |
/** |
324 |
|
* timed get of a forked task throws exception when task completes abnormally |
325 |
|
*/ |
326 |
|
public void testAbnormalForkTimedGet() { |
331 |
|
f.fork(); |
332 |
|
f.get(5L, TimeUnit.SECONDS); |
333 |
|
shouldThrow(); |
334 |
< |
} catch(Exception success) { |
334 |
> |
} catch (ExecutionException success) { |
335 |
> |
} catch (Exception ex) { |
336 |
> |
unexpectedException(ex); |
337 |
|
} |
338 |
|
} |
339 |
|
}; |
340 |
|
mainPool.invoke(a); |
341 |
|
} |
342 |
|
|
343 |
< |
/** |
343 |
> |
/** |
344 |
|
* join of a forked task throws exception when task completes abnormally |
345 |
|
*/ |
346 |
|
public void testAbnormalForkHelpJoin() { |
351 |
|
f.fork(); |
352 |
|
f.helpJoin(); |
353 |
|
shouldThrow(); |
354 |
< |
} catch(FJException success) { |
354 |
> |
} catch (FJException success) { |
355 |
|
} |
356 |
|
} |
357 |
|
}; |
358 |
|
mainPool.invoke(a); |
359 |
|
} |
360 |
|
|
361 |
< |
/** |
361 |
> |
/** |
362 |
|
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
363 |
|
* getException of failed task returns its exception. |
364 |
|
* isCompletedAbnormally of a failed task returns true. |
379 |
|
mainPool.invoke(a); |
380 |
|
} |
381 |
|
|
382 |
< |
/** |
382 |
> |
/** |
383 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
384 |
|
*/ |
385 |
|
public void testAbnormalForkQuietlyJoin() { |
396 |
|
mainPool.invoke(a); |
397 |
|
} |
398 |
|
|
399 |
< |
/** |
399 |
> |
/** |
400 |
|
* invoke task throws exception when task cancelled |
401 |
|
*/ |
402 |
|
public void testCancelledInvoke() { |
407 |
|
f.cancel(true); |
408 |
|
f.invoke(); |
409 |
|
shouldThrow(); |
410 |
< |
} catch(CancellationException success) { |
410 |
> |
} catch (CancellationException success) { |
411 |
|
} |
412 |
|
} |
413 |
|
}; |
414 |
|
mainPool.invoke(a); |
415 |
|
} |
416 |
|
|
417 |
< |
/** |
417 |
> |
/** |
418 |
|
* join of a forked task throws exception when task cancelled |
419 |
|
*/ |
420 |
|
public void testCancelledForkJoin() { |
426 |
|
f.fork(); |
427 |
|
f.join(); |
428 |
|
shouldThrow(); |
429 |
< |
} catch(CancellationException success) { |
429 |
> |
} catch (CancellationException success) { |
430 |
|
} |
431 |
|
} |
432 |
|
}; |
433 |
|
mainPool.invoke(a); |
434 |
|
} |
435 |
|
|
436 |
< |
/** |
436 |
> |
/** |
437 |
|
* get of a forked task throws exception when task cancelled |
438 |
|
*/ |
439 |
|
public void testCancelledForkGet() { |
445 |
|
f.fork(); |
446 |
|
f.get(); |
447 |
|
shouldThrow(); |
448 |
< |
} catch(Exception success) { |
448 |
> |
} catch (CancellationException success) { |
449 |
> |
} catch (Exception ex) { |
450 |
> |
unexpectedException(ex); |
451 |
|
} |
452 |
|
} |
453 |
|
}; |
454 |
|
mainPool.invoke(a); |
455 |
|
} |
456 |
|
|
457 |
< |
/** |
457 |
> |
/** |
458 |
|
* timed get of a forked task throws exception when task cancelled |
459 |
|
*/ |
460 |
|
public void testCancelledForkTimedGet() { |
466 |
|
f.fork(); |
467 |
|
f.get(5L, TimeUnit.SECONDS); |
468 |
|
shouldThrow(); |
469 |
< |
} catch(Exception success) { |
469 |
> |
} catch (CancellationException success) { |
470 |
> |
} catch (Exception ex) { |
471 |
> |
unexpectedException(ex); |
472 |
|
} |
473 |
|
} |
474 |
|
}; |
475 |
|
mainPool.invoke(a); |
476 |
|
} |
477 |
|
|
478 |
< |
/** |
478 |
> |
/** |
479 |
|
* join of a forked task throws exception when task cancelled |
480 |
|
*/ |
481 |
|
public void testCancelledForkHelpJoin() { |
487 |
|
f.fork(); |
488 |
|
f.helpJoin(); |
489 |
|
shouldThrow(); |
490 |
< |
} catch(CancellationException success) { |
490 |
> |
} catch (CancellationException success) { |
491 |
|
} |
492 |
|
} |
493 |
|
}; |
494 |
|
mainPool.invoke(a); |
495 |
|
} |
496 |
|
|
497 |
< |
/** |
497 |
> |
/** |
498 |
|
* quietlyHelpJoin of a forked task returns when task cancelled. |
499 |
|
* getException of cancelled task returns its exception. |
500 |
|
* isCompletedAbnormally of a cancelled task returns true. |
516 |
|
mainPool.invoke(a); |
517 |
|
} |
518 |
|
|
519 |
< |
/** |
519 |
> |
/** |
520 |
|
* quietlyJoin of a forked task returns when task cancelled |
521 |
|
*/ |
522 |
|
public void testCancelledForkQuietlyJoin() { |
598 |
|
|
599 |
|
/** |
600 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
601 |
< |
* |
601 |
> |
* |
602 |
|
*/ |
603 |
|
public void testWorkerGetPoolIndex() { |
604 |
|
RecursiveAction a = new RecursiveAction() { |
626 |
|
a.invoke(); |
627 |
|
} |
628 |
|
|
629 |
< |
/** |
629 |
> |
/** |
630 |
|
* A reinitialized task may be re-invoked |
631 |
|
*/ |
632 |
|
public void testReinitialize() { |
646 |
|
mainPool.invoke(a); |
647 |
|
} |
648 |
|
|
649 |
< |
/** |
649 |
> |
/** |
650 |
|
* invoke task throws exception after invoking completeExceptionally |
651 |
|
*/ |
652 |
|
public void testCompleteExceptionally() { |
657 |
|
f.completeExceptionally(new FJException()); |
658 |
|
f.invoke(); |
659 |
|
shouldThrow(); |
660 |
< |
} catch(FJException success) { |
660 |
> |
} catch (FJException success) { |
661 |
|
} |
662 |
|
} |
663 |
|
}; |
664 |
|
mainPool.invoke(a); |
665 |
|
} |
666 |
|
|
667 |
< |
/** |
667 |
> |
/** |
668 |
|
* invoke task suppresses execution invoking complete |
669 |
|
*/ |
670 |
|
public void testComplete() { |
680 |
|
mainPool.invoke(a); |
681 |
|
} |
682 |
|
|
683 |
< |
/** |
683 |
> |
/** |
684 |
|
* invokeAll(t1, t2) invokes all task arguments |
685 |
|
*/ |
686 |
|
public void testInvokeAll2() { |
698 |
|
mainPool.invoke(a); |
699 |
|
} |
700 |
|
|
701 |
< |
/** |
701 |
> |
/** |
702 |
|
* invokeAll(tasks) with 1 argument invokes task |
703 |
|
*/ |
704 |
|
public void testInvokeAll1() { |
713 |
|
mainPool.invoke(a); |
714 |
|
} |
715 |
|
|
716 |
< |
/** |
716 |
> |
/** |
717 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
718 |
|
*/ |
719 |
|
public void testInvokeAll3() { |
734 |
|
mainPool.invoke(a); |
735 |
|
} |
736 |
|
|
737 |
< |
/** |
737 |
> |
/** |
738 |
|
* invokeAll(collection) invokes all tasks in the collection |
739 |
|
*/ |
740 |
|
public void testInvokeAllCollection() { |
760 |
|
} |
761 |
|
|
762 |
|
|
763 |
< |
/** |
763 |
> |
/** |
764 |
|
* invokeAll(tasks) with any null task throws NPE |
765 |
|
*/ |
766 |
|
public void testInvokeAllNPE() { |
779 |
|
mainPool.invoke(a); |
780 |
|
} |
781 |
|
|
782 |
< |
/** |
782 |
> |
/** |
783 |
|
* invokeAll(t1, t2) throw exception if any task does |
784 |
|
*/ |
785 |
|
public void testAbnormalInvokeAll2() { |
790 |
|
FailingFibAction g = new FailingFibAction(9); |
791 |
|
invokeAll(f, g); |
792 |
|
shouldThrow(); |
793 |
< |
} catch(FJException success) { |
793 |
> |
} catch (FJException success) { |
794 |
|
} |
795 |
|
} |
796 |
|
}; |
797 |
|
mainPool.invoke(a); |
798 |
|
} |
799 |
|
|
800 |
< |
/** |
800 |
> |
/** |
801 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
802 |
|
*/ |
803 |
|
public void testAbnormalInvokeAll1() { |
807 |
|
FailingFibAction g = new FailingFibAction(9); |
808 |
|
invokeAll(g); |
809 |
|
shouldThrow(); |
810 |
< |
} catch(FJException success) { |
810 |
> |
} catch (FJException success) { |
811 |
|
} |
812 |
|
} |
813 |
|
}; |
814 |
|
mainPool.invoke(a); |
815 |
|
} |
816 |
|
|
817 |
< |
/** |
817 |
> |
/** |
818 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
819 |
|
*/ |
820 |
|
public void testAbnormalInvokeAll3() { |
826 |
|
FibAction h = new FibAction(7); |
827 |
|
invokeAll(f, g, h); |
828 |
|
shouldThrow(); |
829 |
< |
} catch(FJException success) { |
829 |
> |
} catch (FJException success) { |
830 |
|
} |
831 |
|
} |
832 |
|
}; |
833 |
|
mainPool.invoke(a); |
834 |
|
} |
835 |
|
|
836 |
< |
/** |
837 |
< |
* invokeAll(collection) throws exception if any task does |
836 |
> |
/** |
837 |
> |
* invokeAll(collection) throws exception if any task does |
838 |
|
*/ |
839 |
|
public void testAbnormalInvokeAllCollection() { |
840 |
|
RecursiveAction a = new RecursiveAction() { |
849 |
|
set.add(h); |
850 |
|
invokeAll(set); |
851 |
|
shouldThrow(); |
852 |
< |
} catch(FJException success) { |
852 |
> |
} catch (FJException success) { |
853 |
|
} |
854 |
|
} |
855 |
|
}; |
856 |
|
mainPool.invoke(a); |
857 |
|
} |
858 |
|
|
859 |
< |
/** |
859 |
> |
/** |
860 |
|
* tryUnfork returns true for most recent unexecuted task, |
861 |
|
* and suppresses execution |
862 |
|
*/ |
876 |
|
singletonPool.invoke(a); |
877 |
|
} |
878 |
|
|
879 |
< |
/** |
879 |
> |
/** |
880 |
|
* getSurplusQueuedTaskCount returns > 0 when |
881 |
|
* there are more tasks than threads |
882 |
|
*/ |
896 |
|
singletonPool.invoke(a); |
897 |
|
} |
898 |
|
|
899 |
< |
/** |
899 |
> |
/** |
900 |
|
* peekNextLocalTask returns most recent unexecuted task. |
901 |
|
*/ |
902 |
|
public void testPeekNextLocalTask() { |
915 |
|
singletonPool.invoke(a); |
916 |
|
} |
917 |
|
|
918 |
< |
/** |
918 |
> |
/** |
919 |
|
* pollNextLocalTask returns most recent unexecuted task |
920 |
|
* without executing it |
921 |
|
*/ |
934 |
|
singletonPool.invoke(a); |
935 |
|
} |
936 |
|
|
937 |
< |
/** |
937 |
> |
/** |
938 |
|
* pollTask returns an unexecuted task |
939 |
|
* without executing it |
940 |
|
*/ |
954 |
|
singletonPool.invoke(a); |
955 |
|
} |
956 |
|
|
957 |
< |
/** |
957 |
> |
/** |
958 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
959 |
|
*/ |
960 |
|
public void testPeekNextLocalTaskAsync() { |
973 |
|
asyncSingletonPool.invoke(a); |
974 |
|
} |
975 |
|
|
976 |
< |
/** |
976 |
> |
/** |
977 |
|
* pollNextLocalTask returns least recent unexecuted task |
978 |
|
* without executing it, in async mode |
979 |
|
*/ |
993 |
|
asyncSingletonPool.invoke(a); |
994 |
|
} |
995 |
|
|
996 |
< |
/** |
996 |
> |
/** |
997 |
|
* pollTask returns an unexecuted task |
998 |
|
* without executing it, in async mode |
999 |
|
*/ |