1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.concurrent.CancellationException; |
9 |
|
import java.util.concurrent.ExecutionException; |
10 |
|
import java.util.concurrent.ForkJoinPool; |
11 |
< |
import java.util.concurrent.ForkJoinWorkerThread; |
11 |
> |
import java.util.concurrent.ForkJoinTask; |
12 |
|
import java.util.concurrent.RecursiveTask; |
13 |
– |
import java.util.concurrent.TimeUnit; |
13 |
|
import java.util.concurrent.TimeoutException; |
14 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
15 |
|
import java.util.HashSet; |
58 |
|
assertNull(a.getException()); |
59 |
|
assertNull(a.getRawResult()); |
60 |
|
|
61 |
< |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
61 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
62 |
|
Thread.currentThread().interrupt(); |
63 |
|
try { |
64 |
|
a.get(); |
89 |
|
assertNull(a.getException()); |
90 |
|
assertSame(expected, a.getRawResult()); |
91 |
|
assertSame(expected, a.join()); |
92 |
+ |
assertFalse(a.cancel(false)); |
93 |
+ |
assertFalse(a.cancel(true)); |
94 |
|
try { |
95 |
|
assertSame(expected, a.get()); |
96 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
146 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
|
} |
148 |
|
|
149 |
< |
void checkTaskThrew(RecursiveTask a, Throwable t) { |
149 |
> |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
150 |
|
assertTrue(a.isDone()); |
151 |
|
assertFalse(a.isCancelled()); |
152 |
|
assertFalse(a.isCompletedNormally()); |
153 |
|
assertTrue(a.isCompletedAbnormally()); |
154 |
< |
assertSame(t, a.getException()); |
154 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
155 |
|
assertNull(a.getRawResult()); |
156 |
+ |
assertFalse(a.cancel(false)); |
157 |
+ |
assertFalse(a.cancel(true)); |
158 |
|
|
159 |
|
try { |
160 |
|
a.join(); |
161 |
|
shouldThrow(); |
162 |
|
} catch (Throwable expected) { |
163 |
< |
assertSame(t, expected); |
163 |
> |
assertSame(t.getClass(), expected.getClass()); |
164 |
|
} |
165 |
|
|
166 |
|
try { |
167 |
|
a.get(); |
168 |
|
shouldThrow(); |
169 |
|
} catch (ExecutionException success) { |
170 |
< |
assertSame(t, success.getCause()); |
170 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
171 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
172 |
|
|
173 |
|
try { |
174 |
|
a.get(5L, SECONDS); |
175 |
|
shouldThrow(); |
176 |
|
} catch (ExecutionException success) { |
177 |
< |
assertSame(t, success.getCause()); |
177 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
178 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
|
} |
180 |
|
|
181 |
< |
static final class FJException extends RuntimeException { |
182 |
< |
FJException() { super(); } |
181 |
> |
public static final class FJException extends RuntimeException { |
182 |
> |
public FJException() { super(); } |
183 |
|
} |
184 |
|
|
185 |
|
// An invalid return value for Fib |
317 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
318 |
|
} |
319 |
|
|
317 |
– |
|
320 |
|
/** |
321 |
|
* helpQuiesce returns when tasks are complete. |
322 |
|
* getQueuedTaskCount returns 0 when quiescent |
326 |
|
public Integer realCompute() { |
327 |
|
FibTask f = new FibTask(8); |
328 |
|
assertSame(f, f.fork()); |
329 |
< |
f.helpQuiesce(); |
329 |
> |
helpQuiesce(); |
330 |
|
assertEquals(0, getQueuedTaskCount()); |
331 |
|
checkCompletedNormally(f, 21); |
332 |
|
return NoResult; |
334 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
335 |
|
} |
336 |
|
|
335 |
– |
|
337 |
|
/** |
338 |
|
* invoke task throws exception when task completes abnormally |
339 |
|
*/ |
345 |
|
f.invoke(); |
346 |
|
shouldThrow(); |
347 |
|
} catch (FJException success) { |
348 |
< |
checkTaskThrew(f, success); |
348 |
> |
checkCompletedAbnormally(f, success); |
349 |
|
} |
350 |
|
return NoResult; |
351 |
|
}}; |
361 |
|
FailingFibTask f = new FailingFibTask(8); |
362 |
|
f.quietlyInvoke(); |
363 |
|
assertTrue(f.getException() instanceof FJException); |
364 |
< |
checkTaskThrew(f, f.getException()); |
364 |
> |
checkCompletedAbnormally(f, f.getException()); |
365 |
|
return NoResult; |
366 |
|
}}; |
367 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
379 |
|
Integer r = f.join(); |
380 |
|
shouldThrow(); |
381 |
|
} catch (FJException success) { |
382 |
< |
checkTaskThrew(f, success); |
382 |
> |
checkCompletedAbnormally(f, success); |
383 |
|
} |
384 |
|
return NoResult; |
385 |
|
}}; |
398 |
|
Integer r = f.get(); |
399 |
|
shouldThrow(); |
400 |
|
} catch (ExecutionException success) { |
401 |
< |
checkTaskThrew(f, success.getCause()); |
401 |
> |
Throwable cause = success.getCause(); |
402 |
> |
assertTrue(cause instanceof FJException); |
403 |
> |
checkCompletedAbnormally(f, cause); |
404 |
|
} |
405 |
|
return NoResult; |
406 |
|
}}; |
419 |
|
Integer r = f.get(5L, SECONDS); |
420 |
|
shouldThrow(); |
421 |
|
} catch (ExecutionException success) { |
422 |
< |
checkTaskThrew(f, success.getCause()); |
422 |
> |
Throwable cause = success.getCause(); |
423 |
> |
assertTrue(cause instanceof FJException); |
424 |
> |
checkCompletedAbnormally(f, cause); |
425 |
|
} |
426 |
|
return NoResult; |
427 |
|
}}; |
438 |
|
assertSame(f, f.fork()); |
439 |
|
f.quietlyJoin(); |
440 |
|
assertTrue(f.getException() instanceof FJException); |
441 |
< |
checkTaskThrew(f, f.getException()); |
441 |
> |
checkCompletedAbnormally(f, f.getException()); |
442 |
|
return NoResult; |
443 |
|
}}; |
444 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
582 |
|
public void testInForkJoinPool2() { |
583 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
584 |
|
public Integer realCompute() { |
585 |
< |
assertTrue(!inForkJoinPool()); |
585 |
> |
assertFalse(inForkJoinPool()); |
586 |
|
return NoResult; |
587 |
|
}}; |
588 |
|
assertSame(NoResult, a.invoke()); |
599 |
|
return NoResult; |
600 |
|
} |
601 |
|
}; |
602 |
< |
a.invoke(); |
602 |
> |
assertSame(NoResult, a.invoke()); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
638 |
|
f.invoke(); |
639 |
|
shouldThrow(); |
640 |
|
} catch (FJException success) { |
641 |
< |
checkTaskThrew(f, success); |
641 |
> |
checkCompletedAbnormally(f, success); |
642 |
|
} |
643 |
|
f.reinitialize(); |
644 |
|
checkNotDone(f); |
660 |
|
Integer r = f.invoke(); |
661 |
|
shouldThrow(); |
662 |
|
} catch (FJException success) { |
663 |
< |
checkTaskThrew(f, success); |
663 |
> |
checkCompletedAbnormally(f, success); |
664 |
|
} |
665 |
|
return NoResult; |
666 |
|
}}; |
692 |
|
FibTask f = new FibTask(8); |
693 |
|
FibTask g = new FibTask(9); |
694 |
|
invokeAll(f, g); |
695 |
< |
checkCompletesNormally(f, 21); |
696 |
< |
checkCompletesNormally(g, 34); |
695 |
> |
checkCompletedNormally(f, 21); |
696 |
> |
checkCompletedNormally(g, 34); |
697 |
|
return NoResult; |
698 |
|
}}; |
699 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
707 |
|
public Integer realCompute() { |
708 |
|
FibTask f = new FibTask(8); |
709 |
|
invokeAll(f); |
710 |
< |
checkCompletesNormally(f, 21); |
710 |
> |
checkCompletedNormally(f, 21); |
711 |
|
return NoResult; |
712 |
|
}}; |
713 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
723 |
|
FibTask g = new FibTask(9); |
724 |
|
FibTask h = new FibTask(7); |
725 |
|
invokeAll(f, g, h); |
726 |
< |
checkCompletesNormally(f, 21); |
727 |
< |
checkCompletesNormally(g, 34); |
728 |
< |
checkCompletesNormally(h, 13); |
726 |
> |
assertTrue(f.isDone()); |
727 |
> |
assertTrue(g.isDone()); |
728 |
> |
assertTrue(h.isDone()); |
729 |
> |
checkCompletedNormally(f, 21); |
730 |
> |
checkCompletedNormally(g, 34); |
731 |
> |
checkCompletedNormally(h, 13); |
732 |
|
return NoResult; |
733 |
|
}}; |
734 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
748 |
|
set.add(g); |
749 |
|
set.add(h); |
750 |
|
invokeAll(set); |
751 |
< |
checkCompletesNormally(f, 21); |
752 |
< |
checkCompletesNormally(g, 34); |
753 |
< |
checkCompletesNormally(h, 13); |
751 |
> |
assertTrue(f.isDone()); |
752 |
> |
assertTrue(g.isDone()); |
753 |
> |
assertTrue(h.isDone()); |
754 |
> |
checkCompletedNormally(f, 21); |
755 |
> |
checkCompletedNormally(g, 34); |
756 |
> |
checkCompletedNormally(h, 13); |
757 |
|
return NoResult; |
758 |
|
}}; |
759 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
760 |
|
} |
761 |
|
|
751 |
– |
|
762 |
|
/** |
763 |
|
* invokeAll(tasks) with any null task throws NPE |
764 |
|
*/ |
789 |
|
invokeAll(f, g); |
790 |
|
shouldThrow(); |
791 |
|
} catch (FJException success) { |
792 |
< |
checkTaskThrew(g, success); |
792 |
> |
checkCompletedAbnormally(g, success); |
793 |
|
} |
794 |
|
return NoResult; |
795 |
|
}}; |
807 |
|
invokeAll(g); |
808 |
|
shouldThrow(); |
809 |
|
} catch (FJException success) { |
810 |
< |
checkTaskThrew(g, success); |
810 |
> |
checkCompletedAbnormally(g, success); |
811 |
|
} |
812 |
|
return NoResult; |
813 |
|
}}; |
827 |
|
invokeAll(f, g, h); |
828 |
|
shouldThrow(); |
829 |
|
} catch (FJException success) { |
830 |
< |
checkTaskThrew(g, success); |
830 |
> |
checkCompletedAbnormally(g, success); |
831 |
|
} |
832 |
|
return NoResult; |
833 |
|
}}; |
851 |
|
invokeAll(set); |
852 |
|
shouldThrow(); |
853 |
|
} catch (FJException success) { |
854 |
< |
checkTaskThrew(f, success); |
854 |
> |
checkCompletedAbnormally(f, success); |
855 |
|
} |
856 |
|
return NoResult; |
857 |
|
}}; |
893 |
|
assertSame(f, f.fork()); |
894 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
895 |
|
helpQuiesce(); |
896 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
897 |
|
checkCompletedNormally(f, 21); |
898 |
|
checkCompletedNormally(g, 34); |
899 |
|
checkCompletedNormally(h, 13); |