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