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