1 |
dl |
1.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 |
jsr166 |
1.27 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
jsr166 |
1.13 |
|
7 |
jsr166 |
1.31 |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
|
|
|
9 |
|
|
import java.util.HashSet; |
10 |
jsr166 |
1.15 |
import java.util.concurrent.CancellationException; |
11 |
|
|
import java.util.concurrent.ExecutionException; |
12 |
|
|
import java.util.concurrent.ForkJoinPool; |
13 |
jsr166 |
1.25 |
import java.util.concurrent.ForkJoinTask; |
14 |
jsr166 |
1.15 |
import java.util.concurrent.RecursiveTask; |
15 |
jsr166 |
1.20 |
import java.util.concurrent.TimeoutException; |
16 |
jsr166 |
1.31 |
|
17 |
|
|
import junit.framework.Test; |
18 |
|
|
import junit.framework.TestSuite; |
19 |
dl |
1.1 |
|
20 |
|
|
public class RecursiveTaskTest extends JSR166TestCase { |
21 |
|
|
|
22 |
|
|
public static void main(String[] args) { |
23 |
jsr166 |
1.32 |
main(suite(), args); |
24 |
dl |
1.1 |
} |
25 |
|
|
public static Test suite() { |
26 |
jsr166 |
1.8 |
return new TestSuite(RecursiveTaskTest.class); |
27 |
dl |
1.1 |
} |
28 |
|
|
|
29 |
jsr166 |
1.13 |
private static ForkJoinPool mainPool() { |
30 |
|
|
return new ForkJoinPool(); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
private static ForkJoinPool singletonPool() { |
34 |
|
|
return new ForkJoinPool(1); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
private static ForkJoinPool asyncSingletonPool() { |
38 |
|
|
return new ForkJoinPool(1, |
39 |
|
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
40 |
|
|
null, true); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
44 |
jsr166 |
1.33 |
try (PoolCleaner cleaner = cleaner(pool)) { |
45 |
jsr166 |
1.20 |
checkNotDone(a); |
46 |
jsr166 |
1.19 |
|
47 |
|
|
T result = pool.invoke(a); |
48 |
|
|
|
49 |
jsr166 |
1.20 |
checkCompletedNormally(a, result); |
50 |
jsr166 |
1.19 |
return result; |
51 |
jsr166 |
1.13 |
} |
52 |
|
|
} |
53 |
dl |
1.1 |
|
54 |
jsr166 |
1.20 |
void checkNotDone(RecursiveTask a) { |
55 |
|
|
assertFalse(a.isDone()); |
56 |
|
|
assertFalse(a.isCompletedNormally()); |
57 |
|
|
assertFalse(a.isCompletedAbnormally()); |
58 |
|
|
assertFalse(a.isCancelled()); |
59 |
|
|
assertNull(a.getException()); |
60 |
|
|
assertNull(a.getRawResult()); |
61 |
|
|
|
62 |
jsr166 |
1.25 |
if (! ForkJoinTask.inForkJoinPool()) { |
63 |
jsr166 |
1.20 |
Thread.currentThread().interrupt(); |
64 |
|
|
try { |
65 |
|
|
a.get(); |
66 |
|
|
shouldThrow(); |
67 |
|
|
} catch (InterruptedException success) { |
68 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
|
|
70 |
|
|
Thread.currentThread().interrupt(); |
71 |
|
|
try { |
72 |
|
|
a.get(5L, SECONDS); |
73 |
|
|
shouldThrow(); |
74 |
|
|
} catch (InterruptedException success) { |
75 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
try { |
79 |
|
|
a.get(0L, SECONDS); |
80 |
|
|
shouldThrow(); |
81 |
|
|
} catch (TimeoutException success) { |
82 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
<T> void checkCompletedNormally(RecursiveTask<T> a, T expected) { |
86 |
|
|
assertTrue(a.isDone()); |
87 |
|
|
assertFalse(a.isCancelled()); |
88 |
|
|
assertTrue(a.isCompletedNormally()); |
89 |
|
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
|
assertNull(a.getException()); |
91 |
|
|
assertSame(expected, a.getRawResult()); |
92 |
|
|
assertSame(expected, a.join()); |
93 |
jsr166 |
1.23 |
assertFalse(a.cancel(false)); |
94 |
|
|
assertFalse(a.cancel(true)); |
95 |
jsr166 |
1.20 |
try { |
96 |
|
|
assertSame(expected, a.get()); |
97 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
98 |
|
|
try { |
99 |
|
|
assertSame(expected, a.get(5L, SECONDS)); |
100 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Waits for the task to complete, and checks that when it does, |
105 |
|
|
* it will have an Integer result equals to the given int. |
106 |
|
|
*/ |
107 |
|
|
void checkCompletesNormally(RecursiveTask<Integer> a, int expected) { |
108 |
|
|
Integer r = a.join(); |
109 |
|
|
assertEquals(expected, (int) r); |
110 |
|
|
checkCompletedNormally(a, r); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* Like checkCompletesNormally, but verifies that the task has |
115 |
|
|
* already completed. |
116 |
|
|
*/ |
117 |
|
|
void checkCompletedNormally(RecursiveTask<Integer> a, int expected) { |
118 |
|
|
Integer r = a.getRawResult(); |
119 |
|
|
assertEquals(expected, (int) r); |
120 |
|
|
checkCompletedNormally(a, r); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
void checkCancelled(RecursiveTask a) { |
124 |
|
|
assertTrue(a.isDone()); |
125 |
|
|
assertTrue(a.isCancelled()); |
126 |
|
|
assertFalse(a.isCompletedNormally()); |
127 |
|
|
assertTrue(a.isCompletedAbnormally()); |
128 |
|
|
assertTrue(a.getException() instanceof CancellationException); |
129 |
|
|
assertNull(a.getRawResult()); |
130 |
|
|
|
131 |
|
|
try { |
132 |
|
|
a.join(); |
133 |
|
|
shouldThrow(); |
134 |
|
|
} catch (CancellationException success) { |
135 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
136 |
|
|
|
137 |
|
|
try { |
138 |
|
|
a.get(); |
139 |
|
|
shouldThrow(); |
140 |
|
|
} catch (CancellationException success) { |
141 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
142 |
|
|
|
143 |
|
|
try { |
144 |
|
|
a.get(5L, SECONDS); |
145 |
|
|
shouldThrow(); |
146 |
|
|
} catch (CancellationException success) { |
147 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
148 |
|
|
} |
149 |
|
|
|
150 |
jsr166 |
1.22 |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
151 |
jsr166 |
1.20 |
assertTrue(a.isDone()); |
152 |
|
|
assertFalse(a.isCancelled()); |
153 |
|
|
assertFalse(a.isCompletedNormally()); |
154 |
|
|
assertTrue(a.isCompletedAbnormally()); |
155 |
dl |
1.26 |
assertSame(t.getClass(), a.getException().getClass()); |
156 |
jsr166 |
1.20 |
assertNull(a.getRawResult()); |
157 |
jsr166 |
1.23 |
assertFalse(a.cancel(false)); |
158 |
|
|
assertFalse(a.cancel(true)); |
159 |
jsr166 |
1.20 |
|
160 |
|
|
try { |
161 |
|
|
a.join(); |
162 |
|
|
shouldThrow(); |
163 |
|
|
} catch (Throwable expected) { |
164 |
dl |
1.26 |
assertSame(t.getClass(), expected.getClass()); |
165 |
jsr166 |
1.20 |
} |
166 |
|
|
|
167 |
|
|
try { |
168 |
|
|
a.get(); |
169 |
|
|
shouldThrow(); |
170 |
|
|
} catch (ExecutionException success) { |
171 |
dl |
1.26 |
assertSame(t.getClass(), success.getCause().getClass()); |
172 |
jsr166 |
1.20 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
173 |
|
|
|
174 |
|
|
try { |
175 |
|
|
a.get(5L, SECONDS); |
176 |
|
|
shouldThrow(); |
177 |
|
|
} catch (ExecutionException success) { |
178 |
dl |
1.26 |
assertSame(t.getClass(), success.getCause().getClass()); |
179 |
jsr166 |
1.20 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
180 |
|
|
} |
181 |
|
|
|
182 |
dl |
1.26 |
public static final class FJException extends RuntimeException { |
183 |
|
|
public FJException() { super(); } |
184 |
dl |
1.1 |
} |
185 |
|
|
|
186 |
jsr166 |
1.35 |
/** An invalid return value for Fib. */ |
187 |
dl |
1.1 |
static final Integer NoResult = Integer.valueOf(-17); |
188 |
|
|
|
189 |
jsr166 |
1.35 |
/** A simple recursive task for testing. */ |
190 |
jsr166 |
1.19 |
final class FibTask extends CheckedRecursiveTask<Integer> { |
191 |
dl |
1.1 |
final int number; |
192 |
|
|
FibTask(int n) { number = n; } |
193 |
jsr166 |
1.19 |
public Integer realCompute() { |
194 |
dl |
1.1 |
int n = number; |
195 |
|
|
if (n <= 1) |
196 |
|
|
return n; |
197 |
|
|
FibTask f1 = new FibTask(n - 1); |
198 |
|
|
f1.fork(); |
199 |
|
|
return (new FibTask(n - 2)).compute() + f1.join(); |
200 |
|
|
} |
201 |
jsr166 |
1.20 |
|
202 |
|
|
public void publicSetRawResult(Integer result) { |
203 |
|
|
setRawResult(result); |
204 |
|
|
} |
205 |
dl |
1.1 |
} |
206 |
|
|
|
207 |
jsr166 |
1.35 |
/** A recursive action failing in base case. */ |
208 |
jsr166 |
1.19 |
final class FailingFibTask extends RecursiveTask<Integer> { |
209 |
dl |
1.1 |
final int number; |
210 |
|
|
int result; |
211 |
|
|
FailingFibTask(int n) { number = n; } |
212 |
|
|
public Integer compute() { |
213 |
|
|
int n = number; |
214 |
|
|
if (n <= 1) |
215 |
|
|
throw new FJException(); |
216 |
|
|
FailingFibTask f1 = new FailingFibTask(n - 1); |
217 |
|
|
f1.fork(); |
218 |
|
|
return (new FibTask(n - 2)).compute() + f1.join(); |
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
jsr166 |
1.2 |
/** |
223 |
dl |
1.1 |
* invoke returns value when task completes normally. |
224 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
225 |
|
|
* completed tasks. getRawResult of a completed non-null task |
226 |
|
|
* returns value; |
227 |
|
|
*/ |
228 |
|
|
public void testInvoke() { |
229 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
230 |
|
|
public Integer realCompute() { |
231 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
232 |
|
|
Integer r = f.invoke(); |
233 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
234 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
235 |
jsr166 |
1.7 |
return r; |
236 |
jsr166 |
1.19 |
}}; |
237 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
238 |
dl |
1.1 |
} |
239 |
|
|
|
240 |
jsr166 |
1.2 |
/** |
241 |
dl |
1.1 |
* quietlyInvoke task returns when task completes normally. |
242 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
243 |
|
|
* completed tasks |
244 |
|
|
*/ |
245 |
|
|
public void testQuietlyInvoke() { |
246 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
247 |
|
|
public Integer realCompute() { |
248 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
249 |
|
|
f.quietlyInvoke(); |
250 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
251 |
|
|
return NoResult; |
252 |
jsr166 |
1.19 |
}}; |
253 |
jsr166 |
1.20 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
254 |
dl |
1.1 |
} |
255 |
|
|
|
256 |
jsr166 |
1.2 |
/** |
257 |
dl |
1.1 |
* join of a forked task returns when task completes |
258 |
|
|
*/ |
259 |
|
|
public void testForkJoin() { |
260 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
261 |
|
|
public Integer realCompute() { |
262 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
263 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
264 |
jsr166 |
1.7 |
Integer r = f.join(); |
265 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
266 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
267 |
jsr166 |
1.7 |
return r; |
268 |
jsr166 |
1.19 |
}}; |
269 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
270 |
dl |
1.1 |
} |
271 |
|
|
|
272 |
jsr166 |
1.2 |
/** |
273 |
dl |
1.1 |
* get of a forked task returns when task completes |
274 |
|
|
*/ |
275 |
|
|
public void testForkGet() { |
276 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
277 |
|
|
public Integer realCompute() throws Exception { |
278 |
|
|
FibTask f = new FibTask(8); |
279 |
|
|
assertSame(f, f.fork()); |
280 |
|
|
Integer r = f.get(); |
281 |
|
|
assertEquals(21, (int) r); |
282 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
283 |
jsr166 |
1.19 |
return r; |
284 |
|
|
}}; |
285 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
286 |
dl |
1.1 |
} |
287 |
|
|
|
288 |
jsr166 |
1.2 |
/** |
289 |
dl |
1.1 |
* timed get of a forked task returns when task completes |
290 |
|
|
*/ |
291 |
|
|
public void testForkTimedGet() { |
292 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
293 |
|
|
public Integer realCompute() throws Exception { |
294 |
|
|
FibTask f = new FibTask(8); |
295 |
|
|
assertSame(f, f.fork()); |
296 |
jsr166 |
1.20 |
Integer r = f.get(5L, SECONDS); |
297 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
298 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
299 |
jsr166 |
1.19 |
return r; |
300 |
|
|
}}; |
301 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
302 |
dl |
1.1 |
} |
303 |
|
|
|
304 |
jsr166 |
1.2 |
/** |
305 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes |
306 |
|
|
*/ |
307 |
|
|
public void testForkQuietlyJoin() { |
308 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
309 |
|
|
public Integer realCompute() { |
310 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
311 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
312 |
jsr166 |
1.7 |
f.quietlyJoin(); |
313 |
|
|
Integer r = f.getRawResult(); |
314 |
jsr166 |
1.19 |
assertEquals(21, (int) r); |
315 |
jsr166 |
1.20 |
checkCompletedNormally(f, r); |
316 |
jsr166 |
1.7 |
return r; |
317 |
jsr166 |
1.19 |
}}; |
318 |
jsr166 |
1.13 |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
319 |
dl |
1.1 |
} |
320 |
|
|
|
321 |
jsr166 |
1.2 |
/** |
322 |
dl |
1.1 |
* helpQuiesce returns when tasks are complete. |
323 |
|
|
* getQueuedTaskCount returns 0 when quiescent |
324 |
|
|
*/ |
325 |
|
|
public void testForkHelpQuiesce() { |
326 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
327 |
|
|
public Integer realCompute() { |
328 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
329 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
330 |
jsr166 |
1.28 |
helpQuiesce(); |
331 |
jsr166 |
1.34 |
while (!f.isDone()) // wait out race |
332 |
|
|
; |
333 |
jsr166 |
1.19 |
assertEquals(0, getQueuedTaskCount()); |
334 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
335 |
|
|
return NoResult; |
336 |
jsr166 |
1.19 |
}}; |
337 |
jsr166 |
1.20 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
338 |
dl |
1.1 |
} |
339 |
|
|
|
340 |
jsr166 |
1.2 |
/** |
341 |
dl |
1.1 |
* invoke task throws exception when task completes abnormally |
342 |
|
|
*/ |
343 |
|
|
public void testAbnormalInvoke() { |
344 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
345 |
|
|
public Integer realCompute() { |
346 |
|
|
FailingFibTask f = new FailingFibTask(8); |
347 |
jsr166 |
1.7 |
try { |
348 |
|
|
f.invoke(); |
349 |
|
|
shouldThrow(); |
350 |
jsr166 |
1.20 |
} catch (FJException success) { |
351 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, success); |
352 |
jsr166 |
1.20 |
} |
353 |
jsr166 |
1.7 |
return NoResult; |
354 |
jsr166 |
1.19 |
}}; |
355 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
356 |
dl |
1.1 |
} |
357 |
|
|
|
358 |
jsr166 |
1.2 |
/** |
359 |
jsr166 |
1.4 |
* quietlyInvoke task returns when task completes abnormally |
360 |
dl |
1.1 |
*/ |
361 |
|
|
public void testAbnormalQuietlyInvoke() { |
362 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
363 |
|
|
public Integer realCompute() { |
364 |
jsr166 |
1.7 |
FailingFibTask f = new FailingFibTask(8); |
365 |
|
|
f.quietlyInvoke(); |
366 |
jsr166 |
1.20 |
assertTrue(f.getException() instanceof FJException); |
367 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, f.getException()); |
368 |
jsr166 |
1.7 |
return NoResult; |
369 |
jsr166 |
1.19 |
}}; |
370 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
371 |
dl |
1.1 |
} |
372 |
|
|
|
373 |
jsr166 |
1.2 |
/** |
374 |
dl |
1.1 |
* join of a forked task throws exception when task completes abnormally |
375 |
|
|
*/ |
376 |
|
|
public void testAbnormalForkJoin() { |
377 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
378 |
|
|
public Integer realCompute() { |
379 |
|
|
FailingFibTask f = new FailingFibTask(8); |
380 |
|
|
assertSame(f, f.fork()); |
381 |
jsr166 |
1.7 |
try { |
382 |
|
|
Integer r = f.join(); |
383 |
|
|
shouldThrow(); |
384 |
jsr166 |
1.20 |
} catch (FJException success) { |
385 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, success); |
386 |
jsr166 |
1.20 |
} |
387 |
jsr166 |
1.7 |
return NoResult; |
388 |
jsr166 |
1.19 |
}}; |
389 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
390 |
dl |
1.1 |
} |
391 |
|
|
|
392 |
jsr166 |
1.2 |
/** |
393 |
dl |
1.1 |
* get of a forked task throws exception when task completes abnormally |
394 |
|
|
*/ |
395 |
|
|
public void testAbnormalForkGet() { |
396 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
397 |
|
|
public Integer realCompute() throws Exception { |
398 |
|
|
FailingFibTask f = new FailingFibTask(8); |
399 |
|
|
assertSame(f, f.fork()); |
400 |
jsr166 |
1.7 |
try { |
401 |
|
|
Integer r = f.get(); |
402 |
|
|
shouldThrow(); |
403 |
jsr166 |
1.20 |
} catch (ExecutionException success) { |
404 |
jsr166 |
1.22 |
Throwable cause = success.getCause(); |
405 |
|
|
assertTrue(cause instanceof FJException); |
406 |
|
|
checkCompletedAbnormally(f, cause); |
407 |
jsr166 |
1.20 |
} |
408 |
jsr166 |
1.7 |
return NoResult; |
409 |
jsr166 |
1.19 |
}}; |
410 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
411 |
dl |
1.1 |
} |
412 |
|
|
|
413 |
jsr166 |
1.2 |
/** |
414 |
dl |
1.1 |
* timed get of a forked task throws exception when task completes abnormally |
415 |
|
|
*/ |
416 |
|
|
public void testAbnormalForkTimedGet() { |
417 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
418 |
|
|
public Integer realCompute() throws Exception { |
419 |
|
|
FailingFibTask f = new FailingFibTask(8); |
420 |
|
|
assertSame(f, f.fork()); |
421 |
jsr166 |
1.7 |
try { |
422 |
jsr166 |
1.20 |
Integer r = f.get(5L, SECONDS); |
423 |
jsr166 |
1.7 |
shouldThrow(); |
424 |
jsr166 |
1.20 |
} catch (ExecutionException success) { |
425 |
jsr166 |
1.22 |
Throwable cause = success.getCause(); |
426 |
|
|
assertTrue(cause instanceof FJException); |
427 |
|
|
checkCompletedAbnormally(f, cause); |
428 |
jsr166 |
1.20 |
} |
429 |
jsr166 |
1.7 |
return NoResult; |
430 |
jsr166 |
1.19 |
}}; |
431 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
432 |
dl |
1.1 |
} |
433 |
|
|
|
434 |
jsr166 |
1.2 |
/** |
435 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes abnormally |
436 |
|
|
*/ |
437 |
|
|
public void testAbnormalForkQuietlyJoin() { |
438 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
439 |
|
|
public Integer realCompute() { |
440 |
jsr166 |
1.7 |
FailingFibTask f = new FailingFibTask(8); |
441 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
442 |
jsr166 |
1.7 |
f.quietlyJoin(); |
443 |
jsr166 |
1.19 |
assertTrue(f.getException() instanceof FJException); |
444 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, f.getException()); |
445 |
jsr166 |
1.7 |
return NoResult; |
446 |
jsr166 |
1.19 |
}}; |
447 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
448 |
dl |
1.1 |
} |
449 |
|
|
|
450 |
jsr166 |
1.2 |
/** |
451 |
dl |
1.1 |
* invoke task throws exception when task cancelled |
452 |
|
|
*/ |
453 |
|
|
public void testCancelledInvoke() { |
454 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
455 |
|
|
public Integer realCompute() { |
456 |
|
|
FibTask f = new FibTask(8); |
457 |
|
|
assertTrue(f.cancel(true)); |
458 |
jsr166 |
1.7 |
try { |
459 |
|
|
Integer r = f.invoke(); |
460 |
|
|
shouldThrow(); |
461 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
462 |
|
|
checkCancelled(f); |
463 |
|
|
} |
464 |
jsr166 |
1.7 |
return NoResult; |
465 |
jsr166 |
1.19 |
}}; |
466 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
467 |
dl |
1.1 |
} |
468 |
|
|
|
469 |
jsr166 |
1.2 |
/** |
470 |
dl |
1.1 |
* join of a forked task throws exception when task cancelled |
471 |
|
|
*/ |
472 |
|
|
public void testCancelledForkJoin() { |
473 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
474 |
|
|
public Integer realCompute() { |
475 |
|
|
FibTask f = new FibTask(8); |
476 |
|
|
assertTrue(f.cancel(true)); |
477 |
|
|
assertSame(f, f.fork()); |
478 |
jsr166 |
1.7 |
try { |
479 |
|
|
Integer r = f.join(); |
480 |
|
|
shouldThrow(); |
481 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
482 |
|
|
checkCancelled(f); |
483 |
|
|
} |
484 |
jsr166 |
1.7 |
return NoResult; |
485 |
jsr166 |
1.19 |
}}; |
486 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
487 |
dl |
1.1 |
} |
488 |
|
|
|
489 |
jsr166 |
1.2 |
/** |
490 |
dl |
1.1 |
* get of a forked task throws exception when task cancelled |
491 |
|
|
*/ |
492 |
|
|
public void testCancelledForkGet() { |
493 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
494 |
|
|
public Integer realCompute() throws Exception { |
495 |
|
|
FibTask f = new FibTask(8); |
496 |
|
|
assertTrue(f.cancel(true)); |
497 |
|
|
assertSame(f, f.fork()); |
498 |
jsr166 |
1.7 |
try { |
499 |
|
|
Integer r = f.get(); |
500 |
|
|
shouldThrow(); |
501 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
502 |
|
|
checkCancelled(f); |
503 |
|
|
} |
504 |
jsr166 |
1.7 |
return NoResult; |
505 |
jsr166 |
1.19 |
}}; |
506 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
507 |
dl |
1.1 |
} |
508 |
|
|
|
509 |
jsr166 |
1.2 |
/** |
510 |
dl |
1.1 |
* timed get of a forked task throws exception when task cancelled |
511 |
|
|
*/ |
512 |
|
|
public void testCancelledForkTimedGet() { |
513 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
514 |
|
|
public Integer realCompute() throws Exception { |
515 |
|
|
FibTask f = new FibTask(8); |
516 |
|
|
assertTrue(f.cancel(true)); |
517 |
|
|
assertSame(f, f.fork()); |
518 |
jsr166 |
1.7 |
try { |
519 |
jsr166 |
1.20 |
Integer r = f.get(5L, SECONDS); |
520 |
jsr166 |
1.7 |
shouldThrow(); |
521 |
jsr166 |
1.20 |
} catch (CancellationException success) { |
522 |
|
|
checkCancelled(f); |
523 |
|
|
} |
524 |
jsr166 |
1.7 |
return NoResult; |
525 |
jsr166 |
1.19 |
}}; |
526 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
527 |
dl |
1.1 |
} |
528 |
|
|
|
529 |
jsr166 |
1.2 |
/** |
530 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task cancelled |
531 |
|
|
*/ |
532 |
|
|
public void testCancelledForkQuietlyJoin() { |
533 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
534 |
|
|
public Integer realCompute() { |
535 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
536 |
jsr166 |
1.19 |
assertTrue(f.cancel(true)); |
537 |
|
|
assertSame(f, f.fork()); |
538 |
jsr166 |
1.7 |
f.quietlyJoin(); |
539 |
jsr166 |
1.20 |
checkCancelled(f); |
540 |
jsr166 |
1.7 |
return NoResult; |
541 |
jsr166 |
1.19 |
}}; |
542 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
543 |
dl |
1.1 |
} |
544 |
|
|
|
545 |
|
|
/** |
546 |
|
|
* getPool of executing task returns its pool |
547 |
|
|
*/ |
548 |
|
|
public void testGetPool() { |
549 |
jsr166 |
1.13 |
final ForkJoinPool mainPool = mainPool(); |
550 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
551 |
|
|
public Integer realCompute() { |
552 |
|
|
assertSame(mainPool, getPool()); |
553 |
jsr166 |
1.7 |
return NoResult; |
554 |
jsr166 |
1.19 |
}}; |
555 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool, a)); |
556 |
dl |
1.1 |
} |
557 |
|
|
|
558 |
|
|
/** |
559 |
|
|
* getPool of non-FJ task returns null |
560 |
|
|
*/ |
561 |
|
|
public void testGetPool2() { |
562 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
563 |
|
|
public Integer realCompute() { |
564 |
|
|
assertNull(getPool()); |
565 |
dl |
1.1 |
return NoResult; |
566 |
jsr166 |
1.19 |
}}; |
567 |
jsr166 |
1.16 |
assertSame(NoResult, a.invoke()); |
568 |
dl |
1.1 |
} |
569 |
jsr166 |
1.2 |
|
570 |
dl |
1.1 |
/** |
571 |
|
|
* inForkJoinPool of executing task returns true |
572 |
|
|
*/ |
573 |
|
|
public void testInForkJoinPool() { |
574 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
575 |
|
|
public Integer realCompute() { |
576 |
|
|
assertTrue(inForkJoinPool()); |
577 |
jsr166 |
1.7 |
return NoResult; |
578 |
jsr166 |
1.19 |
}}; |
579 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
580 |
dl |
1.1 |
} |
581 |
|
|
|
582 |
|
|
/** |
583 |
|
|
* inForkJoinPool of non-FJ task returns false |
584 |
|
|
*/ |
585 |
|
|
public void testInForkJoinPool2() { |
586 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
587 |
|
|
public Integer realCompute() { |
588 |
jsr166 |
1.22 |
assertFalse(inForkJoinPool()); |
589 |
jsr166 |
1.7 |
return NoResult; |
590 |
jsr166 |
1.19 |
}}; |
591 |
jsr166 |
1.16 |
assertSame(NoResult, a.invoke()); |
592 |
dl |
1.1 |
} |
593 |
|
|
|
594 |
|
|
/** |
595 |
dl |
1.17 |
* The value set by setRawResult is returned by getRawResult |
596 |
dl |
1.1 |
*/ |
597 |
|
|
public void testSetRawResult() { |
598 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
599 |
|
|
public Integer realCompute() { |
600 |
jsr166 |
1.7 |
setRawResult(NoResult); |
601 |
jsr166 |
1.19 |
assertSame(NoResult, getRawResult()); |
602 |
jsr166 |
1.7 |
return NoResult; |
603 |
|
|
} |
604 |
|
|
}; |
605 |
jsr166 |
1.24 |
assertSame(NoResult, a.invoke()); |
606 |
dl |
1.1 |
} |
607 |
|
|
|
608 |
jsr166 |
1.2 |
/** |
609 |
jsr166 |
1.20 |
* A reinitialized normally completed task may be re-invoked |
610 |
dl |
1.1 |
*/ |
611 |
|
|
public void testReinitialize() { |
612 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
613 |
|
|
public Integer realCompute() { |
614 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
615 |
jsr166 |
1.20 |
checkNotDone(f); |
616 |
|
|
|
617 |
|
|
for (int i = 0; i < 3; i++) { |
618 |
|
|
Integer r = f.invoke(); |
619 |
|
|
assertEquals(21, (int) r); |
620 |
|
|
checkCompletedNormally(f, r); |
621 |
|
|
f.reinitialize(); |
622 |
|
|
f.publicSetRawResult(null); |
623 |
|
|
checkNotDone(f); |
624 |
|
|
} |
625 |
|
|
return NoResult; |
626 |
|
|
}}; |
627 |
|
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
628 |
|
|
} |
629 |
|
|
|
630 |
|
|
/** |
631 |
|
|
* A reinitialized abnormally completed task may be re-invoked |
632 |
|
|
*/ |
633 |
|
|
public void testReinitializeAbnormal() { |
634 |
|
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
635 |
|
|
public Integer realCompute() { |
636 |
|
|
FailingFibTask f = new FailingFibTask(8); |
637 |
|
|
checkNotDone(f); |
638 |
|
|
|
639 |
|
|
for (int i = 0; i < 3; i++) { |
640 |
|
|
try { |
641 |
|
|
f.invoke(); |
642 |
|
|
shouldThrow(); |
643 |
|
|
} catch (FJException success) { |
644 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, success); |
645 |
jsr166 |
1.20 |
} |
646 |
|
|
f.reinitialize(); |
647 |
|
|
checkNotDone(f); |
648 |
|
|
} |
649 |
jsr166 |
1.7 |
return NoResult; |
650 |
jsr166 |
1.19 |
}}; |
651 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
652 |
dl |
1.1 |
} |
653 |
|
|
|
654 |
jsr166 |
1.2 |
/** |
655 |
dl |
1.1 |
* invoke task throws exception after invoking completeExceptionally |
656 |
|
|
*/ |
657 |
|
|
public void testCompleteExceptionally() { |
658 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
659 |
|
|
public Integer realCompute() { |
660 |
|
|
FibTask f = new FibTask(8); |
661 |
|
|
f.completeExceptionally(new FJException()); |
662 |
jsr166 |
1.7 |
try { |
663 |
|
|
Integer r = f.invoke(); |
664 |
|
|
shouldThrow(); |
665 |
jsr166 |
1.20 |
} catch (FJException success) { |
666 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, success); |
667 |
jsr166 |
1.20 |
} |
668 |
jsr166 |
1.7 |
return NoResult; |
669 |
jsr166 |
1.19 |
}}; |
670 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
671 |
dl |
1.1 |
} |
672 |
|
|
|
673 |
jsr166 |
1.2 |
/** |
674 |
dl |
1.1 |
* invoke task suppresses execution invoking complete |
675 |
|
|
*/ |
676 |
|
|
public void testComplete() { |
677 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
678 |
|
|
public Integer realCompute() { |
679 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
680 |
|
|
f.complete(NoResult); |
681 |
|
|
Integer r = f.invoke(); |
682 |
jsr166 |
1.19 |
assertSame(NoResult, r); |
683 |
jsr166 |
1.20 |
checkCompletedNormally(f, NoResult); |
684 |
jsr166 |
1.7 |
return r; |
685 |
jsr166 |
1.19 |
}}; |
686 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
687 |
dl |
1.1 |
} |
688 |
|
|
|
689 |
jsr166 |
1.2 |
/** |
690 |
dl |
1.1 |
* invokeAll(t1, t2) invokes all task arguments |
691 |
|
|
*/ |
692 |
|
|
public void testInvokeAll2() { |
693 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
694 |
|
|
public Integer realCompute() { |
695 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
696 |
|
|
FibTask g = new FibTask(9); |
697 |
|
|
invokeAll(f, g); |
698 |
jsr166 |
1.21 |
checkCompletedNormally(f, 21); |
699 |
|
|
checkCompletedNormally(g, 34); |
700 |
jsr166 |
1.7 |
return NoResult; |
701 |
jsr166 |
1.19 |
}}; |
702 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
703 |
dl |
1.1 |
} |
704 |
|
|
|
705 |
jsr166 |
1.2 |
/** |
706 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument invokes task |
707 |
|
|
*/ |
708 |
|
|
public void testInvokeAll1() { |
709 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
710 |
|
|
public Integer realCompute() { |
711 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
712 |
|
|
invokeAll(f); |
713 |
jsr166 |
1.21 |
checkCompletedNormally(f, 21); |
714 |
jsr166 |
1.7 |
return NoResult; |
715 |
jsr166 |
1.19 |
}}; |
716 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
717 |
dl |
1.1 |
} |
718 |
|
|
|
719 |
jsr166 |
1.2 |
/** |
720 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument invokes tasks |
721 |
|
|
*/ |
722 |
|
|
public void testInvokeAll3() { |
723 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
724 |
|
|
public Integer realCompute() { |
725 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
726 |
|
|
FibTask g = new FibTask(9); |
727 |
|
|
FibTask h = new FibTask(7); |
728 |
|
|
invokeAll(f, g, h); |
729 |
jsr166 |
1.21 |
assertTrue(f.isDone()); |
730 |
|
|
assertTrue(g.isDone()); |
731 |
|
|
assertTrue(h.isDone()); |
732 |
|
|
checkCompletedNormally(f, 21); |
733 |
|
|
checkCompletedNormally(g, 34); |
734 |
|
|
checkCompletedNormally(h, 13); |
735 |
jsr166 |
1.7 |
return NoResult; |
736 |
jsr166 |
1.19 |
}}; |
737 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
738 |
dl |
1.1 |
} |
739 |
|
|
|
740 |
jsr166 |
1.2 |
/** |
741 |
dl |
1.1 |
* invokeAll(collection) invokes all tasks in the collection |
742 |
|
|
*/ |
743 |
|
|
public void testInvokeAllCollection() { |
744 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
745 |
|
|
public Integer realCompute() { |
746 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
747 |
|
|
FibTask g = new FibTask(9); |
748 |
|
|
FibTask h = new FibTask(7); |
749 |
|
|
HashSet set = new HashSet(); |
750 |
|
|
set.add(f); |
751 |
|
|
set.add(g); |
752 |
|
|
set.add(h); |
753 |
|
|
invokeAll(set); |
754 |
jsr166 |
1.21 |
assertTrue(f.isDone()); |
755 |
|
|
assertTrue(g.isDone()); |
756 |
|
|
assertTrue(h.isDone()); |
757 |
|
|
checkCompletedNormally(f, 21); |
758 |
|
|
checkCompletedNormally(g, 34); |
759 |
|
|
checkCompletedNormally(h, 13); |
760 |
jsr166 |
1.7 |
return NoResult; |
761 |
jsr166 |
1.19 |
}}; |
762 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
763 |
dl |
1.1 |
} |
764 |
|
|
|
765 |
jsr166 |
1.2 |
/** |
766 |
jsr166 |
1.20 |
* invokeAll(tasks) with any null task throws NPE |
767 |
|
|
*/ |
768 |
|
|
public void testInvokeAllNPE() { |
769 |
|
|
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
770 |
|
|
public Integer realCompute() { |
771 |
|
|
FibTask f = new FibTask(8); |
772 |
|
|
FibTask g = new FibTask(9); |
773 |
|
|
FibTask h = null; |
774 |
|
|
try { |
775 |
|
|
invokeAll(f, g, h); |
776 |
|
|
shouldThrow(); |
777 |
|
|
} catch (NullPointerException success) {} |
778 |
|
|
return NoResult; |
779 |
|
|
}}; |
780 |
|
|
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
781 |
|
|
} |
782 |
|
|
|
783 |
|
|
/** |
784 |
dl |
1.1 |
* invokeAll(t1, t2) throw exception if any task does |
785 |
|
|
*/ |
786 |
|
|
public void testAbnormalInvokeAll2() { |
787 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
788 |
|
|
public Integer realCompute() { |
789 |
|
|
FibTask f = new FibTask(8); |
790 |
|
|
FailingFibTask g = new FailingFibTask(9); |
791 |
jsr166 |
1.7 |
try { |
792 |
|
|
invokeAll(f, g); |
793 |
|
|
shouldThrow(); |
794 |
jsr166 |
1.20 |
} catch (FJException success) { |
795 |
jsr166 |
1.22 |
checkCompletedAbnormally(g, success); |
796 |
jsr166 |
1.20 |
} |
797 |
jsr166 |
1.7 |
return NoResult; |
798 |
jsr166 |
1.19 |
}}; |
799 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
800 |
dl |
1.1 |
} |
801 |
|
|
|
802 |
jsr166 |
1.2 |
/** |
803 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument throws exception if task does |
804 |
|
|
*/ |
805 |
|
|
public void testAbnormalInvokeAll1() { |
806 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
807 |
|
|
public Integer realCompute() { |
808 |
|
|
FailingFibTask g = new FailingFibTask(9); |
809 |
jsr166 |
1.7 |
try { |
810 |
|
|
invokeAll(g); |
811 |
|
|
shouldThrow(); |
812 |
jsr166 |
1.20 |
} catch (FJException success) { |
813 |
jsr166 |
1.22 |
checkCompletedAbnormally(g, success); |
814 |
jsr166 |
1.20 |
} |
815 |
jsr166 |
1.7 |
return NoResult; |
816 |
jsr166 |
1.19 |
}}; |
817 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
818 |
dl |
1.1 |
} |
819 |
|
|
|
820 |
jsr166 |
1.2 |
/** |
821 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
822 |
|
|
*/ |
823 |
|
|
public void testAbnormalInvokeAll3() { |
824 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
825 |
|
|
public Integer realCompute() { |
826 |
|
|
FibTask f = new FibTask(8); |
827 |
|
|
FailingFibTask g = new FailingFibTask(9); |
828 |
|
|
FibTask h = new FibTask(7); |
829 |
jsr166 |
1.7 |
try { |
830 |
|
|
invokeAll(f, g, h); |
831 |
|
|
shouldThrow(); |
832 |
jsr166 |
1.20 |
} catch (FJException success) { |
833 |
jsr166 |
1.22 |
checkCompletedAbnormally(g, success); |
834 |
jsr166 |
1.20 |
} |
835 |
jsr166 |
1.7 |
return NoResult; |
836 |
jsr166 |
1.19 |
}}; |
837 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
838 |
dl |
1.1 |
} |
839 |
|
|
|
840 |
jsr166 |
1.2 |
/** |
841 |
jsr166 |
1.3 |
* invokeAll(collection) throws exception if any task does |
842 |
dl |
1.1 |
*/ |
843 |
|
|
public void testAbnormalInvokeAllCollection() { |
844 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
845 |
|
|
public Integer realCompute() { |
846 |
|
|
FailingFibTask f = new FailingFibTask(8); |
847 |
|
|
FibTask g = new FibTask(9); |
848 |
|
|
FibTask h = new FibTask(7); |
849 |
|
|
HashSet set = new HashSet(); |
850 |
|
|
set.add(f); |
851 |
|
|
set.add(g); |
852 |
|
|
set.add(h); |
853 |
jsr166 |
1.7 |
try { |
854 |
|
|
invokeAll(set); |
855 |
|
|
shouldThrow(); |
856 |
jsr166 |
1.20 |
} catch (FJException success) { |
857 |
jsr166 |
1.22 |
checkCompletedAbnormally(f, success); |
858 |
jsr166 |
1.20 |
} |
859 |
jsr166 |
1.7 |
return NoResult; |
860 |
jsr166 |
1.19 |
}}; |
861 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
862 |
dl |
1.1 |
} |
863 |
|
|
|
864 |
jsr166 |
1.2 |
/** |
865 |
dl |
1.1 |
* tryUnfork returns true for most recent unexecuted task, |
866 |
|
|
* and suppresses execution |
867 |
|
|
*/ |
868 |
|
|
public void testTryUnfork() { |
869 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
870 |
|
|
public Integer realCompute() { |
871 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
872 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
873 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
874 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
875 |
|
|
assertTrue(f.tryUnfork()); |
876 |
jsr166 |
1.7 |
helpQuiesce(); |
877 |
jsr166 |
1.20 |
checkNotDone(f); |
878 |
|
|
checkCompletedNormally(g, 34); |
879 |
jsr166 |
1.7 |
return NoResult; |
880 |
jsr166 |
1.19 |
}}; |
881 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
882 |
dl |
1.1 |
} |
883 |
|
|
|
884 |
jsr166 |
1.2 |
/** |
885 |
dl |
1.1 |
* getSurplusQueuedTaskCount returns > 0 when |
886 |
|
|
* there are more tasks than threads |
887 |
|
|
*/ |
888 |
|
|
public void testGetSurplusQueuedTaskCount() { |
889 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
890 |
|
|
public Integer realCompute() { |
891 |
jsr166 |
1.7 |
FibTask h = new FibTask(7); |
892 |
jsr166 |
1.19 |
assertSame(h, h.fork()); |
893 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
894 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
895 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
896 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
897 |
|
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
898 |
jsr166 |
1.7 |
helpQuiesce(); |
899 |
jsr166 |
1.22 |
assertEquals(0, getSurplusQueuedTaskCount()); |
900 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
901 |
|
|
checkCompletedNormally(g, 34); |
902 |
|
|
checkCompletedNormally(h, 13); |
903 |
jsr166 |
1.7 |
return NoResult; |
904 |
jsr166 |
1.19 |
}}; |
905 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
906 |
dl |
1.1 |
} |
907 |
|
|
|
908 |
jsr166 |
1.2 |
/** |
909 |
dl |
1.1 |
* peekNextLocalTask returns most recent unexecuted task. |
910 |
|
|
*/ |
911 |
|
|
public void testPeekNextLocalTask() { |
912 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
913 |
|
|
public Integer realCompute() { |
914 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
915 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
916 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
917 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
918 |
|
|
assertSame(f, peekNextLocalTask()); |
919 |
jsr166 |
1.20 |
checkCompletesNormally(f, 21); |
920 |
jsr166 |
1.7 |
helpQuiesce(); |
921 |
jsr166 |
1.20 |
checkCompletedNormally(g, 34); |
922 |
jsr166 |
1.7 |
return NoResult; |
923 |
jsr166 |
1.19 |
}}; |
924 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
925 |
dl |
1.1 |
} |
926 |
|
|
|
927 |
jsr166 |
1.2 |
/** |
928 |
dl |
1.1 |
* pollNextLocalTask returns most recent unexecuted task |
929 |
|
|
* without executing it |
930 |
|
|
*/ |
931 |
|
|
public void testPollNextLocalTask() { |
932 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
933 |
|
|
public Integer realCompute() { |
934 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
935 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
936 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
937 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
938 |
|
|
assertSame(f, pollNextLocalTask()); |
939 |
jsr166 |
1.7 |
helpQuiesce(); |
940 |
jsr166 |
1.20 |
checkNotDone(f); |
941 |
|
|
checkCompletedNormally(g, 34); |
942 |
jsr166 |
1.7 |
return NoResult; |
943 |
jsr166 |
1.19 |
}}; |
944 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
945 |
dl |
1.1 |
} |
946 |
|
|
|
947 |
jsr166 |
1.2 |
/** |
948 |
jsr166 |
1.13 |
* pollTask returns an unexecuted task without executing it |
949 |
dl |
1.1 |
*/ |
950 |
|
|
public void testPollTask() { |
951 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
952 |
|
|
public Integer realCompute() { |
953 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
954 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
955 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
956 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
957 |
|
|
assertSame(f, pollTask()); |
958 |
jsr166 |
1.7 |
helpQuiesce(); |
959 |
jsr166 |
1.20 |
checkNotDone(f); |
960 |
|
|
checkCompletedNormally(g, 34); |
961 |
jsr166 |
1.7 |
return NoResult; |
962 |
jsr166 |
1.19 |
}}; |
963 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
964 |
dl |
1.1 |
} |
965 |
|
|
|
966 |
jsr166 |
1.2 |
/** |
967 |
dl |
1.1 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
968 |
|
|
*/ |
969 |
|
|
public void testPeekNextLocalTaskAsync() { |
970 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
971 |
|
|
public Integer realCompute() { |
972 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
973 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
974 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
975 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
976 |
|
|
assertSame(g, peekNextLocalTask()); |
977 |
|
|
assertEquals(21, (int) f.join()); |
978 |
jsr166 |
1.7 |
helpQuiesce(); |
979 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
980 |
|
|
checkCompletedNormally(g, 34); |
981 |
jsr166 |
1.7 |
return NoResult; |
982 |
jsr166 |
1.19 |
}}; |
983 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
984 |
dl |
1.1 |
} |
985 |
|
|
|
986 |
jsr166 |
1.2 |
/** |
987 |
jsr166 |
1.20 |
* pollNextLocalTask returns least recent unexecuted task without |
988 |
|
|
* executing it, in async mode |
989 |
dl |
1.1 |
*/ |
990 |
|
|
public void testPollNextLocalTaskAsync() { |
991 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
992 |
|
|
public Integer realCompute() { |
993 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
994 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
995 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
996 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
997 |
|
|
assertSame(g, pollNextLocalTask()); |
998 |
jsr166 |
1.7 |
helpQuiesce(); |
999 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
1000 |
|
|
checkNotDone(g); |
1001 |
jsr166 |
1.7 |
return NoResult; |
1002 |
jsr166 |
1.19 |
}}; |
1003 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1004 |
dl |
1.1 |
} |
1005 |
|
|
|
1006 |
jsr166 |
1.2 |
/** |
1007 |
jsr166 |
1.20 |
* pollTask returns an unexecuted task without executing it, in |
1008 |
|
|
* async mode |
1009 |
dl |
1.1 |
*/ |
1010 |
|
|
public void testPollTaskAsync() { |
1011 |
jsr166 |
1.19 |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1012 |
|
|
public Integer realCompute() { |
1013 |
jsr166 |
1.7 |
FibTask g = new FibTask(9); |
1014 |
jsr166 |
1.19 |
assertSame(g, g.fork()); |
1015 |
jsr166 |
1.7 |
FibTask f = new FibTask(8); |
1016 |
jsr166 |
1.19 |
assertSame(f, f.fork()); |
1017 |
|
|
assertSame(g, pollTask()); |
1018 |
jsr166 |
1.7 |
helpQuiesce(); |
1019 |
jsr166 |
1.20 |
checkCompletedNormally(f, 21); |
1020 |
|
|
checkNotDone(g); |
1021 |
jsr166 |
1.7 |
return NoResult; |
1022 |
jsr166 |
1.19 |
}}; |
1023 |
jsr166 |
1.13 |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1024 |
dl |
1.1 |
} |
1025 |
|
|
|
1026 |
|
|
} |