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 |
jsr166 |
1.28 |
final Thread myself = Thread.currentThread(); |
259 |
jsr166 |
1.24 |
|
260 |
|
|
// test join() |
261 |
|
|
assertSame(f, f.fork()); |
262 |
jsr166 |
1.28 |
myself.interrupt(); |
263 |
|
|
assertTrue(myself.isInterrupted()); |
264 |
jsr166 |
1.24 |
assertNull(f.join()); |
265 |
jsr166 |
1.25 |
Thread.interrupted(); |
266 |
jsr166 |
1.24 |
assertEquals(21, f.result); |
267 |
|
|
checkCompletedNormally(f); |
268 |
|
|
|
269 |
|
|
f.reinitialize(); |
270 |
|
|
f.cancel(true); |
271 |
|
|
assertSame(f, f.fork()); |
272 |
jsr166 |
1.28 |
myself.interrupt(); |
273 |
|
|
assertTrue(myself.isInterrupted()); |
274 |
jsr166 |
1.24 |
try { |
275 |
|
|
f.join(); |
276 |
|
|
shouldThrow(); |
277 |
|
|
} catch (CancellationException success) { |
278 |
jsr166 |
1.25 |
Thread.interrupted(); |
279 |
jsr166 |
1.24 |
checkCancelled(f); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
f.reinitialize(); |
283 |
|
|
f.completeExceptionally(new FJException()); |
284 |
|
|
assertSame(f, f.fork()); |
285 |
jsr166 |
1.28 |
myself.interrupt(); |
286 |
|
|
assertTrue(myself.isInterrupted()); |
287 |
jsr166 |
1.24 |
try { |
288 |
|
|
f.join(); |
289 |
|
|
shouldThrow(); |
290 |
|
|
} catch (FJException success) { |
291 |
jsr166 |
1.25 |
Thread.interrupted(); |
292 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, success); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
// test quietlyJoin() |
296 |
|
|
f.reinitialize(); |
297 |
|
|
assertSame(f, f.fork()); |
298 |
jsr166 |
1.28 |
myself.interrupt(); |
299 |
|
|
assertTrue(myself.isInterrupted()); |
300 |
jsr166 |
1.24 |
f.quietlyJoin(); |
301 |
jsr166 |
1.25 |
Thread.interrupted(); |
302 |
jsr166 |
1.24 |
assertEquals(21, f.result); |
303 |
|
|
checkCompletedNormally(f); |
304 |
|
|
|
305 |
|
|
f.reinitialize(); |
306 |
|
|
f.cancel(true); |
307 |
|
|
assertSame(f, f.fork()); |
308 |
jsr166 |
1.28 |
myself.interrupt(); |
309 |
|
|
assertTrue(myself.isInterrupted()); |
310 |
jsr166 |
1.24 |
f.quietlyJoin(); |
311 |
jsr166 |
1.25 |
Thread.interrupted(); |
312 |
jsr166 |
1.24 |
checkCancelled(f); |
313 |
|
|
|
314 |
|
|
f.reinitialize(); |
315 |
|
|
f.completeExceptionally(new FJException()); |
316 |
|
|
assertSame(f, f.fork()); |
317 |
jsr166 |
1.28 |
myself.interrupt(); |
318 |
|
|
assertTrue(myself.isInterrupted()); |
319 |
jsr166 |
1.24 |
f.quietlyJoin(); |
320 |
jsr166 |
1.25 |
Thread.interrupted(); |
321 |
jsr166 |
1.24 |
checkCompletedAbnormally(f, f.getException()); |
322 |
|
|
}}; |
323 |
|
|
testInvokeOnPool(mainPool(), a); |
324 |
|
|
a.reinitialize(); |
325 |
|
|
testInvokeOnPool(singletonPool(), a); |
326 |
|
|
} |
327 |
|
|
|
328 |
|
|
/** |
329 |
jsr166 |
1.26 |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
330 |
|
|
* succeeds in the presence of interrupts |
331 |
|
|
*/ |
332 |
|
|
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
333 |
|
|
final SynchronousQueue<FibAction[]> sq = |
334 |
|
|
new SynchronousQueue<FibAction[]>(); |
335 |
|
|
RecursiveAction a = new CheckedRecursiveAction() { |
336 |
|
|
public void realCompute() throws InterruptedException { |
337 |
|
|
FibAction[] fibActions = new FibAction[6]; |
338 |
|
|
for (int i = 0; i < fibActions.length; i++) |
339 |
|
|
fibActions[i] = new FibAction(8); |
340 |
|
|
|
341 |
|
|
fibActions[1].cancel(false); |
342 |
|
|
fibActions[2].completeExceptionally(new FJException()); |
343 |
|
|
fibActions[4].cancel(true); |
344 |
|
|
fibActions[5].completeExceptionally(new FJException()); |
345 |
|
|
|
346 |
|
|
for (int i = 0; i < fibActions.length; i++) |
347 |
|
|
fibActions[i].fork(); |
348 |
|
|
|
349 |
|
|
sq.put(fibActions); |
350 |
|
|
|
351 |
|
|
helpQuiesce(); |
352 |
|
|
}}; |
353 |
|
|
|
354 |
|
|
Runnable r = new CheckedRunnable() { |
355 |
|
|
public void realRun() throws InterruptedException { |
356 |
|
|
FibAction[] fibActions = sq.take(); |
357 |
|
|
FibAction f; |
358 |
jsr166 |
1.28 |
final Thread myself = Thread.currentThread(); |
359 |
jsr166 |
1.26 |
|
360 |
|
|
// test join() ------------ |
361 |
|
|
|
362 |
|
|
f = fibActions[0]; |
363 |
jsr166 |
1.27 |
assertFalse(ForkJoinTask.inForkJoinPool()); |
364 |
jsr166 |
1.28 |
myself.interrupt(); |
365 |
|
|
assertTrue(myself.isInterrupted()); |
366 |
jsr166 |
1.26 |
assertNull(f.join()); |
367 |
|
|
assertTrue(Thread.interrupted()); |
368 |
|
|
assertEquals(21, f.result); |
369 |
|
|
checkCompletedNormally(f); |
370 |
|
|
|
371 |
|
|
f = fibActions[1]; |
372 |
jsr166 |
1.28 |
myself.interrupt(); |
373 |
|
|
assertTrue(myself.isInterrupted()); |
374 |
jsr166 |
1.26 |
try { |
375 |
|
|
f.join(); |
376 |
|
|
shouldThrow(); |
377 |
|
|
} catch (CancellationException success) { |
378 |
|
|
assertTrue(Thread.interrupted()); |
379 |
|
|
checkCancelled(f); |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
f = fibActions[2]; |
383 |
jsr166 |
1.28 |
myself.interrupt(); |
384 |
|
|
assertTrue(myself.isInterrupted()); |
385 |
jsr166 |
1.26 |
try { |
386 |
|
|
f.join(); |
387 |
|
|
shouldThrow(); |
388 |
|
|
} catch (FJException success) { |
389 |
|
|
assertTrue(Thread.interrupted()); |
390 |
|
|
checkCompletedAbnormally(f, success); |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
// test quietlyJoin() --------- |
394 |
|
|
|
395 |
|
|
f = fibActions[3]; |
396 |
jsr166 |
1.28 |
myself.interrupt(); |
397 |
|
|
assertTrue(myself.isInterrupted()); |
398 |
jsr166 |
1.26 |
f.quietlyJoin(); |
399 |
|
|
assertTrue(Thread.interrupted()); |
400 |
|
|
assertEquals(21, f.result); |
401 |
|
|
checkCompletedNormally(f); |
402 |
|
|
|
403 |
|
|
f = fibActions[4]; |
404 |
jsr166 |
1.28 |
myself.interrupt(); |
405 |
|
|
assertTrue(myself.isInterrupted()); |
406 |
jsr166 |
1.26 |
f.quietlyJoin(); |
407 |
|
|
assertTrue(Thread.interrupted()); |
408 |
|
|
checkCancelled(f); |
409 |
|
|
|
410 |
|
|
f = fibActions[5]; |
411 |
jsr166 |
1.28 |
myself.interrupt(); |
412 |
|
|
assertTrue(myself.isInterrupted()); |
413 |
jsr166 |
1.26 |
f.quietlyJoin(); |
414 |
|
|
assertTrue(Thread.interrupted()); |
415 |
|
|
assertTrue(f.getException() instanceof FJException); |
416 |
|
|
checkCompletedAbnormally(f, f.getException()); |
417 |
|
|
}}; |
418 |
|
|
|
419 |
|
|
Thread t; |
420 |
|
|
|
421 |
|
|
t = newStartedThread(r); |
422 |
|
|
testInvokeOnPool(mainPool(), a); |
423 |
|
|
awaitTermination(t, LONG_DELAY_MS); |
424 |
|
|
|
425 |
|
|
a.reinitialize(); |
426 |
|
|
t = newStartedThread(r); |
427 |
|
|
testInvokeOnPool(singletonPool(), a); |
428 |
|
|
awaitTermination(t, LONG_DELAY_MS); |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
/** |
432 |
dl |
1.1 |
* get of a forked task returns when task completes |
433 |
|
|
*/ |
434 |
|
|
public void testForkGet() { |
435 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
436 |
|
|
public void realCompute() throws Exception { |
437 |
|
|
FibAction f = new FibAction(8); |
438 |
|
|
assertSame(f, f.fork()); |
439 |
|
|
assertNull(f.get()); |
440 |
|
|
assertEquals(21, f.result); |
441 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
442 |
jsr166 |
1.7 |
}}; |
443 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
444 |
dl |
1.1 |
} |
445 |
|
|
|
446 |
jsr166 |
1.2 |
/** |
447 |
dl |
1.1 |
* timed get of a forked task returns when task completes |
448 |
|
|
*/ |
449 |
|
|
public void testForkTimedGet() { |
450 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
451 |
|
|
public void realCompute() throws Exception { |
452 |
|
|
FibAction f = new FibAction(8); |
453 |
|
|
assertSame(f, f.fork()); |
454 |
jsr166 |
1.19 |
assertNull(f.get(5L, SECONDS)); |
455 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
456 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
457 |
jsr166 |
1.7 |
}}; |
458 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
459 |
dl |
1.1 |
} |
460 |
|
|
|
461 |
jsr166 |
1.2 |
/** |
462 |
dl |
1.1 |
* timed get with null time unit throws NPE |
463 |
|
|
*/ |
464 |
|
|
public void testForkTimedGetNPE() { |
465 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
466 |
|
|
public void realCompute() throws Exception { |
467 |
|
|
FibAction f = new FibAction(8); |
468 |
|
|
assertSame(f, f.fork()); |
469 |
jsr166 |
1.8 |
try { |
470 |
|
|
f.get(5L, null); |
471 |
|
|
shouldThrow(); |
472 |
jsr166 |
1.18 |
} catch (NullPointerException success) {} |
473 |
jsr166 |
1.8 |
}}; |
474 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
475 |
dl |
1.1 |
} |
476 |
|
|
|
477 |
jsr166 |
1.2 |
/** |
478 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes |
479 |
|
|
*/ |
480 |
|
|
public void testForkQuietlyJoin() { |
481 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
482 |
|
|
public void realCompute() { |
483 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
484 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
485 |
jsr166 |
1.7 |
f.quietlyJoin(); |
486 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
487 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
488 |
jsr166 |
1.7 |
}}; |
489 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
490 |
dl |
1.1 |
} |
491 |
|
|
|
492 |
|
|
|
493 |
jsr166 |
1.2 |
/** |
494 |
dl |
1.1 |
* helpQuiesce returns when tasks are complete. |
495 |
|
|
* getQueuedTaskCount returns 0 when quiescent |
496 |
|
|
*/ |
497 |
|
|
public void testForkHelpQuiesce() { |
498 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
499 |
|
|
public void realCompute() { |
500 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
501 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
502 |
jsr166 |
1.7 |
f.helpQuiesce(); |
503 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
504 |
|
|
assertEquals(0, getQueuedTaskCount()); |
505 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
506 |
jsr166 |
1.7 |
}}; |
507 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
508 |
dl |
1.1 |
} |
509 |
|
|
|
510 |
|
|
|
511 |
jsr166 |
1.2 |
/** |
512 |
dl |
1.1 |
* invoke task throws exception when task completes abnormally |
513 |
|
|
*/ |
514 |
|
|
public void testAbnormalInvoke() { |
515 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
516 |
|
|
public void realCompute() { |
517 |
|
|
FailingFibAction f = new FailingFibAction(8); |
518 |
jsr166 |
1.7 |
try { |
519 |
|
|
f.invoke(); |
520 |
|
|
shouldThrow(); |
521 |
jsr166 |
1.19 |
} catch (FJException success) { |
522 |
jsr166 |
1.21 |
checkCompletedAbnormally(f, success); |
523 |
jsr166 |
1.19 |
} |
524 |
jsr166 |
1.7 |
}}; |
525 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
526 |
dl |
1.1 |
} |
527 |
|
|
|
528 |
jsr166 |
1.2 |
/** |
529 |
jsr166 |
1.4 |
* quietlyInvoke task returns when task completes abnormally |
530 |
dl |
1.1 |
*/ |
531 |
|
|
public void testAbnormalQuietlyInvoke() { |
532 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
533 |
|
|
public void realCompute() { |
534 |
jsr166 |
1.7 |
FailingFibAction f = new FailingFibAction(8); |
535 |
|
|
f.quietlyInvoke(); |
536 |
jsr166 |
1.19 |
assertTrue(f.getException() instanceof FJException); |
537 |
jsr166 |
1.21 |
checkCompletedAbnormally(f, f.getException()); |
538 |
jsr166 |
1.7 |
}}; |
539 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
540 |
dl |
1.1 |
} |
541 |
|
|
|
542 |
jsr166 |
1.2 |
/** |
543 |
dl |
1.1 |
* join of a forked task throws exception when task completes abnormally |
544 |
|
|
*/ |
545 |
|
|
public void testAbnormalForkJoin() { |
546 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
547 |
|
|
public void realCompute() { |
548 |
|
|
FailingFibAction f = new FailingFibAction(8); |
549 |
|
|
assertSame(f, f.fork()); |
550 |
jsr166 |
1.7 |
try { |
551 |
|
|
f.join(); |
552 |
|
|
shouldThrow(); |
553 |
jsr166 |
1.19 |
} catch (FJException success) { |
554 |
jsr166 |
1.21 |
checkCompletedAbnormally(f, success); |
555 |
jsr166 |
1.19 |
} |
556 |
jsr166 |
1.7 |
}}; |
557 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
558 |
dl |
1.1 |
} |
559 |
|
|
|
560 |
jsr166 |
1.2 |
/** |
561 |
dl |
1.1 |
* get of a forked task throws exception when task completes abnormally |
562 |
|
|
*/ |
563 |
|
|
public void testAbnormalForkGet() { |
564 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
565 |
|
|
public void realCompute() throws Exception { |
566 |
|
|
FailingFibAction f = new FailingFibAction(8); |
567 |
|
|
assertSame(f, f.fork()); |
568 |
jsr166 |
1.7 |
try { |
569 |
|
|
f.get(); |
570 |
|
|
shouldThrow(); |
571 |
jsr166 |
1.19 |
} catch (ExecutionException success) { |
572 |
jsr166 |
1.21 |
Throwable cause = success.getCause(); |
573 |
|
|
assertTrue(cause instanceof FJException); |
574 |
|
|
checkCompletedAbnormally(f, cause); |
575 |
jsr166 |
1.19 |
} |
576 |
jsr166 |
1.7 |
}}; |
577 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
578 |
dl |
1.1 |
} |
579 |
|
|
|
580 |
jsr166 |
1.2 |
/** |
581 |
dl |
1.1 |
* timed get of a forked task throws exception when task completes abnormally |
582 |
|
|
*/ |
583 |
|
|
public void testAbnormalForkTimedGet() { |
584 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
585 |
|
|
public void realCompute() throws Exception { |
586 |
|
|
FailingFibAction f = new FailingFibAction(8); |
587 |
|
|
assertSame(f, f.fork()); |
588 |
jsr166 |
1.7 |
try { |
589 |
|
|
f.get(5L, TimeUnit.SECONDS); |
590 |
|
|
shouldThrow(); |
591 |
jsr166 |
1.19 |
} catch (ExecutionException success) { |
592 |
jsr166 |
1.21 |
Throwable cause = success.getCause(); |
593 |
|
|
assertTrue(cause instanceof FJException); |
594 |
|
|
checkCompletedAbnormally(f, cause); |
595 |
jsr166 |
1.19 |
} |
596 |
jsr166 |
1.7 |
}}; |
597 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
598 |
dl |
1.1 |
} |
599 |
|
|
|
600 |
jsr166 |
1.2 |
/** |
601 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task completes abnormally |
602 |
|
|
*/ |
603 |
|
|
public void testAbnormalForkQuietlyJoin() { |
604 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
605 |
|
|
public void realCompute() { |
606 |
jsr166 |
1.7 |
FailingFibAction f = new FailingFibAction(8); |
607 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
608 |
jsr166 |
1.7 |
f.quietlyJoin(); |
609 |
jsr166 |
1.18 |
assertTrue(f.getException() instanceof FJException); |
610 |
jsr166 |
1.21 |
checkCompletedAbnormally(f, f.getException()); |
611 |
jsr166 |
1.7 |
}}; |
612 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
613 |
dl |
1.1 |
} |
614 |
|
|
|
615 |
jsr166 |
1.2 |
/** |
616 |
dl |
1.1 |
* invoke task throws exception when task cancelled |
617 |
|
|
*/ |
618 |
|
|
public void testCancelledInvoke() { |
619 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
620 |
|
|
public void realCompute() { |
621 |
|
|
FibAction f = new FibAction(8); |
622 |
|
|
assertTrue(f.cancel(true)); |
623 |
jsr166 |
1.7 |
try { |
624 |
|
|
f.invoke(); |
625 |
|
|
shouldThrow(); |
626 |
jsr166 |
1.19 |
} catch (CancellationException success) { |
627 |
|
|
checkCancelled(f); |
628 |
|
|
} |
629 |
jsr166 |
1.7 |
}}; |
630 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
631 |
dl |
1.1 |
} |
632 |
|
|
|
633 |
jsr166 |
1.2 |
/** |
634 |
dl |
1.1 |
* join of a forked task throws exception when task cancelled |
635 |
|
|
*/ |
636 |
|
|
public void testCancelledForkJoin() { |
637 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
638 |
|
|
public void realCompute() { |
639 |
|
|
FibAction f = new FibAction(8); |
640 |
|
|
assertTrue(f.cancel(true)); |
641 |
|
|
assertSame(f, f.fork()); |
642 |
jsr166 |
1.7 |
try { |
643 |
|
|
f.join(); |
644 |
|
|
shouldThrow(); |
645 |
jsr166 |
1.19 |
} catch (CancellationException success) { |
646 |
|
|
checkCancelled(f); |
647 |
|
|
} |
648 |
jsr166 |
1.7 |
}}; |
649 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
650 |
dl |
1.1 |
} |
651 |
|
|
|
652 |
jsr166 |
1.2 |
/** |
653 |
dl |
1.1 |
* get of a forked task throws exception when task cancelled |
654 |
|
|
*/ |
655 |
|
|
public void testCancelledForkGet() { |
656 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
657 |
|
|
public void realCompute() throws Exception { |
658 |
|
|
FibAction f = new FibAction(8); |
659 |
|
|
assertTrue(f.cancel(true)); |
660 |
|
|
assertSame(f, f.fork()); |
661 |
jsr166 |
1.7 |
try { |
662 |
|
|
f.get(); |
663 |
|
|
shouldThrow(); |
664 |
jsr166 |
1.19 |
} catch (CancellationException success) { |
665 |
|
|
checkCancelled(f); |
666 |
|
|
} |
667 |
jsr166 |
1.7 |
}}; |
668 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
669 |
dl |
1.1 |
} |
670 |
|
|
|
671 |
jsr166 |
1.2 |
/** |
672 |
dl |
1.1 |
* timed get of a forked task throws exception when task cancelled |
673 |
|
|
*/ |
674 |
|
|
public void testCancelledForkTimedGet() { |
675 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
676 |
|
|
public void realCompute() throws Exception { |
677 |
|
|
FibAction f = new FibAction(8); |
678 |
|
|
assertTrue(f.cancel(true)); |
679 |
|
|
assertSame(f, f.fork()); |
680 |
jsr166 |
1.7 |
try { |
681 |
jsr166 |
1.19 |
f.get(5L, SECONDS); |
682 |
jsr166 |
1.7 |
shouldThrow(); |
683 |
jsr166 |
1.19 |
} catch (CancellationException success) { |
684 |
|
|
checkCancelled(f); |
685 |
|
|
} |
686 |
jsr166 |
1.7 |
}}; |
687 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
688 |
dl |
1.1 |
} |
689 |
|
|
|
690 |
jsr166 |
1.2 |
/** |
691 |
dl |
1.1 |
* quietlyJoin of a forked task returns when task cancelled |
692 |
|
|
*/ |
693 |
|
|
public void testCancelledForkQuietlyJoin() { |
694 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
695 |
|
|
public void realCompute() { |
696 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
697 |
jsr166 |
1.18 |
assertTrue(f.cancel(true)); |
698 |
|
|
assertSame(f, f.fork()); |
699 |
jsr166 |
1.7 |
f.quietlyJoin(); |
700 |
jsr166 |
1.19 |
checkCancelled(f); |
701 |
jsr166 |
1.7 |
}}; |
702 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
703 |
dl |
1.1 |
} |
704 |
|
|
|
705 |
|
|
/** |
706 |
|
|
* getPool of executing task returns its pool |
707 |
|
|
*/ |
708 |
|
|
public void testGetPool() { |
709 |
jsr166 |
1.14 |
final ForkJoinPool mainPool = mainPool(); |
710 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
711 |
|
|
public void realCompute() { |
712 |
|
|
assertSame(mainPool, getPool()); |
713 |
jsr166 |
1.7 |
}}; |
714 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool, a); |
715 |
dl |
1.1 |
} |
716 |
|
|
|
717 |
|
|
/** |
718 |
|
|
* getPool of non-FJ task returns null |
719 |
|
|
*/ |
720 |
|
|
public void testGetPool2() { |
721 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
722 |
|
|
public void realCompute() { |
723 |
|
|
assertNull(getPool()); |
724 |
jsr166 |
1.7 |
}}; |
725 |
jsr166 |
1.16 |
assertNull(a.invoke()); |
726 |
dl |
1.1 |
} |
727 |
|
|
|
728 |
|
|
/** |
729 |
|
|
* inForkJoinPool of executing task returns true |
730 |
|
|
*/ |
731 |
|
|
public void testInForkJoinPool() { |
732 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
733 |
|
|
public void realCompute() { |
734 |
|
|
assertTrue(inForkJoinPool()); |
735 |
jsr166 |
1.7 |
}}; |
736 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
737 |
dl |
1.1 |
} |
738 |
|
|
|
739 |
|
|
/** |
740 |
|
|
* inForkJoinPool of non-FJ task returns false |
741 |
|
|
*/ |
742 |
|
|
public void testInForkJoinPool2() { |
743 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
744 |
|
|
public void realCompute() { |
745 |
|
|
assertFalse(inForkJoinPool()); |
746 |
jsr166 |
1.7 |
}}; |
747 |
jsr166 |
1.16 |
assertNull(a.invoke()); |
748 |
dl |
1.1 |
} |
749 |
|
|
|
750 |
|
|
/** |
751 |
|
|
* getPool of current thread in pool returns its pool |
752 |
|
|
*/ |
753 |
|
|
public void testWorkerGetPool() { |
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 |
jsr166 |
1.14 |
(ForkJoinWorkerThread) Thread.currentThread(); |
759 |
jsr166 |
1.18 |
assertSame(mainPool, w.getPool()); |
760 |
jsr166 |
1.7 |
}}; |
761 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool, a); |
762 |
dl |
1.1 |
} |
763 |
|
|
|
764 |
|
|
/** |
765 |
|
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
766 |
|
|
*/ |
767 |
|
|
public void testWorkerGetPoolIndex() { |
768 |
jsr166 |
1.14 |
final ForkJoinPool mainPool = mainPool(); |
769 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
|
|
public void realCompute() { |
771 |
jsr166 |
1.7 |
ForkJoinWorkerThread w = |
772 |
jsr166 |
1.28 |
(ForkJoinWorkerThread) Thread.currentThread(); |
773 |
jsr166 |
1.19 |
assertTrue(w.getPoolIndex() >= 0); |
774 |
|
|
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
775 |
jsr166 |
1.7 |
}}; |
776 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool, a); |
777 |
dl |
1.1 |
} |
778 |
|
|
|
779 |
|
|
|
780 |
|
|
/** |
781 |
|
|
* setRawResult(null) succeeds |
782 |
|
|
*/ |
783 |
|
|
public void testSetRawResult() { |
784 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
785 |
|
|
public void realCompute() { |
786 |
jsr166 |
1.7 |
setRawResult(null); |
787 |
jsr166 |
1.23 |
assertNull(getRawResult()); |
788 |
jsr166 |
1.7 |
}}; |
789 |
jsr166 |
1.16 |
assertNull(a.invoke()); |
790 |
dl |
1.1 |
} |
791 |
|
|
|
792 |
jsr166 |
1.2 |
/** |
793 |
dl |
1.1 |
* A reinitialized task may be re-invoked |
794 |
|
|
*/ |
795 |
|
|
public void testReinitialize() { |
796 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
797 |
|
|
public void realCompute() { |
798 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
799 |
jsr166 |
1.19 |
checkNotDone(f); |
800 |
|
|
|
801 |
|
|
for (int i = 0; i < 3; i++) { |
802 |
|
|
assertNull(f.invoke()); |
803 |
|
|
assertEquals(21, f.result); |
804 |
|
|
checkCompletedNormally(f); |
805 |
|
|
f.reinitialize(); |
806 |
|
|
checkNotDone(f); |
807 |
|
|
} |
808 |
jsr166 |
1.7 |
}}; |
809 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
810 |
dl |
1.1 |
} |
811 |
|
|
|
812 |
jsr166 |
1.2 |
/** |
813 |
dl |
1.1 |
* invoke task throws exception after invoking completeExceptionally |
814 |
|
|
*/ |
815 |
|
|
public void testCompleteExceptionally() { |
816 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
817 |
|
|
public void realCompute() { |
818 |
|
|
FibAction f = new FibAction(8); |
819 |
|
|
f.completeExceptionally(new FJException()); |
820 |
jsr166 |
1.7 |
try { |
821 |
|
|
f.invoke(); |
822 |
|
|
shouldThrow(); |
823 |
jsr166 |
1.19 |
} catch (FJException success) { |
824 |
jsr166 |
1.21 |
checkCompletedAbnormally(f, success); |
825 |
jsr166 |
1.19 |
} |
826 |
jsr166 |
1.7 |
}}; |
827 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
828 |
dl |
1.1 |
} |
829 |
|
|
|
830 |
jsr166 |
1.2 |
/** |
831 |
dl |
1.1 |
* invoke task suppresses execution invoking complete |
832 |
|
|
*/ |
833 |
|
|
public void testComplete() { |
834 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
835 |
|
|
public void realCompute() { |
836 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
837 |
|
|
f.complete(null); |
838 |
jsr166 |
1.18 |
assertNull(f.invoke()); |
839 |
|
|
assertEquals(0, f.result); |
840 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
841 |
jsr166 |
1.7 |
}}; |
842 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
843 |
dl |
1.1 |
} |
844 |
|
|
|
845 |
jsr166 |
1.2 |
/** |
846 |
dl |
1.1 |
* invokeAll(t1, t2) invokes all task arguments |
847 |
|
|
*/ |
848 |
|
|
public void testInvokeAll2() { |
849 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
850 |
|
|
public void realCompute() { |
851 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
852 |
|
|
FibAction g = new FibAction(9); |
853 |
|
|
invokeAll(f, g); |
854 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
855 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
856 |
jsr166 |
1.19 |
checkCompletedNormally(g); |
857 |
jsr166 |
1.18 |
assertEquals(34, g.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 1 argument invokes task |
864 |
|
|
*/ |
865 |
|
|
public void testInvokeAll1() { |
866 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
867 |
|
|
public void realCompute() { |
868 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
869 |
|
|
invokeAll(f); |
870 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
871 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
872 |
jsr166 |
1.7 |
}}; |
873 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
874 |
dl |
1.1 |
} |
875 |
|
|
|
876 |
jsr166 |
1.2 |
/** |
877 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument invokes tasks |
878 |
|
|
*/ |
879 |
|
|
public void testInvokeAll3() { |
880 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
881 |
|
|
public void realCompute() { |
882 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
883 |
|
|
FibAction g = new FibAction(9); |
884 |
|
|
FibAction h = new FibAction(7); |
885 |
|
|
invokeAll(f, g, h); |
886 |
jsr166 |
1.20 |
assertTrue(f.isDone()); |
887 |
|
|
assertTrue(g.isDone()); |
888 |
|
|
assertTrue(h.isDone()); |
889 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
890 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
891 |
jsr166 |
1.19 |
checkCompletedNormally(g); |
892 |
jsr166 |
1.18 |
assertEquals(34, g.result); |
893 |
jsr166 |
1.19 |
checkCompletedNormally(g); |
894 |
jsr166 |
1.18 |
assertEquals(13, h.result); |
895 |
jsr166 |
1.7 |
}}; |
896 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
897 |
dl |
1.1 |
} |
898 |
|
|
|
899 |
jsr166 |
1.2 |
/** |
900 |
dl |
1.1 |
* invokeAll(collection) invokes all tasks in the collection |
901 |
|
|
*/ |
902 |
|
|
public void testInvokeAllCollection() { |
903 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
904 |
|
|
public void realCompute() { |
905 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
906 |
|
|
FibAction g = new FibAction(9); |
907 |
|
|
FibAction h = new FibAction(7); |
908 |
|
|
HashSet set = new HashSet(); |
909 |
|
|
set.add(f); |
910 |
|
|
set.add(g); |
911 |
|
|
set.add(h); |
912 |
|
|
invokeAll(set); |
913 |
jsr166 |
1.20 |
assertTrue(f.isDone()); |
914 |
|
|
assertTrue(g.isDone()); |
915 |
|
|
assertTrue(h.isDone()); |
916 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
917 |
jsr166 |
1.18 |
assertEquals(21, f.result); |
918 |
jsr166 |
1.19 |
checkCompletedNormally(g); |
919 |
jsr166 |
1.18 |
assertEquals(34, g.result); |
920 |
jsr166 |
1.19 |
checkCompletedNormally(g); |
921 |
jsr166 |
1.18 |
assertEquals(13, h.result); |
922 |
jsr166 |
1.7 |
}}; |
923 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
924 |
dl |
1.1 |
} |
925 |
|
|
|
926 |
|
|
|
927 |
jsr166 |
1.2 |
/** |
928 |
dl |
1.1 |
* invokeAll(tasks) with any null task throws NPE |
929 |
|
|
*/ |
930 |
|
|
public void testInvokeAllNPE() { |
931 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
932 |
|
|
public void realCompute() { |
933 |
|
|
FibAction f = new FibAction(8); |
934 |
|
|
FibAction g = new FibAction(9); |
935 |
|
|
FibAction h = null; |
936 |
jsr166 |
1.7 |
try { |
937 |
|
|
invokeAll(f, g, h); |
938 |
|
|
shouldThrow(); |
939 |
jsr166 |
1.18 |
} catch (NullPointerException success) {} |
940 |
jsr166 |
1.7 |
}}; |
941 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
942 |
dl |
1.1 |
} |
943 |
|
|
|
944 |
jsr166 |
1.2 |
/** |
945 |
dl |
1.1 |
* invokeAll(t1, t2) throw exception if any task does |
946 |
|
|
*/ |
947 |
|
|
public void testAbnormalInvokeAll2() { |
948 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
949 |
|
|
public void realCompute() { |
950 |
|
|
FibAction f = new FibAction(8); |
951 |
|
|
FailingFibAction g = new FailingFibAction(9); |
952 |
jsr166 |
1.7 |
try { |
953 |
|
|
invokeAll(f, g); |
954 |
|
|
shouldThrow(); |
955 |
jsr166 |
1.19 |
} catch (FJException success) { |
956 |
jsr166 |
1.21 |
checkCompletedAbnormally(g, success); |
957 |
jsr166 |
1.19 |
} |
958 |
jsr166 |
1.7 |
}}; |
959 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
960 |
dl |
1.1 |
} |
961 |
|
|
|
962 |
jsr166 |
1.2 |
/** |
963 |
dl |
1.1 |
* invokeAll(tasks) with 1 argument throws exception if task does |
964 |
|
|
*/ |
965 |
|
|
public void testAbnormalInvokeAll1() { |
966 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
967 |
|
|
public void realCompute() { |
968 |
|
|
FailingFibAction g = new FailingFibAction(9); |
969 |
jsr166 |
1.7 |
try { |
970 |
|
|
invokeAll(g); |
971 |
|
|
shouldThrow(); |
972 |
jsr166 |
1.19 |
} catch (FJException success) { |
973 |
jsr166 |
1.21 |
checkCompletedAbnormally(g, success); |
974 |
jsr166 |
1.19 |
} |
975 |
jsr166 |
1.7 |
}}; |
976 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
977 |
dl |
1.1 |
} |
978 |
|
|
|
979 |
jsr166 |
1.2 |
/** |
980 |
dl |
1.1 |
* invokeAll(tasks) with > 2 argument throws exception if any task does |
981 |
|
|
*/ |
982 |
|
|
public void testAbnormalInvokeAll3() { |
983 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
984 |
|
|
public void realCompute() { |
985 |
|
|
FibAction f = new FibAction(8); |
986 |
|
|
FailingFibAction g = new FailingFibAction(9); |
987 |
|
|
FibAction h = new FibAction(7); |
988 |
jsr166 |
1.7 |
try { |
989 |
|
|
invokeAll(f, g, h); |
990 |
|
|
shouldThrow(); |
991 |
jsr166 |
1.19 |
} catch (FJException success) { |
992 |
jsr166 |
1.21 |
checkCompletedAbnormally(g, success); |
993 |
jsr166 |
1.19 |
} |
994 |
jsr166 |
1.7 |
}}; |
995 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
996 |
dl |
1.1 |
} |
997 |
|
|
|
998 |
jsr166 |
1.2 |
/** |
999 |
jsr166 |
1.3 |
* invokeAll(collection) throws exception if any task does |
1000 |
dl |
1.1 |
*/ |
1001 |
|
|
public void testAbnormalInvokeAllCollection() { |
1002 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1003 |
|
|
public void realCompute() { |
1004 |
|
|
FailingFibAction f = new FailingFibAction(8); |
1005 |
|
|
FibAction g = new FibAction(9); |
1006 |
|
|
FibAction h = new FibAction(7); |
1007 |
|
|
HashSet set = new HashSet(); |
1008 |
|
|
set.add(f); |
1009 |
|
|
set.add(g); |
1010 |
|
|
set.add(h); |
1011 |
jsr166 |
1.7 |
try { |
1012 |
|
|
invokeAll(set); |
1013 |
|
|
shouldThrow(); |
1014 |
jsr166 |
1.19 |
} catch (FJException success) { |
1015 |
jsr166 |
1.21 |
checkCompletedAbnormally(f, success); |
1016 |
jsr166 |
1.19 |
} |
1017 |
jsr166 |
1.7 |
}}; |
1018 |
jsr166 |
1.14 |
testInvokeOnPool(mainPool(), a); |
1019 |
dl |
1.1 |
} |
1020 |
|
|
|
1021 |
jsr166 |
1.2 |
/** |
1022 |
dl |
1.1 |
* tryUnfork returns true for most recent unexecuted task, |
1023 |
|
|
* and suppresses execution |
1024 |
|
|
*/ |
1025 |
|
|
public void testTryUnfork() { |
1026 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1027 |
|
|
public void realCompute() { |
1028 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1029 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1030 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1031 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1032 |
|
|
assertTrue(f.tryUnfork()); |
1033 |
jsr166 |
1.7 |
helpQuiesce(); |
1034 |
jsr166 |
1.19 |
checkNotDone(f); |
1035 |
|
|
checkCompletedNormally(g); |
1036 |
jsr166 |
1.7 |
}}; |
1037 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1038 |
dl |
1.1 |
} |
1039 |
|
|
|
1040 |
jsr166 |
1.2 |
/** |
1041 |
dl |
1.1 |
* getSurplusQueuedTaskCount returns > 0 when |
1042 |
|
|
* there are more tasks than threads |
1043 |
|
|
*/ |
1044 |
|
|
public void testGetSurplusQueuedTaskCount() { |
1045 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1046 |
|
|
public void realCompute() { |
1047 |
jsr166 |
1.7 |
FibAction h = new FibAction(7); |
1048 |
jsr166 |
1.18 |
assertSame(h, h.fork()); |
1049 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1050 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1051 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1052 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1053 |
|
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1054 |
jsr166 |
1.7 |
helpQuiesce(); |
1055 |
jsr166 |
1.21 |
assertEquals(0, getSurplusQueuedTaskCount()); |
1056 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
1057 |
|
|
checkCompletedNormally(g); |
1058 |
|
|
checkCompletedNormally(h); |
1059 |
jsr166 |
1.7 |
}}; |
1060 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1061 |
dl |
1.1 |
} |
1062 |
|
|
|
1063 |
jsr166 |
1.2 |
/** |
1064 |
dl |
1.1 |
* peekNextLocalTask returns most recent unexecuted task. |
1065 |
|
|
*/ |
1066 |
|
|
public void testPeekNextLocalTask() { |
1067 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1068 |
|
|
public void realCompute() { |
1069 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1070 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1071 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1072 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1073 |
|
|
assertSame(f, peekNextLocalTask()); |
1074 |
|
|
assertNull(f.join()); |
1075 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
1076 |
jsr166 |
1.7 |
helpQuiesce(); |
1077 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
1078 |
|
|
checkCompletedNormally(g); |
1079 |
jsr166 |
1.7 |
}}; |
1080 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1081 |
dl |
1.1 |
} |
1082 |
|
|
|
1083 |
jsr166 |
1.2 |
/** |
1084 |
dl |
1.1 |
* pollNextLocalTask returns most recent unexecuted task |
1085 |
|
|
* without executing it |
1086 |
|
|
*/ |
1087 |
|
|
public void testPollNextLocalTask() { |
1088 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1089 |
|
|
public void realCompute() { |
1090 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1091 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1092 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1093 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1094 |
|
|
assertSame(f, pollNextLocalTask()); |
1095 |
jsr166 |
1.7 |
helpQuiesce(); |
1096 |
jsr166 |
1.19 |
checkNotDone(f); |
1097 |
|
|
checkCompletedNormally(g); |
1098 |
jsr166 |
1.7 |
}}; |
1099 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1100 |
dl |
1.1 |
} |
1101 |
|
|
|
1102 |
jsr166 |
1.2 |
/** |
1103 |
jsr166 |
1.19 |
* pollTask returns an unexecuted task without executing it |
1104 |
dl |
1.1 |
*/ |
1105 |
|
|
public void testPollTask() { |
1106 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1107 |
|
|
public void realCompute() { |
1108 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1109 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1110 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1111 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1112 |
|
|
assertSame(f, pollTask()); |
1113 |
jsr166 |
1.7 |
helpQuiesce(); |
1114 |
jsr166 |
1.19 |
checkNotDone(f); |
1115 |
|
|
checkCompletedNormally(g); |
1116 |
jsr166 |
1.7 |
}}; |
1117 |
jsr166 |
1.14 |
testInvokeOnPool(singletonPool(), a); |
1118 |
dl |
1.1 |
} |
1119 |
|
|
|
1120 |
jsr166 |
1.2 |
/** |
1121 |
dl |
1.1 |
* peekNextLocalTask returns least recent unexecuted task in async mode |
1122 |
|
|
*/ |
1123 |
|
|
public void testPeekNextLocalTaskAsync() { |
1124 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1125 |
|
|
public void realCompute() { |
1126 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1127 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1128 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1129 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1130 |
|
|
assertSame(g, peekNextLocalTask()); |
1131 |
|
|
assertNull(f.join()); |
1132 |
jsr166 |
1.7 |
helpQuiesce(); |
1133 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
1134 |
|
|
checkCompletedNormally(g); |
1135 |
jsr166 |
1.7 |
}}; |
1136 |
jsr166 |
1.14 |
testInvokeOnPool(asyncSingletonPool(), a); |
1137 |
dl |
1.1 |
} |
1138 |
|
|
|
1139 |
jsr166 |
1.2 |
/** |
1140 |
jsr166 |
1.19 |
* pollNextLocalTask returns least recent unexecuted task without |
1141 |
|
|
* executing it, in async mode |
1142 |
dl |
1.1 |
*/ |
1143 |
|
|
public void testPollNextLocalTaskAsync() { |
1144 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1145 |
|
|
public void realCompute() { |
1146 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1147 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1148 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1149 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1150 |
|
|
assertSame(g, pollNextLocalTask()); |
1151 |
jsr166 |
1.7 |
helpQuiesce(); |
1152 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
1153 |
|
|
checkNotDone(g); |
1154 |
jsr166 |
1.7 |
}}; |
1155 |
jsr166 |
1.14 |
testInvokeOnPool(asyncSingletonPool(), a); |
1156 |
dl |
1.1 |
} |
1157 |
|
|
|
1158 |
jsr166 |
1.2 |
/** |
1159 |
jsr166 |
1.19 |
* pollTask returns an unexecuted task without executing it, in |
1160 |
|
|
* async mode |
1161 |
dl |
1.1 |
*/ |
1162 |
|
|
public void testPollTaskAsync() { |
1163 |
jsr166 |
1.18 |
RecursiveAction a = new CheckedRecursiveAction() { |
1164 |
|
|
public void realCompute() { |
1165 |
jsr166 |
1.7 |
FibAction g = new FibAction(9); |
1166 |
jsr166 |
1.18 |
assertSame(g, g.fork()); |
1167 |
jsr166 |
1.7 |
FibAction f = new FibAction(8); |
1168 |
jsr166 |
1.18 |
assertSame(f, f.fork()); |
1169 |
|
|
assertSame(g, pollTask()); |
1170 |
jsr166 |
1.7 |
helpQuiesce(); |
1171 |
jsr166 |
1.19 |
checkCompletedNormally(f); |
1172 |
|
|
checkNotDone(g); |
1173 |
jsr166 |
1.7 |
}}; |
1174 |
jsr166 |
1.14 |
testInvokeOnPool(asyncSingletonPool(), a); |
1175 |
dl |
1.1 |
} |
1176 |
|
|
|
1177 |
|
|
} |