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