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.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
> |
|
9 |
> |
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
11 |
|
import java.util.concurrent.ExecutionException; |
12 |
|
import java.util.concurrent.ForkJoinPool; |
13 |
< |
import java.util.concurrent.ForkJoinWorkerThread; |
13 |
> |
import java.util.concurrent.ForkJoinTask; |
14 |
|
import java.util.concurrent.RecursiveTask; |
13 |
– |
import java.util.concurrent.TimeUnit; |
15 |
|
import java.util.concurrent.TimeoutException; |
16 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
17 |
< |
import java.util.HashSet; |
16 |
> |
|
17 |
> |
import junit.framework.Test; |
18 |
> |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(RecursiveTaskTest.class); |
41 |
|
} |
42 |
|
|
43 |
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
44 |
< |
try { |
44 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
45 |
|
checkNotDone(a); |
46 |
|
|
47 |
|
T result = pool.invoke(a); |
48 |
|
|
49 |
|
checkCompletedNormally(a, result); |
50 |
|
return result; |
49 |
– |
} finally { |
50 |
– |
joinPool(pool); |
51 |
|
} |
52 |
|
} |
53 |
|
|
59 |
|
assertNull(a.getException()); |
60 |
|
assertNull(a.getRawResult()); |
61 |
|
|
62 |
< |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
62 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
63 |
|
Thread.currentThread().interrupt(); |
64 |
|
try { |
65 |
|
a.get(); |
90 |
|
assertNull(a.getException()); |
91 |
|
assertSame(expected, a.getRawResult()); |
92 |
|
assertSame(expected, a.join()); |
93 |
+ |
assertFalse(a.cancel(false)); |
94 |
+ |
assertFalse(a.cancel(true)); |
95 |
|
try { |
96 |
|
assertSame(expected, a.get()); |
97 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
148 |
|
} |
149 |
|
|
150 |
< |
void checkTaskThrew(RecursiveTask a, Throwable t) { |
150 |
> |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
151 |
|
assertTrue(a.isDone()); |
152 |
|
assertFalse(a.isCancelled()); |
153 |
|
assertFalse(a.isCompletedNormally()); |
154 |
|
assertTrue(a.isCompletedAbnormally()); |
155 |
< |
assertSame(t, a.getException()); |
155 |
> |
assertSame(t.getClass(), a.getException().getClass()); |
156 |
|
assertNull(a.getRawResult()); |
157 |
+ |
assertFalse(a.cancel(false)); |
158 |
+ |
assertFalse(a.cancel(true)); |
159 |
|
|
160 |
|
try { |
161 |
|
a.join(); |
162 |
|
shouldThrow(); |
163 |
|
} catch (Throwable expected) { |
164 |
< |
assertSame(t, expected); |
164 |
> |
assertSame(t.getClass(), expected.getClass()); |
165 |
|
} |
166 |
|
|
167 |
|
try { |
168 |
|
a.get(); |
169 |
|
shouldThrow(); |
170 |
|
} catch (ExecutionException success) { |
171 |
< |
assertSame(t, success.getCause()); |
171 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
172 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
173 |
|
|
174 |
|
try { |
175 |
|
a.get(5L, SECONDS); |
176 |
|
shouldThrow(); |
177 |
|
} catch (ExecutionException success) { |
178 |
< |
assertSame(t, success.getCause()); |
178 |
> |
assertSame(t.getClass(), success.getCause().getClass()); |
179 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
180 |
|
} |
181 |
|
|
182 |
< |
static final class FJException extends RuntimeException { |
183 |
< |
FJException() { super(); } |
182 |
> |
public static final class FJException extends RuntimeException { |
183 |
> |
public FJException() { super(); } |
184 |
|
} |
185 |
|
|
186 |
< |
// An invalid return value for Fib |
186 |
> |
/** An invalid return value for Fib. */ |
187 |
|
static final Integer NoResult = Integer.valueOf(-17); |
188 |
|
|
189 |
< |
// A simple recursive task for testing |
189 |
> |
/** A simple recursive task for testing. */ |
190 |
|
final class FibTask extends CheckedRecursiveTask<Integer> { |
191 |
|
final int number; |
192 |
|
FibTask(int n) { number = n; } |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
< |
// A recursive action failing in base case |
207 |
> |
/** A recursive action failing in base case. */ |
208 |
|
final class FailingFibTask extends RecursiveTask<Integer> { |
209 |
|
final int number; |
210 |
|
int result; |
318 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
319 |
|
} |
320 |
|
|
317 |
– |
|
321 |
|
/** |
322 |
|
* helpQuiesce returns when tasks are complete. |
323 |
|
* getQueuedTaskCount returns 0 when quiescent |
327 |
|
public Integer realCompute() { |
328 |
|
FibTask f = new FibTask(8); |
329 |
|
assertSame(f, f.fork()); |
330 |
< |
f.helpQuiesce(); |
330 |
> |
helpQuiesce(); |
331 |
> |
while (!f.isDone()) // wait out race |
332 |
> |
; |
333 |
|
assertEquals(0, getQueuedTaskCount()); |
334 |
|
checkCompletedNormally(f, 21); |
335 |
|
return NoResult; |
337 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
338 |
|
} |
339 |
|
|
335 |
– |
|
340 |
|
/** |
341 |
|
* invoke task throws exception when task completes abnormally |
342 |
|
*/ |
348 |
|
f.invoke(); |
349 |
|
shouldThrow(); |
350 |
|
} catch (FJException success) { |
351 |
< |
checkTaskThrew(f, success); |
351 |
> |
checkCompletedAbnormally(f, success); |
352 |
|
} |
353 |
|
return NoResult; |
354 |
|
}}; |
364 |
|
FailingFibTask f = new FailingFibTask(8); |
365 |
|
f.quietlyInvoke(); |
366 |
|
assertTrue(f.getException() instanceof FJException); |
367 |
< |
checkTaskThrew(f, f.getException()); |
367 |
> |
checkCompletedAbnormally(f, f.getException()); |
368 |
|
return NoResult; |
369 |
|
}}; |
370 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
382 |
|
Integer r = f.join(); |
383 |
|
shouldThrow(); |
384 |
|
} catch (FJException success) { |
385 |
< |
checkTaskThrew(f, success); |
385 |
> |
checkCompletedAbnormally(f, success); |
386 |
|
} |
387 |
|
return NoResult; |
388 |
|
}}; |
401 |
|
Integer r = f.get(); |
402 |
|
shouldThrow(); |
403 |
|
} catch (ExecutionException success) { |
404 |
< |
checkTaskThrew(f, success.getCause()); |
404 |
> |
Throwable cause = success.getCause(); |
405 |
> |
assertTrue(cause instanceof FJException); |
406 |
> |
checkCompletedAbnormally(f, cause); |
407 |
|
} |
408 |
|
return NoResult; |
409 |
|
}}; |
422 |
|
Integer r = f.get(5L, SECONDS); |
423 |
|
shouldThrow(); |
424 |
|
} catch (ExecutionException success) { |
425 |
< |
checkTaskThrew(f, success.getCause()); |
425 |
> |
Throwable cause = success.getCause(); |
426 |
> |
assertTrue(cause instanceof FJException); |
427 |
> |
checkCompletedAbnormally(f, cause); |
428 |
|
} |
429 |
|
return NoResult; |
430 |
|
}}; |
441 |
|
assertSame(f, f.fork()); |
442 |
|
f.quietlyJoin(); |
443 |
|
assertTrue(f.getException() instanceof FJException); |
444 |
< |
checkTaskThrew(f, f.getException()); |
444 |
> |
checkCompletedAbnormally(f, f.getException()); |
445 |
|
return NoResult; |
446 |
|
}}; |
447 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
585 |
|
public void testInForkJoinPool2() { |
586 |
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
587 |
|
public Integer realCompute() { |
588 |
< |
assertTrue(!inForkJoinPool()); |
588 |
> |
assertFalse(inForkJoinPool()); |
589 |
|
return NoResult; |
590 |
|
}}; |
591 |
|
assertSame(NoResult, a.invoke()); |
602 |
|
return NoResult; |
603 |
|
} |
604 |
|
}; |
605 |
< |
a.invoke(); |
605 |
> |
assertSame(NoResult, a.invoke()); |
606 |
|
} |
607 |
|
|
608 |
|
/** |
641 |
|
f.invoke(); |
642 |
|
shouldThrow(); |
643 |
|
} catch (FJException success) { |
644 |
< |
checkTaskThrew(f, success); |
644 |
> |
checkCompletedAbnormally(f, success); |
645 |
|
} |
646 |
|
f.reinitialize(); |
647 |
|
checkNotDone(f); |
663 |
|
Integer r = f.invoke(); |
664 |
|
shouldThrow(); |
665 |
|
} catch (FJException success) { |
666 |
< |
checkTaskThrew(f, success); |
666 |
> |
checkCompletedAbnormally(f, success); |
667 |
|
} |
668 |
|
return NoResult; |
669 |
|
}}; |
695 |
|
FibTask f = new FibTask(8); |
696 |
|
FibTask g = new FibTask(9); |
697 |
|
invokeAll(f, g); |
698 |
< |
checkCompletesNormally(f, 21); |
699 |
< |
checkCompletesNormally(g, 34); |
698 |
> |
checkCompletedNormally(f, 21); |
699 |
> |
checkCompletedNormally(g, 34); |
700 |
|
return NoResult; |
701 |
|
}}; |
702 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
710 |
|
public Integer realCompute() { |
711 |
|
FibTask f = new FibTask(8); |
712 |
|
invokeAll(f); |
713 |
< |
checkCompletesNormally(f, 21); |
713 |
> |
checkCompletedNormally(f, 21); |
714 |
|
return NoResult; |
715 |
|
}}; |
716 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
726 |
|
FibTask g = new FibTask(9); |
727 |
|
FibTask h = new FibTask(7); |
728 |
|
invokeAll(f, g, h); |
729 |
< |
checkCompletesNormally(f, 21); |
730 |
< |
checkCompletesNormally(g, 34); |
731 |
< |
checkCompletesNormally(h, 13); |
729 |
> |
assertTrue(f.isDone()); |
730 |
> |
assertTrue(g.isDone()); |
731 |
> |
assertTrue(h.isDone()); |
732 |
> |
checkCompletedNormally(f, 21); |
733 |
> |
checkCompletedNormally(g, 34); |
734 |
> |
checkCompletedNormally(h, 13); |
735 |
|
return NoResult; |
736 |
|
}}; |
737 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
751 |
|
set.add(g); |
752 |
|
set.add(h); |
753 |
|
invokeAll(set); |
754 |
< |
checkCompletesNormally(f, 21); |
755 |
< |
checkCompletesNormally(g, 34); |
756 |
< |
checkCompletesNormally(h, 13); |
754 |
> |
assertTrue(f.isDone()); |
755 |
> |
assertTrue(g.isDone()); |
756 |
> |
assertTrue(h.isDone()); |
757 |
> |
checkCompletedNormally(f, 21); |
758 |
> |
checkCompletedNormally(g, 34); |
759 |
> |
checkCompletedNormally(h, 13); |
760 |
|
return NoResult; |
761 |
|
}}; |
762 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
763 |
|
} |
764 |
|
|
751 |
– |
|
765 |
|
/** |
766 |
|
* invokeAll(tasks) with any null task throws NPE |
767 |
|
*/ |
792 |
|
invokeAll(f, g); |
793 |
|
shouldThrow(); |
794 |
|
} catch (FJException success) { |
795 |
< |
checkTaskThrew(g, success); |
795 |
> |
checkCompletedAbnormally(g, success); |
796 |
|
} |
797 |
|
return NoResult; |
798 |
|
}}; |
810 |
|
invokeAll(g); |
811 |
|
shouldThrow(); |
812 |
|
} catch (FJException success) { |
813 |
< |
checkTaskThrew(g, success); |
813 |
> |
checkCompletedAbnormally(g, success); |
814 |
|
} |
815 |
|
return NoResult; |
816 |
|
}}; |
830 |
|
invokeAll(f, g, h); |
831 |
|
shouldThrow(); |
832 |
|
} catch (FJException success) { |
833 |
< |
checkTaskThrew(g, success); |
833 |
> |
checkCompletedAbnormally(g, success); |
834 |
|
} |
835 |
|
return NoResult; |
836 |
|
}}; |
854 |
|
invokeAll(set); |
855 |
|
shouldThrow(); |
856 |
|
} catch (FJException success) { |
857 |
< |
checkTaskThrew(f, success); |
857 |
> |
checkCompletedAbnormally(f, success); |
858 |
|
} |
859 |
|
return NoResult; |
860 |
|
}}; |
896 |
|
assertSame(f, f.fork()); |
897 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
898 |
|
helpQuiesce(); |
899 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
900 |
|
checkCompletedNormally(f, 21); |
901 |
|
checkCompletedNormally(g, 34); |
902 |
|
checkCompletedNormally(h, 13); |