3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
– |
import junit.framework.*; |
7 |
– |
import java.util.concurrent.*; |
8 |
– |
import java.util.*; |
6 |
|
|
7 |
+ |
import junit.framework.*; |
8 |
+ |
import java.util.concurrent.CancellationException; |
9 |
+ |
import java.util.concurrent.SynchronousQueue; |
10 |
+ |
import java.util.concurrent.ExecutionException; |
11 |
+ |
import java.util.concurrent.ForkJoinPool; |
12 |
+ |
import java.util.concurrent.ForkJoinTask; |
13 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
14 |
+ |
import java.util.concurrent.RecursiveAction; |
15 |
+ |
import java.util.concurrent.TimeUnit; |
16 |
+ |
import java.util.concurrent.TimeoutException; |
17 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
+ |
import java.util.HashSet; |
19 |
|
|
20 |
|
public class RecursiveActionTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
|
junit.textui.TestRunner.run(suite()); |
24 |
|
} |
25 |
+ |
|
26 |
|
public static Test suite() { |
27 |
|
return new TestSuite(RecursiveActionTest.class); |
28 |
|
} |
29 |
|
|
30 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
31 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
32 |
< |
static final ForkJoinPool asyncSingletonPool = |
33 |
< |
new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
34 |
< |
null, true); |
30 |
> |
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 |
> |
checkNotDone(a); |
47 |
> |
|
48 |
> |
assertNull(pool.invoke(a)); |
49 |
> |
|
50 |
> |
checkCompletedNormally(a); |
51 |
> |
} finally { |
52 |
> |
joinPool(pool); |
53 |
> |
} |
54 |
> |
} |
55 |
> |
|
56 |
> |
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 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
65 |
> |
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 |
> |
assertFalse(a.cancel(false)); |
96 |
> |
assertFalse(a.cancel(true)); |
97 |
> |
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 |
> |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
133 |
> |
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 |
> |
assertFalse(a.cancel(false)); |
140 |
> |
assertFalse(a.cancel(true)); |
141 |
> |
|
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 |
|
static final class FJException extends RuntimeException { |
165 |
|
FJException() { super(); } |
166 |
|
} |
167 |
|
|
168 |
|
// A simple recursive action for testing |
169 |
< |
static final class FibAction extends RecursiveAction { |
169 |
> |
final class FibAction extends CheckedRecursiveAction { |
170 |
|
final int number; |
171 |
|
int result; |
172 |
|
FibAction(int n) { number = n; } |
173 |
< |
public void compute() { |
173 |
> |
public void realCompute() { |
174 |
|
int n = number; |
175 |
|
if (n <= 1) |
176 |
|
result = n; |
205 |
|
* invoke returns when task completes normally. |
206 |
|
* isCompletedAbnormally and isCancelled return false for normally |
207 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
70 |
– |
* |
208 |
|
*/ |
209 |
|
public void testInvoke() { |
210 |
< |
RecursiveAction a = new RecursiveAction() { |
211 |
< |
public void compute() { |
210 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
211 |
> |
public void realCompute() { |
212 |
|
FibAction f = new FibAction(8); |
213 |
< |
f.invoke(); |
214 |
< |
threadAssertTrue(f.result == 21); |
215 |
< |
threadAssertTrue(f.isDone()); |
79 |
< |
threadAssertFalse(f.isCancelled()); |
80 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
81 |
< |
threadAssertTrue(f.getRawResult() == null); |
213 |
> |
assertNull(f.invoke()); |
214 |
> |
assertEquals(21, f.result); |
215 |
> |
checkCompletedNormally(f); |
216 |
|
}}; |
217 |
< |
mainPool.invoke(a); |
217 |
> |
testInvokeOnPool(mainPool(), a); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
223 |
|
* completed tasks |
224 |
|
*/ |
225 |
|
public void testQuietlyInvoke() { |
226 |
< |
RecursiveAction a = new RecursiveAction() { |
227 |
< |
public void compute() { |
226 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
227 |
> |
public void realCompute() { |
228 |
|
FibAction f = new FibAction(8); |
229 |
|
f.quietlyInvoke(); |
230 |
< |
threadAssertTrue(f.result == 21); |
231 |
< |
threadAssertTrue(f.isDone()); |
98 |
< |
threadAssertFalse(f.isCancelled()); |
99 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
100 |
< |
threadAssertTrue(f.getRawResult() == null); |
230 |
> |
assertEquals(21, f.result); |
231 |
> |
checkCompletedNormally(f); |
232 |
|
}}; |
233 |
< |
mainPool.invoke(a); |
233 |
> |
testInvokeOnPool(mainPool(), a); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
|
* join of a forked task returns when task completes |
238 |
|
*/ |
239 |
|
public void testForkJoin() { |
240 |
< |
RecursiveAction a = new RecursiveAction() { |
241 |
< |
public void compute() { |
240 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
241 |
> |
public void realCompute() { |
242 |
|
FibAction f = new FibAction(8); |
243 |
< |
f.fork(); |
244 |
< |
f.join(); |
245 |
< |
threadAssertTrue(f.result == 21); |
246 |
< |
threadAssertTrue(f.isDone()); |
116 |
< |
threadAssertTrue(f.getRawResult() == null); |
243 |
> |
assertSame(f, f.fork()); |
244 |
> |
assertNull(f.join()); |
245 |
> |
assertEquals(21, f.result); |
246 |
> |
checkCompletedNormally(f); |
247 |
|
}}; |
248 |
< |
mainPool.invoke(a); |
248 |
> |
testInvokeOnPool(mainPool(), a); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
< |
* get of a forked task returns when task completes |
252 |
> |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
253 |
|
*/ |
254 |
< |
public void testForkGet() { |
255 |
< |
RecursiveAction a = new RecursiveAction() { |
256 |
< |
public void compute() { |
254 |
> |
public void testJoinIgnoresInterrupts() { |
255 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
256 |
> |
public void realCompute() { |
257 |
> |
FibAction f = new FibAction(8); |
258 |
> |
final Thread myself = Thread.currentThread(); |
259 |
> |
|
260 |
> |
// test join() |
261 |
> |
assertSame(f, f.fork()); |
262 |
> |
myself.interrupt(); |
263 |
> |
assertTrue(myself.isInterrupted()); |
264 |
> |
assertNull(f.join()); |
265 |
> |
Thread.interrupted(); |
266 |
> |
assertEquals(21, f.result); |
267 |
> |
checkCompletedNormally(f); |
268 |
> |
|
269 |
> |
f.reinitialize(); |
270 |
> |
f.cancel(true); |
271 |
> |
assertSame(f, f.fork()); |
272 |
> |
myself.interrupt(); |
273 |
> |
assertTrue(myself.isInterrupted()); |
274 |
|
try { |
275 |
< |
FibAction f = new FibAction(8); |
276 |
< |
f.fork(); |
277 |
< |
f.get(); |
278 |
< |
threadAssertTrue(f.result == 21); |
279 |
< |
threadAssertTrue(f.isDone()); |
133 |
< |
} catch (Exception ex) { |
134 |
< |
unexpectedException(ex); |
275 |
> |
f.join(); |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (CancellationException success) { |
278 |
> |
Thread.interrupted(); |
279 |
> |
checkCancelled(f); |
280 |
|
} |
281 |
+ |
|
282 |
+ |
f.reinitialize(); |
283 |
+ |
f.completeExceptionally(new FJException()); |
284 |
+ |
assertSame(f, f.fork()); |
285 |
+ |
myself.interrupt(); |
286 |
+ |
assertTrue(myself.isInterrupted()); |
287 |
+ |
try { |
288 |
+ |
f.join(); |
289 |
+ |
shouldThrow(); |
290 |
+ |
} catch (FJException success) { |
291 |
+ |
Thread.interrupted(); |
292 |
+ |
checkCompletedAbnormally(f, success); |
293 |
+ |
} |
294 |
+ |
|
295 |
+ |
// test quietlyJoin() |
296 |
+ |
f.reinitialize(); |
297 |
+ |
assertSame(f, f.fork()); |
298 |
+ |
myself.interrupt(); |
299 |
+ |
assertTrue(myself.isInterrupted()); |
300 |
+ |
f.quietlyJoin(); |
301 |
+ |
Thread.interrupted(); |
302 |
+ |
assertEquals(21, f.result); |
303 |
+ |
checkCompletedNormally(f); |
304 |
+ |
|
305 |
+ |
f.reinitialize(); |
306 |
+ |
f.cancel(true); |
307 |
+ |
assertSame(f, f.fork()); |
308 |
+ |
myself.interrupt(); |
309 |
+ |
assertTrue(myself.isInterrupted()); |
310 |
+ |
f.quietlyJoin(); |
311 |
+ |
Thread.interrupted(); |
312 |
+ |
checkCancelled(f); |
313 |
+ |
|
314 |
+ |
f.reinitialize(); |
315 |
+ |
f.completeExceptionally(new FJException()); |
316 |
+ |
assertSame(f, f.fork()); |
317 |
+ |
myself.interrupt(); |
318 |
+ |
assertTrue(myself.isInterrupted()); |
319 |
+ |
f.quietlyJoin(); |
320 |
+ |
Thread.interrupted(); |
321 |
+ |
checkCompletedAbnormally(f, f.getException()); |
322 |
|
}}; |
323 |
< |
mainPool.invoke(a); |
323 |
> |
testInvokeOnPool(mainPool(), a); |
324 |
> |
a.reinitialize(); |
325 |
> |
testInvokeOnPool(singletonPool(), a); |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* timed get of a forked task returns when task completes |
329 |
> |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
330 |
> |
* succeeds in the presence of interrupts |
331 |
|
*/ |
332 |
< |
public void testForkTimedGet() { |
333 |
< |
RecursiveAction a = new RecursiveAction() { |
334 |
< |
public void compute() { |
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 |
> |
final Thread myself = Thread.currentThread(); |
359 |
> |
|
360 |
> |
// test join() ------------ |
361 |
> |
|
362 |
> |
f = fibActions[0]; |
363 |
> |
assertFalse(ForkJoinTask.inForkJoinPool()); |
364 |
> |
myself.interrupt(); |
365 |
> |
assertTrue(myself.isInterrupted()); |
366 |
> |
assertNull(f.join()); |
367 |
> |
assertTrue(Thread.interrupted()); |
368 |
> |
assertEquals(21, f.result); |
369 |
> |
checkCompletedNormally(f); |
370 |
> |
|
371 |
> |
f = fibActions[1]; |
372 |
> |
myself.interrupt(); |
373 |
> |
assertTrue(myself.isInterrupted()); |
374 |
|
try { |
375 |
< |
FibAction f = new FibAction(8); |
376 |
< |
f.fork(); |
377 |
< |
f.get(5L, TimeUnit.SECONDS); |
378 |
< |
threadAssertTrue(f.result == 21); |
379 |
< |
threadAssertTrue(f.isDone()); |
152 |
< |
} catch (Exception ex) { |
153 |
< |
unexpectedException(ex); |
375 |
> |
f.join(); |
376 |
> |
shouldThrow(); |
377 |
> |
} catch (CancellationException success) { |
378 |
> |
assertTrue(Thread.interrupted()); |
379 |
> |
checkCancelled(f); |
380 |
|
} |
381 |
+ |
|
382 |
+ |
f = fibActions[2]; |
383 |
+ |
myself.interrupt(); |
384 |
+ |
assertTrue(myself.isInterrupted()); |
385 |
+ |
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 |
+ |
myself.interrupt(); |
397 |
+ |
assertTrue(myself.isInterrupted()); |
398 |
+ |
f.quietlyJoin(); |
399 |
+ |
assertTrue(Thread.interrupted()); |
400 |
+ |
assertEquals(21, f.result); |
401 |
+ |
checkCompletedNormally(f); |
402 |
+ |
|
403 |
+ |
f = fibActions[4]; |
404 |
+ |
myself.interrupt(); |
405 |
+ |
assertTrue(myself.isInterrupted()); |
406 |
+ |
f.quietlyJoin(); |
407 |
+ |
assertTrue(Thread.interrupted()); |
408 |
+ |
checkCancelled(f); |
409 |
+ |
|
410 |
+ |
f = fibActions[5]; |
411 |
+ |
myself.interrupt(); |
412 |
+ |
assertTrue(myself.isInterrupted()); |
413 |
+ |
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 |
+ |
* get of a forked task returns when task completes |
433 |
+ |
*/ |
434 |
+ |
public void testForkGet() { |
435 |
+ |
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 |
+ |
checkCompletedNormally(f); |
442 |
+ |
}}; |
443 |
+ |
testInvokeOnPool(mainPool(), a); |
444 |
+ |
} |
445 |
+ |
|
446 |
+ |
/** |
447 |
+ |
* timed get of a forked task returns when task completes |
448 |
+ |
*/ |
449 |
+ |
public void testForkTimedGet() { |
450 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
451 |
+ |
public void realCompute() throws Exception { |
452 |
+ |
FibAction f = new FibAction(8); |
453 |
+ |
assertSame(f, f.fork()); |
454 |
+ |
assertNull(f.get(5L, SECONDS)); |
455 |
+ |
assertEquals(21, f.result); |
456 |
+ |
checkCompletedNormally(f); |
457 |
|
}}; |
458 |
< |
mainPool.invoke(a); |
458 |
> |
testInvokeOnPool(mainPool(), a); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
|
* timed get with null time unit throws NPE |
463 |
|
*/ |
464 |
|
public void testForkTimedGetNPE() { |
465 |
< |
RecursiveAction a = new RecursiveAction() { |
466 |
< |
public void compute() { |
465 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
466 |
> |
public void realCompute() throws Exception { |
467 |
> |
FibAction f = new FibAction(8); |
468 |
> |
assertSame(f, f.fork()); |
469 |
|
try { |
166 |
– |
FibAction f = new FibAction(8); |
167 |
– |
f.fork(); |
470 |
|
f.get(5L, null); |
471 |
|
shouldThrow(); |
472 |
< |
} catch (NullPointerException success) { |
171 |
< |
} catch (Exception ex) { |
172 |
< |
unexpectedException(ex); |
173 |
< |
} |
472 |
> |
} catch (NullPointerException success) {} |
473 |
|
}}; |
474 |
< |
mainPool.invoke(a); |
474 |
> |
testInvokeOnPool(mainPool(), a); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
|
* quietlyJoin of a forked task returns when task completes |
479 |
|
*/ |
480 |
|
public void testForkQuietlyJoin() { |
481 |
< |
RecursiveAction a = new RecursiveAction() { |
482 |
< |
public void compute() { |
481 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
482 |
> |
public void realCompute() { |
483 |
|
FibAction f = new FibAction(8); |
484 |
< |
f.fork(); |
484 |
> |
assertSame(f, f.fork()); |
485 |
|
f.quietlyJoin(); |
486 |
< |
threadAssertTrue(f.result == 21); |
487 |
< |
threadAssertTrue(f.isDone()); |
486 |
> |
assertEquals(21, f.result); |
487 |
> |
checkCompletedNormally(f); |
488 |
|
}}; |
489 |
< |
mainPool.invoke(a); |
489 |
> |
testInvokeOnPool(mainPool(), a); |
490 |
|
} |
491 |
|
|
492 |
|
|
495 |
|
* getQueuedTaskCount returns 0 when quiescent |
496 |
|
*/ |
497 |
|
public void testForkHelpQuiesce() { |
498 |
< |
RecursiveAction a = new RecursiveAction() { |
499 |
< |
public void compute() { |
498 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
499 |
> |
public void realCompute() { |
500 |
|
FibAction f = new FibAction(8); |
501 |
< |
f.fork(); |
501 |
> |
assertSame(f, f.fork()); |
502 |
|
f.helpQuiesce(); |
503 |
< |
threadAssertTrue(f.result == 21); |
504 |
< |
threadAssertTrue(f.isDone()); |
505 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
503 |
> |
assertEquals(21, f.result); |
504 |
> |
assertEquals(0, getQueuedTaskCount()); |
505 |
> |
checkCompletedNormally(f); |
506 |
|
}}; |
507 |
< |
mainPool.invoke(a); |
507 |
> |
testInvokeOnPool(mainPool(), a); |
508 |
|
} |
509 |
|
|
510 |
|
|
512 |
|
* invoke task throws exception when task completes abnormally |
513 |
|
*/ |
514 |
|
public void testAbnormalInvoke() { |
515 |
< |
RecursiveAction a = new RecursiveAction() { |
516 |
< |
public void compute() { |
515 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
516 |
> |
public void realCompute() { |
517 |
> |
FailingFibAction f = new FailingFibAction(8); |
518 |
|
try { |
219 |
– |
FailingFibAction f = new FailingFibAction(8); |
519 |
|
f.invoke(); |
520 |
|
shouldThrow(); |
521 |
|
} catch (FJException success) { |
522 |
+ |
checkCompletedAbnormally(f, success); |
523 |
|
} |
524 |
|
}}; |
525 |
< |
mainPool.invoke(a); |
525 |
> |
testInvokeOnPool(mainPool(), a); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
|
* quietlyInvoke task returns when task completes abnormally |
530 |
|
*/ |
531 |
|
public void testAbnormalQuietlyInvoke() { |
532 |
< |
RecursiveAction a = new RecursiveAction() { |
533 |
< |
public void compute() { |
532 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
533 |
> |
public void realCompute() { |
534 |
|
FailingFibAction f = new FailingFibAction(8); |
535 |
|
f.quietlyInvoke(); |
536 |
< |
threadAssertTrue(f.isDone()); |
536 |
> |
assertTrue(f.getException() instanceof FJException); |
537 |
> |
checkCompletedAbnormally(f, f.getException()); |
538 |
|
}}; |
539 |
< |
mainPool.invoke(a); |
539 |
> |
testInvokeOnPool(mainPool(), a); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
|
* join of a forked task throws exception when task completes abnormally |
544 |
|
*/ |
545 |
|
public void testAbnormalForkJoin() { |
546 |
< |
RecursiveAction a = new RecursiveAction() { |
547 |
< |
public void compute() { |
546 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
547 |
> |
public void realCompute() { |
548 |
> |
FailingFibAction f = new FailingFibAction(8); |
549 |
> |
assertSame(f, f.fork()); |
550 |
|
try { |
248 |
– |
FailingFibAction f = new FailingFibAction(8); |
249 |
– |
f.fork(); |
551 |
|
f.join(); |
552 |
|
shouldThrow(); |
553 |
|
} catch (FJException success) { |
554 |
+ |
checkCompletedAbnormally(f, success); |
555 |
|
} |
556 |
|
}}; |
557 |
< |
mainPool.invoke(a); |
557 |
> |
testInvokeOnPool(mainPool(), a); |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
|
* get of a forked task throws exception when task completes abnormally |
562 |
|
*/ |
563 |
|
public void testAbnormalForkGet() { |
564 |
< |
RecursiveAction a = new RecursiveAction() { |
565 |
< |
public void compute() { |
564 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
565 |
> |
public void realCompute() throws Exception { |
566 |
> |
FailingFibAction f = new FailingFibAction(8); |
567 |
> |
assertSame(f, f.fork()); |
568 |
|
try { |
265 |
– |
FailingFibAction f = new FailingFibAction(8); |
266 |
– |
f.fork(); |
569 |
|
f.get(); |
570 |
|
shouldThrow(); |
571 |
|
} catch (ExecutionException success) { |
572 |
< |
} catch (Exception ex) { |
573 |
< |
unexpectedException(ex); |
572 |
> |
Throwable cause = success.getCause(); |
573 |
> |
assertTrue(cause instanceof FJException); |
574 |
> |
checkCompletedAbnormally(f, cause); |
575 |
|
} |
576 |
|
}}; |
577 |
< |
mainPool.invoke(a); |
577 |
> |
testInvokeOnPool(mainPool(), a); |
578 |
|
} |
579 |
|
|
580 |
|
/** |
581 |
|
* timed get of a forked task throws exception when task completes abnormally |
582 |
|
*/ |
583 |
|
public void testAbnormalForkTimedGet() { |
584 |
< |
RecursiveAction a = new RecursiveAction() { |
585 |
< |
public void compute() { |
584 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
585 |
> |
public void realCompute() throws Exception { |
586 |
> |
FailingFibAction f = new FailingFibAction(8); |
587 |
> |
assertSame(f, f.fork()); |
588 |
|
try { |
284 |
– |
FailingFibAction f = new FailingFibAction(8); |
285 |
– |
f.fork(); |
589 |
|
f.get(5L, TimeUnit.SECONDS); |
590 |
|
shouldThrow(); |
591 |
|
} catch (ExecutionException success) { |
592 |
< |
} catch (Exception ex) { |
593 |
< |
unexpectedException(ex); |
592 |
> |
Throwable cause = success.getCause(); |
593 |
> |
assertTrue(cause instanceof FJException); |
594 |
> |
checkCompletedAbnormally(f, cause); |
595 |
|
} |
596 |
|
}}; |
597 |
< |
mainPool.invoke(a); |
597 |
> |
testInvokeOnPool(mainPool(), a); |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
602 |
|
*/ |
603 |
|
public void testAbnormalForkQuietlyJoin() { |
604 |
< |
RecursiveAction a = new RecursiveAction() { |
605 |
< |
public void compute() { |
604 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
605 |
> |
public void realCompute() { |
606 |
|
FailingFibAction f = new FailingFibAction(8); |
607 |
< |
f.fork(); |
607 |
> |
assertSame(f, f.fork()); |
608 |
|
f.quietlyJoin(); |
609 |
< |
threadAssertTrue(f.isDone()); |
610 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
307 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
609 |
> |
assertTrue(f.getException() instanceof FJException); |
610 |
> |
checkCompletedAbnormally(f, f.getException()); |
611 |
|
}}; |
612 |
< |
mainPool.invoke(a); |
612 |
> |
testInvokeOnPool(mainPool(), a); |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
|
* invoke task throws exception when task cancelled |
617 |
|
*/ |
618 |
|
public void testCancelledInvoke() { |
619 |
< |
RecursiveAction a = new RecursiveAction() { |
620 |
< |
public void compute() { |
619 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
620 |
> |
public void realCompute() { |
621 |
> |
FibAction f = new FibAction(8); |
622 |
> |
assertTrue(f.cancel(true)); |
623 |
|
try { |
319 |
– |
FibAction f = new FibAction(8); |
320 |
– |
f.cancel(true); |
624 |
|
f.invoke(); |
625 |
|
shouldThrow(); |
626 |
|
} catch (CancellationException success) { |
627 |
+ |
checkCancelled(f); |
628 |
|
} |
629 |
|
}}; |
630 |
< |
mainPool.invoke(a); |
630 |
> |
testInvokeOnPool(mainPool(), a); |
631 |
|
} |
632 |
|
|
633 |
|
/** |
634 |
|
* join of a forked task throws exception when task cancelled |
635 |
|
*/ |
636 |
|
public void testCancelledForkJoin() { |
637 |
< |
RecursiveAction a = new RecursiveAction() { |
638 |
< |
public void compute() { |
637 |
> |
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 |
|
try { |
336 |
– |
FibAction f = new FibAction(8); |
337 |
– |
f.cancel(true); |
338 |
– |
f.fork(); |
643 |
|
f.join(); |
644 |
|
shouldThrow(); |
645 |
|
} catch (CancellationException success) { |
646 |
+ |
checkCancelled(f); |
647 |
|
} |
648 |
|
}}; |
649 |
< |
mainPool.invoke(a); |
649 |
> |
testInvokeOnPool(mainPool(), a); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
|
* get of a forked task throws exception when task cancelled |
654 |
|
*/ |
655 |
|
public void testCancelledForkGet() { |
656 |
< |
RecursiveAction a = new RecursiveAction() { |
657 |
< |
public void compute() { |
656 |
> |
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 |
|
try { |
354 |
– |
FibAction f = new FibAction(8); |
355 |
– |
f.cancel(true); |
356 |
– |
f.fork(); |
662 |
|
f.get(); |
663 |
|
shouldThrow(); |
664 |
|
} catch (CancellationException success) { |
665 |
< |
} catch (Exception ex) { |
361 |
< |
unexpectedException(ex); |
665 |
> |
checkCancelled(f); |
666 |
|
} |
667 |
|
}}; |
668 |
< |
mainPool.invoke(a); |
668 |
> |
testInvokeOnPool(mainPool(), a); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
|
* timed get of a forked task throws exception when task cancelled |
673 |
|
*/ |
674 |
|
public void testCancelledForkTimedGet() { |
675 |
< |
RecursiveAction a = new RecursiveAction() { |
676 |
< |
public void compute() { |
675 |
> |
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 |
|
try { |
681 |
< |
FibAction f = new FibAction(8); |
375 |
< |
f.cancel(true); |
376 |
< |
f.fork(); |
377 |
< |
f.get(5L, TimeUnit.SECONDS); |
681 |
> |
f.get(5L, SECONDS); |
682 |
|
shouldThrow(); |
683 |
|
} catch (CancellationException success) { |
684 |
< |
} catch (Exception ex) { |
381 |
< |
unexpectedException(ex); |
684 |
> |
checkCancelled(f); |
685 |
|
} |
686 |
|
}}; |
687 |
< |
mainPool.invoke(a); |
687 |
> |
testInvokeOnPool(mainPool(), a); |
688 |
|
} |
689 |
|
|
690 |
|
/** |
691 |
|
* quietlyJoin of a forked task returns when task cancelled |
692 |
|
*/ |
693 |
|
public void testCancelledForkQuietlyJoin() { |
694 |
< |
RecursiveAction a = new RecursiveAction() { |
695 |
< |
public void compute() { |
694 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
695 |
> |
public void realCompute() { |
696 |
|
FibAction f = new FibAction(8); |
697 |
< |
f.cancel(true); |
698 |
< |
f.fork(); |
697 |
> |
assertTrue(f.cancel(true)); |
698 |
> |
assertSame(f, f.fork()); |
699 |
|
f.quietlyJoin(); |
700 |
< |
threadAssertTrue(f.isDone()); |
398 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
399 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
700 |
> |
checkCancelled(f); |
701 |
|
}}; |
702 |
< |
mainPool.invoke(a); |
702 |
> |
testInvokeOnPool(mainPool(), a); |
703 |
|
} |
704 |
|
|
705 |
|
/** |
706 |
|
* getPool of executing task returns its pool |
707 |
|
*/ |
708 |
|
public void testGetPool() { |
709 |
< |
RecursiveAction a = new RecursiveAction() { |
710 |
< |
public void compute() { |
711 |
< |
threadAssertTrue(getPool() == mainPool); |
709 |
> |
final ForkJoinPool mainPool = mainPool(); |
710 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
711 |
> |
public void realCompute() { |
712 |
> |
assertSame(mainPool, getPool()); |
713 |
|
}}; |
714 |
< |
mainPool.invoke(a); |
714 |
> |
testInvokeOnPool(mainPool, a); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* getPool of non-FJ task returns null |
719 |
|
*/ |
720 |
|
public void testGetPool2() { |
721 |
< |
RecursiveAction a = new RecursiveAction() { |
722 |
< |
public void compute() { |
723 |
< |
threadAssertTrue(getPool() == null); |
721 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
722 |
> |
public void realCompute() { |
723 |
> |
assertNull(getPool()); |
724 |
|
}}; |
725 |
< |
a.invoke(); |
725 |
> |
assertNull(a.invoke()); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
|
* inForkJoinPool of executing task returns true |
730 |
|
*/ |
731 |
|
public void testInForkJoinPool() { |
732 |
< |
RecursiveAction a = new RecursiveAction() { |
733 |
< |
public void compute() { |
734 |
< |
threadAssertTrue(inForkJoinPool()); |
732 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
733 |
> |
public void realCompute() { |
734 |
> |
assertTrue(inForkJoinPool()); |
735 |
|
}}; |
736 |
< |
mainPool.invoke(a); |
736 |
> |
testInvokeOnPool(mainPool(), a); |
737 |
|
} |
738 |
|
|
739 |
|
/** |
740 |
|
* inForkJoinPool of non-FJ task returns false |
741 |
|
*/ |
742 |
|
public void testInForkJoinPool2() { |
743 |
< |
RecursiveAction a = new RecursiveAction() { |
744 |
< |
public void compute() { |
745 |
< |
threadAssertTrue(!inForkJoinPool()); |
743 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
744 |
> |
public void realCompute() { |
745 |
> |
assertFalse(inForkJoinPool()); |
746 |
|
}}; |
747 |
< |
a.invoke(); |
747 |
> |
assertNull(a.invoke()); |
748 |
|
} |
749 |
|
|
750 |
|
/** |
751 |
|
* getPool of current thread in pool returns its pool |
752 |
|
*/ |
753 |
|
public void testWorkerGetPool() { |
754 |
< |
RecursiveAction a = new RecursiveAction() { |
755 |
< |
public void compute() { |
754 |
> |
final ForkJoinPool mainPool = mainPool(); |
755 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
756 |
> |
public void realCompute() { |
757 |
|
ForkJoinWorkerThread w = |
758 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
759 |
< |
threadAssertTrue(w.getPool() == mainPool); |
758 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
759 |
> |
assertSame(mainPool, w.getPool()); |
760 |
|
}}; |
761 |
< |
mainPool.invoke(a); |
761 |
> |
testInvokeOnPool(mainPool, a); |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
|
* getPoolIndex of current thread in pool returns 0 <= value < poolSize |
463 |
– |
* |
766 |
|
*/ |
767 |
|
public void testWorkerGetPoolIndex() { |
768 |
< |
RecursiveAction a = new RecursiveAction() { |
769 |
< |
public void compute() { |
768 |
> |
final ForkJoinPool mainPool = mainPool(); |
769 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
> |
public void realCompute() { |
771 |
|
ForkJoinWorkerThread w = |
772 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
773 |
< |
int idx = w.getPoolIndex(); |
774 |
< |
threadAssertTrue(idx >= 0); |
472 |
< |
threadAssertTrue(idx < mainPool.getPoolSize()); |
772 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
773 |
> |
assertTrue(w.getPoolIndex() >= 0); |
774 |
> |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
775 |
|
}}; |
776 |
< |
mainPool.invoke(a); |
776 |
> |
testInvokeOnPool(mainPool, a); |
777 |
|
} |
778 |
|
|
779 |
|
|
781 |
|
* setRawResult(null) succeeds |
782 |
|
*/ |
783 |
|
public void testSetRawResult() { |
784 |
< |
RecursiveAction a = new RecursiveAction() { |
785 |
< |
public void compute() { |
784 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
785 |
> |
public void realCompute() { |
786 |
|
setRawResult(null); |
787 |
+ |
assertNull(getRawResult()); |
788 |
|
}}; |
789 |
< |
a.invoke(); |
789 |
> |
assertNull(a.invoke()); |
790 |
|
} |
791 |
|
|
792 |
|
/** |
793 |
|
* A reinitialized task may be re-invoked |
794 |
|
*/ |
795 |
|
public void testReinitialize() { |
796 |
< |
RecursiveAction a = new RecursiveAction() { |
797 |
< |
public void compute() { |
796 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
797 |
> |
public void realCompute() { |
798 |
|
FibAction f = new FibAction(8); |
799 |
< |
f.invoke(); |
800 |
< |
threadAssertTrue(f.result == 21); |
801 |
< |
threadAssertTrue(f.isDone()); |
802 |
< |
threadAssertFalse(f.isCancelled()); |
803 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
804 |
< |
f.reinitialize(); |
805 |
< |
f.invoke(); |
806 |
< |
threadAssertTrue(f.result == 21); |
799 |
> |
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 |
|
}}; |
809 |
< |
mainPool.invoke(a); |
809 |
> |
testInvokeOnPool(mainPool(), a); |
810 |
|
} |
811 |
|
|
812 |
|
/** |
813 |
|
* invoke task throws exception after invoking completeExceptionally |
814 |
|
*/ |
815 |
|
public void testCompleteExceptionally() { |
816 |
< |
RecursiveAction a = new RecursiveAction() { |
817 |
< |
public void compute() { |
816 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
817 |
> |
public void realCompute() { |
818 |
> |
FibAction f = new FibAction(8); |
819 |
> |
f.completeExceptionally(new FJException()); |
820 |
|
try { |
515 |
– |
FibAction f = new FibAction(8); |
516 |
– |
f.completeExceptionally(new FJException()); |
821 |
|
f.invoke(); |
822 |
|
shouldThrow(); |
823 |
|
} catch (FJException success) { |
824 |
+ |
checkCompletedAbnormally(f, success); |
825 |
|
} |
826 |
|
}}; |
827 |
< |
mainPool.invoke(a); |
827 |
> |
testInvokeOnPool(mainPool(), a); |
828 |
|
} |
829 |
|
|
830 |
|
/** |
831 |
|
* invoke task suppresses execution invoking complete |
832 |
|
*/ |
833 |
|
public void testComplete() { |
834 |
< |
RecursiveAction a = new RecursiveAction() { |
835 |
< |
public void compute() { |
834 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
835 |
> |
public void realCompute() { |
836 |
|
FibAction f = new FibAction(8); |
837 |
|
f.complete(null); |
838 |
< |
f.invoke(); |
839 |
< |
threadAssertTrue(f.isDone()); |
840 |
< |
threadAssertTrue(f.result == 0); |
838 |
> |
assertNull(f.invoke()); |
839 |
> |
assertEquals(0, f.result); |
840 |
> |
checkCompletedNormally(f); |
841 |
|
}}; |
842 |
< |
mainPool.invoke(a); |
842 |
> |
testInvokeOnPool(mainPool(), a); |
843 |
|
} |
844 |
|
|
845 |
|
/** |
846 |
|
* invokeAll(t1, t2) invokes all task arguments |
847 |
|
*/ |
848 |
|
public void testInvokeAll2() { |
849 |
< |
RecursiveAction a = new RecursiveAction() { |
850 |
< |
public void compute() { |
849 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
850 |
> |
public void realCompute() { |
851 |
|
FibAction f = new FibAction(8); |
852 |
|
FibAction g = new FibAction(9); |
853 |
|
invokeAll(f, g); |
854 |
< |
threadAssertTrue(f.isDone()); |
855 |
< |
threadAssertTrue(f.result == 21); |
856 |
< |
threadAssertTrue(g.isDone()); |
857 |
< |
threadAssertTrue(g.result == 34); |
854 |
> |
checkCompletedNormally(f); |
855 |
> |
assertEquals(21, f.result); |
856 |
> |
checkCompletedNormally(g); |
857 |
> |
assertEquals(34, g.result); |
858 |
|
}}; |
859 |
< |
mainPool.invoke(a); |
859 |
> |
testInvokeOnPool(mainPool(), a); |
860 |
|
} |
861 |
|
|
862 |
|
/** |
863 |
|
* invokeAll(tasks) with 1 argument invokes task |
864 |
|
*/ |
865 |
|
public void testInvokeAll1() { |
866 |
< |
RecursiveAction a = new RecursiveAction() { |
867 |
< |
public void compute() { |
866 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
867 |
> |
public void realCompute() { |
868 |
|
FibAction f = new FibAction(8); |
869 |
|
invokeAll(f); |
870 |
< |
threadAssertTrue(f.isDone()); |
871 |
< |
threadAssertTrue(f.result == 21); |
870 |
> |
checkCompletedNormally(f); |
871 |
> |
assertEquals(21, f.result); |
872 |
|
}}; |
873 |
< |
mainPool.invoke(a); |
873 |
> |
testInvokeOnPool(mainPool(), a); |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
878 |
|
*/ |
879 |
|
public void testInvokeAll3() { |
880 |
< |
RecursiveAction a = new RecursiveAction() { |
881 |
< |
public void compute() { |
880 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
881 |
> |
public void realCompute() { |
882 |
|
FibAction f = new FibAction(8); |
883 |
|
FibAction g = new FibAction(9); |
884 |
|
FibAction h = new FibAction(7); |
885 |
|
invokeAll(f, g, h); |
886 |
< |
threadAssertTrue(f.isDone()); |
887 |
< |
threadAssertTrue(f.result == 21); |
888 |
< |
threadAssertTrue(g.isDone()); |
889 |
< |
threadAssertTrue(g.result == 34); |
890 |
< |
threadAssertTrue(h.isDone()); |
891 |
< |
threadAssertTrue(h.result == 13); |
886 |
> |
assertTrue(f.isDone()); |
887 |
> |
assertTrue(g.isDone()); |
888 |
> |
assertTrue(h.isDone()); |
889 |
> |
checkCompletedNormally(f); |
890 |
> |
assertEquals(21, f.result); |
891 |
> |
checkCompletedNormally(g); |
892 |
> |
assertEquals(34, g.result); |
893 |
> |
checkCompletedNormally(g); |
894 |
> |
assertEquals(13, h.result); |
895 |
|
}}; |
896 |
< |
mainPool.invoke(a); |
896 |
> |
testInvokeOnPool(mainPool(), a); |
897 |
|
} |
898 |
|
|
899 |
|
/** |
900 |
|
* invokeAll(collection) invokes all tasks in the collection |
901 |
|
*/ |
902 |
|
public void testInvokeAllCollection() { |
903 |
< |
RecursiveAction a = new RecursiveAction() { |
904 |
< |
public void compute() { |
903 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
904 |
> |
public void realCompute() { |
905 |
|
FibAction f = new FibAction(8); |
906 |
|
FibAction g = new FibAction(9); |
907 |
|
FibAction h = new FibAction(7); |
910 |
|
set.add(g); |
911 |
|
set.add(h); |
912 |
|
invokeAll(set); |
913 |
< |
threadAssertTrue(f.isDone()); |
914 |
< |
threadAssertTrue(f.result == 21); |
915 |
< |
threadAssertTrue(g.isDone()); |
916 |
< |
threadAssertTrue(g.result == 34); |
917 |
< |
threadAssertTrue(h.isDone()); |
918 |
< |
threadAssertTrue(h.result == 13); |
913 |
> |
assertTrue(f.isDone()); |
914 |
> |
assertTrue(g.isDone()); |
915 |
> |
assertTrue(h.isDone()); |
916 |
> |
checkCompletedNormally(f); |
917 |
> |
assertEquals(21, f.result); |
918 |
> |
checkCompletedNormally(g); |
919 |
> |
assertEquals(34, g.result); |
920 |
> |
checkCompletedNormally(g); |
921 |
> |
assertEquals(13, h.result); |
922 |
|
}}; |
923 |
< |
mainPool.invoke(a); |
923 |
> |
testInvokeOnPool(mainPool(), a); |
924 |
|
} |
925 |
|
|
926 |
|
|
928 |
|
* invokeAll(tasks) with any null task throws NPE |
929 |
|
*/ |
930 |
|
public void testInvokeAllNPE() { |
931 |
< |
RecursiveAction a = new RecursiveAction() { |
932 |
< |
public void compute() { |
931 |
> |
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 |
|
try { |
623 |
– |
FibAction f = new FibAction(8); |
624 |
– |
FibAction g = new FibAction(9); |
625 |
– |
FibAction h = null; |
937 |
|
invokeAll(f, g, h); |
938 |
|
shouldThrow(); |
939 |
< |
} catch (NullPointerException success) { |
629 |
< |
} |
939 |
> |
} catch (NullPointerException success) {} |
940 |
|
}}; |
941 |
< |
mainPool.invoke(a); |
941 |
> |
testInvokeOnPool(mainPool(), a); |
942 |
|
} |
943 |
|
|
944 |
|
/** |
945 |
|
* invokeAll(t1, t2) throw exception if any task does |
946 |
|
*/ |
947 |
|
public void testAbnormalInvokeAll2() { |
948 |
< |
RecursiveAction a = new RecursiveAction() { |
949 |
< |
public void compute() { |
948 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
949 |
> |
public void realCompute() { |
950 |
> |
FibAction f = new FibAction(8); |
951 |
> |
FailingFibAction g = new FailingFibAction(9); |
952 |
|
try { |
641 |
– |
FibAction f = new FibAction(8); |
642 |
– |
FailingFibAction g = new FailingFibAction(9); |
953 |
|
invokeAll(f, g); |
954 |
|
shouldThrow(); |
955 |
|
} catch (FJException success) { |
956 |
+ |
checkCompletedAbnormally(g, success); |
957 |
|
} |
958 |
|
}}; |
959 |
< |
mainPool.invoke(a); |
959 |
> |
testInvokeOnPool(mainPool(), a); |
960 |
|
} |
961 |
|
|
962 |
|
/** |
963 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
964 |
|
*/ |
965 |
|
public void testAbnormalInvokeAll1() { |
966 |
< |
RecursiveAction a = new RecursiveAction() { |
967 |
< |
public void compute() { |
966 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
967 |
> |
public void realCompute() { |
968 |
> |
FailingFibAction g = new FailingFibAction(9); |
969 |
|
try { |
658 |
– |
FailingFibAction g = new FailingFibAction(9); |
970 |
|
invokeAll(g); |
971 |
|
shouldThrow(); |
972 |
|
} catch (FJException success) { |
973 |
+ |
checkCompletedAbnormally(g, success); |
974 |
|
} |
975 |
|
}}; |
976 |
< |
mainPool.invoke(a); |
976 |
> |
testInvokeOnPool(mainPool(), a); |
977 |
|
} |
978 |
|
|
979 |
|
/** |
980 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
981 |
|
*/ |
982 |
|
public void testAbnormalInvokeAll3() { |
983 |
< |
RecursiveAction a = new RecursiveAction() { |
984 |
< |
public void compute() { |
983 |
> |
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 |
|
try { |
674 |
– |
FibAction f = new FibAction(8); |
675 |
– |
FailingFibAction g = new FailingFibAction(9); |
676 |
– |
FibAction h = new FibAction(7); |
989 |
|
invokeAll(f, g, h); |
990 |
|
shouldThrow(); |
991 |
|
} catch (FJException success) { |
992 |
+ |
checkCompletedAbnormally(g, success); |
993 |
|
} |
994 |
|
}}; |
995 |
< |
mainPool.invoke(a); |
995 |
> |
testInvokeOnPool(mainPool(), a); |
996 |
|
} |
997 |
|
|
998 |
|
/** |
999 |
|
* invokeAll(collection) throws exception if any task does |
1000 |
|
*/ |
1001 |
|
public void testAbnormalInvokeAllCollection() { |
1002 |
< |
RecursiveAction a = new RecursiveAction() { |
1003 |
< |
public void compute() { |
1002 |
> |
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 |
|
try { |
692 |
– |
FailingFibAction f = new FailingFibAction(8); |
693 |
– |
FibAction g = new FibAction(9); |
694 |
– |
FibAction h = new FibAction(7); |
695 |
– |
HashSet set = new HashSet(); |
696 |
– |
set.add(f); |
697 |
– |
set.add(g); |
698 |
– |
set.add(h); |
1012 |
|
invokeAll(set); |
1013 |
|
shouldThrow(); |
1014 |
|
} catch (FJException success) { |
1015 |
+ |
checkCompletedAbnormally(f, success); |
1016 |
|
} |
1017 |
|
}}; |
1018 |
< |
mainPool.invoke(a); |
1018 |
> |
testInvokeOnPool(mainPool(), a); |
1019 |
|
} |
1020 |
|
|
1021 |
|
/** |
1023 |
|
* and suppresses execution |
1024 |
|
*/ |
1025 |
|
public void testTryUnfork() { |
1026 |
< |
RecursiveAction a = new RecursiveAction() { |
1027 |
< |
public void compute() { |
1026 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1027 |
> |
public void realCompute() { |
1028 |
|
FibAction g = new FibAction(9); |
1029 |
< |
g.fork(); |
1029 |
> |
assertSame(g, g.fork()); |
1030 |
|
FibAction f = new FibAction(8); |
1031 |
< |
f.fork(); |
1032 |
< |
threadAssertTrue(f.tryUnfork()); |
1031 |
> |
assertSame(f, f.fork()); |
1032 |
> |
assertTrue(f.tryUnfork()); |
1033 |
|
helpQuiesce(); |
1034 |
< |
threadAssertFalse(f.isDone()); |
1035 |
< |
threadAssertTrue(g.isDone()); |
1034 |
> |
checkNotDone(f); |
1035 |
> |
checkCompletedNormally(g); |
1036 |
|
}}; |
1037 |
< |
singletonPool.invoke(a); |
1037 |
> |
testInvokeOnPool(singletonPool(), a); |
1038 |
|
} |
1039 |
|
|
1040 |
|
/** |
1042 |
|
* there are more tasks than threads |
1043 |
|
*/ |
1044 |
|
public void testGetSurplusQueuedTaskCount() { |
1045 |
< |
RecursiveAction a = new RecursiveAction() { |
1046 |
< |
public void compute() { |
1045 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1046 |
> |
public void realCompute() { |
1047 |
|
FibAction h = new FibAction(7); |
1048 |
< |
h.fork(); |
1048 |
> |
assertSame(h, h.fork()); |
1049 |
|
FibAction g = new FibAction(9); |
1050 |
< |
g.fork(); |
1050 |
> |
assertSame(g, g.fork()); |
1051 |
|
FibAction f = new FibAction(8); |
1052 |
< |
f.fork(); |
1053 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
1052 |
> |
assertSame(f, f.fork()); |
1053 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
1054 |
|
helpQuiesce(); |
1055 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
1056 |
+ |
checkCompletedNormally(f); |
1057 |
+ |
checkCompletedNormally(g); |
1058 |
+ |
checkCompletedNormally(h); |
1059 |
|
}}; |
1060 |
< |
singletonPool.invoke(a); |
1060 |
> |
testInvokeOnPool(singletonPool(), a); |
1061 |
|
} |
1062 |
|
|
1063 |
|
/** |
1064 |
|
* peekNextLocalTask returns most recent unexecuted task. |
1065 |
|
*/ |
1066 |
|
public void testPeekNextLocalTask() { |
1067 |
< |
RecursiveAction a = new RecursiveAction() { |
1068 |
< |
public void compute() { |
1067 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1068 |
> |
public void realCompute() { |
1069 |
|
FibAction g = new FibAction(9); |
1070 |
< |
g.fork(); |
1070 |
> |
assertSame(g, g.fork()); |
1071 |
|
FibAction f = new FibAction(8); |
1072 |
< |
f.fork(); |
1073 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
1074 |
< |
f.join(); |
1075 |
< |
threadAssertTrue(f.isDone()); |
1072 |
> |
assertSame(f, f.fork()); |
1073 |
> |
assertSame(f, peekNextLocalTask()); |
1074 |
> |
assertNull(f.join()); |
1075 |
> |
checkCompletedNormally(f); |
1076 |
|
helpQuiesce(); |
1077 |
+ |
checkCompletedNormally(f); |
1078 |
+ |
checkCompletedNormally(g); |
1079 |
|
}}; |
1080 |
< |
singletonPool.invoke(a); |
1080 |
> |
testInvokeOnPool(singletonPool(), a); |
1081 |
|
} |
1082 |
|
|
1083 |
|
/** |
1085 |
|
* without executing it |
1086 |
|
*/ |
1087 |
|
public void testPollNextLocalTask() { |
1088 |
< |
RecursiveAction a = new RecursiveAction() { |
1089 |
< |
public void compute() { |
1088 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1089 |
> |
public void realCompute() { |
1090 |
|
FibAction g = new FibAction(9); |
1091 |
< |
g.fork(); |
1091 |
> |
assertSame(g, g.fork()); |
1092 |
|
FibAction f = new FibAction(8); |
1093 |
< |
f.fork(); |
1094 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
1093 |
> |
assertSame(f, f.fork()); |
1094 |
> |
assertSame(f, pollNextLocalTask()); |
1095 |
|
helpQuiesce(); |
1096 |
< |
threadAssertFalse(f.isDone()); |
1096 |
> |
checkNotDone(f); |
1097 |
> |
checkCompletedNormally(g); |
1098 |
|
}}; |
1099 |
< |
singletonPool.invoke(a); |
1099 |
> |
testInvokeOnPool(singletonPool(), a); |
1100 |
|
} |
1101 |
|
|
1102 |
|
/** |
1103 |
< |
* pollTask returns an unexecuted task |
783 |
< |
* without executing it |
1103 |
> |
* pollTask returns an unexecuted task without executing it |
1104 |
|
*/ |
1105 |
|
public void testPollTask() { |
1106 |
< |
RecursiveAction a = new RecursiveAction() { |
1107 |
< |
public void compute() { |
1106 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1107 |
> |
public void realCompute() { |
1108 |
|
FibAction g = new FibAction(9); |
1109 |
< |
g.fork(); |
1109 |
> |
assertSame(g, g.fork()); |
1110 |
|
FibAction f = new FibAction(8); |
1111 |
< |
f.fork(); |
1112 |
< |
threadAssertTrue(pollTask() == f); |
1111 |
> |
assertSame(f, f.fork()); |
1112 |
> |
assertSame(f, pollTask()); |
1113 |
|
helpQuiesce(); |
1114 |
< |
threadAssertFalse(f.isDone()); |
1115 |
< |
threadAssertTrue(g.isDone()); |
1114 |
> |
checkNotDone(f); |
1115 |
> |
checkCompletedNormally(g); |
1116 |
|
}}; |
1117 |
< |
singletonPool.invoke(a); |
1117 |
> |
testInvokeOnPool(singletonPool(), a); |
1118 |
|
} |
1119 |
|
|
1120 |
|
/** |
1121 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1122 |
|
*/ |
1123 |
|
public void testPeekNextLocalTaskAsync() { |
1124 |
< |
RecursiveAction a = new RecursiveAction() { |
1125 |
< |
public void compute() { |
1124 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1125 |
> |
public void realCompute() { |
1126 |
|
FibAction g = new FibAction(9); |
1127 |
< |
g.fork(); |
1127 |
> |
assertSame(g, g.fork()); |
1128 |
|
FibAction f = new FibAction(8); |
1129 |
< |
f.fork(); |
1130 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
1131 |
< |
f.join(); |
1129 |
> |
assertSame(f, f.fork()); |
1130 |
> |
assertSame(g, peekNextLocalTask()); |
1131 |
> |
assertNull(f.join()); |
1132 |
|
helpQuiesce(); |
1133 |
< |
threadAssertTrue(f.isDone()); |
1133 |
> |
checkCompletedNormally(f); |
1134 |
> |
checkCompletedNormally(g); |
1135 |
|
}}; |
1136 |
< |
asyncSingletonPool.invoke(a); |
1136 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
1137 |
|
} |
1138 |
|
|
1139 |
|
/** |
1140 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1141 |
< |
* without executing it, in async mode |
1140 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1141 |
> |
* executing it, in async mode |
1142 |
|
*/ |
1143 |
|
public void testPollNextLocalTaskAsync() { |
1144 |
< |
RecursiveAction a = new RecursiveAction() { |
1145 |
< |
public void compute() { |
1144 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1145 |
> |
public void realCompute() { |
1146 |
|
FibAction g = new FibAction(9); |
1147 |
< |
g.fork(); |
1147 |
> |
assertSame(g, g.fork()); |
1148 |
|
FibAction f = new FibAction(8); |
1149 |
< |
f.fork(); |
1150 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
1149 |
> |
assertSame(f, f.fork()); |
1150 |
> |
assertSame(g, pollNextLocalTask()); |
1151 |
|
helpQuiesce(); |
1152 |
< |
threadAssertTrue(f.isDone()); |
1153 |
< |
threadAssertFalse(g.isDone()); |
1152 |
> |
checkCompletedNormally(f); |
1153 |
> |
checkNotDone(g); |
1154 |
|
}}; |
1155 |
< |
asyncSingletonPool.invoke(a); |
1155 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
1156 |
|
} |
1157 |
|
|
1158 |
|
/** |
1159 |
< |
* pollTask returns an unexecuted task |
1160 |
< |
* without executing it, in async mode |
1159 |
> |
* pollTask returns an unexecuted task without executing it, in |
1160 |
> |
* async mode |
1161 |
|
*/ |
1162 |
|
public void testPollTaskAsync() { |
1163 |
< |
RecursiveAction a = new RecursiveAction() { |
1164 |
< |
public void compute() { |
1163 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1164 |
> |
public void realCompute() { |
1165 |
|
FibAction g = new FibAction(9); |
1166 |
< |
g.fork(); |
1166 |
> |
assertSame(g, g.fork()); |
1167 |
|
FibAction f = new FibAction(8); |
1168 |
< |
f.fork(); |
1169 |
< |
threadAssertTrue(pollTask() == g); |
1168 |
> |
assertSame(f, f.fork()); |
1169 |
> |
assertSame(g, pollTask()); |
1170 |
|
helpQuiesce(); |
1171 |
< |
threadAssertTrue(f.isDone()); |
1172 |
< |
threadAssertFalse(g.isDone()); |
1171 |
> |
checkCompletedNormally(f); |
1172 |
> |
checkNotDone(g); |
1173 |
|
}}; |
1174 |
< |
asyncSingletonPool.invoke(a); |
1174 |
> |
testInvokeOnPool(asyncSingletonPool(), a); |
1175 |
|
} |
1176 |
|
|
1177 |
|
} |