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); |
17 |
> |
return new TestSuite(ForkJoinTaskTest.class); |
18 |
|
} |
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) { |
279 |
< |
unexpectedException(); |
278 |
> |
} catch (Exception ex) { |
279 |
> |
unexpectedException(ex); |
280 |
|
} |
281 |
|
} |
282 |
|
}; |
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) { |
299 |
< |
unexpectedException(); |
298 |
> |
} catch (Exception ex) { |
299 |
> |
unexpectedException(ex); |
300 |
|
} |
301 |
|
} |
302 |
|
}; |
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) { |
318 |
< |
unexpectedException(); |
316 |
> |
shouldThrow(); |
317 |
> |
} catch (NullPointerException success) { |
318 |
> |
} catch (Exception ex) { |
319 |
> |
unexpectedException(ex); |
320 |
|
} |
321 |
|
} |
322 |
|
}; |
323 |
|
mainPool.invoke(a); |
324 |
|
} |
325 |
|
|
326 |
< |
/** |
326 |
> |
/** |
327 |
|
* helpJoin of a forked task returns when task completes |
328 |
|
*/ |
329 |
|
public void testForkHelpJoin() { |
339 |
|
mainPool.invoke(a); |
340 |
|
} |
341 |
|
|
342 |
< |
/** |
342 |
> |
/** |
343 |
|
* quietlyJoin of a forked task returns when task completes |
344 |
|
*/ |
345 |
|
public void testForkQuietlyJoin() { |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
< |
/** |
359 |
> |
/** |
360 |
|
* quietlyHelpJoin of a forked task returns when task completes |
361 |
|
*/ |
362 |
|
public void testForkQuietlyHelpJoin() { |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
< |
/** |
376 |
> |
/** |
377 |
|
* helpQuiesce returns when tasks are complete. |
378 |
|
* getQueuedTaskCount returns 0 when quiescent |
379 |
|
*/ |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
< |
/** |
395 |
> |
/** |
396 |
|
* invoke task throws exception when task completes abnormally |
397 |
|
*/ |
398 |
|
public void testAbnormalInvoke() { |
402 |
|
FailingAsyncFib f = new FailingAsyncFib(8); |
403 |
|
f.invoke(); |
404 |
|
shouldThrow(); |
405 |
< |
} catch(FJException success) { |
405 |
> |
} catch (FJException success) { |
406 |
|
} |
407 |
|
} |
408 |
|
}; |
409 |
|
mainPool.invoke(a); |
410 |
|
} |
411 |
|
|
412 |
< |
/** |
413 |
< |
* quietelyInvoke task returns when task completes abnormally |
412 |
> |
/** |
413 |
> |
* quietlyInvoke task returns when task completes abnormally |
414 |
|
*/ |
415 |
|
public void testAbnormalQuietlyInvoke() { |
416 |
|
RecursiveAction a = new RecursiveAction() { |
423 |
|
mainPool.invoke(a); |
424 |
|
} |
425 |
|
|
426 |
< |
/** |
426 |
> |
/** |
427 |
|
* join of a forked task throws exception when task completes abnormally |
428 |
|
*/ |
429 |
|
public void testAbnormalForkJoin() { |
434 |
|
f.fork(); |
435 |
|
f.join(); |
436 |
|
shouldThrow(); |
437 |
< |
} catch(FJException success) { |
437 |
> |
} catch (FJException success) { |
438 |
|
} |
439 |
|
} |
440 |
|
}; |
441 |
|
mainPool.invoke(a); |
442 |
|
} |
443 |
|
|
444 |
< |
/** |
444 |
> |
/** |
445 |
|
* get of a forked task throws exception when task completes abnormally |
446 |
|
*/ |
447 |
|
public void testAbnormalForkGet() { |
452 |
|
f.fork(); |
453 |
|
f.get(); |
454 |
|
shouldThrow(); |
455 |
< |
} catch(Exception success) { |
455 |
> |
} catch (ExecutionException success) { |
456 |
> |
} catch (Exception ex) { |
457 |
> |
unexpectedException(ex); |
458 |
|
} |
459 |
|
} |
460 |
|
}; |
461 |
|
mainPool.invoke(a); |
462 |
|
} |
463 |
|
|
464 |
< |
/** |
464 |
> |
/** |
465 |
|
* timed get of a forked task throws exception when task completes abnormally |
466 |
|
*/ |
467 |
|
public void testAbnormalForkTimedGet() { |
472 |
|
f.fork(); |
473 |
|
f.get(5L, TimeUnit.SECONDS); |
474 |
|
shouldThrow(); |
475 |
< |
} catch(Exception success) { |
475 |
> |
} catch (ExecutionException success) { |
476 |
> |
} catch (Exception ex) { |
477 |
> |
unexpectedException(ex); |
478 |
|
} |
479 |
|
} |
480 |
|
}; |
481 |
|
mainPool.invoke(a); |
482 |
|
} |
483 |
|
|
484 |
< |
/** |
484 |
> |
/** |
485 |
|
* join of a forked task throws exception when task completes abnormally |
486 |
|
*/ |
487 |
|
public void testAbnormalForkHelpJoin() { |
492 |
|
f.fork(); |
493 |
|
f.helpJoin(); |
494 |
|
shouldThrow(); |
495 |
< |
} catch(FJException success) { |
495 |
> |
} catch (FJException success) { |
496 |
|
} |
497 |
|
} |
498 |
|
}; |
499 |
|
mainPool.invoke(a); |
500 |
|
} |
501 |
|
|
502 |
< |
/** |
502 |
> |
/** |
503 |
|
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
504 |
|
* getException of failed task returns its exception. |
505 |
|
* isCompletedAbnormally of a failed task returns true. |
520 |
|
mainPool.invoke(a); |
521 |
|
} |
522 |
|
|
523 |
< |
/** |
523 |
> |
/** |
524 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
525 |
|
*/ |
526 |
|
public void testAbnormalForkQuietlyJoin() { |
537 |
|
mainPool.invoke(a); |
538 |
|
} |
539 |
|
|
540 |
< |
/** |
540 |
> |
/** |
541 |
|
* invoke task throws exception when task cancelled |
542 |
|
*/ |
543 |
|
public void testCancelledInvoke() { |
548 |
|
f.cancel(true); |
549 |
|
f.invoke(); |
550 |
|
shouldThrow(); |
551 |
< |
} catch(CancellationException success) { |
551 |
> |
} catch (CancellationException success) { |
552 |
|
} |
553 |
|
} |
554 |
|
}; |
555 |
|
mainPool.invoke(a); |
556 |
|
} |
557 |
|
|
558 |
< |
/** |
558 |
> |
/** |
559 |
|
* join of a forked task throws exception when task cancelled |
560 |
|
*/ |
561 |
|
public void testCancelledForkJoin() { |
567 |
|
f.fork(); |
568 |
|
f.join(); |
569 |
|
shouldThrow(); |
570 |
< |
} catch(CancellationException success) { |
570 |
> |
} catch (CancellationException success) { |
571 |
|
} |
572 |
|
} |
573 |
|
}; |
574 |
|
mainPool.invoke(a); |
575 |
|
} |
576 |
|
|
577 |
< |
/** |
577 |
> |
/** |
578 |
|
* get of a forked task throws exception when task cancelled |
579 |
|
*/ |
580 |
|
public void testCancelledForkGet() { |
586 |
|
f.fork(); |
587 |
|
f.get(); |
588 |
|
shouldThrow(); |
589 |
< |
} catch(Exception success) { |
589 |
> |
} catch (CancellationException success) { |
590 |
> |
} catch (Exception ex) { |
591 |
> |
unexpectedException(ex); |
592 |
|
} |
593 |
|
} |
594 |
|
}; |
595 |
|
mainPool.invoke(a); |
596 |
|
} |
597 |
|
|
598 |
< |
/** |
598 |
> |
/** |
599 |
|
* timed get of a forked task throws exception when task cancelled |
600 |
|
*/ |
601 |
|
public void testCancelledForkTimedGet() { |
607 |
|
f.fork(); |
608 |
|
f.get(5L, TimeUnit.SECONDS); |
609 |
|
shouldThrow(); |
610 |
< |
} catch(Exception success) { |
610 |
> |
} catch (CancellationException success) { |
611 |
> |
} catch (Exception ex) { |
612 |
> |
unexpectedException(ex); |
613 |
|
} |
614 |
|
} |
615 |
|
}; |
616 |
|
mainPool.invoke(a); |
617 |
|
} |
618 |
|
|
619 |
< |
/** |
619 |
> |
/** |
620 |
|
* join of a forked task throws exception when task cancelled |
621 |
|
*/ |
622 |
|
public void testCancelledForkHelpJoin() { |
628 |
|
f.fork(); |
629 |
|
f.helpJoin(); |
630 |
|
shouldThrow(); |
631 |
< |
} catch(CancellationException success) { |
631 |
> |
} catch (CancellationException success) { |
632 |
|
} |
633 |
|
} |
634 |
|
}; |
635 |
|
mainPool.invoke(a); |
636 |
|
} |
637 |
|
|
638 |
< |
/** |
638 |
> |
/** |
639 |
|
* quietlyHelpJoin of a forked task returns when task cancelled. |
640 |
|
* getException of cancelled task returns its exception. |
641 |
|
* isCompletedAbnormally of a cancelled task returns true. |
657 |
|
mainPool.invoke(a); |
658 |
|
} |
659 |
|
|
660 |
< |
/** |
660 |
> |
/** |
661 |
|
* quietlyJoin of a forked task returns when task cancelled |
662 |
|
*/ |
663 |
|
public void testCancelledForkQuietlyJoin() { |
735 |
|
a.invoke(); |
736 |
|
} |
737 |
|
|
738 |
< |
/** |
738 |
> |
/** |
739 |
|
* invoke task throws exception after invoking completeExceptionally |
740 |
|
*/ |
741 |
|
public void testCompleteExceptionally() { |
746 |
|
f.completeExceptionally(new FJException()); |
747 |
|
f.invoke(); |
748 |
|
shouldThrow(); |
749 |
< |
} catch(FJException success) { |
749 |
> |
} catch (FJException success) { |
750 |
|
} |
751 |
|
} |
752 |
|
}; |
753 |
|
mainPool.invoke(a); |
754 |
|
} |
755 |
|
|
756 |
< |
/** |
756 |
> |
/** |
757 |
|
* invokeAll(t1, t2) invokes all task arguments |
758 |
|
*/ |
759 |
|
public void testInvokeAll2() { |
771 |
|
mainPool.invoke(a); |
772 |
|
} |
773 |
|
|
774 |
< |
/** |
774 |
> |
/** |
775 |
|
* invokeAll(tasks) with 1 argument invokes task |
776 |
|
*/ |
777 |
|
public void testInvokeAll1() { |
786 |
|
mainPool.invoke(a); |
787 |
|
} |
788 |
|
|
789 |
< |
/** |
789 |
> |
/** |
790 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
791 |
|
*/ |
792 |
|
public void testInvokeAll3() { |
807 |
|
mainPool.invoke(a); |
808 |
|
} |
809 |
|
|
810 |
< |
/** |
810 |
> |
/** |
811 |
|
* invokeAll(collection) invokes all tasks in the collection |
812 |
|
*/ |
813 |
|
public void testInvokeAllCollection() { |
833 |
|
} |
834 |
|
|
835 |
|
|
836 |
< |
/** |
836 |
> |
/** |
837 |
|
* invokeAll(tasks) with any null task throws NPE |
838 |
|
*/ |
839 |
|
public void testInvokeAllNPE() { |
852 |
|
mainPool.invoke(a); |
853 |
|
} |
854 |
|
|
855 |
< |
/** |
855 |
> |
/** |
856 |
|
* invokeAll(t1, t2) throw exception if any task does |
857 |
|
*/ |
858 |
|
public void testAbnormalInvokeAll2() { |
863 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
864 |
|
invokeAll(f, g); |
865 |
|
shouldThrow(); |
866 |
< |
} catch(FJException success) { |
866 |
> |
} catch (FJException success) { |
867 |
|
} |
868 |
|
} |
869 |
|
}; |
870 |
|
mainPool.invoke(a); |
871 |
|
} |
872 |
|
|
873 |
< |
/** |
873 |
> |
/** |
874 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
875 |
|
*/ |
876 |
|
public void testAbnormalInvokeAll1() { |
880 |
|
FailingAsyncFib g = new FailingAsyncFib(9); |
881 |
|
invokeAll(g); |
882 |
|
shouldThrow(); |
883 |
< |
} catch(FJException success) { |
883 |
> |
} catch (FJException success) { |
884 |
|
} |
885 |
|
} |
886 |
|
}; |
887 |
|
mainPool.invoke(a); |
888 |
|
} |
889 |
|
|
890 |
< |
/** |
890 |
> |
/** |
891 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
892 |
|
*/ |
893 |
|
public void testAbnormalInvokeAll3() { |
899 |
|
AsyncFib h = new AsyncFib(7); |
900 |
|
invokeAll(f, g, h); |
901 |
|
shouldThrow(); |
902 |
< |
} catch(FJException success) { |
902 |
> |
} catch (FJException success) { |
903 |
|
} |
904 |
|
} |
905 |
|
}; |
906 |
|
mainPool.invoke(a); |
907 |
|
} |
908 |
|
|
909 |
< |
/** |
909 |
> |
/** |
910 |
|
* invokeAll(collection) throws exception if any task does |
911 |
|
*/ |
912 |
|
public void testAbnormalInvokeAllCollection() { |
922 |
|
set.add(h); |
923 |
|
invokeAll(set); |
924 |
|
shouldThrow(); |
925 |
< |
} catch(FJException success) { |
925 |
> |
} catch (FJException success) { |
926 |
|
} |
927 |
|
} |
928 |
|
}; |
929 |
|
mainPool.invoke(a); |
930 |
|
} |
931 |
|
|
932 |
< |
/** |
932 |
> |
/** |
933 |
|
* tryUnfork returns true for most recent unexecuted task, |
934 |
|
* and suppresses execution |
935 |
|
*/ |
949 |
|
singletonPool.invoke(a); |
950 |
|
} |
951 |
|
|
952 |
< |
/** |
952 |
> |
/** |
953 |
|
* getSurplusQueuedTaskCount returns > 0 when |
954 |
|
* there are more tasks than threads |
955 |
|
*/ |
969 |
|
singletonPool.invoke(a); |
970 |
|
} |
971 |
|
|
972 |
< |
/** |
972 |
> |
/** |
973 |
|
* peekNextLocalTask returns most recent unexecuted task. |
974 |
|
*/ |
975 |
|
public void testPeekNextLocalTask() { |
988 |
|
singletonPool.invoke(a); |
989 |
|
} |
990 |
|
|
991 |
< |
/** |
991 |
> |
/** |
992 |
|
* pollNextLocalTask returns most recent unexecuted task |
993 |
|
* without executing it |
994 |
|
*/ |
1007 |
|
singletonPool.invoke(a); |
1008 |
|
} |
1009 |
|
|
1010 |
< |
/** |
1010 |
> |
/** |
1011 |
|
* pollTask returns an unexecuted task |
1012 |
|
* without executing it |
1013 |
|
*/ |
1027 |
|
singletonPool.invoke(a); |
1028 |
|
} |
1029 |
|
|
1030 |
< |
/** |
1030 |
> |
/** |
1031 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1032 |
|
*/ |
1033 |
|
public void testPeekNextLocalTaskAsync() { |
1046 |
|
asyncSingletonPool.invoke(a); |
1047 |
|
} |
1048 |
|
|
1049 |
< |
/** |
1049 |
> |
/** |
1050 |
|
* pollNextLocalTask returns least recent unexecuted task |
1051 |
|
* without executing it, in async mode |
1052 |
|
*/ |
1066 |
|
asyncSingletonPool.invoke(a); |
1067 |
|
} |
1068 |
|
|
1069 |
< |
/** |
1069 |
> |
/** |
1070 |
|
* pollTask returns an unexecuted task |
1071 |
|
* without executing it, in async mode |
1072 |
|
*/ |