11 |
|
public class ForkJoinTaskTest 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(ForkJoinTaskTest.class); |
19 |
|
|
20 |
|
/** |
21 |
|
* Testing coverage notes: |
22 |
< |
* |
22 |
> |
* |
23 |
|
* To test extension methods and overrides, most tests use |
24 |
|
* BinaryAsyncAction extension class that processes joins |
25 |
|
* differently than supplied Recursive forms. |
26 |
< |
*/ |
26 |
> |
*/ |
27 |
|
|
28 |
|
static final ForkJoinPool mainPool = new ForkJoinPool(); |
29 |
|
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
40 |
|
private volatile int controlState; |
41 |
|
|
42 |
|
static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater = |
43 |
< |
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
43 |
> |
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
44 |
|
"controlState"); |
45 |
|
|
46 |
|
private BinaryAsyncAction parent; |
92 |
|
break; |
93 |
|
try { |
94 |
|
p.onComplete(a, s); |
95 |
< |
} catch(Throwable rex) { |
95 |
> |
} catch (Throwable rex) { |
96 |
|
p.completeExceptionally(rex); |
97 |
|
return; |
98 |
|
} |
129 |
|
return controlState; |
130 |
|
} |
131 |
|
|
132 |
< |
protected final boolean compareAndSetControlState(int expect, |
132 |
> |
protected final boolean compareAndSetControlState(int expect, |
133 |
|
int update) { |
134 |
|
return controlStateUpdater.compareAndSet(this, expect, update); |
135 |
|
} |
150 |
|
|
151 |
|
static final class AsyncFib extends BinaryAsyncAction { |
152 |
|
int number; |
153 |
< |
public AsyncFib(int n) { |
153 |
> |
public AsyncFib(int n) { |
154 |
|
this.number = n; |
155 |
|
} |
156 |
< |
|
156 |
> |
|
157 |
|
public final boolean exec() { |
158 |
|
AsyncFib f = this; |
159 |
|
int n = f.number; |
179 |
|
|
180 |
|
static final class FailingAsyncFib extends BinaryAsyncAction { |
181 |
|
int number; |
182 |
< |
public FailingAsyncFib(int n) { |
182 |
> |
public FailingAsyncFib(int n) { |
183 |
|
this.number = n; |
184 |
|
} |
185 |
< |
|
185 |
> |
|
186 |
|
public final boolean exec() { |
187 |
|
FailingAsyncFib f = this; |
188 |
|
int n = f.number; |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
< |
/** |
208 |
> |
/** |
209 |
|
* invoke returns when task completes normally. |
210 |
|
* isCompletedAbnormally and isCancelled return false for normally |
211 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
212 |
< |
* |
212 |
> |
* |
213 |
|
*/ |
214 |
|
public void testInvoke() { |
215 |
|
RecursiveAction a = new RecursiveAction() { |
226 |
|
mainPool.invoke(a); |
227 |
|
} |
228 |
|
|
229 |
< |
/** |
229 |
> |
/** |
230 |
|
* quietlyInvoke task returns when task completes normally. |
231 |
|
* isCompletedAbnormally and isCancelled return false for normally |
232 |
|
* completed tasks |
246 |
|
mainPool.invoke(a); |
247 |
|
} |
248 |
|
|
249 |
< |
/** |
249 |
> |
/** |
250 |
|
* join of a forked task returns when task completes |
251 |
|
*/ |
252 |
|
public void testForkJoin() { |
263 |
|
mainPool.invoke(a); |
264 |
|
} |
265 |
|
|
266 |
< |
/** |
266 |
> |
/** |
267 |
|
* get of a forked task returns when task completes |
268 |
|
*/ |
269 |
|
public void testForkGet() { |
275 |
|
f.get(); |
276 |
|
threadAssertTrue(f.number == 21); |
277 |
|
threadAssertTrue(f.isDone()); |
278 |
< |
} catch(Exception ex) { |
278 |
> |
} catch (Exception ex) { |
279 |
|
unexpectedException(); |
280 |
|
} |
281 |
|
} |
283 |
|
mainPool.invoke(a); |
284 |
|
} |
285 |
|
|
286 |
< |
/** |
286 |
> |
/** |
287 |
|
* timed get of a forked task returns when task completes |
288 |
|
*/ |
289 |
|
public void testForkTimedGet() { |
295 |
|
f.get(5L, TimeUnit.SECONDS); |
296 |
|
threadAssertTrue(f.number == 21); |
297 |
|
threadAssertTrue(f.isDone()); |
298 |
< |
} catch(Exception ex) { |
298 |
> |
} catch (Exception ex) { |
299 |
|
unexpectedException(); |
300 |
|
} |
301 |
|
} |
303 |
|
mainPool.invoke(a); |
304 |
|
} |
305 |
|
|
306 |
< |
/** |
306 |
> |
/** |
307 |
|
* timed get with null time unit throws NPE |
308 |
|
*/ |
309 |
|
public void testForkTimedGetNPE() { |
313 |
|
AsyncFib f = new AsyncFib(8); |
314 |
|
f.fork(); |
315 |
|
f.get(5L, null); |
316 |
< |
} catch(NullPointerException success) { |
317 |
< |
} catch(Exception ex) { |
316 |
> |
} catch (NullPointerException success) { |
317 |
> |
} catch (Exception ex) { |
318 |
|
unexpectedException(); |
319 |
|
} |
320 |
|
} |
322 |
|
mainPool.invoke(a); |
323 |
|
} |
324 |
|
|
325 |
< |
/** |
325 |
> |
/** |
326 |
|
* helpJoin of a forked task returns when task completes |
327 |
|
*/ |
328 |
|
public void testForkHelpJoin() { |
338 |
|
mainPool.invoke(a); |
339 |
|
} |
340 |
|
|
341 |
< |
/** |
341 |
> |
/** |
342 |
|
* quietlyJoin of a forked task returns when task completes |
343 |
|
*/ |
344 |
|
public void testForkQuietlyJoin() { |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
< |
/** |
358 |
> |
/** |
359 |
|
* quietlyHelpJoin of a forked task returns when task completes |
360 |
|
*/ |
361 |
|
public void testForkQuietlyHelpJoin() { |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
< |
/** |
375 |
> |
/** |
376 |
|
* helpQuiesce returns when tasks are complete. |
377 |
|
* getQueuedTaskCount returns 0 when quiescent |
378 |
|
*/ |
391 |
|
} |
392 |
|
|
393 |
|
|
394 |
< |
/** |
394 |
> |
/** |
395 |
|
* invoke task throws exception when task completes abnormally |
396 |
|
*/ |
397 |
|
public void testAbnormalInvoke() { |
401 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
402 |
|
f.invoke(); |
403 |
|
shouldThrow(); |
404 |
< |
} catch(FJException success) { |
404 |
> |
} catch (FJException success) { |
405 |
|
} |
406 |
|
} |
407 |
|
}; |
408 |
|
mainPool.invoke(a); |
409 |
|
} |
410 |
|
|
411 |
< |
/** |
411 |
> |
/** |
412 |
|
* quietelyInvoke task returns when task completes abnormally |
413 |
|
*/ |
414 |
|
public void testAbnormalQuietlyInvoke() { |
422 |
|
mainPool.invoke(a); |
423 |
|
} |
424 |
|
|
425 |
< |
/** |
425 |
> |
/** |
426 |
|
* join of a forked task throws exception when task completes abnormally |
427 |
|
*/ |
428 |
|
public void testAbnormalForkJoin() { |
433 |
|
f.fork(); |
434 |
|
f.join(); |
435 |
|
shouldThrow(); |
436 |
< |
} catch(FJException success) { |
436 |
> |
} catch (FJException success) { |
437 |
|
} |
438 |
|
} |
439 |
|
}; |
440 |
|
mainPool.invoke(a); |
441 |
|
} |
442 |
|
|
443 |
< |
/** |
443 |
> |
/** |
444 |
|
* get of a forked task throws exception when task completes abnormally |
445 |
|
*/ |
446 |
|
public void testAbnormalForkGet() { |
451 |
|
f.fork(); |
452 |
|
f.get(); |
453 |
|
shouldThrow(); |
454 |
< |
} catch(Exception success) { |
454 |
> |
} catch (Exception success) { |
455 |
|
} |
456 |
|
} |
457 |
|
}; |
458 |
|
mainPool.invoke(a); |
459 |
|
} |
460 |
|
|
461 |
< |
/** |
461 |
> |
/** |
462 |
|
* timed get of a forked task throws exception when task completes abnormally |
463 |
|
*/ |
464 |
|
public void testAbnormalForkTimedGet() { |
469 |
|
f.fork(); |
470 |
|
f.get(5L, TimeUnit.SECONDS); |
471 |
|
shouldThrow(); |
472 |
< |
} catch(Exception success) { |
472 |
> |
} catch (Exception success) { |
473 |
|
} |
474 |
|
} |
475 |
|
}; |
476 |
|
mainPool.invoke(a); |
477 |
|
} |
478 |
|
|
479 |
< |
/** |
479 |
> |
/** |
480 |
|
* join of a forked task throws exception when task completes abnormally |
481 |
|
*/ |
482 |
|
public void testAbnormalForkHelpJoin() { |
487 |
|
f.fork(); |
488 |
|
f.helpJoin(); |
489 |
|
shouldThrow(); |
490 |
< |
} catch(FJException success) { |
490 |
> |
} catch (FJException success) { |
491 |
|
} |
492 |
|
} |
493 |
|
}; |
494 |
|
mainPool.invoke(a); |
495 |
|
} |
496 |
|
|
497 |
< |
/** |
497 |
> |
/** |
498 |
|
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
499 |
|
* getException of failed task returns its exception. |
500 |
|
* isCompletedAbnormally of a failed task returns true. |
515 |
|
mainPool.invoke(a); |
516 |
|
} |
517 |
|
|
518 |
< |
/** |
518 |
> |
/** |
519 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
520 |
|
*/ |
521 |
|
public void testAbnormalForkQuietlyJoin() { |
532 |
|
mainPool.invoke(a); |
533 |
|
} |
534 |
|
|
535 |
< |
/** |
535 |
> |
/** |
536 |
|
* invoke task throws exception when task cancelled |
537 |
|
*/ |
538 |
|
public void testCancelledInvoke() { |
543 |
|
f.cancel(true); |
544 |
|
f.invoke(); |
545 |
|
shouldThrow(); |
546 |
< |
} catch(CancellationException success) { |
546 |
> |
} catch (CancellationException success) { |
547 |
|
} |
548 |
|
} |
549 |
|
}; |
550 |
|
mainPool.invoke(a); |
551 |
|
} |
552 |
|
|
553 |
< |
/** |
553 |
> |
/** |
554 |
|
* join of a forked task throws exception when task cancelled |
555 |
|
*/ |
556 |
|
public void testCancelledForkJoin() { |
562 |
|
f.fork(); |
563 |
|
f.join(); |
564 |
|
shouldThrow(); |
565 |
< |
} catch(CancellationException success) { |
565 |
> |
} catch (CancellationException success) { |
566 |
|
} |
567 |
|
} |
568 |
|
}; |
569 |
|
mainPool.invoke(a); |
570 |
|
} |
571 |
|
|
572 |
< |
/** |
572 |
> |
/** |
573 |
|
* get of a forked task throws exception when task cancelled |
574 |
|
*/ |
575 |
|
public void testCancelledForkGet() { |
581 |
|
f.fork(); |
582 |
|
f.get(); |
583 |
|
shouldThrow(); |
584 |
< |
} catch(Exception success) { |
584 |
> |
} catch (Exception success) { |
585 |
|
} |
586 |
|
} |
587 |
|
}; |
588 |
|
mainPool.invoke(a); |
589 |
|
} |
590 |
|
|
591 |
< |
/** |
591 |
> |
/** |
592 |
|
* timed get of a forked task throws exception when task cancelled |
593 |
|
*/ |
594 |
|
public void testCancelledForkTimedGet() { |
600 |
|
f.fork(); |
601 |
|
f.get(5L, TimeUnit.SECONDS); |
602 |
|
shouldThrow(); |
603 |
< |
} catch(Exception success) { |
603 |
> |
} catch (Exception success) { |
604 |
|
} |
605 |
|
} |
606 |
|
}; |
607 |
|
mainPool.invoke(a); |
608 |
|
} |
609 |
|
|
610 |
< |
/** |
610 |
> |
/** |
611 |
|
* join of a forked task throws exception when task cancelled |
612 |
|
*/ |
613 |
|
public void testCancelledForkHelpJoin() { |
619 |
|
f.fork(); |
620 |
|
f.helpJoin(); |
621 |
|
shouldThrow(); |
622 |
< |
} catch(CancellationException success) { |
622 |
> |
} catch (CancellationException success) { |
623 |
|
} |
624 |
|
} |
625 |
|
}; |
626 |
|
mainPool.invoke(a); |
627 |
|
} |
628 |
|
|
629 |
< |
/** |
629 |
> |
/** |
630 |
|
* quietlyHelpJoin of a forked task returns when task cancelled. |
631 |
|
* getException of cancelled task returns its exception. |
632 |
|
* isCompletedAbnormally of a cancelled task returns true. |
648 |
|
mainPool.invoke(a); |
649 |
|
} |
650 |
|
|
651 |
< |
/** |
651 |
> |
/** |
652 |
|
* quietlyJoin of a forked task returns when task cancelled |
653 |
|
*/ |
654 |
|
public void testCancelledForkQuietlyJoin() { |
726 |
|
a.invoke(); |
727 |
|
} |
728 |
|
|
729 |
< |
/** |
729 |
> |
/** |
730 |
|
* invoke task throws exception after invoking completeExceptionally |
731 |
|
*/ |
732 |
|
public void testCompleteExceptionally() { |
737 |
|
f.completeExceptionally(new FJException()); |
738 |
|
f.invoke(); |
739 |
|
shouldThrow(); |
740 |
< |
} catch(FJException success) { |
740 |
> |
} catch (FJException success) { |
741 |
|
} |
742 |
|
} |
743 |
|
}; |
744 |
|
mainPool.invoke(a); |
745 |
|
} |
746 |
|
|
747 |
< |
/** |
747 |
> |
/** |
748 |
|
* invokeAll(t1, t2) invokes all task arguments |
749 |
|
*/ |
750 |
|
public void testInvokeAll2() { |
762 |
|
mainPool.invoke(a); |
763 |
|
} |
764 |
|
|
765 |
< |
/** |
765 |
> |
/** |
766 |
|
* invokeAll(tasks) with 1 argument invokes task |
767 |
|
*/ |
768 |
|
public void testInvokeAll1() { |
777 |
|
mainPool.invoke(a); |
778 |
|
} |
779 |
|
|
780 |
< |
/** |
780 |
> |
/** |
781 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
782 |
|
*/ |
783 |
|
public void testInvokeAll3() { |
798 |
|
mainPool.invoke(a); |
799 |
|
} |
800 |
|
|
801 |
< |
/** |
801 |
> |
/** |
802 |
|
* invokeAll(collection) invokes all tasks in the collection |
803 |
|
*/ |
804 |
|
public void testInvokeAllCollection() { |
824 |
|
} |
825 |
|
|
826 |
|
|
827 |
< |
/** |
827 |
> |
/** |
828 |
|
* invokeAll(tasks) with any null task throws NPE |
829 |
|
*/ |
830 |
|
public void testInvokeAllNPE() { |
843 |
|
mainPool.invoke(a); |
844 |
|
} |
845 |
|
|
846 |
< |
/** |
846 |
> |
/** |
847 |
|
* invokeAll(t1, t2) throw exception if any task does |
848 |
|
*/ |
849 |
|
public void testAbnormalInvokeAll2() { |
854 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
855 |
|
invokeAll(f, g); |
856 |
|
shouldThrow(); |
857 |
< |
} catch(FJException success) { |
857 |
> |
} catch (FJException success) { |
858 |
|
} |
859 |
|
} |
860 |
|
}; |
861 |
|
mainPool.invoke(a); |
862 |
|
} |
863 |
|
|
864 |
< |
/** |
864 |
> |
/** |
865 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
866 |
|
*/ |
867 |
|
public void testAbnormalInvokeAll1() { |
871 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
872 |
|
invokeAll(g); |
873 |
|
shouldThrow(); |
874 |
< |
} catch(FJException success) { |
874 |
> |
} catch (FJException success) { |
875 |
|
} |
876 |
|
} |
877 |
|
}; |
878 |
|
mainPool.invoke(a); |
879 |
|
} |
880 |
|
|
881 |
< |
/** |
881 |
> |
/** |
882 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
883 |
|
*/ |
884 |
|
public void testAbnormalInvokeAll3() { |
890 |
|
AsyncFib h = new AsyncFib(7); |
891 |
|
invokeAll(f, g, h); |
892 |
|
shouldThrow(); |
893 |
< |
} catch(FJException success) { |
893 |
> |
} catch (FJException success) { |
894 |
|
} |
895 |
|
} |
896 |
|
}; |
897 |
|
mainPool.invoke(a); |
898 |
|
} |
899 |
|
|
900 |
< |
/** |
900 |
> |
/** |
901 |
|
* invokeAll(collection) throws exception if any task does |
902 |
|
*/ |
903 |
|
public void testAbnormalInvokeAllCollection() { |
913 |
|
set.add(h); |
914 |
|
invokeAll(set); |
915 |
|
shouldThrow(); |
916 |
< |
} catch(FJException success) { |
916 |
> |
} catch (FJException success) { |
917 |
|
} |
918 |
|
} |
919 |
|
}; |
920 |
|
mainPool.invoke(a); |
921 |
|
} |
922 |
|
|
923 |
< |
/** |
923 |
> |
/** |
924 |
|
* tryUnfork returns true for most recent unexecuted task, |
925 |
|
* and suppresses execution |
926 |
|
*/ |
940 |
|
singletonPool.invoke(a); |
941 |
|
} |
942 |
|
|
943 |
< |
/** |
943 |
> |
/** |
944 |
|
* getSurplusQueuedTaskCount returns > 0 when |
945 |
|
* there are more tasks than threads |
946 |
|
*/ |
960 |
|
singletonPool.invoke(a); |
961 |
|
} |
962 |
|
|
963 |
< |
/** |
963 |
> |
/** |
964 |
|
* peekNextLocalTask returns most recent unexecuted task. |
965 |
|
*/ |
966 |
|
public void testPeekNextLocalTask() { |
979 |
|
singletonPool.invoke(a); |
980 |
|
} |
981 |
|
|
982 |
< |
/** |
982 |
> |
/** |
983 |
|
* pollNextLocalTask returns most recent unexecuted task |
984 |
|
* without executing it |
985 |
|
*/ |
998 |
|
singletonPool.invoke(a); |
999 |
|
} |
1000 |
|
|
1001 |
< |
/** |
1001 |
> |
/** |
1002 |
|
* pollTask returns an unexecuted task |
1003 |
|
* without executing it |
1004 |
|
*/ |
1018 |
|
singletonPool.invoke(a); |
1019 |
|
} |
1020 |
|
|
1021 |
< |
/** |
1021 |
> |
/** |
1022 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1023 |
|
*/ |
1024 |
|
public void testPeekNextLocalTaskAsync() { |
1037 |
|
asyncSingletonPool.invoke(a); |
1038 |
|
} |
1039 |
|
|
1040 |
< |
/** |
1040 |
> |
/** |
1041 |
|
* pollNextLocalTask returns least recent unexecuted task |
1042 |
|
* without executing it, in async mode |
1043 |
|
*/ |
1057 |
|
asyncSingletonPool.invoke(a); |
1058 |
|
} |
1059 |
|
|
1060 |
< |
/** |
1060 |
> |
/** |
1061 |
|
* pollTask returns an unexecuted task |
1062 |
|
* without executing it, in async mode |
1063 |
|
*/ |