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