11 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
12 |
|
import java.util.concurrent.RecursiveAction; |
13 |
|
import java.util.concurrent.TimeUnit; |
14 |
+ |
import java.util.concurrent.TimeoutException; |
15 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
import java.util.HashSet; |
17 |
|
|
18 |
|
public class RecursiveActionTest extends JSR166TestCase { |
41 |
|
|
42 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
43 |
|
try { |
44 |
< |
assertTrue(pool.invoke(a) == null); |
44 |
> |
checkNotDone(a); |
45 |
> |
|
46 |
> |
assertNull(pool.invoke(a)); |
47 |
> |
|
48 |
> |
checkCompletedNormally(a); |
49 |
|
} finally { |
50 |
|
joinPool(pool); |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
+ |
void checkNotDone(RecursiveAction a) { |
55 |
+ |
assertFalse(a.isDone()); |
56 |
+ |
assertFalse(a.isCompletedNormally()); |
57 |
+ |
assertFalse(a.isCompletedAbnormally()); |
58 |
+ |
assertFalse(a.isCancelled()); |
59 |
+ |
assertNull(a.getException()); |
60 |
+ |
assertNull(a.getRawResult()); |
61 |
+ |
|
62 |
+ |
if (! (Thread.currentThread() instanceof ForkJoinWorkerThread)) { |
63 |
+ |
Thread.currentThread().interrupt(); |
64 |
+ |
try { |
65 |
+ |
a.get(); |
66 |
+ |
shouldThrow(); |
67 |
+ |
} catch (InterruptedException success) { |
68 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
+ |
|
70 |
+ |
Thread.currentThread().interrupt(); |
71 |
+ |
try { |
72 |
+ |
a.get(5L, SECONDS); |
73 |
+ |
shouldThrow(); |
74 |
+ |
} catch (InterruptedException success) { |
75 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
+ |
} |
77 |
+ |
|
78 |
+ |
try { |
79 |
+ |
a.get(0L, SECONDS); |
80 |
+ |
shouldThrow(); |
81 |
+ |
} catch (TimeoutException success) { |
82 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
void checkCompletedNormally(RecursiveAction a) { |
86 |
+ |
assertTrue(a.isDone()); |
87 |
+ |
assertFalse(a.isCancelled()); |
88 |
+ |
assertTrue(a.isCompletedNormally()); |
89 |
+ |
assertFalse(a.isCompletedAbnormally()); |
90 |
+ |
assertNull(a.getException()); |
91 |
+ |
assertNull(a.getRawResult()); |
92 |
+ |
assertNull(a.join()); |
93 |
+ |
assertFalse(a.cancel(false)); |
94 |
+ |
assertFalse(a.cancel(true)); |
95 |
+ |
try { |
96 |
+ |
assertNull(a.get()); |
97 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
98 |
+ |
try { |
99 |
+ |
assertNull(a.get(5L, SECONDS)); |
100 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
101 |
+ |
} |
102 |
+ |
|
103 |
+ |
void checkCancelled(RecursiveAction a) { |
104 |
+ |
assertTrue(a.isDone()); |
105 |
+ |
assertTrue(a.isCancelled()); |
106 |
+ |
assertFalse(a.isCompletedNormally()); |
107 |
+ |
assertTrue(a.isCompletedAbnormally()); |
108 |
+ |
assertTrue(a.getException() instanceof CancellationException); |
109 |
+ |
assertNull(a.getRawResult()); |
110 |
+ |
|
111 |
+ |
try { |
112 |
+ |
a.join(); |
113 |
+ |
shouldThrow(); |
114 |
+ |
} catch (CancellationException success) { |
115 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
+ |
|
117 |
+ |
try { |
118 |
+ |
a.get(); |
119 |
+ |
shouldThrow(); |
120 |
+ |
} catch (CancellationException success) { |
121 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
+ |
|
123 |
+ |
try { |
124 |
+ |
a.get(5L, SECONDS); |
125 |
+ |
shouldThrow(); |
126 |
+ |
} catch (CancellationException success) { |
127 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
128 |
+ |
} |
129 |
+ |
|
130 |
+ |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
131 |
+ |
assertTrue(a.isDone()); |
132 |
+ |
assertFalse(a.isCancelled()); |
133 |
+ |
assertFalse(a.isCompletedNormally()); |
134 |
+ |
assertTrue(a.isCompletedAbnormally()); |
135 |
+ |
assertSame(t, a.getException()); |
136 |
+ |
assertNull(a.getRawResult()); |
137 |
+ |
assertFalse(a.cancel(false)); |
138 |
+ |
assertFalse(a.cancel(true)); |
139 |
+ |
|
140 |
+ |
try { |
141 |
+ |
a.join(); |
142 |
+ |
shouldThrow(); |
143 |
+ |
} catch (Throwable expected) { |
144 |
+ |
assertSame(t, expected); |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
try { |
148 |
+ |
a.get(); |
149 |
+ |
shouldThrow(); |
150 |
+ |
} catch (ExecutionException success) { |
151 |
+ |
assertSame(t, success.getCause()); |
152 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
+ |
|
154 |
+ |
try { |
155 |
+ |
a.get(5L, SECONDS); |
156 |
+ |
shouldThrow(); |
157 |
+ |
} catch (ExecutionException success) { |
158 |
+ |
assertSame(t, success.getCause()); |
159 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
160 |
+ |
} |
161 |
+ |
|
162 |
|
static final class FJException extends RuntimeException { |
163 |
|
FJException() { super(); } |
164 |
|
} |
165 |
|
|
166 |
|
// A simple recursive action for testing |
167 |
< |
static final class FibAction extends RecursiveAction { |
167 |
> |
final class FibAction extends CheckedRecursiveAction { |
168 |
|
final int number; |
169 |
|
int result; |
170 |
|
FibAction(int n) { number = n; } |
171 |
< |
public void compute() { |
171 |
> |
public void realCompute() { |
172 |
|
int n = number; |
173 |
|
if (n <= 1) |
174 |
|
result = n; |
205 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
206 |
|
*/ |
207 |
|
public void testInvoke() { |
208 |
< |
RecursiveAction a = new RecursiveAction() { |
209 |
< |
public void compute() { |
208 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
209 |
> |
public void realCompute() { |
210 |
|
FibAction f = new FibAction(8); |
211 |
< |
f.invoke(); |
212 |
< |
threadAssertTrue(f.result == 21); |
213 |
< |
threadAssertTrue(f.isDone()); |
100 |
< |
threadAssertFalse(f.isCancelled()); |
101 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
102 |
< |
threadAssertTrue(f.getRawResult() == null); |
211 |
> |
assertNull(f.invoke()); |
212 |
> |
assertEquals(21, f.result); |
213 |
> |
checkCompletedNormally(f); |
214 |
|
}}; |
215 |
|
testInvokeOnPool(mainPool(), a); |
216 |
|
} |
221 |
|
* completed tasks |
222 |
|
*/ |
223 |
|
public void testQuietlyInvoke() { |
224 |
< |
RecursiveAction a = new RecursiveAction() { |
225 |
< |
public void compute() { |
224 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
225 |
> |
public void realCompute() { |
226 |
|
FibAction f = new FibAction(8); |
227 |
|
f.quietlyInvoke(); |
228 |
< |
threadAssertTrue(f.result == 21); |
229 |
< |
threadAssertTrue(f.isDone()); |
119 |
< |
threadAssertFalse(f.isCancelled()); |
120 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
121 |
< |
threadAssertTrue(f.getRawResult() == null); |
228 |
> |
assertEquals(21, f.result); |
229 |
> |
checkCompletedNormally(f); |
230 |
|
}}; |
231 |
|
testInvokeOnPool(mainPool(), a); |
232 |
|
} |
235 |
|
* join of a forked task returns when task completes |
236 |
|
*/ |
237 |
|
public void testForkJoin() { |
238 |
< |
RecursiveAction a = new RecursiveAction() { |
239 |
< |
public void compute() { |
238 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
239 |
> |
public void realCompute() { |
240 |
|
FibAction f = new FibAction(8); |
241 |
< |
f.fork(); |
242 |
< |
f.join(); |
243 |
< |
threadAssertTrue(f.result == 21); |
244 |
< |
threadAssertTrue(f.isDone()); |
137 |
< |
threadAssertTrue(f.getRawResult() == null); |
241 |
> |
assertSame(f, f.fork()); |
242 |
> |
assertNull(f.join()); |
243 |
> |
assertEquals(21, f.result); |
244 |
> |
checkCompletedNormally(f); |
245 |
|
}}; |
246 |
|
testInvokeOnPool(mainPool(), a); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* get of a forked task returns when task completes |
250 |
> |
* join/quietlyJoin of a forked task ignores interrupts |
251 |
|
*/ |
252 |
< |
public void testForkGet() { |
253 |
< |
RecursiveAction a = new RecursiveAction() { |
254 |
< |
public void compute() { |
252 |
> |
public void testJoinIgnoresInterrupts() { |
253 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
254 |
> |
public void realCompute() { |
255 |
> |
FibAction f = new FibAction(8); |
256 |
> |
|
257 |
> |
// test join() |
258 |
> |
assertSame(f, f.fork()); |
259 |
> |
Thread.currentThread().interrupt(); |
260 |
> |
assertNull(f.join()); |
261 |
> |
assertTrue(Thread.interrupted()); |
262 |
> |
assertEquals(21, f.result); |
263 |
> |
checkCompletedNormally(f); |
264 |
> |
|
265 |
> |
f.reinitialize(); |
266 |
> |
f.cancel(true); |
267 |
> |
assertSame(f, f.fork()); |
268 |
> |
Thread.currentThread().interrupt(); |
269 |
|
try { |
270 |
< |
FibAction f = new FibAction(8); |
271 |
< |
f.fork(); |
272 |
< |
f.get(); |
273 |
< |
threadAssertTrue(f.result == 21); |
274 |
< |
threadAssertTrue(f.isDone()); |
154 |
< |
} catch (Exception ex) { |
155 |
< |
unexpectedException(ex); |
270 |
> |
f.join(); |
271 |
> |
shouldThrow(); |
272 |
> |
} catch (CancellationException success) { |
273 |
> |
assertTrue(Thread.interrupted()); |
274 |
> |
checkCancelled(f); |
275 |
|
} |
276 |
+ |
|
277 |
+ |
f.reinitialize(); |
278 |
+ |
f.completeExceptionally(new FJException()); |
279 |
+ |
assertSame(f, f.fork()); |
280 |
+ |
Thread.currentThread().interrupt(); |
281 |
+ |
try { |
282 |
+ |
f.join(); |
283 |
+ |
shouldThrow(); |
284 |
+ |
} catch (FJException success) { |
285 |
+ |
assertTrue(Thread.interrupted()); |
286 |
+ |
checkCompletedAbnormally(f, success); |
287 |
+ |
} |
288 |
+ |
|
289 |
+ |
// test quietlyJoin() |
290 |
+ |
f.reinitialize(); |
291 |
+ |
assertSame(f, f.fork()); |
292 |
+ |
Thread.currentThread().interrupt(); |
293 |
+ |
f.quietlyJoin(); |
294 |
+ |
assertTrue(Thread.interrupted()); |
295 |
+ |
assertEquals(21, f.result); |
296 |
+ |
checkCompletedNormally(f); |
297 |
+ |
|
298 |
+ |
f.reinitialize(); |
299 |
+ |
f.cancel(true); |
300 |
+ |
assertSame(f, f.fork()); |
301 |
+ |
Thread.currentThread().interrupt(); |
302 |
+ |
f.quietlyJoin(); |
303 |
+ |
assertTrue(Thread.interrupted()); |
304 |
+ |
checkCancelled(f); |
305 |
+ |
|
306 |
+ |
f.reinitialize(); |
307 |
+ |
f.completeExceptionally(new FJException()); |
308 |
+ |
assertSame(f, f.fork()); |
309 |
+ |
Thread.currentThread().interrupt(); |
310 |
+ |
f.quietlyJoin(); |
311 |
+ |
assertTrue(Thread.interrupted()); |
312 |
+ |
checkCompletedAbnormally(f, f.getException()); |
313 |
+ |
}}; |
314 |
+ |
testInvokeOnPool(mainPool(), a); |
315 |
+ |
a.reinitialize(); |
316 |
+ |
testInvokeOnPool(singletonPool(), a); |
317 |
+ |
} |
318 |
+ |
|
319 |
+ |
/** |
320 |
+ |
* get of a forked task returns when task completes |
321 |
+ |
*/ |
322 |
+ |
public void testForkGet() { |
323 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
324 |
+ |
public void realCompute() throws Exception { |
325 |
+ |
FibAction f = new FibAction(8); |
326 |
+ |
assertSame(f, f.fork()); |
327 |
+ |
assertNull(f.get()); |
328 |
+ |
assertEquals(21, f.result); |
329 |
+ |
checkCompletedNormally(f); |
330 |
|
}}; |
331 |
|
testInvokeOnPool(mainPool(), a); |
332 |
|
} |
335 |
|
* timed get of a forked task returns when task completes |
336 |
|
*/ |
337 |
|
public void testForkTimedGet() { |
338 |
< |
RecursiveAction a = new RecursiveAction() { |
339 |
< |
public void compute() { |
340 |
< |
try { |
341 |
< |
FibAction f = new FibAction(8); |
342 |
< |
f.fork(); |
343 |
< |
f.get(5L, TimeUnit.SECONDS); |
344 |
< |
threadAssertTrue(f.result == 21); |
172 |
< |
threadAssertTrue(f.isDone()); |
173 |
< |
} catch (Exception ex) { |
174 |
< |
unexpectedException(ex); |
175 |
< |
} |
338 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
339 |
> |
public void realCompute() throws Exception { |
340 |
> |
FibAction f = new FibAction(8); |
341 |
> |
assertSame(f, f.fork()); |
342 |
> |
assertNull(f.get(5L, SECONDS)); |
343 |
> |
assertEquals(21, f.result); |
344 |
> |
checkCompletedNormally(f); |
345 |
|
}}; |
346 |
|
testInvokeOnPool(mainPool(), a); |
347 |
|
} |
350 |
|
* timed get with null time unit throws NPE |
351 |
|
*/ |
352 |
|
public void testForkTimedGetNPE() { |
353 |
< |
RecursiveAction a = new RecursiveAction() { |
354 |
< |
public void compute() { |
353 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
354 |
> |
public void realCompute() throws Exception { |
355 |
> |
FibAction f = new FibAction(8); |
356 |
> |
assertSame(f, f.fork()); |
357 |
|
try { |
187 |
– |
FibAction f = new FibAction(8); |
188 |
– |
f.fork(); |
358 |
|
f.get(5L, null); |
359 |
|
shouldThrow(); |
360 |
< |
} catch (NullPointerException success) { |
192 |
< |
} catch (Exception ex) { |
193 |
< |
unexpectedException(ex); |
194 |
< |
} |
360 |
> |
} catch (NullPointerException success) {} |
361 |
|
}}; |
362 |
|
testInvokeOnPool(mainPool(), a); |
363 |
|
} |
366 |
|
* quietlyJoin of a forked task returns when task completes |
367 |
|
*/ |
368 |
|
public void testForkQuietlyJoin() { |
369 |
< |
RecursiveAction a = new RecursiveAction() { |
370 |
< |
public void compute() { |
369 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
370 |
> |
public void realCompute() { |
371 |
|
FibAction f = new FibAction(8); |
372 |
< |
f.fork(); |
372 |
> |
assertSame(f, f.fork()); |
373 |
|
f.quietlyJoin(); |
374 |
< |
threadAssertTrue(f.result == 21); |
375 |
< |
threadAssertTrue(f.isDone()); |
374 |
> |
assertEquals(21, f.result); |
375 |
> |
checkCompletedNormally(f); |
376 |
|
}}; |
377 |
|
testInvokeOnPool(mainPool(), a); |
378 |
|
} |
383 |
|
* getQueuedTaskCount returns 0 when quiescent |
384 |
|
*/ |
385 |
|
public void testForkHelpQuiesce() { |
386 |
< |
RecursiveAction a = new RecursiveAction() { |
387 |
< |
public void compute() { |
386 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
387 |
> |
public void realCompute() { |
388 |
|
FibAction f = new FibAction(8); |
389 |
< |
f.fork(); |
389 |
> |
assertSame(f, f.fork()); |
390 |
|
f.helpQuiesce(); |
391 |
< |
threadAssertTrue(f.result == 21); |
392 |
< |
threadAssertTrue(f.isDone()); |
393 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
391 |
> |
assertEquals(21, f.result); |
392 |
> |
assertEquals(0, getQueuedTaskCount()); |
393 |
> |
checkCompletedNormally(f); |
394 |
|
}}; |
395 |
|
testInvokeOnPool(mainPool(), a); |
396 |
|
} |
400 |
|
* invoke task throws exception when task completes abnormally |
401 |
|
*/ |
402 |
|
public void testAbnormalInvoke() { |
403 |
< |
RecursiveAction a = new RecursiveAction() { |
404 |
< |
public void compute() { |
403 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
404 |
> |
public void realCompute() { |
405 |
> |
FailingFibAction f = new FailingFibAction(8); |
406 |
|
try { |
240 |
– |
FailingFibAction f = new FailingFibAction(8); |
407 |
|
f.invoke(); |
408 |
|
shouldThrow(); |
409 |
|
} catch (FJException success) { |
410 |
+ |
checkCompletedAbnormally(f, success); |
411 |
|
} |
412 |
|
}}; |
413 |
|
testInvokeOnPool(mainPool(), a); |
417 |
|
* quietlyInvoke task returns when task completes abnormally |
418 |
|
*/ |
419 |
|
public void testAbnormalQuietlyInvoke() { |
420 |
< |
RecursiveAction a = new RecursiveAction() { |
421 |
< |
public void compute() { |
420 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
421 |
> |
public void realCompute() { |
422 |
|
FailingFibAction f = new FailingFibAction(8); |
423 |
|
f.quietlyInvoke(); |
424 |
< |
threadAssertTrue(f.isDone()); |
424 |
> |
assertTrue(f.getException() instanceof FJException); |
425 |
> |
checkCompletedAbnormally(f, f.getException()); |
426 |
|
}}; |
427 |
|
testInvokeOnPool(mainPool(), a); |
428 |
|
} |
431 |
|
* join of a forked task throws exception when task completes abnormally |
432 |
|
*/ |
433 |
|
public void testAbnormalForkJoin() { |
434 |
< |
RecursiveAction a = new RecursiveAction() { |
435 |
< |
public void compute() { |
434 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
435 |
> |
public void realCompute() { |
436 |
> |
FailingFibAction f = new FailingFibAction(8); |
437 |
> |
assertSame(f, f.fork()); |
438 |
|
try { |
269 |
– |
FailingFibAction f = new FailingFibAction(8); |
270 |
– |
f.fork(); |
439 |
|
f.join(); |
440 |
|
shouldThrow(); |
441 |
|
} catch (FJException success) { |
442 |
+ |
checkCompletedAbnormally(f, success); |
443 |
|
} |
444 |
|
}}; |
445 |
|
testInvokeOnPool(mainPool(), a); |
449 |
|
* get of a forked task throws exception when task completes abnormally |
450 |
|
*/ |
451 |
|
public void testAbnormalForkGet() { |
452 |
< |
RecursiveAction a = new RecursiveAction() { |
453 |
< |
public void compute() { |
452 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
453 |
> |
public void realCompute() throws Exception { |
454 |
> |
FailingFibAction f = new FailingFibAction(8); |
455 |
> |
assertSame(f, f.fork()); |
456 |
|
try { |
286 |
– |
FailingFibAction f = new FailingFibAction(8); |
287 |
– |
f.fork(); |
457 |
|
f.get(); |
458 |
|
shouldThrow(); |
459 |
|
} catch (ExecutionException success) { |
460 |
< |
} catch (Exception ex) { |
461 |
< |
unexpectedException(ex); |
460 |
> |
Throwable cause = success.getCause(); |
461 |
> |
assertTrue(cause instanceof FJException); |
462 |
> |
checkCompletedAbnormally(f, cause); |
463 |
|
} |
464 |
|
}}; |
465 |
|
testInvokeOnPool(mainPool(), a); |
469 |
|
* timed get of a forked task throws exception when task completes abnormally |
470 |
|
*/ |
471 |
|
public void testAbnormalForkTimedGet() { |
472 |
< |
RecursiveAction a = new RecursiveAction() { |
473 |
< |
public void compute() { |
472 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
473 |
> |
public void realCompute() throws Exception { |
474 |
> |
FailingFibAction f = new FailingFibAction(8); |
475 |
> |
assertSame(f, f.fork()); |
476 |
|
try { |
305 |
– |
FailingFibAction f = new FailingFibAction(8); |
306 |
– |
f.fork(); |
477 |
|
f.get(5L, TimeUnit.SECONDS); |
478 |
|
shouldThrow(); |
479 |
|
} catch (ExecutionException success) { |
480 |
< |
} catch (Exception ex) { |
481 |
< |
unexpectedException(ex); |
480 |
> |
Throwable cause = success.getCause(); |
481 |
> |
assertTrue(cause instanceof FJException); |
482 |
> |
checkCompletedAbnormally(f, cause); |
483 |
|
} |
484 |
|
}}; |
485 |
|
testInvokeOnPool(mainPool(), a); |
489 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
490 |
|
*/ |
491 |
|
public void testAbnormalForkQuietlyJoin() { |
492 |
< |
RecursiveAction a = new RecursiveAction() { |
493 |
< |
public void compute() { |
492 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
493 |
> |
public void realCompute() { |
494 |
|
FailingFibAction f = new FailingFibAction(8); |
495 |
< |
f.fork(); |
495 |
> |
assertSame(f, f.fork()); |
496 |
|
f.quietlyJoin(); |
497 |
< |
threadAssertTrue(f.isDone()); |
498 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
328 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
497 |
> |
assertTrue(f.getException() instanceof FJException); |
498 |
> |
checkCompletedAbnormally(f, f.getException()); |
499 |
|
}}; |
500 |
|
testInvokeOnPool(mainPool(), a); |
501 |
|
} |
504 |
|
* invoke task throws exception when task cancelled |
505 |
|
*/ |
506 |
|
public void testCancelledInvoke() { |
507 |
< |
RecursiveAction a = new RecursiveAction() { |
508 |
< |
public void compute() { |
507 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
508 |
> |
public void realCompute() { |
509 |
> |
FibAction f = new FibAction(8); |
510 |
> |
assertTrue(f.cancel(true)); |
511 |
|
try { |
340 |
– |
FibAction f = new FibAction(8); |
341 |
– |
f.cancel(true); |
512 |
|
f.invoke(); |
513 |
|
shouldThrow(); |
514 |
|
} catch (CancellationException success) { |
515 |
+ |
checkCancelled(f); |
516 |
|
} |
517 |
|
}}; |
518 |
|
testInvokeOnPool(mainPool(), a); |
522 |
|
* join of a forked task throws exception when task cancelled |
523 |
|
*/ |
524 |
|
public void testCancelledForkJoin() { |
525 |
< |
RecursiveAction a = new RecursiveAction() { |
526 |
< |
public void compute() { |
525 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
526 |
> |
public void realCompute() { |
527 |
> |
FibAction f = new FibAction(8); |
528 |
> |
assertTrue(f.cancel(true)); |
529 |
> |
assertSame(f, f.fork()); |
530 |
|
try { |
357 |
– |
FibAction f = new FibAction(8); |
358 |
– |
f.cancel(true); |
359 |
– |
f.fork(); |
531 |
|
f.join(); |
532 |
|
shouldThrow(); |
533 |
|
} catch (CancellationException success) { |
534 |
+ |
checkCancelled(f); |
535 |
|
} |
536 |
|
}}; |
537 |
|
testInvokeOnPool(mainPool(), a); |
541 |
|
* get of a forked task throws exception when task cancelled |
542 |
|
*/ |
543 |
|
public void testCancelledForkGet() { |
544 |
< |
RecursiveAction a = new RecursiveAction() { |
545 |
< |
public void compute() { |
544 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
545 |
> |
public void realCompute() throws Exception { |
546 |
> |
FibAction f = new FibAction(8); |
547 |
> |
assertTrue(f.cancel(true)); |
548 |
> |
assertSame(f, f.fork()); |
549 |
|
try { |
375 |
– |
FibAction f = new FibAction(8); |
376 |
– |
f.cancel(true); |
377 |
– |
f.fork(); |
550 |
|
f.get(); |
551 |
|
shouldThrow(); |
552 |
|
} catch (CancellationException success) { |
553 |
< |
} catch (Exception ex) { |
382 |
< |
unexpectedException(ex); |
553 |
> |
checkCancelled(f); |
554 |
|
} |
555 |
|
}}; |
556 |
|
testInvokeOnPool(mainPool(), a); |
560 |
|
* timed get of a forked task throws exception when task cancelled |
561 |
|
*/ |
562 |
|
public void testCancelledForkTimedGet() { |
563 |
< |
RecursiveAction a = new RecursiveAction() { |
564 |
< |
public void compute() { |
563 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
564 |
> |
public void realCompute() throws Exception { |
565 |
> |
FibAction f = new FibAction(8); |
566 |
> |
assertTrue(f.cancel(true)); |
567 |
> |
assertSame(f, f.fork()); |
568 |
|
try { |
569 |
< |
FibAction f = new FibAction(8); |
396 |
< |
f.cancel(true); |
397 |
< |
f.fork(); |
398 |
< |
f.get(5L, TimeUnit.SECONDS); |
569 |
> |
f.get(5L, SECONDS); |
570 |
|
shouldThrow(); |
571 |
|
} catch (CancellationException success) { |
572 |
< |
} catch (Exception ex) { |
402 |
< |
unexpectedException(ex); |
572 |
> |
checkCancelled(f); |
573 |
|
} |
574 |
|
}}; |
575 |
|
testInvokeOnPool(mainPool(), a); |
579 |
|
* quietlyJoin of a forked task returns when task cancelled |
580 |
|
*/ |
581 |
|
public void testCancelledForkQuietlyJoin() { |
582 |
< |
RecursiveAction a = new RecursiveAction() { |
583 |
< |
public void compute() { |
582 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
583 |
> |
public void realCompute() { |
584 |
|
FibAction f = new FibAction(8); |
585 |
< |
f.cancel(true); |
586 |
< |
f.fork(); |
585 |
> |
assertTrue(f.cancel(true)); |
586 |
> |
assertSame(f, f.fork()); |
587 |
|
f.quietlyJoin(); |
588 |
< |
threadAssertTrue(f.isDone()); |
419 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
420 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
588 |
> |
checkCancelled(f); |
589 |
|
}}; |
590 |
|
testInvokeOnPool(mainPool(), a); |
591 |
|
} |
595 |
|
*/ |
596 |
|
public void testGetPool() { |
597 |
|
final ForkJoinPool mainPool = mainPool(); |
598 |
< |
RecursiveAction a = new RecursiveAction() { |
599 |
< |
public void compute() { |
600 |
< |
threadAssertTrue(getPool() == mainPool); |
598 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
599 |
> |
public void realCompute() { |
600 |
> |
assertSame(mainPool, getPool()); |
601 |
|
}}; |
602 |
|
testInvokeOnPool(mainPool, a); |
603 |
|
} |
606 |
|
* getPool of non-FJ task returns null |
607 |
|
*/ |
608 |
|
public void testGetPool2() { |
609 |
< |
RecursiveAction a = new RecursiveAction() { |
610 |
< |
public void compute() { |
611 |
< |
threadAssertTrue(getPool() == null); |
609 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
610 |
> |
public void realCompute() { |
611 |
> |
assertNull(getPool()); |
612 |
|
}}; |
613 |
< |
a.invoke(); |
613 |
> |
assertNull(a.invoke()); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
|
* inForkJoinPool of executing task returns true |
618 |
|
*/ |
619 |
|
public void testInForkJoinPool() { |
620 |
< |
RecursiveAction a = new RecursiveAction() { |
621 |
< |
public void compute() { |
622 |
< |
threadAssertTrue(inForkJoinPool()); |
620 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
621 |
> |
public void realCompute() { |
622 |
> |
assertTrue(inForkJoinPool()); |
623 |
|
}}; |
624 |
|
testInvokeOnPool(mainPool(), a); |
625 |
|
} |
628 |
|
* inForkJoinPool of non-FJ task returns false |
629 |
|
*/ |
630 |
|
public void testInForkJoinPool2() { |
631 |
< |
RecursiveAction a = new RecursiveAction() { |
632 |
< |
public void compute() { |
633 |
< |
threadAssertTrue(!inForkJoinPool()); |
631 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
632 |
> |
public void realCompute() { |
633 |
> |
assertFalse(inForkJoinPool()); |
634 |
|
}}; |
635 |
< |
a.invoke(); |
635 |
> |
assertNull(a.invoke()); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
640 |
|
*/ |
641 |
|
public void testWorkerGetPool() { |
642 |
|
final ForkJoinPool mainPool = mainPool(); |
643 |
< |
RecursiveAction a = new RecursiveAction() { |
644 |
< |
public void compute() { |
643 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
644 |
> |
public void realCompute() { |
645 |
|
ForkJoinWorkerThread w = |
646 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
647 |
< |
threadAssertTrue(w.getPool() == mainPool); |
647 |
> |
assertSame(mainPool, w.getPool()); |
648 |
|
}}; |
649 |
|
testInvokeOnPool(mainPool, a); |
650 |
|
} |
654 |
|
*/ |
655 |
|
public void testWorkerGetPoolIndex() { |
656 |
|
final ForkJoinPool mainPool = mainPool(); |
657 |
< |
RecursiveAction a = new RecursiveAction() { |
658 |
< |
public void compute() { |
657 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
658 |
> |
public void realCompute() { |
659 |
|
ForkJoinWorkerThread w = |
660 |
|
(ForkJoinWorkerThread)(Thread.currentThread()); |
661 |
< |
int idx = w.getPoolIndex(); |
662 |
< |
threadAssertTrue(idx >= 0); |
495 |
< |
threadAssertTrue(idx < mainPool.getPoolSize()); |
661 |
> |
assertTrue(w.getPoolIndex() >= 0); |
662 |
> |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
663 |
|
}}; |
664 |
|
testInvokeOnPool(mainPool, a); |
665 |
|
} |
669 |
|
* setRawResult(null) succeeds |
670 |
|
*/ |
671 |
|
public void testSetRawResult() { |
672 |
< |
RecursiveAction a = new RecursiveAction() { |
673 |
< |
public void compute() { |
672 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
673 |
> |
public void realCompute() { |
674 |
|
setRawResult(null); |
675 |
+ |
assertNull(getRawResult()); |
676 |
|
}}; |
677 |
< |
a.invoke(); |
677 |
> |
assertNull(a.invoke()); |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
|
* A reinitialized task may be re-invoked |
682 |
|
*/ |
683 |
|
public void testReinitialize() { |
684 |
< |
RecursiveAction a = new RecursiveAction() { |
685 |
< |
public void compute() { |
684 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
685 |
> |
public void realCompute() { |
686 |
|
FibAction f = new FibAction(8); |
687 |
< |
f.invoke(); |
688 |
< |
threadAssertTrue(f.result == 21); |
689 |
< |
threadAssertTrue(f.isDone()); |
690 |
< |
threadAssertFalse(f.isCancelled()); |
691 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
692 |
< |
f.reinitialize(); |
693 |
< |
f.invoke(); |
694 |
< |
threadAssertTrue(f.result == 21); |
687 |
> |
checkNotDone(f); |
688 |
> |
|
689 |
> |
for (int i = 0; i < 3; i++) { |
690 |
> |
assertNull(f.invoke()); |
691 |
> |
assertEquals(21, f.result); |
692 |
> |
checkCompletedNormally(f); |
693 |
> |
f.reinitialize(); |
694 |
> |
checkNotDone(f); |
695 |
> |
} |
696 |
|
}}; |
697 |
|
testInvokeOnPool(mainPool(), a); |
698 |
|
} |
701 |
|
* invoke task throws exception after invoking completeExceptionally |
702 |
|
*/ |
703 |
|
public void testCompleteExceptionally() { |
704 |
< |
RecursiveAction a = new RecursiveAction() { |
705 |
< |
public void compute() { |
704 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
705 |
> |
public void realCompute() { |
706 |
> |
FibAction f = new FibAction(8); |
707 |
> |
f.completeExceptionally(new FJException()); |
708 |
|
try { |
538 |
– |
FibAction f = new FibAction(8); |
539 |
– |
f.completeExceptionally(new FJException()); |
709 |
|
f.invoke(); |
710 |
|
shouldThrow(); |
711 |
|
} catch (FJException success) { |
712 |
+ |
checkCompletedAbnormally(f, success); |
713 |
|
} |
714 |
|
}}; |
715 |
|
testInvokeOnPool(mainPool(), a); |
719 |
|
* invoke task suppresses execution invoking complete |
720 |
|
*/ |
721 |
|
public void testComplete() { |
722 |
< |
RecursiveAction a = new RecursiveAction() { |
723 |
< |
public void compute() { |
722 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
723 |
> |
public void realCompute() { |
724 |
|
FibAction f = new FibAction(8); |
725 |
|
f.complete(null); |
726 |
< |
f.invoke(); |
727 |
< |
threadAssertTrue(f.isDone()); |
728 |
< |
threadAssertTrue(f.result == 0); |
726 |
> |
assertNull(f.invoke()); |
727 |
> |
assertEquals(0, f.result); |
728 |
> |
checkCompletedNormally(f); |
729 |
|
}}; |
730 |
|
testInvokeOnPool(mainPool(), a); |
731 |
|
} |
734 |
|
* invokeAll(t1, t2) invokes all task arguments |
735 |
|
*/ |
736 |
|
public void testInvokeAll2() { |
737 |
< |
RecursiveAction a = new RecursiveAction() { |
738 |
< |
public void compute() { |
737 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
738 |
> |
public void realCompute() { |
739 |
|
FibAction f = new FibAction(8); |
740 |
|
FibAction g = new FibAction(9); |
741 |
|
invokeAll(f, g); |
742 |
< |
threadAssertTrue(f.isDone()); |
743 |
< |
threadAssertTrue(f.result == 21); |
744 |
< |
threadAssertTrue(g.isDone()); |
745 |
< |
threadAssertTrue(g.result == 34); |
742 |
> |
checkCompletedNormally(f); |
743 |
> |
assertEquals(21, f.result); |
744 |
> |
checkCompletedNormally(g); |
745 |
> |
assertEquals(34, g.result); |
746 |
|
}}; |
747 |
|
testInvokeOnPool(mainPool(), a); |
748 |
|
} |
751 |
|
* invokeAll(tasks) with 1 argument invokes task |
752 |
|
*/ |
753 |
|
public void testInvokeAll1() { |
754 |
< |
RecursiveAction a = new RecursiveAction() { |
755 |
< |
public void compute() { |
754 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
755 |
> |
public void realCompute() { |
756 |
|
FibAction f = new FibAction(8); |
757 |
|
invokeAll(f); |
758 |
< |
threadAssertTrue(f.isDone()); |
759 |
< |
threadAssertTrue(f.result == 21); |
758 |
> |
checkCompletedNormally(f); |
759 |
> |
assertEquals(21, f.result); |
760 |
|
}}; |
761 |
|
testInvokeOnPool(mainPool(), a); |
762 |
|
} |
765 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
766 |
|
*/ |
767 |
|
public void testInvokeAll3() { |
768 |
< |
RecursiveAction a = new RecursiveAction() { |
769 |
< |
public void compute() { |
768 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
769 |
> |
public void realCompute() { |
770 |
|
FibAction f = new FibAction(8); |
771 |
|
FibAction g = new FibAction(9); |
772 |
|
FibAction h = new FibAction(7); |
773 |
|
invokeAll(f, g, h); |
774 |
< |
threadAssertTrue(f.isDone()); |
775 |
< |
threadAssertTrue(f.result == 21); |
776 |
< |
threadAssertTrue(g.isDone()); |
777 |
< |
threadAssertTrue(g.result == 34); |
778 |
< |
threadAssertTrue(h.isDone()); |
779 |
< |
threadAssertTrue(h.result == 13); |
774 |
> |
assertTrue(f.isDone()); |
775 |
> |
assertTrue(g.isDone()); |
776 |
> |
assertTrue(h.isDone()); |
777 |
> |
checkCompletedNormally(f); |
778 |
> |
assertEquals(21, f.result); |
779 |
> |
checkCompletedNormally(g); |
780 |
> |
assertEquals(34, g.result); |
781 |
> |
checkCompletedNormally(g); |
782 |
> |
assertEquals(13, h.result); |
783 |
|
}}; |
784 |
|
testInvokeOnPool(mainPool(), a); |
785 |
|
} |
788 |
|
* invokeAll(collection) invokes all tasks in the collection |
789 |
|
*/ |
790 |
|
public void testInvokeAllCollection() { |
791 |
< |
RecursiveAction a = new RecursiveAction() { |
792 |
< |
public void compute() { |
791 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
792 |
> |
public void realCompute() { |
793 |
|
FibAction f = new FibAction(8); |
794 |
|
FibAction g = new FibAction(9); |
795 |
|
FibAction h = new FibAction(7); |
798 |
|
set.add(g); |
799 |
|
set.add(h); |
800 |
|
invokeAll(set); |
801 |
< |
threadAssertTrue(f.isDone()); |
802 |
< |
threadAssertTrue(f.result == 21); |
803 |
< |
threadAssertTrue(g.isDone()); |
804 |
< |
threadAssertTrue(g.result == 34); |
805 |
< |
threadAssertTrue(h.isDone()); |
806 |
< |
threadAssertTrue(h.result == 13); |
801 |
> |
assertTrue(f.isDone()); |
802 |
> |
assertTrue(g.isDone()); |
803 |
> |
assertTrue(h.isDone()); |
804 |
> |
checkCompletedNormally(f); |
805 |
> |
assertEquals(21, f.result); |
806 |
> |
checkCompletedNormally(g); |
807 |
> |
assertEquals(34, g.result); |
808 |
> |
checkCompletedNormally(g); |
809 |
> |
assertEquals(13, h.result); |
810 |
|
}}; |
811 |
|
testInvokeOnPool(mainPool(), a); |
812 |
|
} |
816 |
|
* invokeAll(tasks) with any null task throws NPE |
817 |
|
*/ |
818 |
|
public void testInvokeAllNPE() { |
819 |
< |
RecursiveAction a = new RecursiveAction() { |
820 |
< |
public void compute() { |
819 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
820 |
> |
public void realCompute() { |
821 |
> |
FibAction f = new FibAction(8); |
822 |
> |
FibAction g = new FibAction(9); |
823 |
> |
FibAction h = null; |
824 |
|
try { |
646 |
– |
FibAction f = new FibAction(8); |
647 |
– |
FibAction g = new FibAction(9); |
648 |
– |
FibAction h = null; |
825 |
|
invokeAll(f, g, h); |
826 |
|
shouldThrow(); |
827 |
< |
} catch (NullPointerException success) { |
652 |
< |
} |
827 |
> |
} catch (NullPointerException success) {} |
828 |
|
}}; |
829 |
|
testInvokeOnPool(mainPool(), a); |
830 |
|
} |
833 |
|
* invokeAll(t1, t2) throw exception if any task does |
834 |
|
*/ |
835 |
|
public void testAbnormalInvokeAll2() { |
836 |
< |
RecursiveAction a = new RecursiveAction() { |
837 |
< |
public void compute() { |
836 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
837 |
> |
public void realCompute() { |
838 |
> |
FibAction f = new FibAction(8); |
839 |
> |
FailingFibAction g = new FailingFibAction(9); |
840 |
|
try { |
664 |
– |
FibAction f = new FibAction(8); |
665 |
– |
FailingFibAction g = new FailingFibAction(9); |
841 |
|
invokeAll(f, g); |
842 |
|
shouldThrow(); |
843 |
|
} catch (FJException success) { |
844 |
+ |
checkCompletedAbnormally(g, success); |
845 |
|
} |
846 |
|
}}; |
847 |
|
testInvokeOnPool(mainPool(), a); |
851 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
852 |
|
*/ |
853 |
|
public void testAbnormalInvokeAll1() { |
854 |
< |
RecursiveAction a = new RecursiveAction() { |
855 |
< |
public void compute() { |
854 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
855 |
> |
public void realCompute() { |
856 |
> |
FailingFibAction g = new FailingFibAction(9); |
857 |
|
try { |
681 |
– |
FailingFibAction g = new FailingFibAction(9); |
858 |
|
invokeAll(g); |
859 |
|
shouldThrow(); |
860 |
|
} catch (FJException success) { |
861 |
+ |
checkCompletedAbnormally(g, success); |
862 |
|
} |
863 |
|
}}; |
864 |
|
testInvokeOnPool(mainPool(), a); |
868 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
869 |
|
*/ |
870 |
|
public void testAbnormalInvokeAll3() { |
871 |
< |
RecursiveAction a = new RecursiveAction() { |
872 |
< |
public void compute() { |
871 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
872 |
> |
public void realCompute() { |
873 |
> |
FibAction f = new FibAction(8); |
874 |
> |
FailingFibAction g = new FailingFibAction(9); |
875 |
> |
FibAction h = new FibAction(7); |
876 |
|
try { |
697 |
– |
FibAction f = new FibAction(8); |
698 |
– |
FailingFibAction g = new FailingFibAction(9); |
699 |
– |
FibAction h = new FibAction(7); |
877 |
|
invokeAll(f, g, h); |
878 |
|
shouldThrow(); |
879 |
|
} catch (FJException success) { |
880 |
+ |
checkCompletedAbnormally(g, success); |
881 |
|
} |
882 |
|
}}; |
883 |
|
testInvokeOnPool(mainPool(), a); |
887 |
|
* invokeAll(collection) throws exception if any task does |
888 |
|
*/ |
889 |
|
public void testAbnormalInvokeAllCollection() { |
890 |
< |
RecursiveAction a = new RecursiveAction() { |
891 |
< |
public void compute() { |
890 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
891 |
> |
public void realCompute() { |
892 |
> |
FailingFibAction f = new FailingFibAction(8); |
893 |
> |
FibAction g = new FibAction(9); |
894 |
> |
FibAction h = new FibAction(7); |
895 |
> |
HashSet set = new HashSet(); |
896 |
> |
set.add(f); |
897 |
> |
set.add(g); |
898 |
> |
set.add(h); |
899 |
|
try { |
715 |
– |
FailingFibAction f = new FailingFibAction(8); |
716 |
– |
FibAction g = new FibAction(9); |
717 |
– |
FibAction h = new FibAction(7); |
718 |
– |
HashSet set = new HashSet(); |
719 |
– |
set.add(f); |
720 |
– |
set.add(g); |
721 |
– |
set.add(h); |
900 |
|
invokeAll(set); |
901 |
|
shouldThrow(); |
902 |
|
} catch (FJException success) { |
903 |
+ |
checkCompletedAbnormally(f, success); |
904 |
|
} |
905 |
|
}}; |
906 |
|
testInvokeOnPool(mainPool(), a); |
911 |
|
* and suppresses execution |
912 |
|
*/ |
913 |
|
public void testTryUnfork() { |
914 |
< |
RecursiveAction a = new RecursiveAction() { |
915 |
< |
public void compute() { |
914 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
915 |
> |
public void realCompute() { |
916 |
|
FibAction g = new FibAction(9); |
917 |
< |
g.fork(); |
917 |
> |
assertSame(g, g.fork()); |
918 |
|
FibAction f = new FibAction(8); |
919 |
< |
f.fork(); |
920 |
< |
threadAssertTrue(f.tryUnfork()); |
919 |
> |
assertSame(f, f.fork()); |
920 |
> |
assertTrue(f.tryUnfork()); |
921 |
|
helpQuiesce(); |
922 |
< |
threadAssertFalse(f.isDone()); |
923 |
< |
threadAssertTrue(g.isDone()); |
922 |
> |
checkNotDone(f); |
923 |
> |
checkCompletedNormally(g); |
924 |
|
}}; |
925 |
|
testInvokeOnPool(singletonPool(), a); |
926 |
|
} |
930 |
|
* there are more tasks than threads |
931 |
|
*/ |
932 |
|
public void testGetSurplusQueuedTaskCount() { |
933 |
< |
RecursiveAction a = new RecursiveAction() { |
934 |
< |
public void compute() { |
933 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
934 |
> |
public void realCompute() { |
935 |
|
FibAction h = new FibAction(7); |
936 |
< |
h.fork(); |
936 |
> |
assertSame(h, h.fork()); |
937 |
|
FibAction g = new FibAction(9); |
938 |
< |
g.fork(); |
938 |
> |
assertSame(g, g.fork()); |
939 |
|
FibAction f = new FibAction(8); |
940 |
< |
f.fork(); |
941 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
940 |
> |
assertSame(f, f.fork()); |
941 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
942 |
|
helpQuiesce(); |
943 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
944 |
+ |
checkCompletedNormally(f); |
945 |
+ |
checkCompletedNormally(g); |
946 |
+ |
checkCompletedNormally(h); |
947 |
|
}}; |
948 |
|
testInvokeOnPool(singletonPool(), a); |
949 |
|
} |
952 |
|
* peekNextLocalTask returns most recent unexecuted task. |
953 |
|
*/ |
954 |
|
public void testPeekNextLocalTask() { |
955 |
< |
RecursiveAction a = new RecursiveAction() { |
956 |
< |
public void compute() { |
955 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
956 |
> |
public void realCompute() { |
957 |
|
FibAction g = new FibAction(9); |
958 |
< |
g.fork(); |
958 |
> |
assertSame(g, g.fork()); |
959 |
|
FibAction f = new FibAction(8); |
960 |
< |
f.fork(); |
961 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
962 |
< |
f.join(); |
963 |
< |
threadAssertTrue(f.isDone()); |
960 |
> |
assertSame(f, f.fork()); |
961 |
> |
assertSame(f, peekNextLocalTask()); |
962 |
> |
assertNull(f.join()); |
963 |
> |
checkCompletedNormally(f); |
964 |
|
helpQuiesce(); |
965 |
+ |
checkCompletedNormally(f); |
966 |
+ |
checkCompletedNormally(g); |
967 |
|
}}; |
968 |
|
testInvokeOnPool(singletonPool(), a); |
969 |
|
} |
973 |
|
* without executing it |
974 |
|
*/ |
975 |
|
public void testPollNextLocalTask() { |
976 |
< |
RecursiveAction a = new RecursiveAction() { |
977 |
< |
public void compute() { |
976 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
977 |
> |
public void realCompute() { |
978 |
|
FibAction g = new FibAction(9); |
979 |
< |
g.fork(); |
979 |
> |
assertSame(g, g.fork()); |
980 |
|
FibAction f = new FibAction(8); |
981 |
< |
f.fork(); |
982 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
981 |
> |
assertSame(f, f.fork()); |
982 |
> |
assertSame(f, pollNextLocalTask()); |
983 |
|
helpQuiesce(); |
984 |
< |
threadAssertFalse(f.isDone()); |
984 |
> |
checkNotDone(f); |
985 |
> |
checkCompletedNormally(g); |
986 |
|
}}; |
987 |
|
testInvokeOnPool(singletonPool(), a); |
988 |
|
} |
989 |
|
|
990 |
|
/** |
991 |
< |
* pollTask returns an unexecuted task |
806 |
< |
* without executing it |
991 |
> |
* pollTask returns an unexecuted task without executing it |
992 |
|
*/ |
993 |
|
public void testPollTask() { |
994 |
< |
RecursiveAction a = new RecursiveAction() { |
995 |
< |
public void compute() { |
994 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
995 |
> |
public void realCompute() { |
996 |
|
FibAction g = new FibAction(9); |
997 |
< |
g.fork(); |
997 |
> |
assertSame(g, g.fork()); |
998 |
|
FibAction f = new FibAction(8); |
999 |
< |
f.fork(); |
1000 |
< |
threadAssertTrue(pollTask() == f); |
999 |
> |
assertSame(f, f.fork()); |
1000 |
> |
assertSame(f, pollTask()); |
1001 |
|
helpQuiesce(); |
1002 |
< |
threadAssertFalse(f.isDone()); |
1003 |
< |
threadAssertTrue(g.isDone()); |
1002 |
> |
checkNotDone(f); |
1003 |
> |
checkCompletedNormally(g); |
1004 |
|
}}; |
1005 |
|
testInvokeOnPool(singletonPool(), a); |
1006 |
|
} |
1009 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
1010 |
|
*/ |
1011 |
|
public void testPeekNextLocalTaskAsync() { |
1012 |
< |
RecursiveAction a = new RecursiveAction() { |
1013 |
< |
public void compute() { |
1012 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1013 |
> |
public void realCompute() { |
1014 |
|
FibAction g = new FibAction(9); |
1015 |
< |
g.fork(); |
1015 |
> |
assertSame(g, g.fork()); |
1016 |
|
FibAction f = new FibAction(8); |
1017 |
< |
f.fork(); |
1018 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
1019 |
< |
f.join(); |
1017 |
> |
assertSame(f, f.fork()); |
1018 |
> |
assertSame(g, peekNextLocalTask()); |
1019 |
> |
assertNull(f.join()); |
1020 |
|
helpQuiesce(); |
1021 |
< |
threadAssertTrue(f.isDone()); |
1021 |
> |
checkCompletedNormally(f); |
1022 |
> |
checkCompletedNormally(g); |
1023 |
|
}}; |
1024 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1025 |
|
} |
1026 |
|
|
1027 |
|
/** |
1028 |
< |
* pollNextLocalTask returns least recent unexecuted task |
1029 |
< |
* without executing it, in async mode |
1028 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
1029 |
> |
* executing it, in async mode |
1030 |
|
*/ |
1031 |
|
public void testPollNextLocalTaskAsync() { |
1032 |
< |
RecursiveAction a = new RecursiveAction() { |
1033 |
< |
public void compute() { |
1032 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1033 |
> |
public void realCompute() { |
1034 |
|
FibAction g = new FibAction(9); |
1035 |
< |
g.fork(); |
1035 |
> |
assertSame(g, g.fork()); |
1036 |
|
FibAction f = new FibAction(8); |
1037 |
< |
f.fork(); |
1038 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
1037 |
> |
assertSame(f, f.fork()); |
1038 |
> |
assertSame(g, pollNextLocalTask()); |
1039 |
|
helpQuiesce(); |
1040 |
< |
threadAssertTrue(f.isDone()); |
1041 |
< |
threadAssertFalse(g.isDone()); |
1040 |
> |
checkCompletedNormally(f); |
1041 |
> |
checkNotDone(g); |
1042 |
|
}}; |
1043 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1044 |
|
} |
1045 |
|
|
1046 |
|
/** |
1047 |
< |
* pollTask returns an unexecuted task |
1048 |
< |
* without executing it, in async mode |
1047 |
> |
* pollTask returns an unexecuted task without executing it, in |
1048 |
> |
* async mode |
1049 |
|
*/ |
1050 |
|
public void testPollTaskAsync() { |
1051 |
< |
RecursiveAction a = new RecursiveAction() { |
1052 |
< |
public void compute() { |
1051 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
1052 |
> |
public void realCompute() { |
1053 |
|
FibAction g = new FibAction(9); |
1054 |
< |
g.fork(); |
1054 |
> |
assertSame(g, g.fork()); |
1055 |
|
FibAction f = new FibAction(8); |
1056 |
< |
f.fork(); |
1057 |
< |
threadAssertTrue(pollTask() == g); |
1056 |
> |
assertSame(f, f.fork()); |
1057 |
> |
assertSame(g, pollTask()); |
1058 |
|
helpQuiesce(); |
1059 |
< |
threadAssertTrue(f.isDone()); |
1060 |
< |
threadAssertFalse(g.isDone()); |
1059 |
> |
checkCompletedNormally(f); |
1060 |
> |
checkNotDone(g); |
1061 |
|
}}; |
1062 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1063 |
|
} |