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