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