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 |
jsr166 |
1.31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
jsr166 |
1.40 |
|
7 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
|
|
|
9 |
jsr166 |
1.45 |
import java.util.Arrays; |
10 |
jsr166 |
1.39 |
import java.util.HashSet; |
11 |
|
|
import java.util.concurrent.CancellationException; |
12 |
dl |
1.8 |
import java.util.concurrent.ExecutionException; |
13 |
|
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
|
import java.util.concurrent.ForkJoinTask; |
15 |
|
|
import java.util.concurrent.RecursiveAction; |
16 |
jsr166 |
1.24 |
import java.util.concurrent.TimeoutException; |
17 |
jsr166 |
1.15 |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
18 |
jsr166 |
1.40 |
|
19 |
|
|
import junit.framework.Test; |
20 |
|
|
import junit.framework.TestSuite; |
21 |
dl |
1.1 |
|
22 |
|
|
public class ForkJoinTaskTest extends JSR166TestCase { |
23 |
|
|
|
24 |
|
|
public static void main(String[] args) { |
25 |
jsr166 |
1.41 |
main(suite(), args); |
26 |
dl |
1.1 |
} |
27 |
jsr166 |
1.12 |
|
28 |
dl |
1.1 |
public static Test suite() { |
29 |
jsr166 |
1.5 |
return new TestSuite(ForkJoinTaskTest.class); |
30 |
dl |
1.1 |
} |
31 |
|
|
|
32 |
dl |
1.20 |
// Runs with "mainPool" use > 1 thread. singletonPool tests use 1 |
33 |
jsr166 |
1.21 |
static final int mainPoolSize = |
34 |
dl |
1.20 |
Math.max(2, Runtime.getRuntime().availableProcessors()); |
35 |
|
|
|
36 |
jsr166 |
1.14 |
private static ForkJoinPool mainPool() { |
37 |
dl |
1.20 |
return new ForkJoinPool(mainPoolSize); |
38 |
jsr166 |
1.14 |
} |
39 |
|
|
|
40 |
|
|
private static ForkJoinPool singletonPool() { |
41 |
|
|
return new ForkJoinPool(1); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
private static ForkJoinPool asyncSingletonPool() { |
45 |
|
|
return new ForkJoinPool(1, |
46 |
|
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
47 |
|
|
null, true); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
51 |
jsr166 |
1.42 |
try (PoolCleaner cleaner = cleaner(pool)) { |
52 |
jsr166 |
1.17 |
assertFalse(a.isDone()); |
53 |
|
|
assertFalse(a.isCompletedNormally()); |
54 |
|
|
assertFalse(a.isCompletedAbnormally()); |
55 |
|
|
assertFalse(a.isCancelled()); |
56 |
|
|
assertNull(a.getException()); |
57 |
jsr166 |
1.24 |
assertNull(a.getRawResult()); |
58 |
jsr166 |
1.17 |
|
59 |
|
|
assertNull(pool.invoke(a)); |
60 |
|
|
|
61 |
|
|
assertTrue(a.isDone()); |
62 |
|
|
assertTrue(a.isCompletedNormally()); |
63 |
|
|
assertFalse(a.isCompletedAbnormally()); |
64 |
|
|
assertFalse(a.isCancelled()); |
65 |
|
|
assertNull(a.getException()); |
66 |
jsr166 |
1.24 |
assertNull(a.getRawResult()); |
67 |
jsr166 |
1.14 |
} |
68 |
|
|
} |
69 |
|
|
|
70 |
jsr166 |
1.24 |
void checkNotDone(ForkJoinTask a) { |
71 |
|
|
assertFalse(a.isDone()); |
72 |
|
|
assertFalse(a.isCompletedNormally()); |
73 |
|
|
assertFalse(a.isCompletedAbnormally()); |
74 |
|
|
assertFalse(a.isCancelled()); |
75 |
|
|
assertNull(a.getException()); |
76 |
|
|
assertNull(a.getRawResult()); |
77 |
|
|
|
78 |
|
|
try { |
79 |
jsr166 |
1.52 |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
80 |
jsr166 |
1.24 |
shouldThrow(); |
81 |
|
|
} catch (TimeoutException success) { |
82 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
<T> void checkCompletedNormally(ForkJoinTask<T> a) { |
86 |
|
|
checkCompletedNormally(a, null); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
<T> void checkCompletedNormally(ForkJoinTask<T> a, T expected) { |
90 |
|
|
assertTrue(a.isDone()); |
91 |
|
|
assertFalse(a.isCancelled()); |
92 |
|
|
assertTrue(a.isCompletedNormally()); |
93 |
|
|
assertFalse(a.isCompletedAbnormally()); |
94 |
|
|
assertNull(a.getException()); |
95 |
|
|
assertSame(expected, a.getRawResult()); |
96 |
jsr166 |
1.28 |
|
97 |
|
|
{ |
98 |
|
|
Thread.currentThread().interrupt(); |
99 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
100 |
jsr166 |
1.28 |
assertSame(expected, a.join()); |
101 |
jsr166 |
1.52 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
102 |
jsr166 |
1.29 |
Thread.interrupted(); |
103 |
jsr166 |
1.28 |
} |
104 |
|
|
|
105 |
|
|
{ |
106 |
|
|
Thread.currentThread().interrupt(); |
107 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
108 |
jsr166 |
1.28 |
a.quietlyJoin(); // should be no-op |
109 |
jsr166 |
1.52 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
110 |
jsr166 |
1.29 |
Thread.interrupted(); |
111 |
jsr166 |
1.28 |
} |
112 |
|
|
|
113 |
jsr166 |
1.25 |
assertFalse(a.cancel(false)); |
114 |
|
|
assertFalse(a.cancel(true)); |
115 |
jsr166 |
1.24 |
try { |
116 |
|
|
assertSame(expected, a.get()); |
117 |
jsr166 |
1.52 |
assertSame(expected, a.get(randomTimeout(), randomTimeUnit())); |
118 |
jsr166 |
1.24 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
void checkCancelled(ForkJoinTask a) { |
122 |
|
|
assertTrue(a.isDone()); |
123 |
|
|
assertTrue(a.isCancelled()); |
124 |
|
|
assertFalse(a.isCompletedNormally()); |
125 |
|
|
assertTrue(a.isCompletedAbnormally()); |
126 |
|
|
assertTrue(a.getException() instanceof CancellationException); |
127 |
|
|
assertNull(a.getRawResult()); |
128 |
jsr166 |
1.25 |
assertTrue(a.cancel(false)); |
129 |
|
|
assertTrue(a.cancel(true)); |
130 |
jsr166 |
1.24 |
|
131 |
|
|
try { |
132 |
jsr166 |
1.28 |
Thread.currentThread().interrupt(); |
133 |
jsr166 |
1.24 |
a.join(); |
134 |
|
|
shouldThrow(); |
135 |
|
|
} catch (CancellationException success) { |
136 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
jsr166 |
1.29 |
Thread.interrupted(); |
138 |
jsr166 |
1.24 |
|
139 |
jsr166 |
1.28 |
{ |
140 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
141 |
jsr166 |
1.28 |
a.quietlyJoin(); // should be no-op |
142 |
jsr166 |
1.52 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
143 |
jsr166 |
1.28 |
} |
144 |
|
|
|
145 |
jsr166 |
1.24 |
try { |
146 |
|
|
a.get(); |
147 |
|
|
shouldThrow(); |
148 |
|
|
} catch (CancellationException success) { |
149 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
150 |
|
|
|
151 |
|
|
try { |
152 |
jsr166 |
1.52 |
a.get(randomTimeout(), randomTimeUnit()); |
153 |
jsr166 |
1.24 |
shouldThrow(); |
154 |
|
|
} catch (CancellationException success) { |
155 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
void checkCompletedAbnormally(ForkJoinTask a, Throwable t) { |
159 |
|
|
assertTrue(a.isDone()); |
160 |
|
|
assertFalse(a.isCancelled()); |
161 |
|
|
assertFalse(a.isCompletedNormally()); |
162 |
|
|
assertTrue(a.isCompletedAbnormally()); |
163 |
dl |
1.30 |
assertSame(t.getClass(), a.getException().getClass()); |
164 |
jsr166 |
1.24 |
assertNull(a.getRawResult()); |
165 |
jsr166 |
1.25 |
assertFalse(a.cancel(false)); |
166 |
|
|
assertFalse(a.cancel(true)); |
167 |
jsr166 |
1.24 |
|
168 |
|
|
try { |
169 |
jsr166 |
1.28 |
Thread.currentThread().interrupt(); |
170 |
jsr166 |
1.24 |
a.join(); |
171 |
|
|
shouldThrow(); |
172 |
|
|
} catch (Throwable expected) { |
173 |
dl |
1.30 |
assertSame(t.getClass(), expected.getClass()); |
174 |
jsr166 |
1.24 |
} |
175 |
jsr166 |
1.29 |
Thread.interrupted(); |
176 |
jsr166 |
1.24 |
|
177 |
jsr166 |
1.28 |
{ |
178 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
179 |
jsr166 |
1.28 |
a.quietlyJoin(); // should be no-op |
180 |
jsr166 |
1.52 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
181 |
jsr166 |
1.28 |
} |
182 |
|
|
|
183 |
jsr166 |
1.24 |
try { |
184 |
|
|
a.get(); |
185 |
|
|
shouldThrow(); |
186 |
|
|
} catch (ExecutionException success) { |
187 |
dl |
1.30 |
assertSame(t.getClass(), success.getCause().getClass()); |
188 |
jsr166 |
1.24 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
189 |
|
|
|
190 |
|
|
try { |
191 |
jsr166 |
1.52 |
a.get(randomTimeout(), randomTimeUnit()); |
192 |
jsr166 |
1.24 |
shouldThrow(); |
193 |
|
|
} catch (ExecutionException success) { |
194 |
dl |
1.30 |
assertSame(t.getClass(), success.getCause().getClass()); |
195 |
jsr166 |
1.24 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
196 |
|
|
} |
197 |
|
|
|
198 |
jsr166 |
1.14 |
/* |
199 |
dl |
1.1 |
* Testing coverage notes: |
200 |
jsr166 |
1.2 |
* |
201 |
dl |
1.1 |
* To test extension methods and overrides, most tests use |
202 |
|
|
* BinaryAsyncAction extension class that processes joins |
203 |
|
|
* differently than supplied Recursive forms. |
204 |
jsr166 |
1.2 |
*/ |
205 |
dl |
1.1 |
|
206 |
dl |
1.30 |
public static final class FJException extends RuntimeException { |
207 |
dl |
1.1 |
FJException() { super(); } |
208 |
|
|
} |
209 |
|
|
|
210 |
jsr166 |
1.19 |
abstract static class BinaryAsyncAction extends ForkJoinTask<Void> { |
211 |
dl |
1.1 |
private volatile int controlState; |
212 |
|
|
|
213 |
|
|
static final AtomicIntegerFieldUpdater<BinaryAsyncAction> controlStateUpdater = |
214 |
jsr166 |
1.2 |
AtomicIntegerFieldUpdater.newUpdater(BinaryAsyncAction.class, |
215 |
dl |
1.1 |
"controlState"); |
216 |
|
|
|
217 |
dl |
1.43 |
private volatile BinaryAsyncAction parent; |
218 |
dl |
1.1 |
|
219 |
dl |
1.43 |
private volatile BinaryAsyncAction sibling; |
220 |
dl |
1.1 |
|
221 |
|
|
protected BinaryAsyncAction() { |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
public final Void getRawResult() { return null; } |
225 |
|
|
protected final void setRawResult(Void mustBeNull) { } |
226 |
|
|
|
227 |
|
|
public final void linkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) { |
228 |
|
|
x.parent = y.parent = this; |
229 |
|
|
x.sibling = y; |
230 |
|
|
y.sibling = x; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
protected boolean onException() { |
237 |
|
|
return true; |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
public void linkAndForkSubtasks(BinaryAsyncAction x, BinaryAsyncAction y) { |
241 |
|
|
linkSubtasks(x, y); |
242 |
|
|
y.fork(); |
243 |
|
|
x.fork(); |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
private void completeThis() { |
247 |
|
|
super.complete(null); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
private void completeThisExceptionally(Throwable ex) { |
251 |
|
|
super.completeExceptionally(ex); |
252 |
|
|
} |
253 |
|
|
|
254 |
dl |
1.46 |
public boolean cancel(boolean mayInterruptIfRunning) { |
255 |
|
|
if (super.cancel(mayInterruptIfRunning)) { |
256 |
|
|
completeExceptionally(new FJException()); |
257 |
|
|
return true; |
258 |
|
|
} |
259 |
|
|
return false; |
260 |
|
|
} |
261 |
jsr166 |
1.47 |
|
262 |
dl |
1.1 |
public final void complete() { |
263 |
|
|
BinaryAsyncAction a = this; |
264 |
|
|
for (;;) { |
265 |
|
|
BinaryAsyncAction s = a.sibling; |
266 |
|
|
BinaryAsyncAction p = a.parent; |
267 |
|
|
a.sibling = null; |
268 |
|
|
a.parent = null; |
269 |
|
|
a.completeThis(); |
270 |
|
|
if (p == null || p.compareAndSetControlState(0, 1)) |
271 |
|
|
break; |
272 |
|
|
try { |
273 |
|
|
p.onComplete(a, s); |
274 |
jsr166 |
1.2 |
} catch (Throwable rex) { |
275 |
dl |
1.1 |
p.completeExceptionally(rex); |
276 |
|
|
return; |
277 |
|
|
} |
278 |
|
|
a = p; |
279 |
|
|
} |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
public final void completeExceptionally(Throwable ex) { |
283 |
dl |
1.46 |
for (BinaryAsyncAction a = this;;) { |
284 |
dl |
1.1 |
a.completeThisExceptionally(ex); |
285 |
|
|
BinaryAsyncAction s = a.sibling; |
286 |
dl |
1.46 |
if (s != null && !s.isDone()) |
287 |
|
|
s.completeExceptionally(ex); |
288 |
|
|
if ((a = a.parent) == null) |
289 |
dl |
1.1 |
break; |
290 |
|
|
} |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
public final BinaryAsyncAction getParent() { |
294 |
|
|
return parent; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
public BinaryAsyncAction getSibling() { |
298 |
|
|
return sibling; |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
public void reinitialize() { |
302 |
|
|
parent = sibling = null; |
303 |
|
|
super.reinitialize(); |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
protected final int getControlState() { |
307 |
|
|
return controlState; |
308 |
|
|
} |
309 |
|
|
|
310 |
jsr166 |
1.2 |
protected final boolean compareAndSetControlState(int expect, |
311 |
dl |
1.1 |
int update) { |
312 |
|
|
return controlStateUpdater.compareAndSet(this, expect, update); |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
protected final void setControlState(int value) { |
316 |
|
|
controlState = value; |
317 |
|
|
} |
318 |
|
|
|
319 |
|
|
protected final void incrementControlState() { |
320 |
|
|
controlStateUpdater.incrementAndGet(this); |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
protected final void decrementControlState() { |
324 |
|
|
controlStateUpdater.decrementAndGet(this); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
} |
328 |
|
|
|
329 |
jsr166 |
1.6 |
static final class AsyncFib extends BinaryAsyncAction { |
330 |
dl |
1.1 |
int number; |
331 |
jsr166 |
1.2 |
public AsyncFib(int n) { |
332 |
dl |
1.1 |
this.number = n; |
333 |
|
|
} |
334 |
jsr166 |
1.2 |
|
335 |
dl |
1.1 |
public final boolean exec() { |
336 |
|
|
AsyncFib f = this; |
337 |
|
|
int n = f.number; |
338 |
dl |
1.48 |
while (n > 1) { |
339 |
|
|
AsyncFib p = f; |
340 |
|
|
AsyncFib r = new AsyncFib(n - 2); |
341 |
|
|
f = new AsyncFib(--n); |
342 |
|
|
p.linkSubtasks(r, f); |
343 |
|
|
r.fork(); |
344 |
dl |
1.1 |
} |
345 |
|
|
f.complete(); |
346 |
|
|
return false; |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
350 |
|
|
number = ((AsyncFib)x).number + ((AsyncFib)y).number; |
351 |
|
|
} |
352 |
|
|
} |
353 |
|
|
|
354 |
jsr166 |
1.6 |
static final class FailingAsyncFib extends BinaryAsyncAction { |
355 |
dl |
1.1 |
int number; |
356 |
jsr166 |
1.2 |
public FailingAsyncFib(int n) { |
357 |
dl |
1.1 |
this.number = n; |
358 |
|
|
} |
359 |
jsr166 |
1.2 |
|
360 |
dl |
1.1 |
public final boolean exec() { |
361 |
|
|
FailingAsyncFib f = this; |
362 |
|
|
int n = f.number; |
363 |
dl |
1.48 |
while (n > 1) { |
364 |
|
|
FailingAsyncFib p = f; |
365 |
|
|
FailingAsyncFib r = new FailingAsyncFib(n - 2); |
366 |
|
|
f = new FailingAsyncFib(--n); |
367 |
|
|
p.linkSubtasks(r, f); |
368 |
|
|
r.fork(); |
369 |
dl |
1.1 |
} |
370 |
|
|
f.complete(); |
371 |
|
|
return false; |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) { |
375 |
|
|
completeExceptionally(new FJException()); |
376 |
|
|
} |
377 |
|
|
} |
378 |
|
|
|
379 |
jsr166 |
1.2 |
/** |
380 |
dl |
1.1 |
* invoke returns when task completes normally. |
381 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
382 |
jsr166 |
1.26 |
* completed tasks; getRawResult returns null. |
383 |
dl |
1.1 |
*/ |
384 |
|
|
public void testInvoke() { |
385 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
386 |
jsr166 |
1.34 |
protected void realCompute() { |
387 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
388 |
jsr166 |
1.17 |
assertNull(f.invoke()); |
389 |
|
|
assertEquals(21, f.number); |
390 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
391 |
jsr166 |
1.12 |
}}; |
392 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
393 |
dl |
1.1 |
} |
394 |
|
|
|
395 |
jsr166 |
1.2 |
/** |
396 |
dl |
1.1 |
* quietlyInvoke task returns when task completes normally. |
397 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
398 |
|
|
* completed tasks |
399 |
|
|
*/ |
400 |
|
|
public void testQuietlyInvoke() { |
401 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
402 |
jsr166 |
1.34 |
protected void realCompute() { |
403 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
404 |
|
|
f.quietlyInvoke(); |
405 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
406 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
407 |
jsr166 |
1.12 |
}}; |
408 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
409 |
dl |
1.1 |
} |
410 |
|
|
|
411 |
jsr166 |
1.2 |
/** |
412 |
dl |
1.1 |
* join of a forked task returns when task completes |
413 |
|
|
*/ |
414 |
|
|
public void testForkJoin() { |
415 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
416 |
jsr166 |
1.34 |
protected void realCompute() { |
417 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
418 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
419 |
|
|
assertNull(f.join()); |
420 |
|
|
assertEquals(21, f.number); |
421 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
422 |
jsr166 |
1.12 |
}}; |
423 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
424 |
dl |
1.1 |
} |
425 |
|
|
|
426 |
jsr166 |
1.2 |
/** |
427 |
dl |
1.1 |
* get of a forked task returns when task completes |
428 |
|
|
*/ |
429 |
|
|
public void testForkGet() { |
430 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
431 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
432 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
433 |
|
|
assertSame(f, f.fork()); |
434 |
|
|
assertNull(f.get()); |
435 |
|
|
assertEquals(21, f.number); |
436 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
437 |
jsr166 |
1.12 |
}}; |
438 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
439 |
dl |
1.1 |
} |
440 |
|
|
|
441 |
jsr166 |
1.2 |
/** |
442 |
dl |
1.1 |
* timed get of a forked task returns when task completes |
443 |
|
|
*/ |
444 |
|
|
public void testForkTimedGet() { |
445 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
446 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
447 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
448 |
|
|
assertSame(f, f.fork()); |
449 |
jsr166 |
1.18 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
450 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
451 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
452 |
jsr166 |
1.12 |
}}; |
453 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
454 |
dl |
1.1 |
} |
455 |
|
|
|
456 |
jsr166 |
1.2 |
/** |
457 |
dl |
1.1 |
* timed get with null time unit throws NPE |
458 |
|
|
*/ |
459 |
|
|
public void testForkTimedGetNPE() { |
460 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
461 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
462 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
463 |
|
|
assertSame(f, f.fork()); |
464 |
jsr166 |
1.12 |
try { |
465 |
jsr166 |
1.52 |
f.get(randomTimeout(), null); |
466 |
jsr166 |
1.12 |
shouldThrow(); |
467 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
468 |
jsr166 |
1.12 |
}}; |
469 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
470 |
dl |
1.1 |
} |
471 |
|
|
|
472 |
jsr166 |
1.2 |
/** |
473 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes |
474 |
|
|
*/ |
475 |
|
|
public void testForkQuietlyJoin() { |
476 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
477 |
jsr166 |
1.34 |
protected void realCompute() { |
478 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
479 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
480 |
jsr166 |
1.12 |
f.quietlyJoin(); |
481 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
482 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
483 |
jsr166 |
1.12 |
}}; |
484 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
485 |
dl |
1.1 |
} |
486 |
|
|
|
487 |
jsr166 |
1.2 |
/** |
488 |
dl |
1.1 |
* helpQuiesce returns when tasks are complete. |
489 |
|
|
* getQueuedTaskCount returns 0 when quiescent |
490 |
|
|
*/ |
491 |
|
|
public void testForkHelpQuiesce() { |
492 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
493 |
jsr166 |
1.34 |
protected void realCompute() { |
494 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
495 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
496 |
jsr166 |
1.32 |
helpQuiesce(); |
497 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
498 |
|
|
assertEquals(0, getQueuedTaskCount()); |
499 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
500 |
jsr166 |
1.12 |
}}; |
501 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
502 |
dl |
1.1 |
} |
503 |
|
|
|
504 |
jsr166 |
1.2 |
/** |
505 |
dl |
1.1 |
* invoke task throws exception when task completes abnormally |
506 |
|
|
*/ |
507 |
|
|
public void testAbnormalInvoke() { |
508 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
509 |
jsr166 |
1.34 |
protected void realCompute() { |
510 |
jsr166 |
1.17 |
FailingAsyncFib f = new FailingAsyncFib(8); |
511 |
jsr166 |
1.12 |
try { |
512 |
|
|
f.invoke(); |
513 |
|
|
shouldThrow(); |
514 |
jsr166 |
1.24 |
} catch (FJException success) { |
515 |
|
|
checkCompletedAbnormally(f, success); |
516 |
|
|
} |
517 |
jsr166 |
1.12 |
}}; |
518 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
519 |
dl |
1.1 |
} |
520 |
|
|
|
521 |
jsr166 |
1.2 |
/** |
522 |
jsr166 |
1.3 |
* quietlyInvoke task returns when task completes abnormally |
523 |
dl |
1.1 |
*/ |
524 |
|
|
public void testAbnormalQuietlyInvoke() { |
525 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
526 |
jsr166 |
1.34 |
protected void realCompute() { |
527 |
jsr166 |
1.12 |
FailingAsyncFib f = new FailingAsyncFib(8); |
528 |
|
|
f.quietlyInvoke(); |
529 |
jsr166 |
1.24 |
assertTrue(f.getException() instanceof FJException); |
530 |
|
|
checkCompletedAbnormally(f, f.getException()); |
531 |
jsr166 |
1.12 |
}}; |
532 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
533 |
dl |
1.1 |
} |
534 |
|
|
|
535 |
jsr166 |
1.2 |
/** |
536 |
dl |
1.1 |
* join of a forked task throws exception when task completes abnormally |
537 |
|
|
*/ |
538 |
|
|
public void testAbnormalForkJoin() { |
539 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
540 |
jsr166 |
1.34 |
protected void realCompute() { |
541 |
jsr166 |
1.17 |
FailingAsyncFib f = new FailingAsyncFib(8); |
542 |
|
|
assertSame(f, f.fork()); |
543 |
jsr166 |
1.12 |
try { |
544 |
|
|
f.join(); |
545 |
|
|
shouldThrow(); |
546 |
jsr166 |
1.24 |
} catch (FJException success) { |
547 |
|
|
checkCompletedAbnormally(f, success); |
548 |
|
|
} |
549 |
jsr166 |
1.12 |
}}; |
550 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
551 |
dl |
1.1 |
} |
552 |
|
|
|
553 |
jsr166 |
1.2 |
/** |
554 |
dl |
1.1 |
* get of a forked task throws exception when task completes abnormally |
555 |
|
|
*/ |
556 |
|
|
public void testAbnormalForkGet() { |
557 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
558 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
559 |
jsr166 |
1.17 |
FailingAsyncFib f = new FailingAsyncFib(8); |
560 |
|
|
assertSame(f, f.fork()); |
561 |
jsr166 |
1.12 |
try { |
562 |
|
|
f.get(); |
563 |
|
|
shouldThrow(); |
564 |
jsr166 |
1.18 |
} catch (ExecutionException success) { |
565 |
|
|
Throwable cause = success.getCause(); |
566 |
|
|
assertTrue(cause instanceof FJException); |
567 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, cause); |
568 |
jsr166 |
1.18 |
} |
569 |
jsr166 |
1.12 |
}}; |
570 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
571 |
dl |
1.1 |
} |
572 |
|
|
|
573 |
jsr166 |
1.2 |
/** |
574 |
dl |
1.1 |
* timed get of a forked task throws exception when task completes abnormally |
575 |
|
|
*/ |
576 |
|
|
public void testAbnormalForkTimedGet() { |
577 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
578 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
579 |
jsr166 |
1.17 |
FailingAsyncFib f = new FailingAsyncFib(8); |
580 |
|
|
assertSame(f, f.fork()); |
581 |
jsr166 |
1.12 |
try { |
582 |
jsr166 |
1.18 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
583 |
jsr166 |
1.12 |
shouldThrow(); |
584 |
jsr166 |
1.18 |
} catch (ExecutionException success) { |
585 |
|
|
Throwable cause = success.getCause(); |
586 |
|
|
assertTrue(cause instanceof FJException); |
587 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, cause); |
588 |
jsr166 |
1.18 |
} |
589 |
jsr166 |
1.12 |
}}; |
590 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
591 |
dl |
1.1 |
} |
592 |
|
|
|
593 |
jsr166 |
1.2 |
/** |
594 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes abnormally |
595 |
|
|
*/ |
596 |
|
|
public void testAbnormalForkQuietlyJoin() { |
597 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
598 |
jsr166 |
1.34 |
protected void realCompute() { |
599 |
jsr166 |
1.12 |
FailingAsyncFib f = new FailingAsyncFib(8); |
600 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
601 |
jsr166 |
1.12 |
f.quietlyJoin(); |
602 |
jsr166 |
1.17 |
assertTrue(f.getException() instanceof FJException); |
603 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, f.getException()); |
604 |
jsr166 |
1.12 |
}}; |
605 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
606 |
dl |
1.1 |
} |
607 |
|
|
|
608 |
jsr166 |
1.2 |
/** |
609 |
dl |
1.1 |
* invoke task throws exception when task cancelled |
610 |
|
|
*/ |
611 |
|
|
public void testCancelledInvoke() { |
612 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
613 |
jsr166 |
1.34 |
protected void realCompute() { |
614 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
615 |
|
|
assertTrue(f.cancel(true)); |
616 |
jsr166 |
1.12 |
try { |
617 |
|
|
f.invoke(); |
618 |
|
|
shouldThrow(); |
619 |
jsr166 |
1.18 |
} catch (CancellationException success) { |
620 |
jsr166 |
1.24 |
checkCancelled(f); |
621 |
jsr166 |
1.18 |
} |
622 |
jsr166 |
1.12 |
}}; |
623 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
624 |
dl |
1.1 |
} |
625 |
|
|
|
626 |
jsr166 |
1.2 |
/** |
627 |
dl |
1.1 |
* join of a forked task throws exception when task cancelled |
628 |
|
|
*/ |
629 |
|
|
public void testCancelledForkJoin() { |
630 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
631 |
jsr166 |
1.34 |
protected void realCompute() { |
632 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
633 |
|
|
assertTrue(f.cancel(true)); |
634 |
|
|
assertSame(f, f.fork()); |
635 |
jsr166 |
1.12 |
try { |
636 |
|
|
f.join(); |
637 |
|
|
shouldThrow(); |
638 |
jsr166 |
1.18 |
} catch (CancellationException success) { |
639 |
jsr166 |
1.24 |
checkCancelled(f); |
640 |
jsr166 |
1.18 |
} |
641 |
jsr166 |
1.12 |
}}; |
642 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
643 |
dl |
1.1 |
} |
644 |
|
|
|
645 |
jsr166 |
1.2 |
/** |
646 |
dl |
1.1 |
* get of a forked task throws exception when task cancelled |
647 |
|
|
*/ |
648 |
|
|
public void testCancelledForkGet() { |
649 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
650 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
651 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
652 |
|
|
assertTrue(f.cancel(true)); |
653 |
|
|
assertSame(f, f.fork()); |
654 |
jsr166 |
1.12 |
try { |
655 |
|
|
f.get(); |
656 |
|
|
shouldThrow(); |
657 |
jsr166 |
1.18 |
} catch (CancellationException success) { |
658 |
jsr166 |
1.24 |
checkCancelled(f); |
659 |
jsr166 |
1.18 |
} |
660 |
jsr166 |
1.12 |
}}; |
661 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
662 |
dl |
1.1 |
} |
663 |
|
|
|
664 |
jsr166 |
1.2 |
/** |
665 |
dl |
1.1 |
* timed get of a forked task throws exception when task cancelled |
666 |
|
|
*/ |
667 |
jsr166 |
1.17 |
public void testCancelledForkTimedGet() throws Exception { |
668 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
669 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
670 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
671 |
|
|
assertTrue(f.cancel(true)); |
672 |
|
|
assertSame(f, f.fork()); |
673 |
|
|
try { |
674 |
jsr166 |
1.18 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
675 |
jsr166 |
1.12 |
shouldThrow(); |
676 |
jsr166 |
1.18 |
} catch (CancellationException success) { |
677 |
jsr166 |
1.24 |
checkCancelled(f); |
678 |
jsr166 |
1.18 |
} |
679 |
jsr166 |
1.12 |
}}; |
680 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
681 |
dl |
1.1 |
} |
682 |
|
|
|
683 |
jsr166 |
1.2 |
/** |
684 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task cancelled |
685 |
|
|
*/ |
686 |
|
|
public void testCancelledForkQuietlyJoin() { |
687 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
688 |
jsr166 |
1.34 |
protected void realCompute() { |
689 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
690 |
jsr166 |
1.17 |
assertTrue(f.cancel(true)); |
691 |
|
|
assertSame(f, f.fork()); |
692 |
jsr166 |
1.12 |
f.quietlyJoin(); |
693 |
jsr166 |
1.24 |
checkCancelled(f); |
694 |
jsr166 |
1.12 |
}}; |
695 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
696 |
dl |
1.1 |
} |
697 |
|
|
|
698 |
|
|
/** |
699 |
|
|
* getPool of executing task returns its pool |
700 |
|
|
*/ |
701 |
|
|
public void testGetPool() { |
702 |
jsr166 |
1.14 |
final ForkJoinPool mainPool = mainPool(); |
703 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
704 |
jsr166 |
1.34 |
protected void realCompute() { |
705 |
jsr166 |
1.17 |
assertSame(mainPool, getPool()); |
706 |
jsr166 |
1.12 |
}}; |
707 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool, a); |
708 |
dl |
1.1 |
} |
709 |
|
|
|
710 |
|
|
/** |
711 |
|
|
* getPool of non-FJ task returns null |
712 |
|
|
*/ |
713 |
|
|
public void testGetPool2() { |
714 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
715 |
jsr166 |
1.34 |
protected void realCompute() { |
716 |
jsr166 |
1.17 |
assertNull(getPool()); |
717 |
jsr166 |
1.12 |
}}; |
718 |
jsr166 |
1.16 |
assertNull(a.invoke()); |
719 |
dl |
1.1 |
} |
720 |
|
|
|
721 |
|
|
/** |
722 |
|
|
* inForkJoinPool of executing task returns true |
723 |
|
|
*/ |
724 |
|
|
public void testInForkJoinPool() { |
725 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
726 |
jsr166 |
1.34 |
protected void realCompute() { |
727 |
jsr166 |
1.17 |
assertTrue(inForkJoinPool()); |
728 |
jsr166 |
1.12 |
}}; |
729 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
730 |
dl |
1.1 |
} |
731 |
|
|
|
732 |
|
|
/** |
733 |
|
|
* inForkJoinPool of non-FJ task returns false |
734 |
|
|
*/ |
735 |
|
|
public void testInForkJoinPool2() { |
736 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
737 |
jsr166 |
1.34 |
protected void realCompute() { |
738 |
jsr166 |
1.24 |
assertFalse(inForkJoinPool()); |
739 |
jsr166 |
1.12 |
}}; |
740 |
jsr166 |
1.16 |
assertNull(a.invoke()); |
741 |
dl |
1.1 |
} |
742 |
|
|
|
743 |
|
|
/** |
744 |
|
|
* setRawResult(null) succeeds |
745 |
|
|
*/ |
746 |
|
|
public void testSetRawResult() { |
747 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
748 |
jsr166 |
1.34 |
protected void realCompute() { |
749 |
jsr166 |
1.12 |
setRawResult(null); |
750 |
jsr166 |
1.27 |
assertNull(getRawResult()); |
751 |
jsr166 |
1.12 |
}}; |
752 |
jsr166 |
1.16 |
assertNull(a.invoke()); |
753 |
dl |
1.1 |
} |
754 |
|
|
|
755 |
jsr166 |
1.2 |
/** |
756 |
dl |
1.1 |
* invoke task throws exception after invoking completeExceptionally |
757 |
|
|
*/ |
758 |
|
|
public void testCompleteExceptionally() { |
759 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
760 |
jsr166 |
1.34 |
protected void realCompute() { |
761 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
762 |
|
|
f.completeExceptionally(new FJException()); |
763 |
jsr166 |
1.12 |
try { |
764 |
|
|
f.invoke(); |
765 |
|
|
shouldThrow(); |
766 |
jsr166 |
1.24 |
} catch (FJException success) { |
767 |
|
|
checkCompletedAbnormally(f, success); |
768 |
|
|
} |
769 |
jsr166 |
1.12 |
}}; |
770 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
771 |
dl |
1.1 |
} |
772 |
|
|
|
773 |
jsr166 |
1.2 |
/** |
774 |
jsr166 |
1.49 |
* completeExceptionally(null) surprisingly has the same effect as |
775 |
|
|
* completeExceptionally(new RuntimeException()) |
776 |
|
|
*/ |
777 |
|
|
public void testCompleteExceptionally_null() { |
778 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
779 |
|
|
protected void realCompute() { |
780 |
|
|
AsyncFib f = new AsyncFib(8); |
781 |
|
|
f.completeExceptionally(null); |
782 |
|
|
try { |
783 |
|
|
f.invoke(); |
784 |
|
|
shouldThrow(); |
785 |
|
|
} catch (RuntimeException success) { |
786 |
|
|
assertSame(success.getClass(), RuntimeException.class); |
787 |
|
|
assertNull(success.getCause()); |
788 |
|
|
checkCompletedAbnormally(f, success); |
789 |
|
|
} |
790 |
|
|
}}; |
791 |
|
|
testInvokeOnPool(mainPool(), a); |
792 |
|
|
} |
793 |
|
|
|
794 |
|
|
/** |
795 |
dl |
1.1 |
* invokeAll(t1, t2) invokes all task arguments |
796 |
|
|
*/ |
797 |
|
|
public void testInvokeAll2() { |
798 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
799 |
jsr166 |
1.34 |
protected void realCompute() { |
800 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
801 |
|
|
AsyncFib g = new AsyncFib(9); |
802 |
|
|
invokeAll(f, g); |
803 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
804 |
|
|
assertEquals(34, g.number); |
805 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
806 |
|
|
checkCompletedNormally(g); |
807 |
jsr166 |
1.12 |
}}; |
808 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
809 |
dl |
1.1 |
} |
810 |
|
|
|
811 |
jsr166 |
1.2 |
/** |
812 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument invokes task |
813 |
|
|
*/ |
814 |
|
|
public void testInvokeAll1() { |
815 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
816 |
jsr166 |
1.34 |
protected void realCompute() { |
817 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
818 |
|
|
invokeAll(f); |
819 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
820 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
821 |
jsr166 |
1.12 |
}}; |
822 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
823 |
dl |
1.1 |
} |
824 |
|
|
|
825 |
jsr166 |
1.2 |
/** |
826 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument invokes tasks |
827 |
|
|
*/ |
828 |
|
|
public void testInvokeAll3() { |
829 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
830 |
jsr166 |
1.34 |
protected void realCompute() { |
831 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
832 |
|
|
AsyncFib g = new AsyncFib(9); |
833 |
|
|
AsyncFib h = new AsyncFib(7); |
834 |
|
|
invokeAll(f, g, h); |
835 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
836 |
|
|
assertEquals(34, g.number); |
837 |
|
|
assertEquals(13, h.number); |
838 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
839 |
|
|
checkCompletedNormally(g); |
840 |
|
|
checkCompletedNormally(h); |
841 |
jsr166 |
1.12 |
}}; |
842 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
843 |
dl |
1.1 |
} |
844 |
|
|
|
845 |
jsr166 |
1.2 |
/** |
846 |
dl |
1.1 |
* invokeAll(collection) invokes all tasks in the collection |
847 |
|
|
*/ |
848 |
|
|
public void testInvokeAllCollection() { |
849 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
850 |
jsr166 |
1.34 |
protected void realCompute() { |
851 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
852 |
|
|
AsyncFib g = new AsyncFib(9); |
853 |
|
|
AsyncFib h = new AsyncFib(7); |
854 |
|
|
HashSet set = new HashSet(); |
855 |
|
|
set.add(f); |
856 |
|
|
set.add(g); |
857 |
|
|
set.add(h); |
858 |
|
|
invokeAll(set); |
859 |
jsr166 |
1.17 |
assertEquals(21, f.number); |
860 |
|
|
assertEquals(34, g.number); |
861 |
|
|
assertEquals(13, h.number); |
862 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
863 |
|
|
checkCompletedNormally(g); |
864 |
|
|
checkCompletedNormally(h); |
865 |
jsr166 |
1.12 |
}}; |
866 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
867 |
dl |
1.1 |
} |
868 |
|
|
|
869 |
jsr166 |
1.2 |
/** |
870 |
dl |
1.1 |
* invokeAll(tasks) with any null task throws NPE |
871 |
|
|
*/ |
872 |
|
|
public void testInvokeAllNPE() { |
873 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
874 |
jsr166 |
1.34 |
protected void realCompute() { |
875 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
876 |
|
|
AsyncFib g = new AsyncFib(9); |
877 |
|
|
AsyncFib h = null; |
878 |
jsr166 |
1.12 |
try { |
879 |
|
|
invokeAll(f, g, h); |
880 |
|
|
shouldThrow(); |
881 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
882 |
jsr166 |
1.12 |
}}; |
883 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
884 |
dl |
1.1 |
} |
885 |
|
|
|
886 |
jsr166 |
1.2 |
/** |
887 |
dl |
1.1 |
* invokeAll(t1, t2) throw exception if any task does |
888 |
|
|
*/ |
889 |
|
|
public void testAbnormalInvokeAll2() { |
890 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
891 |
jsr166 |
1.34 |
protected void realCompute() { |
892 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
893 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
894 |
jsr166 |
1.45 |
ForkJoinTask[] tasks = { f, g }; |
895 |
jsr166 |
1.50 |
shuffle(tasks); |
896 |
jsr166 |
1.12 |
try { |
897 |
jsr166 |
1.45 |
invokeAll(tasks); |
898 |
jsr166 |
1.12 |
shouldThrow(); |
899 |
jsr166 |
1.24 |
} catch (FJException success) { |
900 |
|
|
checkCompletedAbnormally(g, success); |
901 |
|
|
} |
902 |
jsr166 |
1.12 |
}}; |
903 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
904 |
dl |
1.1 |
} |
905 |
|
|
|
906 |
jsr166 |
1.2 |
/** |
907 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument throws exception if task does |
908 |
|
|
*/ |
909 |
|
|
public void testAbnormalInvokeAll1() { |
910 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
911 |
jsr166 |
1.34 |
protected void realCompute() { |
912 |
jsr166 |
1.17 |
FailingAsyncFib g = new FailingAsyncFib(9); |
913 |
jsr166 |
1.12 |
try { |
914 |
|
|
invokeAll(g); |
915 |
|
|
shouldThrow(); |
916 |
jsr166 |
1.24 |
} catch (FJException success) { |
917 |
|
|
checkCompletedAbnormally(g, success); |
918 |
|
|
} |
919 |
jsr166 |
1.12 |
}}; |
920 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
921 |
dl |
1.1 |
} |
922 |
|
|
|
923 |
jsr166 |
1.2 |
/** |
924 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
925 |
|
|
*/ |
926 |
|
|
public void testAbnormalInvokeAll3() { |
927 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
928 |
jsr166 |
1.34 |
protected void realCompute() { |
929 |
jsr166 |
1.17 |
AsyncFib f = new AsyncFib(8); |
930 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
931 |
|
|
AsyncFib h = new AsyncFib(7); |
932 |
jsr166 |
1.45 |
ForkJoinTask[] tasks = { f, g, h }; |
933 |
jsr166 |
1.50 |
shuffle(tasks); |
934 |
jsr166 |
1.12 |
try { |
935 |
jsr166 |
1.45 |
invokeAll(tasks); |
936 |
jsr166 |
1.12 |
shouldThrow(); |
937 |
jsr166 |
1.24 |
} catch (FJException success) { |
938 |
|
|
checkCompletedAbnormally(g, success); |
939 |
|
|
} |
940 |
jsr166 |
1.12 |
}}; |
941 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
942 |
dl |
1.1 |
} |
943 |
|
|
|
944 |
jsr166 |
1.2 |
/** |
945 |
jsr166 |
1.37 |
* invokeAll(collection) throws exception if any task does |
946 |
dl |
1.1 |
*/ |
947 |
|
|
public void testAbnormalInvokeAllCollection() { |
948 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
949 |
jsr166 |
1.34 |
protected void realCompute() { |
950 |
jsr166 |
1.17 |
FailingAsyncFib f = new FailingAsyncFib(8); |
951 |
|
|
AsyncFib g = new AsyncFib(9); |
952 |
|
|
AsyncFib h = new AsyncFib(7); |
953 |
jsr166 |
1.45 |
ForkJoinTask[] tasks = { f, g, h }; |
954 |
jsr166 |
1.50 |
shuffle(tasks); |
955 |
jsr166 |
1.12 |
try { |
956 |
jsr166 |
1.50 |
invokeAll(Arrays.asList(tasks)); |
957 |
jsr166 |
1.12 |
shouldThrow(); |
958 |
jsr166 |
1.24 |
} catch (FJException success) { |
959 |
|
|
checkCompletedAbnormally(f, success); |
960 |
|
|
} |
961 |
jsr166 |
1.12 |
}}; |
962 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
963 |
dl |
1.1 |
} |
964 |
|
|
|
965 |
jsr166 |
1.2 |
/** |
966 |
dl |
1.1 |
* tryUnfork returns true for most recent unexecuted task, |
967 |
|
|
* and suppresses execution |
968 |
|
|
*/ |
969 |
|
|
public void testTryUnfork() { |
970 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
971 |
jsr166 |
1.34 |
protected void realCompute() { |
972 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
973 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
974 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
975 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
976 |
|
|
assertTrue(f.tryUnfork()); |
977 |
jsr166 |
1.12 |
helpQuiesce(); |
978 |
jsr166 |
1.24 |
checkNotDone(f); |
979 |
|
|
checkCompletedNormally(g); |
980 |
jsr166 |
1.12 |
}}; |
981 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
982 |
dl |
1.1 |
} |
983 |
|
|
|
984 |
jsr166 |
1.2 |
/** |
985 |
dl |
1.1 |
* getSurplusQueuedTaskCount returns > 0 when |
986 |
|
|
* there are more tasks than threads |
987 |
|
|
*/ |
988 |
|
|
public void testGetSurplusQueuedTaskCount() { |
989 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
990 |
jsr166 |
1.34 |
protected void realCompute() { |
991 |
jsr166 |
1.12 |
AsyncFib h = new AsyncFib(7); |
992 |
jsr166 |
1.17 |
assertSame(h, h.fork()); |
993 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
994 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
995 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
996 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
997 |
|
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
998 |
jsr166 |
1.12 |
helpQuiesce(); |
999 |
jsr166 |
1.24 |
assertEquals(0, getSurplusQueuedTaskCount()); |
1000 |
|
|
checkCompletedNormally(f); |
1001 |
|
|
checkCompletedNormally(g); |
1002 |
|
|
checkCompletedNormally(h); |
1003 |
jsr166 |
1.12 |
}}; |
1004 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1005 |
dl |
1.1 |
} |
1006 |
|
|
|
1007 |
jsr166 |
1.2 |
/** |
1008 |
dl |
1.1 |
* peekNextLocalTask returns most recent unexecuted task. |
1009 |
|
|
*/ |
1010 |
|
|
public void testPeekNextLocalTask() { |
1011 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
1012 |
jsr166 |
1.34 |
protected void realCompute() { |
1013 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
1014 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
1015 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
1016 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
1017 |
|
|
assertSame(f, peekNextLocalTask()); |
1018 |
|
|
assertNull(f.join()); |
1019 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1020 |
jsr166 |
1.12 |
helpQuiesce(); |
1021 |
jsr166 |
1.24 |
checkCompletedNormally(g); |
1022 |
jsr166 |
1.12 |
}}; |
1023 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1024 |
dl |
1.1 |
} |
1025 |
|
|
|
1026 |
jsr166 |
1.2 |
/** |
1027 |
jsr166 |
1.24 |
* pollNextLocalTask returns most recent unexecuted task without |
1028 |
|
|
* executing it |
1029 |
dl |
1.1 |
*/ |
1030 |
|
|
public void testPollNextLocalTask() { |
1031 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
1032 |
jsr166 |
1.34 |
protected void realCompute() { |
1033 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
1034 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
1035 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
1036 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
1037 |
|
|
assertSame(f, pollNextLocalTask()); |
1038 |
jsr166 |
1.12 |
helpQuiesce(); |
1039 |
jsr166 |
1.24 |
checkNotDone(f); |
1040 |
|
|
assertEquals(34, g.number); |
1041 |
|
|
checkCompletedNormally(g); |
1042 |
jsr166 |
1.12 |
}}; |
1043 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1044 |
dl |
1.1 |
} |
1045 |
|
|
|
1046 |
jsr166 |
1.2 |
/** |
1047 |
jsr166 |
1.24 |
* pollTask returns an unexecuted task without executing it |
1048 |
dl |
1.1 |
*/ |
1049 |
|
|
public void testPollTask() { |
1050 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
1051 |
jsr166 |
1.34 |
protected void realCompute() { |
1052 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
1053 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
1054 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
1055 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
1056 |
|
|
assertSame(f, pollTask()); |
1057 |
jsr166 |
1.12 |
helpQuiesce(); |
1058 |
jsr166 |
1.24 |
checkNotDone(f); |
1059 |
|
|
checkCompletedNormally(g); |
1060 |
jsr166 |
1.12 |
}}; |
1061 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1062 |
dl |
1.1 |
} |
1063 |
|
|
|
1064 |
jsr166 |
1.2 |
/** |
1065 |
dl |
1.1 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
1066 |
|
|
*/ |
1067 |
|
|
public void testPeekNextLocalTaskAsync() { |
1068 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
1069 |
jsr166 |
1.34 |
protected void realCompute() { |
1070 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
1071 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
1072 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
1073 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
1074 |
|
|
assertSame(g, peekNextLocalTask()); |
1075 |
|
|
assertNull(f.join()); |
1076 |
jsr166 |
1.12 |
helpQuiesce(); |
1077 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1078 |
|
|
assertEquals(34, g.number); |
1079 |
|
|
checkCompletedNormally(g); |
1080 |
jsr166 |
1.12 |
}}; |
1081 |
jsr166 |
1.14 |
testInvokeOnPool(asyncSingletonPool(), a); |
1082 |
dl |
1.1 |
} |
1083 |
|
|
|
1084 |
jsr166 |
1.2 |
/** |
1085 |
jsr166 |
1.24 |
* pollNextLocalTask returns least recent unexecuted task without |
1086 |
|
|
* executing it, in async mode |
1087 |
dl |
1.1 |
*/ |
1088 |
|
|
public void testPollNextLocalTaskAsync() { |
1089 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
1090 |
jsr166 |
1.34 |
protected void realCompute() { |
1091 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
1092 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
1093 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
1094 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
1095 |
|
|
assertSame(g, pollNextLocalTask()); |
1096 |
jsr166 |
1.12 |
helpQuiesce(); |
1097 |
jsr166 |
1.24 |
assertEquals(21, f.number); |
1098 |
|
|
checkCompletedNormally(f); |
1099 |
|
|
checkNotDone(g); |
1100 |
jsr166 |
1.12 |
}}; |
1101 |
jsr166 |
1.14 |
testInvokeOnPool(asyncSingletonPool(), a); |
1102 |
dl |
1.1 |
} |
1103 |
|
|
|
1104 |
jsr166 |
1.2 |
/** |
1105 |
jsr166 |
1.24 |
* pollTask returns an unexecuted task without executing it, in |
1106 |
|
|
* async mode |
1107 |
dl |
1.1 |
*/ |
1108 |
|
|
public void testPollTaskAsync() { |
1109 |
jsr166 |
1.17 |
RecursiveAction a = new CheckedRecursiveAction() { |
1110 |
jsr166 |
1.34 |
protected void realCompute() { |
1111 |
jsr166 |
1.12 |
AsyncFib g = new AsyncFib(9); |
1112 |
jsr166 |
1.17 |
assertSame(g, g.fork()); |
1113 |
jsr166 |
1.12 |
AsyncFib f = new AsyncFib(8); |
1114 |
jsr166 |
1.17 |
assertSame(f, f.fork()); |
1115 |
|
|
assertSame(g, pollTask()); |
1116 |
jsr166 |
1.12 |
helpQuiesce(); |
1117 |
jsr166 |
1.24 |
assertEquals(21, f.number); |
1118 |
|
|
checkCompletedNormally(f); |
1119 |
|
|
checkNotDone(g); |
1120 |
jsr166 |
1.12 |
}}; |
1121 |
jsr166 |
1.14 |
testInvokeOnPool(asyncSingletonPool(), a); |
1122 |
dl |
1.1 |
} |
1123 |
dl |
1.20 |
|
1124 |
|
|
// versions for singleton pools |
1125 |
|
|
|
1126 |
|
|
/** |
1127 |
|
|
* invoke returns when task completes normally. |
1128 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
1129 |
jsr166 |
1.26 |
* completed tasks; getRawResult returns null. |
1130 |
dl |
1.20 |
*/ |
1131 |
|
|
public void testInvokeSingleton() { |
1132 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1133 |
jsr166 |
1.34 |
protected void realCompute() { |
1134 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1135 |
|
|
assertNull(f.invoke()); |
1136 |
|
|
assertEquals(21, f.number); |
1137 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1138 |
dl |
1.20 |
}}; |
1139 |
|
|
testInvokeOnPool(singletonPool(), a); |
1140 |
|
|
} |
1141 |
|
|
|
1142 |
|
|
/** |
1143 |
|
|
* quietlyInvoke task returns when task completes normally. |
1144 |
|
|
* isCompletedAbnormally and isCancelled return false for normally |
1145 |
|
|
* completed tasks |
1146 |
|
|
*/ |
1147 |
|
|
public void testQuietlyInvokeSingleton() { |
1148 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1149 |
jsr166 |
1.34 |
protected void realCompute() { |
1150 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1151 |
|
|
f.quietlyInvoke(); |
1152 |
|
|
assertEquals(21, f.number); |
1153 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1154 |
dl |
1.20 |
}}; |
1155 |
|
|
testInvokeOnPool(singletonPool(), a); |
1156 |
|
|
} |
1157 |
|
|
|
1158 |
|
|
/** |
1159 |
|
|
* join of a forked task returns when task completes |
1160 |
|
|
*/ |
1161 |
|
|
public void testForkJoinSingleton() { |
1162 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1163 |
jsr166 |
1.34 |
protected void realCompute() { |
1164 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1165 |
|
|
assertSame(f, f.fork()); |
1166 |
|
|
assertNull(f.join()); |
1167 |
|
|
assertEquals(21, f.number); |
1168 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1169 |
dl |
1.20 |
}}; |
1170 |
|
|
testInvokeOnPool(singletonPool(), a); |
1171 |
|
|
} |
1172 |
|
|
|
1173 |
|
|
/** |
1174 |
|
|
* get of a forked task returns when task completes |
1175 |
|
|
*/ |
1176 |
|
|
public void testForkGetSingleton() { |
1177 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1178 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1179 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1180 |
|
|
assertSame(f, f.fork()); |
1181 |
|
|
assertNull(f.get()); |
1182 |
|
|
assertEquals(21, f.number); |
1183 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1184 |
dl |
1.20 |
}}; |
1185 |
|
|
testInvokeOnPool(singletonPool(), a); |
1186 |
|
|
} |
1187 |
|
|
|
1188 |
|
|
/** |
1189 |
|
|
* timed get of a forked task returns when task completes |
1190 |
|
|
*/ |
1191 |
|
|
public void testForkTimedGetSingleton() { |
1192 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1193 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1194 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1195 |
|
|
assertSame(f, f.fork()); |
1196 |
|
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
1197 |
|
|
assertEquals(21, f.number); |
1198 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1199 |
dl |
1.20 |
}}; |
1200 |
|
|
testInvokeOnPool(singletonPool(), a); |
1201 |
|
|
} |
1202 |
|
|
|
1203 |
|
|
/** |
1204 |
|
|
* timed get with null time unit throws NPE |
1205 |
|
|
*/ |
1206 |
|
|
public void testForkTimedGetNPESingleton() { |
1207 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1208 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1209 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1210 |
|
|
assertSame(f, f.fork()); |
1211 |
|
|
try { |
1212 |
jsr166 |
1.52 |
f.get(randomTimeout(), null); |
1213 |
dl |
1.20 |
shouldThrow(); |
1214 |
|
|
} catch (NullPointerException success) {} |
1215 |
|
|
}}; |
1216 |
|
|
testInvokeOnPool(singletonPool(), a); |
1217 |
|
|
} |
1218 |
|
|
|
1219 |
|
|
/** |
1220 |
|
|
* quietlyJoin of a forked task returns when task completes |
1221 |
|
|
*/ |
1222 |
|
|
public void testForkQuietlyJoinSingleton() { |
1223 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1224 |
jsr166 |
1.34 |
protected void realCompute() { |
1225 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1226 |
|
|
assertSame(f, f.fork()); |
1227 |
|
|
f.quietlyJoin(); |
1228 |
|
|
assertEquals(21, f.number); |
1229 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1230 |
dl |
1.20 |
}}; |
1231 |
|
|
testInvokeOnPool(singletonPool(), a); |
1232 |
|
|
} |
1233 |
|
|
|
1234 |
|
|
/** |
1235 |
|
|
* helpQuiesce returns when tasks are complete. |
1236 |
|
|
* getQueuedTaskCount returns 0 when quiescent |
1237 |
|
|
*/ |
1238 |
|
|
public void testForkHelpQuiesceSingleton() { |
1239 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1240 |
jsr166 |
1.34 |
protected void realCompute() { |
1241 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1242 |
|
|
assertSame(f, f.fork()); |
1243 |
jsr166 |
1.32 |
helpQuiesce(); |
1244 |
jsr166 |
1.24 |
assertEquals(0, getQueuedTaskCount()); |
1245 |
dl |
1.20 |
assertEquals(21, f.number); |
1246 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1247 |
dl |
1.20 |
}}; |
1248 |
|
|
testInvokeOnPool(singletonPool(), a); |
1249 |
|
|
} |
1250 |
|
|
|
1251 |
|
|
/** |
1252 |
|
|
* invoke task throws exception when task completes abnormally |
1253 |
|
|
*/ |
1254 |
|
|
public void testAbnormalInvokeSingleton() { |
1255 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1256 |
jsr166 |
1.34 |
protected void realCompute() { |
1257 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1258 |
|
|
try { |
1259 |
|
|
f.invoke(); |
1260 |
|
|
shouldThrow(); |
1261 |
jsr166 |
1.24 |
} catch (FJException success) { |
1262 |
|
|
checkCompletedAbnormally(f, success); |
1263 |
|
|
} |
1264 |
dl |
1.20 |
}}; |
1265 |
|
|
testInvokeOnPool(singletonPool(), a); |
1266 |
|
|
} |
1267 |
|
|
|
1268 |
|
|
/** |
1269 |
|
|
* quietlyInvoke task returns when task completes abnormally |
1270 |
|
|
*/ |
1271 |
|
|
public void testAbnormalQuietlyInvokeSingleton() { |
1272 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1273 |
jsr166 |
1.34 |
protected void realCompute() { |
1274 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1275 |
|
|
f.quietlyInvoke(); |
1276 |
jsr166 |
1.24 |
assertTrue(f.getException() instanceof FJException); |
1277 |
|
|
checkCompletedAbnormally(f, f.getException()); |
1278 |
dl |
1.20 |
}}; |
1279 |
|
|
testInvokeOnPool(singletonPool(), a); |
1280 |
|
|
} |
1281 |
|
|
|
1282 |
|
|
/** |
1283 |
|
|
* join of a forked task throws exception when task completes abnormally |
1284 |
|
|
*/ |
1285 |
|
|
public void testAbnormalForkJoinSingleton() { |
1286 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1287 |
jsr166 |
1.34 |
protected void realCompute() { |
1288 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1289 |
|
|
assertSame(f, f.fork()); |
1290 |
|
|
try { |
1291 |
|
|
f.join(); |
1292 |
|
|
shouldThrow(); |
1293 |
jsr166 |
1.24 |
} catch (FJException success) { |
1294 |
|
|
checkCompletedAbnormally(f, success); |
1295 |
|
|
} |
1296 |
dl |
1.20 |
}}; |
1297 |
|
|
testInvokeOnPool(singletonPool(), a); |
1298 |
|
|
} |
1299 |
|
|
|
1300 |
|
|
/** |
1301 |
|
|
* get of a forked task throws exception when task completes abnormally |
1302 |
|
|
*/ |
1303 |
|
|
public void testAbnormalForkGetSingleton() { |
1304 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1305 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1306 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1307 |
|
|
assertSame(f, f.fork()); |
1308 |
|
|
try { |
1309 |
|
|
f.get(); |
1310 |
|
|
shouldThrow(); |
1311 |
|
|
} catch (ExecutionException success) { |
1312 |
|
|
Throwable cause = success.getCause(); |
1313 |
|
|
assertTrue(cause instanceof FJException); |
1314 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, cause); |
1315 |
dl |
1.20 |
} |
1316 |
|
|
}}; |
1317 |
|
|
testInvokeOnPool(singletonPool(), a); |
1318 |
|
|
} |
1319 |
|
|
|
1320 |
|
|
/** |
1321 |
|
|
* timed get of a forked task throws exception when task completes abnormally |
1322 |
|
|
*/ |
1323 |
|
|
public void testAbnormalForkTimedGetSingleton() { |
1324 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1325 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1326 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1327 |
|
|
assertSame(f, f.fork()); |
1328 |
|
|
try { |
1329 |
|
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1330 |
|
|
shouldThrow(); |
1331 |
|
|
} catch (ExecutionException success) { |
1332 |
|
|
Throwable cause = success.getCause(); |
1333 |
|
|
assertTrue(cause instanceof FJException); |
1334 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, cause); |
1335 |
dl |
1.20 |
} |
1336 |
|
|
}}; |
1337 |
|
|
testInvokeOnPool(singletonPool(), a); |
1338 |
|
|
} |
1339 |
|
|
|
1340 |
|
|
/** |
1341 |
|
|
* quietlyJoin of a forked task returns when task completes abnormally |
1342 |
|
|
*/ |
1343 |
|
|
public void testAbnormalForkQuietlyJoinSingleton() { |
1344 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1345 |
jsr166 |
1.34 |
protected void realCompute() { |
1346 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1347 |
|
|
assertSame(f, f.fork()); |
1348 |
|
|
f.quietlyJoin(); |
1349 |
|
|
assertTrue(f.getException() instanceof FJException); |
1350 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, f.getException()); |
1351 |
dl |
1.20 |
}}; |
1352 |
|
|
testInvokeOnPool(singletonPool(), a); |
1353 |
|
|
} |
1354 |
|
|
|
1355 |
|
|
/** |
1356 |
|
|
* invoke task throws exception when task cancelled |
1357 |
|
|
*/ |
1358 |
|
|
public void testCancelledInvokeSingleton() { |
1359 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1360 |
jsr166 |
1.34 |
protected void realCompute() { |
1361 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1362 |
|
|
assertTrue(f.cancel(true)); |
1363 |
|
|
try { |
1364 |
|
|
f.invoke(); |
1365 |
|
|
shouldThrow(); |
1366 |
|
|
} catch (CancellationException success) { |
1367 |
jsr166 |
1.24 |
checkCancelled(f); |
1368 |
dl |
1.20 |
} |
1369 |
|
|
}}; |
1370 |
|
|
testInvokeOnPool(singletonPool(), a); |
1371 |
|
|
} |
1372 |
|
|
|
1373 |
|
|
/** |
1374 |
|
|
* join of a forked task throws exception when task cancelled |
1375 |
|
|
*/ |
1376 |
|
|
public void testCancelledForkJoinSingleton() { |
1377 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1378 |
jsr166 |
1.34 |
protected void realCompute() { |
1379 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1380 |
|
|
assertTrue(f.cancel(true)); |
1381 |
|
|
assertSame(f, f.fork()); |
1382 |
|
|
try { |
1383 |
|
|
f.join(); |
1384 |
|
|
shouldThrow(); |
1385 |
|
|
} catch (CancellationException success) { |
1386 |
jsr166 |
1.24 |
checkCancelled(f); |
1387 |
dl |
1.20 |
} |
1388 |
|
|
}}; |
1389 |
|
|
testInvokeOnPool(singletonPool(), a); |
1390 |
|
|
} |
1391 |
|
|
|
1392 |
|
|
/** |
1393 |
|
|
* get of a forked task throws exception when task cancelled |
1394 |
|
|
*/ |
1395 |
|
|
public void testCancelledForkGetSingleton() { |
1396 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1397 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1398 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1399 |
|
|
assertTrue(f.cancel(true)); |
1400 |
|
|
assertSame(f, f.fork()); |
1401 |
|
|
try { |
1402 |
|
|
f.get(); |
1403 |
|
|
shouldThrow(); |
1404 |
|
|
} catch (CancellationException success) { |
1405 |
jsr166 |
1.24 |
checkCancelled(f); |
1406 |
dl |
1.20 |
} |
1407 |
|
|
}}; |
1408 |
|
|
testInvokeOnPool(singletonPool(), a); |
1409 |
|
|
} |
1410 |
|
|
|
1411 |
|
|
/** |
1412 |
|
|
* timed get of a forked task throws exception when task cancelled |
1413 |
|
|
*/ |
1414 |
|
|
public void testCancelledForkTimedGetSingleton() throws Exception { |
1415 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1416 |
jsr166 |
1.34 |
protected void realCompute() throws Exception { |
1417 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1418 |
|
|
assertTrue(f.cancel(true)); |
1419 |
|
|
assertSame(f, f.fork()); |
1420 |
|
|
try { |
1421 |
|
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
1422 |
|
|
shouldThrow(); |
1423 |
|
|
} catch (CancellationException success) { |
1424 |
jsr166 |
1.24 |
checkCancelled(f); |
1425 |
dl |
1.20 |
} |
1426 |
|
|
}}; |
1427 |
|
|
testInvokeOnPool(singletonPool(), a); |
1428 |
|
|
} |
1429 |
|
|
|
1430 |
|
|
/** |
1431 |
|
|
* quietlyJoin of a forked task returns when task cancelled |
1432 |
|
|
*/ |
1433 |
|
|
public void testCancelledForkQuietlyJoinSingleton() { |
1434 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1435 |
jsr166 |
1.34 |
protected void realCompute() { |
1436 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1437 |
|
|
assertTrue(f.cancel(true)); |
1438 |
|
|
assertSame(f, f.fork()); |
1439 |
|
|
f.quietlyJoin(); |
1440 |
jsr166 |
1.24 |
checkCancelled(f); |
1441 |
dl |
1.20 |
}}; |
1442 |
|
|
testInvokeOnPool(singletonPool(), a); |
1443 |
|
|
} |
1444 |
|
|
|
1445 |
|
|
/** |
1446 |
|
|
* invoke task throws exception after invoking completeExceptionally |
1447 |
|
|
*/ |
1448 |
|
|
public void testCompleteExceptionallySingleton() { |
1449 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1450 |
jsr166 |
1.34 |
protected void realCompute() { |
1451 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1452 |
|
|
f.completeExceptionally(new FJException()); |
1453 |
|
|
try { |
1454 |
|
|
f.invoke(); |
1455 |
|
|
shouldThrow(); |
1456 |
jsr166 |
1.24 |
} catch (FJException success) { |
1457 |
|
|
checkCompletedAbnormally(f, success); |
1458 |
|
|
} |
1459 |
dl |
1.20 |
}}; |
1460 |
|
|
testInvokeOnPool(singletonPool(), a); |
1461 |
|
|
} |
1462 |
|
|
|
1463 |
|
|
/** |
1464 |
|
|
* invokeAll(t1, t2) invokes all task arguments |
1465 |
|
|
*/ |
1466 |
|
|
public void testInvokeAll2Singleton() { |
1467 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1468 |
jsr166 |
1.34 |
protected void realCompute() { |
1469 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1470 |
|
|
AsyncFib g = new AsyncFib(9); |
1471 |
|
|
invokeAll(f, g); |
1472 |
|
|
assertEquals(21, f.number); |
1473 |
|
|
assertEquals(34, g.number); |
1474 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1475 |
|
|
checkCompletedNormally(g); |
1476 |
dl |
1.20 |
}}; |
1477 |
|
|
testInvokeOnPool(singletonPool(), a); |
1478 |
|
|
} |
1479 |
|
|
|
1480 |
|
|
/** |
1481 |
|
|
* invokeAll(tasks) with 1 argument invokes task |
1482 |
|
|
*/ |
1483 |
|
|
public void testInvokeAll1Singleton() { |
1484 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1485 |
jsr166 |
1.34 |
protected void realCompute() { |
1486 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1487 |
|
|
invokeAll(f); |
1488 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1489 |
dl |
1.20 |
assertEquals(21, f.number); |
1490 |
|
|
}}; |
1491 |
|
|
testInvokeOnPool(singletonPool(), a); |
1492 |
|
|
} |
1493 |
|
|
|
1494 |
|
|
/** |
1495 |
|
|
* invokeAll(tasks) with > 2 argument invokes tasks |
1496 |
|
|
*/ |
1497 |
|
|
public void testInvokeAll3Singleton() { |
1498 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1499 |
jsr166 |
1.34 |
protected void realCompute() { |
1500 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1501 |
|
|
AsyncFib g = new AsyncFib(9); |
1502 |
|
|
AsyncFib h = new AsyncFib(7); |
1503 |
|
|
invokeAll(f, g, h); |
1504 |
|
|
assertEquals(21, f.number); |
1505 |
|
|
assertEquals(34, g.number); |
1506 |
|
|
assertEquals(13, h.number); |
1507 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1508 |
|
|
checkCompletedNormally(g); |
1509 |
|
|
checkCompletedNormally(h); |
1510 |
dl |
1.20 |
}}; |
1511 |
|
|
testInvokeOnPool(singletonPool(), a); |
1512 |
|
|
} |
1513 |
|
|
|
1514 |
|
|
/** |
1515 |
|
|
* invokeAll(collection) invokes all tasks in the collection |
1516 |
|
|
*/ |
1517 |
|
|
public void testInvokeAllCollectionSingleton() { |
1518 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1519 |
jsr166 |
1.34 |
protected void realCompute() { |
1520 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1521 |
|
|
AsyncFib g = new AsyncFib(9); |
1522 |
|
|
AsyncFib h = new AsyncFib(7); |
1523 |
|
|
HashSet set = new HashSet(); |
1524 |
|
|
set.add(f); |
1525 |
|
|
set.add(g); |
1526 |
|
|
set.add(h); |
1527 |
|
|
invokeAll(set); |
1528 |
|
|
assertEquals(21, f.number); |
1529 |
|
|
assertEquals(34, g.number); |
1530 |
|
|
assertEquals(13, h.number); |
1531 |
jsr166 |
1.24 |
checkCompletedNormally(f); |
1532 |
|
|
checkCompletedNormally(g); |
1533 |
|
|
checkCompletedNormally(h); |
1534 |
dl |
1.20 |
}}; |
1535 |
|
|
testInvokeOnPool(singletonPool(), a); |
1536 |
|
|
} |
1537 |
|
|
|
1538 |
|
|
/** |
1539 |
|
|
* invokeAll(tasks) with any null task throws NPE |
1540 |
|
|
*/ |
1541 |
|
|
public void testInvokeAllNPESingleton() { |
1542 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1543 |
jsr166 |
1.34 |
protected void realCompute() { |
1544 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1545 |
|
|
AsyncFib g = new AsyncFib(9); |
1546 |
|
|
AsyncFib h = null; |
1547 |
|
|
try { |
1548 |
|
|
invokeAll(f, g, h); |
1549 |
|
|
shouldThrow(); |
1550 |
|
|
} catch (NullPointerException success) {} |
1551 |
|
|
}}; |
1552 |
|
|
testInvokeOnPool(singletonPool(), a); |
1553 |
|
|
} |
1554 |
|
|
|
1555 |
|
|
/** |
1556 |
|
|
* invokeAll(t1, t2) throw exception if any task does |
1557 |
|
|
*/ |
1558 |
|
|
public void testAbnormalInvokeAll2Singleton() { |
1559 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1560 |
jsr166 |
1.34 |
protected void realCompute() { |
1561 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1562 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1563 |
jsr166 |
1.45 |
ForkJoinTask[] tasks = { f, g }; |
1564 |
jsr166 |
1.50 |
shuffle(tasks); |
1565 |
dl |
1.20 |
try { |
1566 |
jsr166 |
1.45 |
invokeAll(tasks); |
1567 |
dl |
1.20 |
shouldThrow(); |
1568 |
jsr166 |
1.24 |
} catch (FJException success) { |
1569 |
|
|
checkCompletedAbnormally(g, success); |
1570 |
|
|
} |
1571 |
dl |
1.20 |
}}; |
1572 |
|
|
testInvokeOnPool(singletonPool(), a); |
1573 |
|
|
} |
1574 |
|
|
|
1575 |
|
|
/** |
1576 |
|
|
* invokeAll(tasks) with 1 argument throws exception if task does |
1577 |
|
|
*/ |
1578 |
|
|
public void testAbnormalInvokeAll1Singleton() { |
1579 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1580 |
jsr166 |
1.34 |
protected void realCompute() { |
1581 |
dl |
1.20 |
FailingAsyncFib g = new FailingAsyncFib(9); |
1582 |
|
|
try { |
1583 |
|
|
invokeAll(g); |
1584 |
|
|
shouldThrow(); |
1585 |
jsr166 |
1.24 |
} catch (FJException success) { |
1586 |
|
|
checkCompletedAbnormally(g, success); |
1587 |
|
|
} |
1588 |
dl |
1.20 |
}}; |
1589 |
|
|
testInvokeOnPool(singletonPool(), a); |
1590 |
|
|
} |
1591 |
|
|
|
1592 |
|
|
/** |
1593 |
|
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
1594 |
|
|
*/ |
1595 |
|
|
public void testAbnormalInvokeAll3Singleton() { |
1596 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1597 |
jsr166 |
1.34 |
protected void realCompute() { |
1598 |
dl |
1.20 |
AsyncFib f = new AsyncFib(8); |
1599 |
|
|
FailingAsyncFib g = new FailingAsyncFib(9); |
1600 |
|
|
AsyncFib h = new AsyncFib(7); |
1601 |
jsr166 |
1.45 |
ForkJoinTask[] tasks = { f, g, h }; |
1602 |
jsr166 |
1.50 |
shuffle(tasks); |
1603 |
dl |
1.20 |
try { |
1604 |
jsr166 |
1.45 |
invokeAll(tasks); |
1605 |
dl |
1.20 |
shouldThrow(); |
1606 |
jsr166 |
1.24 |
} catch (FJException success) { |
1607 |
|
|
checkCompletedAbnormally(g, success); |
1608 |
|
|
} |
1609 |
dl |
1.20 |
}}; |
1610 |
|
|
testInvokeOnPool(singletonPool(), a); |
1611 |
|
|
} |
1612 |
|
|
|
1613 |
|
|
/** |
1614 |
jsr166 |
1.37 |
* invokeAll(collection) throws exception if any task does |
1615 |
dl |
1.20 |
*/ |
1616 |
|
|
public void testAbnormalInvokeAllCollectionSingleton() { |
1617 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1618 |
jsr166 |
1.34 |
protected void realCompute() { |
1619 |
dl |
1.20 |
FailingAsyncFib f = new FailingAsyncFib(8); |
1620 |
|
|
AsyncFib g = new AsyncFib(9); |
1621 |
|
|
AsyncFib h = new AsyncFib(7); |
1622 |
jsr166 |
1.45 |
ForkJoinTask[] tasks = { f, g, h }; |
1623 |
jsr166 |
1.50 |
shuffle(tasks); |
1624 |
dl |
1.20 |
try { |
1625 |
jsr166 |
1.50 |
invokeAll(Arrays.asList(tasks)); |
1626 |
dl |
1.20 |
shouldThrow(); |
1627 |
jsr166 |
1.24 |
} catch (FJException success) { |
1628 |
|
|
checkCompletedAbnormally(f, success); |
1629 |
|
|
} |
1630 |
dl |
1.20 |
}}; |
1631 |
|
|
testInvokeOnPool(singletonPool(), a); |
1632 |
|
|
} |
1633 |
|
|
|
1634 |
dl |
1.35 |
/** |
1635 |
|
|
* ForkJoinTask.quietlyComplete returns when task completes |
1636 |
|
|
* normally without setting a value. The most recent value |
1637 |
|
|
* established by setRawResult(V) (or null by default) is returned |
1638 |
|
|
* from invoke. |
1639 |
|
|
*/ |
1640 |
|
|
public void testQuietlyComplete() { |
1641 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
1642 |
|
|
protected void realCompute() { |
1643 |
|
|
AsyncFib f = new AsyncFib(8); |
1644 |
|
|
f.quietlyComplete(); |
1645 |
|
|
assertEquals(8, f.number); |
1646 |
|
|
checkCompletedNormally(f); |
1647 |
|
|
}}; |
1648 |
|
|
testInvokeOnPool(mainPool(), a); |
1649 |
|
|
} |
1650 |
|
|
|
1651 |
dl |
1.1 |
} |