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.ForkJoinTask; |
12 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
13 |
|
import java.util.concurrent.RecursiveTask; |
14 |
|
import java.util.concurrent.TimeUnit; |
60 |
|
assertNull(a.getException()); |
61 |
|
assertNull(a.getRawResult()); |
62 |
|
|
63 |
< |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
63 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
64 |
|
Thread.currentThread().interrupt(); |
65 |
|
try { |
66 |
|
a.get(); |
91 |
|
assertNull(a.getException()); |
92 |
|
assertSame(expected, a.getRawResult()); |
93 |
|
assertSame(expected, a.join()); |
94 |
+ |
assertFalse(a.cancel(false)); |
95 |
+ |
assertFalse(a.cancel(true)); |
96 |
|
try { |
97 |
|
assertSame(expected, a.get()); |
98 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
148 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
|
} |
150 |
|
|
151 |
< |
void checkTaskThrew(RecursiveTask a, Throwable t) { |
151 |
> |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
152 |
|
assertTrue(a.isDone()); |
153 |
|
assertFalse(a.isCancelled()); |
154 |
|
assertFalse(a.isCompletedNormally()); |
155 |
|
assertTrue(a.isCompletedAbnormally()); |
156 |
< |
assertSame(t, a.getException()); |
156 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
157 |
|
assertNull(a.getRawResult()); |
158 |
+ |
assertFalse(a.cancel(false)); |
159 |
+ |
assertFalse(a.cancel(true)); |
160 |
|
|
161 |
|
try { |
162 |
|
a.join(); |
163 |
|
shouldThrow(); |
164 |
|
} catch (Throwable expected) { |
165 |
< |
assertSame(t, expected); |
165 |
> |
assertSame(t.getClass(), expected.getClass()); |
166 |
|
} |
167 |
|
|
168 |
|
try { |
169 |
|
a.get(); |
170 |
|
shouldThrow(); |
171 |
|
} catch (ExecutionException success) { |
172 |
< |
assertSame(t, success.getCause()); |
172 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
173 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
174 |
|
|
175 |
|
try { |
176 |
|
a.get(5L, SECONDS); |
177 |
|
shouldThrow(); |
178 |
|
} catch (ExecutionException success) { |
179 |
< |
assertSame(t, success.getCause()); |
179 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
180 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
181 |
|
} |
182 |
|
|
183 |
< |
static final class FJException extends RuntimeException { |
184 |
< |
FJException() { super(); } |
183 |
> |
public static final class FJException extends RuntimeException { |
184 |
> |
public FJException() { super(); } |
185 |
|
} |
186 |
|
|
187 |
|
// An invalid return value for Fib |
319 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
320 |
|
} |
321 |
|
|
317 |
– |
|
322 |
|
/** |
323 |
|
* helpQuiesce returns when tasks are complete. |
324 |
|
* getQueuedTaskCount returns 0 when quiescent |
328 |
|
public Integer realCompute() { |
329 |
|
FibTask f = new FibTask(8); |
330 |
|
assertSame(f, f.fork()); |
331 |
< |
f.helpQuiesce(); |
331 |
> |
helpQuiesce(); |
332 |
|
assertEquals(0, getQueuedTaskCount()); |
333 |
|
checkCompletedNormally(f, 21); |
334 |
|
return NoResult; |
336 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
337 |
|
} |
338 |
|
|
335 |
– |
|
339 |
|
/** |
340 |
|
* invoke task throws exception when task completes abnormally |
341 |
|
*/ |
347 |
|
f.invoke(); |
348 |
|
shouldThrow(); |
349 |
|
} catch (FJException success) { |
350 |
< |
checkTaskThrew(f, success); |
350 |
> |
checkCompletedAbnormally(f, success); |
351 |
|
} |
352 |
|
return NoResult; |
353 |
|
}}; |
363 |
|
FailingFibTask f = new FailingFibTask(8); |
364 |
|
f.quietlyInvoke(); |
365 |
|
assertTrue(f.getException() instanceof FJException); |
366 |
< |
checkTaskThrew(f, f.getException()); |
366 |
> |
checkCompletedAbnormally(f, f.getException()); |
367 |
|
return NoResult; |
368 |
|
}}; |
369 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
381 |
|
Integer r = f.join(); |
382 |
|
shouldThrow(); |
383 |
|
} catch (FJException success) { |
384 |
< |
checkTaskThrew(f, success); |
384 |
> |
checkCompletedAbnormally(f, success); |
385 |
|
} |
386 |
|
return NoResult; |
387 |
|
}}; |
400 |
|
Integer r = f.get(); |
401 |
|
shouldThrow(); |
402 |
|
} catch (ExecutionException success) { |
403 |
< |
checkTaskThrew(f, success.getCause()); |
403 |
> |
Throwable cause = success.getCause(); |
404 |
> |
assertTrue(cause instanceof FJException); |
405 |
> |
checkCompletedAbnormally(f, cause); |
406 |
|
} |
407 |
|
return NoResult; |
408 |
|
}}; |
421 |
|
Integer r = f.get(5L, SECONDS); |
422 |
|
shouldThrow(); |
423 |
|
} catch (ExecutionException success) { |
424 |
< |
checkTaskThrew(f, success.getCause()); |
424 |
> |
Throwable cause = success.getCause(); |
425 |
> |
assertTrue(cause instanceof FJException); |
426 |
> |
checkCompletedAbnormally(f, cause); |
427 |
|
} |
428 |
|
return NoResult; |
429 |
|
}}; |
440 |
|
assertSame(f, f.fork()); |
441 |
|
f.quietlyJoin(); |
442 |
|
assertTrue(f.getException() instanceof FJException); |
443 |
< |
checkTaskThrew(f, f.getException()); |
443 |
> |
checkCompletedAbnormally(f, f.getException()); |
444 |
|
return NoResult; |
445 |
|
}}; |
446 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
584 |
|
public void testInForkJoinPool2() { |
585 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
586 |
|
public Integer realCompute() { |
587 |
< |
assertTrue(!inForkJoinPool()); |
587 |
> |
assertFalse(inForkJoinPool()); |
588 |
|
return NoResult; |
589 |
|
}}; |
590 |
|
assertSame(NoResult, a.invoke()); |
601 |
|
return NoResult; |
602 |
|
} |
603 |
|
}; |
604 |
< |
a.invoke(); |
604 |
> |
assertSame(NoResult, a.invoke()); |
605 |
|
} |
606 |
|
|
607 |
|
/** |
640 |
|
f.invoke(); |
641 |
|
shouldThrow(); |
642 |
|
} catch (FJException success) { |
643 |
< |
checkTaskThrew(f, success); |
643 |
> |
checkCompletedAbnormally(f, success); |
644 |
|
} |
645 |
|
f.reinitialize(); |
646 |
|
checkNotDone(f); |
662 |
|
Integer r = f.invoke(); |
663 |
|
shouldThrow(); |
664 |
|
} catch (FJException success) { |
665 |
< |
checkTaskThrew(f, success); |
665 |
> |
checkCompletedAbnormally(f, success); |
666 |
|
} |
667 |
|
return NoResult; |
668 |
|
}}; |
694 |
|
FibTask f = new FibTask(8); |
695 |
|
FibTask g = new FibTask(9); |
696 |
|
invokeAll(f, g); |
697 |
< |
checkCompletesNormally(f, 21); |
698 |
< |
checkCompletesNormally(g, 34); |
697 |
> |
checkCompletedNormally(f, 21); |
698 |
> |
checkCompletedNormally(g, 34); |
699 |
|
return NoResult; |
700 |
|
}}; |
701 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
709 |
|
public Integer realCompute() { |
710 |
|
FibTask f = new FibTask(8); |
711 |
|
invokeAll(f); |
712 |
< |
checkCompletesNormally(f, 21); |
712 |
> |
checkCompletedNormally(f, 21); |
713 |
|
return NoResult; |
714 |
|
}}; |
715 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
725 |
|
FibTask g = new FibTask(9); |
726 |
|
FibTask h = new FibTask(7); |
727 |
|
invokeAll(f, g, h); |
728 |
< |
checkCompletesNormally(f, 21); |
729 |
< |
checkCompletesNormally(g, 34); |
730 |
< |
checkCompletesNormally(h, 13); |
728 |
> |
assertTrue(f.isDone()); |
729 |
> |
assertTrue(g.isDone()); |
730 |
> |
assertTrue(h.isDone()); |
731 |
> |
checkCompletedNormally(f, 21); |
732 |
> |
checkCompletedNormally(g, 34); |
733 |
> |
checkCompletedNormally(h, 13); |
734 |
|
return NoResult; |
735 |
|
}}; |
736 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
750 |
|
set.add(g); |
751 |
|
set.add(h); |
752 |
|
invokeAll(set); |
753 |
< |
checkCompletesNormally(f, 21); |
754 |
< |
checkCompletesNormally(g, 34); |
755 |
< |
checkCompletesNormally(h, 13); |
753 |
> |
assertTrue(f.isDone()); |
754 |
> |
assertTrue(g.isDone()); |
755 |
> |
assertTrue(h.isDone()); |
756 |
> |
checkCompletedNormally(f, 21); |
757 |
> |
checkCompletedNormally(g, 34); |
758 |
> |
checkCompletedNormally(h, 13); |
759 |
|
return NoResult; |
760 |
|
}}; |
761 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
762 |
|
} |
763 |
|
|
751 |
– |
|
764 |
|
/** |
765 |
|
* invokeAll(tasks) with any null task throws NPE |
766 |
|
*/ |
791 |
|
invokeAll(f, g); |
792 |
|
shouldThrow(); |
793 |
|
} catch (FJException success) { |
794 |
< |
checkTaskThrew(g, success); |
794 |
> |
checkCompletedAbnormally(g, success); |
795 |
|
} |
796 |
|
return NoResult; |
797 |
|
}}; |
809 |
|
invokeAll(g); |
810 |
|
shouldThrow(); |
811 |
|
} catch (FJException success) { |
812 |
< |
checkTaskThrew(g, success); |
812 |
> |
checkCompletedAbnormally(g, success); |
813 |
|
} |
814 |
|
return NoResult; |
815 |
|
}}; |
829 |
|
invokeAll(f, g, h); |
830 |
|
shouldThrow(); |
831 |
|
} catch (FJException success) { |
832 |
< |
checkTaskThrew(g, success); |
832 |
> |
checkCompletedAbnormally(g, success); |
833 |
|
} |
834 |
|
return NoResult; |
835 |
|
}}; |
853 |
|
invokeAll(set); |
854 |
|
shouldThrow(); |
855 |
|
} catch (FJException success) { |
856 |
< |
checkTaskThrew(f, success); |
856 |
> |
checkCompletedAbnormally(f, success); |
857 |
|
} |
858 |
|
return NoResult; |
859 |
|
}}; |
895 |
|
assertSame(f, f.fork()); |
896 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
897 |
|
helpQuiesce(); |
898 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
899 |
|
checkCompletedNormally(f, 21); |
900 |
|
checkCompletedNormally(g, 34); |
901 |
|
checkCompletedNormally(h, 13); |