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