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 |
349 |
|
f.invoke(); |
350 |
|
shouldThrow(); |
351 |
|
} catch (FJException success) { |
352 |
< |
checkTaskThrew(f, success); |
352 |
> |
checkCompletedAbnormally(f, success); |
353 |
|
} |
354 |
|
return NoResult; |
355 |
|
}}; |
365 |
|
FailingFibTask f = new FailingFibTask(8); |
366 |
|
f.quietlyInvoke(); |
367 |
|
assertTrue(f.getException() instanceof FJException); |
368 |
< |
checkTaskThrew(f, f.getException()); |
368 |
> |
checkCompletedAbnormally(f, f.getException()); |
369 |
|
return NoResult; |
370 |
|
}}; |
371 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
383 |
|
Integer r = f.join(); |
384 |
|
shouldThrow(); |
385 |
|
} catch (FJException success) { |
386 |
< |
checkTaskThrew(f, success); |
386 |
> |
checkCompletedAbnormally(f, success); |
387 |
|
} |
388 |
|
return NoResult; |
389 |
|
}}; |
402 |
|
Integer r = f.get(); |
403 |
|
shouldThrow(); |
404 |
|
} catch (ExecutionException success) { |
405 |
< |
checkTaskThrew(f, success.getCause()); |
405 |
> |
Throwable cause = success.getCause(); |
406 |
> |
assertTrue(cause instanceof FJException); |
407 |
> |
checkCompletedAbnormally(f, cause); |
408 |
|
} |
409 |
|
return NoResult; |
410 |
|
}}; |
423 |
|
Integer r = f.get(5L, SECONDS); |
424 |
|
shouldThrow(); |
425 |
|
} catch (ExecutionException success) { |
426 |
< |
checkTaskThrew(f, success.getCause()); |
426 |
> |
Throwable cause = success.getCause(); |
427 |
> |
assertTrue(cause instanceof FJException); |
428 |
> |
checkCompletedAbnormally(f, cause); |
429 |
|
} |
430 |
|
return NoResult; |
431 |
|
}}; |
442 |
|
assertSame(f, f.fork()); |
443 |
|
f.quietlyJoin(); |
444 |
|
assertTrue(f.getException() instanceof FJException); |
445 |
< |
checkTaskThrew(f, f.getException()); |
445 |
> |
checkCompletedAbnormally(f, f.getException()); |
446 |
|
return NoResult; |
447 |
|
}}; |
448 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
586 |
|
public void testInForkJoinPool2() { |
587 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
588 |
|
public Integer realCompute() { |
589 |
< |
assertTrue(!inForkJoinPool()); |
589 |
> |
assertFalse(inForkJoinPool()); |
590 |
|
return NoResult; |
591 |
|
}}; |
592 |
|
assertSame(NoResult, a.invoke()); |
603 |
|
return NoResult; |
604 |
|
} |
605 |
|
}; |
606 |
< |
a.invoke(); |
606 |
> |
assertSame(NoResult, a.invoke()); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
642 |
|
f.invoke(); |
643 |
|
shouldThrow(); |
644 |
|
} catch (FJException success) { |
645 |
< |
checkTaskThrew(f, success); |
645 |
> |
checkCompletedAbnormally(f, success); |
646 |
|
} |
647 |
|
f.reinitialize(); |
648 |
|
checkNotDone(f); |
664 |
|
Integer r = f.invoke(); |
665 |
|
shouldThrow(); |
666 |
|
} catch (FJException success) { |
667 |
< |
checkTaskThrew(f, success); |
667 |
> |
checkCompletedAbnormally(f, success); |
668 |
|
} |
669 |
|
return NoResult; |
670 |
|
}}; |
794 |
|
invokeAll(f, g); |
795 |
|
shouldThrow(); |
796 |
|
} catch (FJException success) { |
797 |
< |
checkTaskThrew(g, success); |
797 |
> |
checkCompletedAbnormally(g, success); |
798 |
|
} |
799 |
|
return NoResult; |
800 |
|
}}; |
812 |
|
invokeAll(g); |
813 |
|
shouldThrow(); |
814 |
|
} catch (FJException success) { |
815 |
< |
checkTaskThrew(g, success); |
815 |
> |
checkCompletedAbnormally(g, success); |
816 |
|
} |
817 |
|
return NoResult; |
818 |
|
}}; |
832 |
|
invokeAll(f, g, h); |
833 |
|
shouldThrow(); |
834 |
|
} catch (FJException success) { |
835 |
< |
checkTaskThrew(g, success); |
835 |
> |
checkCompletedAbnormally(g, success); |
836 |
|
} |
837 |
|
return NoResult; |
838 |
|
}}; |
856 |
|
invokeAll(set); |
857 |
|
shouldThrow(); |
858 |
|
} catch (FJException success) { |
859 |
< |
checkTaskThrew(f, success); |
859 |
> |
checkCompletedAbnormally(f, success); |
860 |
|
} |
861 |
|
return NoResult; |
862 |
|
}}; |
898 |
|
assertSame(f, f.fork()); |
899 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
900 |
|
helpQuiesce(); |
901 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
902 |
|
checkCompletedNormally(f, 21); |
903 |
|
checkCompletedNormally(g, 34); |
904 |
|
checkCompletedNormally(h, 13); |