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.ForkJoinTask; |
12 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
14 |
|
import java.util.concurrent.RecursiveTask; |
14 |
– |
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; |
50 |
– |
} finally { |
51 |
– |
joinPool(pool); |
51 |
|
} |
52 |
|
} |
53 |
|
|
318 |
|
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
319 |
|
} |
320 |
|
|
322 |
– |
|
321 |
|
/** |
322 |
|
* helpQuiesce returns when tasks are complete. |
323 |
|
* getQueuedTaskCount returns 0 when quiescent |
328 |
|
FibTask f = new FibTask(8); |
329 |
|
assertSame(f, f.fork()); |
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 |
|
|
340 |
– |
|
340 |
|
/** |
341 |
|
* invoke task throws exception when task completes abnormally |
342 |
|
*/ |
762 |
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
763 |
|
} |
764 |
|
|
766 |
– |
|
765 |
|
/** |
766 |
|
* invokeAll(tasks) with any null task throws NPE |
767 |
|
*/ |