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