3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
+ |
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.concurrent.*; |
9 |
|
import java.util.*; |
10 |
|
|
10 |
– |
|
11 |
|
public class RecursiveActionTest extends JSR166TestCase { |
12 |
|
|
13 |
|
public static void main(String[] args) { |
14 |
|
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
+ |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(RecursiveActionTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
22 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
23 |
< |
static final ForkJoinPool asyncSingletonPool = |
24 |
< |
new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
25 |
< |
null, true); |
21 |
> |
private static ForkJoinPool mainPool() { |
22 |
> |
return new ForkJoinPool(); |
23 |
> |
} |
24 |
> |
|
25 |
> |
private static ForkJoinPool singletonPool() { |
26 |
> |
return new ForkJoinPool(1); |
27 |
> |
} |
28 |
> |
|
29 |
> |
private static ForkJoinPool asyncSingletonPool() { |
30 |
> |
return new ForkJoinPool(1, |
31 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
32 |
> |
null, true); |
33 |
> |
} |
34 |
> |
|
35 |
> |
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
36 |
> |
try { |
37 |
> |
assertTrue(pool.invoke(a) == null); |
38 |
> |
} finally { |
39 |
> |
joinPool(pool); |
40 |
> |
} |
41 |
> |
} |
42 |
|
|
43 |
|
static final class FJException extends RuntimeException { |
44 |
|
FJException() { super(); } |
84 |
|
* invoke returns when task completes normally. |
85 |
|
* isCompletedAbnormally and isCancelled return false for normally |
86 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
70 |
– |
* |
87 |
|
*/ |
88 |
|
public void testInvoke() { |
89 |
|
RecursiveAction a = new RecursiveAction() { |
96 |
|
threadAssertFalse(f.isCompletedAbnormally()); |
97 |
|
threadAssertTrue(f.getRawResult() == null); |
98 |
|
}}; |
99 |
< |
mainPool.invoke(a); |
99 |
> |
testInvokeOnPool(mainPool(), a); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
115 |
|
threadAssertFalse(f.isCompletedAbnormally()); |
116 |
|
threadAssertTrue(f.getRawResult() == null); |
117 |
|
}}; |
118 |
< |
mainPool.invoke(a); |
118 |
> |
testInvokeOnPool(mainPool(), a); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
131 |
|
threadAssertTrue(f.isDone()); |
132 |
|
threadAssertTrue(f.getRawResult() == null); |
133 |
|
}}; |
134 |
< |
mainPool.invoke(a); |
134 |
> |
testInvokeOnPool(mainPool(), a); |
135 |
|
} |
136 |
|
|
137 |
|
/** |
150 |
|
unexpectedException(ex); |
151 |
|
} |
152 |
|
}}; |
153 |
< |
mainPool.invoke(a); |
153 |
> |
testInvokeOnPool(mainPool(), a); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
169 |
|
unexpectedException(ex); |
170 |
|
} |
171 |
|
}}; |
172 |
< |
mainPool.invoke(a); |
172 |
> |
testInvokeOnPool(mainPool(), a); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
188 |
|
unexpectedException(ex); |
189 |
|
} |
190 |
|
}}; |
191 |
< |
mainPool.invoke(a); |
191 |
> |
testInvokeOnPool(mainPool(), a); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
203 |
|
threadAssertTrue(f.result == 21); |
204 |
|
threadAssertTrue(f.isDone()); |
205 |
|
}}; |
206 |
< |
mainPool.invoke(a); |
206 |
> |
testInvokeOnPool(mainPool(), a); |
207 |
|
} |
208 |
|
|
209 |
|
|
221 |
|
threadAssertTrue(f.isDone()); |
222 |
|
threadAssertTrue(getQueuedTaskCount() == 0); |
223 |
|
}}; |
224 |
< |
mainPool.invoke(a); |
224 |
> |
testInvokeOnPool(mainPool(), a); |
225 |
|
} |
226 |
|
|
227 |
|
|
238 |
|
} catch (FJException success) { |
239 |
|
} |
240 |
|
}}; |
241 |
< |
mainPool.invoke(a); |
241 |
> |
testInvokeOnPool(mainPool(), a); |
242 |
|
} |
243 |
|
|
244 |
|
/** |
251 |
|
f.quietlyInvoke(); |
252 |
|
threadAssertTrue(f.isDone()); |
253 |
|
}}; |
254 |
< |
mainPool.invoke(a); |
254 |
> |
testInvokeOnPool(mainPool(), a); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
268 |
|
} catch (FJException success) { |
269 |
|
} |
270 |
|
}}; |
271 |
< |
mainPool.invoke(a); |
271 |
> |
testInvokeOnPool(mainPool(), a); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
287 |
|
unexpectedException(ex); |
288 |
|
} |
289 |
|
}}; |
290 |
< |
mainPool.invoke(a); |
290 |
> |
testInvokeOnPool(mainPool(), a); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
306 |
|
unexpectedException(ex); |
307 |
|
} |
308 |
|
}}; |
309 |
< |
mainPool.invoke(a); |
309 |
> |
testInvokeOnPool(mainPool(), a); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
322 |
|
threadAssertTrue(f.isCompletedAbnormally()); |
323 |
|
threadAssertTrue(f.getException() instanceof FJException); |
324 |
|
}}; |
325 |
< |
mainPool.invoke(a); |
325 |
> |
testInvokeOnPool(mainPool(), a); |
326 |
|
} |
327 |
|
|
328 |
|
/** |
339 |
|
} catch (CancellationException success) { |
340 |
|
} |
341 |
|
}}; |
342 |
< |
mainPool.invoke(a); |
342 |
> |
testInvokeOnPool(mainPool(), a); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
357 |
|
} catch (CancellationException success) { |
358 |
|
} |
359 |
|
}}; |
360 |
< |
mainPool.invoke(a); |
360 |
> |
testInvokeOnPool(mainPool(), a); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
377 |
|
unexpectedException(ex); |
378 |
|
} |
379 |
|
}}; |
380 |
< |
mainPool.invoke(a); |
380 |
> |
testInvokeOnPool(mainPool(), a); |
381 |
|
} |
382 |
|
|
383 |
|
/** |
397 |
|
unexpectedException(ex); |
398 |
|
} |
399 |
|
}}; |
400 |
< |
mainPool.invoke(a); |
400 |
> |
testInvokeOnPool(mainPool(), a); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
414 |
|
threadAssertTrue(f.isCompletedAbnormally()); |
415 |
|
threadAssertTrue(f.getException() instanceof CancellationException); |
416 |
|
}}; |
417 |
< |
mainPool.invoke(a); |
417 |
> |
testInvokeOnPool(mainPool(), a); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
|
* getPool of executing task returns its pool |
422 |
|
*/ |
423 |
|
public void testGetPool() { |
424 |
+ |
final ForkJoinPool mainPool = mainPool(); |
425 |
|
RecursiveAction a = new RecursiveAction() { |
426 |
|
public void compute() { |
427 |
|
threadAssertTrue(getPool() == mainPool); |
428 |
|
}}; |
429 |
< |
mainPool.invoke(a); |
429 |
> |
testInvokeOnPool(mainPool, a); |
430 |
|
} |
431 |
|
|
432 |
|
/** |
448 |
|
public void compute() { |
449 |
|
threadAssertTrue(inForkJoinPool()); |
450 |
|
}}; |
451 |
< |
mainPool.invoke(a); |
451 |
> |
testInvokeOnPool(mainPool(), a); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
466 |
|
* getPool of current thread in pool returns its pool |
467 |
|
*/ |
468 |
|
public void testWorkerGetPool() { |
469 |
+ |
final ForkJoinPool mainPool = mainPool(); |
470 |
|
RecursiveAction a = new RecursiveAction() { |
471 |
|
public void compute() { |
472 |
|
ForkJoinWorkerThread w = |
473 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
473 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
474 |
|
threadAssertTrue(w.getPool() == mainPool); |
475 |
|
}}; |
476 |
< |
mainPool.invoke(a); |
476 |
> |
testInvokeOnPool(mainPool, a); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
463 |
– |
* |
481 |
|
*/ |
482 |
|
public void testWorkerGetPoolIndex() { |
483 |
+ |
final ForkJoinPool mainPool = mainPool(); |
484 |
|
RecursiveAction a = new RecursiveAction() { |
485 |
|
public void compute() { |
486 |
|
ForkJoinWorkerThread w = |
489 |
|
threadAssertTrue(idx >= 0); |
490 |
|
threadAssertTrue(idx < mainPool.getPoolSize()); |
491 |
|
}}; |
492 |
< |
mainPool.invoke(a); |
492 |
> |
testInvokeOnPool(mainPool, a); |
493 |
|
} |
494 |
|
|
495 |
|
|
520 |
|
f.invoke(); |
521 |
|
threadAssertTrue(f.result == 21); |
522 |
|
}}; |
523 |
< |
mainPool.invoke(a); |
523 |
> |
testInvokeOnPool(mainPool(), a); |
524 |
|
} |
525 |
|
|
526 |
|
/** |
537 |
|
} catch (FJException success) { |
538 |
|
} |
539 |
|
}}; |
540 |
< |
mainPool.invoke(a); |
540 |
> |
testInvokeOnPool(mainPool(), a); |
541 |
|
} |
542 |
|
|
543 |
|
/** |
552 |
|
threadAssertTrue(f.isDone()); |
553 |
|
threadAssertTrue(f.result == 0); |
554 |
|
}}; |
555 |
< |
mainPool.invoke(a); |
555 |
> |
testInvokeOnPool(mainPool(), a); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
569 |
|
threadAssertTrue(g.isDone()); |
570 |
|
threadAssertTrue(g.result == 34); |
571 |
|
}}; |
572 |
< |
mainPool.invoke(a); |
572 |
> |
testInvokeOnPool(mainPool(), a); |
573 |
|
} |
574 |
|
|
575 |
|
/** |
583 |
|
threadAssertTrue(f.isDone()); |
584 |
|
threadAssertTrue(f.result == 21); |
585 |
|
}}; |
586 |
< |
mainPool.invoke(a); |
586 |
> |
testInvokeOnPool(mainPool(), a); |
587 |
|
} |
588 |
|
|
589 |
|
/** |
603 |
|
threadAssertTrue(h.isDone()); |
604 |
|
threadAssertTrue(h.result == 13); |
605 |
|
}}; |
606 |
< |
mainPool.invoke(a); |
606 |
> |
testInvokeOnPool(mainPool(), a); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
627 |
|
threadAssertTrue(h.isDone()); |
628 |
|
threadAssertTrue(h.result == 13); |
629 |
|
}}; |
630 |
< |
mainPool.invoke(a); |
630 |
> |
testInvokeOnPool(mainPool(), a); |
631 |
|
} |
632 |
|
|
633 |
|
|
646 |
|
} catch (NullPointerException success) { |
647 |
|
} |
648 |
|
}}; |
649 |
< |
mainPool.invoke(a); |
649 |
> |
testInvokeOnPool(mainPool(), a); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
663 |
|
} catch (FJException success) { |
664 |
|
} |
665 |
|
}}; |
666 |
< |
mainPool.invoke(a); |
666 |
> |
testInvokeOnPool(mainPool(), a); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
679 |
|
} catch (FJException success) { |
680 |
|
} |
681 |
|
}}; |
682 |
< |
mainPool.invoke(a); |
682 |
> |
testInvokeOnPool(mainPool(), a); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
697 |
|
} catch (FJException success) { |
698 |
|
} |
699 |
|
}}; |
700 |
< |
mainPool.invoke(a); |
700 |
> |
testInvokeOnPool(mainPool(), a); |
701 |
|
} |
702 |
|
|
703 |
|
/** |
719 |
|
} catch (FJException success) { |
720 |
|
} |
721 |
|
}}; |
722 |
< |
mainPool.invoke(a); |
722 |
> |
testInvokeOnPool(mainPool(), a); |
723 |
|
} |
724 |
|
|
725 |
|
/** |
738 |
|
threadAssertFalse(f.isDone()); |
739 |
|
threadAssertTrue(g.isDone()); |
740 |
|
}}; |
741 |
< |
singletonPool.invoke(a); |
741 |
> |
testInvokeOnPool(singletonPool(), a); |
742 |
|
} |
743 |
|
|
744 |
|
/** |
757 |
|
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
758 |
|
helpQuiesce(); |
759 |
|
}}; |
760 |
< |
singletonPool.invoke(a); |
760 |
> |
testInvokeOnPool(singletonPool(), a); |
761 |
|
} |
762 |
|
|
763 |
|
/** |
775 |
|
threadAssertTrue(f.isDone()); |
776 |
|
helpQuiesce(); |
777 |
|
}}; |
778 |
< |
singletonPool.invoke(a); |
778 |
> |
testInvokeOnPool(singletonPool(), a); |
779 |
|
} |
780 |
|
|
781 |
|
/** |
793 |
|
helpQuiesce(); |
794 |
|
threadAssertFalse(f.isDone()); |
795 |
|
}}; |
796 |
< |
singletonPool.invoke(a); |
796 |
> |
testInvokeOnPool(singletonPool(), a); |
797 |
|
} |
798 |
|
|
799 |
|
/** |
812 |
|
threadAssertFalse(f.isDone()); |
813 |
|
threadAssertTrue(g.isDone()); |
814 |
|
}}; |
815 |
< |
singletonPool.invoke(a); |
815 |
> |
testInvokeOnPool(singletonPool(), a); |
816 |
|
} |
817 |
|
|
818 |
|
/** |
830 |
|
helpQuiesce(); |
831 |
|
threadAssertTrue(f.isDone()); |
832 |
|
}}; |
833 |
< |
asyncSingletonPool.invoke(a); |
833 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
834 |
|
} |
835 |
|
|
836 |
|
/** |
849 |
|
threadAssertTrue(f.isDone()); |
850 |
|
threadAssertFalse(g.isDone()); |
851 |
|
}}; |
852 |
< |
asyncSingletonPool.invoke(a); |
852 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
853 |
|
} |
854 |
|
|
855 |
|
/** |
868 |
|
threadAssertTrue(f.isDone()); |
869 |
|
threadAssertFalse(g.isDone()); |
870 |
|
}}; |
871 |
< |
asyncSingletonPool.invoke(a); |
871 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
872 |
|
} |
873 |
|
|
874 |
|
} |