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 |
> |
} catch (NullPointerException success) { |
176 |
> |
} catch (Exception ex) { |
177 |
|
unexpectedException(); |
178 |
|
} |
179 |
|
} |
181 |
|
mainPool.invoke(a); |
182 |
|
} |
183 |
|
|
184 |
< |
/** |
184 |
> |
/** |
185 |
|
* helpJoin of a forked task returns when task completes |
186 |
|
*/ |
187 |
|
public void testForkHelpJoin() { |
197 |
|
mainPool.invoke(a); |
198 |
|
} |
199 |
|
|
200 |
< |
/** |
200 |
> |
/** |
201 |
|
* quietlyJoin of a forked task returns when task completes |
202 |
|
*/ |
203 |
|
public void testForkQuietlyJoin() { |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
< |
/** |
217 |
> |
/** |
218 |
|
* quietlyHelpJoin of a forked task returns when task completes |
219 |
|
*/ |
220 |
|
public void testForkQuietlyHelpJoin() { |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
< |
/** |
234 |
> |
/** |
235 |
|
* helpQuiesce returns when tasks are complete. |
236 |
|
* getQueuedTaskCount returns 0 when quiescent |
237 |
|
*/ |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
< |
/** |
253 |
> |
/** |
254 |
|
* invoke task throws exception when task completes abnormally |
255 |
|
*/ |
256 |
|
public void testAbnormalInvoke() { |
260 |
|
FailingFibAction f = new FailingFibAction(8); |
261 |
|
f.invoke(); |
262 |
|
shouldThrow(); |
263 |
< |
} catch(FJException success) { |
263 |
> |
} catch (FJException success) { |
264 |
|
} |
265 |
|
} |
266 |
|
}; |
267 |
|
mainPool.invoke(a); |
268 |
|
} |
269 |
|
|
270 |
< |
/** |
271 |
< |
* quietelyInvoke task returns when task completes abnormally |
270 |
> |
/** |
271 |
> |
* quietlyInvoke task returns when task completes abnormally |
272 |
|
*/ |
273 |
|
public void testAbnormalQuietlyInvoke() { |
274 |
|
RecursiveAction a = new RecursiveAction() { |
281 |
|
mainPool.invoke(a); |
282 |
|
} |
283 |
|
|
284 |
< |
/** |
284 |
> |
/** |
285 |
|
* join of a forked task throws exception when task completes abnormally |
286 |
|
*/ |
287 |
|
public void testAbnormalForkJoin() { |
292 |
|
f.fork(); |
293 |
|
f.join(); |
294 |
|
shouldThrow(); |
295 |
< |
} catch(FJException success) { |
295 |
> |
} catch (FJException success) { |
296 |
|
} |
297 |
|
} |
298 |
|
}; |
299 |
|
mainPool.invoke(a); |
300 |
|
} |
301 |
|
|
302 |
< |
/** |
302 |
> |
/** |
303 |
|
* get of a forked task throws exception when task completes abnormally |
304 |
|
*/ |
305 |
|
public void testAbnormalForkGet() { |
310 |
|
f.fork(); |
311 |
|
f.get(); |
312 |
|
shouldThrow(); |
313 |
< |
} catch(Exception success) { |
313 |
> |
} catch (Exception success) { |
314 |
|
} |
315 |
|
} |
316 |
|
}; |
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() { |
328 |
|
f.fork(); |
329 |
|
f.get(5L, TimeUnit.SECONDS); |
330 |
|
shouldThrow(); |
331 |
< |
} catch(Exception success) { |
331 |
> |
} catch (Exception success) { |
332 |
|
} |
333 |
|
} |
334 |
|
}; |
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() { |
346 |
|
f.fork(); |
347 |
|
f.helpJoin(); |
348 |
|
shouldThrow(); |
349 |
< |
} catch(FJException success) { |
349 |
> |
} catch (FJException success) { |
350 |
|
} |
351 |
|
} |
352 |
|
}; |
353 |
|
mainPool.invoke(a); |
354 |
|
} |
355 |
|
|
356 |
< |
/** |
356 |
> |
/** |
357 |
|
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
358 |
|
* getException of failed task returns its exception. |
359 |
|
* isCompletedAbnormally of a failed task returns true. |
374 |
|
mainPool.invoke(a); |
375 |
|
} |
376 |
|
|
377 |
< |
/** |
377 |
> |
/** |
378 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
379 |
|
*/ |
380 |
|
public void testAbnormalForkQuietlyJoin() { |
391 |
|
mainPool.invoke(a); |
392 |
|
} |
393 |
|
|
394 |
< |
/** |
394 |
> |
/** |
395 |
|
* invoke task throws exception when task cancelled |
396 |
|
*/ |
397 |
|
public void testCancelledInvoke() { |
402 |
|
f.cancel(true); |
403 |
|
f.invoke(); |
404 |
|
shouldThrow(); |
405 |
< |
} catch(CancellationException success) { |
405 |
> |
} catch (CancellationException success) { |
406 |
|
} |
407 |
|
} |
408 |
|
}; |
409 |
|
mainPool.invoke(a); |
410 |
|
} |
411 |
|
|
412 |
< |
/** |
412 |
> |
/** |
413 |
|
* join of a forked task throws exception when task cancelled |
414 |
|
*/ |
415 |
|
public void testCancelledForkJoin() { |
421 |
|
f.fork(); |
422 |
|
f.join(); |
423 |
|
shouldThrow(); |
424 |
< |
} catch(CancellationException success) { |
424 |
> |
} catch (CancellationException success) { |
425 |
|
} |
426 |
|
} |
427 |
|
}; |
428 |
|
mainPool.invoke(a); |
429 |
|
} |
430 |
|
|
431 |
< |
/** |
431 |
> |
/** |
432 |
|
* get of a forked task throws exception when task cancelled |
433 |
|
*/ |
434 |
|
public void testCancelledForkGet() { |
440 |
|
f.fork(); |
441 |
|
f.get(); |
442 |
|
shouldThrow(); |
443 |
< |
} catch(Exception success) { |
443 |
> |
} catch (Exception success) { |
444 |
|
} |
445 |
|
} |
446 |
|
}; |
447 |
|
mainPool.invoke(a); |
448 |
|
} |
449 |
|
|
450 |
< |
/** |
450 |
> |
/** |
451 |
|
* timed get of a forked task throws exception when task cancelled |
452 |
|
*/ |
453 |
|
public void testCancelledForkTimedGet() { |
459 |
|
f.fork(); |
460 |
|
f.get(5L, TimeUnit.SECONDS); |
461 |
|
shouldThrow(); |
462 |
< |
} catch(Exception success) { |
462 |
> |
} catch (Exception success) { |
463 |
|
} |
464 |
|
} |
465 |
|
}; |
466 |
|
mainPool.invoke(a); |
467 |
|
} |
468 |
|
|
469 |
< |
/** |
469 |
> |
/** |
470 |
|
* join of a forked task throws exception when task cancelled |
471 |
|
*/ |
472 |
|
public void testCancelledForkHelpJoin() { |
478 |
|
f.fork(); |
479 |
|
f.helpJoin(); |
480 |
|
shouldThrow(); |
481 |
< |
} catch(CancellationException success) { |
481 |
> |
} catch (CancellationException success) { |
482 |
|
} |
483 |
|
} |
484 |
|
}; |
485 |
|
mainPool.invoke(a); |
486 |
|
} |
487 |
|
|
488 |
< |
/** |
488 |
> |
/** |
489 |
|
* quietlyHelpJoin of a forked task returns when task cancelled. |
490 |
|
* getException of cancelled task returns its exception. |
491 |
|
* isCompletedAbnormally of a cancelled task returns true. |
507 |
|
mainPool.invoke(a); |
508 |
|
} |
509 |
|
|
510 |
< |
/** |
510 |
> |
/** |
511 |
|
* quietlyJoin of a forked task returns when task cancelled |
512 |
|
*/ |
513 |
|
public void testCancelledForkQuietlyJoin() { |
589 |
|
|
590 |
|
/** |
591 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
592 |
< |
* |
592 |
> |
* |
593 |
|
*/ |
594 |
|
public void testWorkerGetPoolIndex() { |
595 |
|
RecursiveAction a = new RecursiveAction() { |
617 |
|
a.invoke(); |
618 |
|
} |
619 |
|
|
620 |
< |
/** |
620 |
> |
/** |
621 |
|
* A reinitialized task may be re-invoked |
622 |
|
*/ |
623 |
|
public void testReinitialize() { |
637 |
|
mainPool.invoke(a); |
638 |
|
} |
639 |
|
|
640 |
< |
/** |
640 |
> |
/** |
641 |
|
* invoke task throws exception after invoking completeExceptionally |
642 |
|
*/ |
643 |
|
public void testCompleteExceptionally() { |
648 |
|
f.completeExceptionally(new FJException()); |
649 |
|
f.invoke(); |
650 |
|
shouldThrow(); |
651 |
< |
} catch(FJException success) { |
651 |
> |
} catch (FJException success) { |
652 |
|
} |
653 |
|
} |
654 |
|
}; |
655 |
|
mainPool.invoke(a); |
656 |
|
} |
657 |
|
|
658 |
< |
/** |
658 |
> |
/** |
659 |
|
* invoke task suppresses execution invoking complete |
660 |
|
*/ |
661 |
|
public void testComplete() { |
671 |
|
mainPool.invoke(a); |
672 |
|
} |
673 |
|
|
674 |
< |
/** |
674 |
> |
/** |
675 |
|
* invokeAll(t1, t2) invokes all task arguments |
676 |
|
*/ |
677 |
|
public void testInvokeAll2() { |
689 |
|
mainPool.invoke(a); |
690 |
|
} |
691 |
|
|
692 |
< |
/** |
692 |
> |
/** |
693 |
|
* invokeAll(tasks) with 1 argument invokes task |
694 |
|
*/ |
695 |
|
public void testInvokeAll1() { |
704 |
|
mainPool.invoke(a); |
705 |
|
} |
706 |
|
|
707 |
< |
/** |
707 |
> |
/** |
708 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
709 |
|
*/ |
710 |
|
public void testInvokeAll3() { |
725 |
|
mainPool.invoke(a); |
726 |
|
} |
727 |
|
|
728 |
< |
/** |
728 |
> |
/** |
729 |
|
* invokeAll(collection) invokes all tasks in the collection |
730 |
|
*/ |
731 |
|
public void testInvokeAllCollection() { |
751 |
|
} |
752 |
|
|
753 |
|
|
754 |
< |
/** |
754 |
> |
/** |
755 |
|
* invokeAll(tasks) with any null task throws NPE |
756 |
|
*/ |
757 |
|
public void testInvokeAllNPE() { |
770 |
|
mainPool.invoke(a); |
771 |
|
} |
772 |
|
|
773 |
< |
/** |
773 |
> |
/** |
774 |
|
* invokeAll(t1, t2) throw exception if any task does |
775 |
|
*/ |
776 |
|
public void testAbnormalInvokeAll2() { |
781 |
|
FailingFibAction g = new FailingFibAction(9); |
782 |
|
invokeAll(f, g); |
783 |
|
shouldThrow(); |
784 |
< |
} catch(FJException success) { |
784 |
> |
} catch (FJException success) { |
785 |
|
} |
786 |
|
} |
787 |
|
}; |
788 |
|
mainPool.invoke(a); |
789 |
|
} |
790 |
|
|
791 |
< |
/** |
791 |
> |
/** |
792 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
793 |
|
*/ |
794 |
|
public void testAbnormalInvokeAll1() { |
798 |
|
FailingFibAction g = new FailingFibAction(9); |
799 |
|
invokeAll(g); |
800 |
|
shouldThrow(); |
801 |
< |
} catch(FJException success) { |
801 |
> |
} catch (FJException success) { |
802 |
|
} |
803 |
|
} |
804 |
|
}; |
805 |
|
mainPool.invoke(a); |
806 |
|
} |
807 |
|
|
808 |
< |
/** |
808 |
> |
/** |
809 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
810 |
|
*/ |
811 |
|
public void testAbnormalInvokeAll3() { |
817 |
|
FibAction h = new FibAction(7); |
818 |
|
invokeAll(f, g, h); |
819 |
|
shouldThrow(); |
820 |
< |
} catch(FJException success) { |
820 |
> |
} catch (FJException success) { |
821 |
|
} |
822 |
|
} |
823 |
|
}; |
824 |
|
mainPool.invoke(a); |
825 |
|
} |
826 |
|
|
827 |
< |
/** |
828 |
< |
* invokeAll(collection) throws exception if any task does |
827 |
> |
/** |
828 |
> |
* invokeAll(collection) throws exception if any task does |
829 |
|
*/ |
830 |
|
public void testAbnormalInvokeAllCollection() { |
831 |
|
RecursiveAction a = new RecursiveAction() { |
840 |
|
set.add(h); |
841 |
|
invokeAll(set); |
842 |
|
shouldThrow(); |
843 |
< |
} catch(FJException success) { |
843 |
> |
} catch (FJException success) { |
844 |
|
} |
845 |
|
} |
846 |
|
}; |
847 |
|
mainPool.invoke(a); |
848 |
|
} |
849 |
|
|
850 |
< |
/** |
850 |
> |
/** |
851 |
|
* tryUnfork returns true for most recent unexecuted task, |
852 |
|
* and suppresses execution |
853 |
|
*/ |
867 |
|
singletonPool.invoke(a); |
868 |
|
} |
869 |
|
|
870 |
< |
/** |
870 |
> |
/** |
871 |
|
* getSurplusQueuedTaskCount returns > 0 when |
872 |
|
* there are more tasks than threads |
873 |
|
*/ |
887 |
|
singletonPool.invoke(a); |
888 |
|
} |
889 |
|
|
890 |
< |
/** |
890 |
> |
/** |
891 |
|
* peekNextLocalTask returns most recent unexecuted task. |
892 |
|
*/ |
893 |
|
public void testPeekNextLocalTask() { |
906 |
|
singletonPool.invoke(a); |
907 |
|
} |
908 |
|
|
909 |
< |
/** |
909 |
> |
/** |
910 |
|
* pollNextLocalTask returns most recent unexecuted task |
911 |
|
* without executing it |
912 |
|
*/ |
925 |
|
singletonPool.invoke(a); |
926 |
|
} |
927 |
|
|
928 |
< |
/** |
928 |
> |
/** |
929 |
|
* pollTask returns an unexecuted task |
930 |
|
* without executing it |
931 |
|
*/ |
945 |
|
singletonPool.invoke(a); |
946 |
|
} |
947 |
|
|
948 |
< |
/** |
948 |
> |
/** |
949 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
950 |
|
*/ |
951 |
|
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 |
|
*/ |
984 |
|
asyncSingletonPool.invoke(a); |
985 |
|
} |
986 |
|
|
987 |
< |
/** |
987 |
> |
/** |
988 |
|
* pollTask returns an unexecuted task |
989 |
|
* without executing it, in async mode |
990 |
|
*/ |