5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.concurrent.*; |
9 |
< |
import java.util.*; |
8 |
> |
import java.util.concurrent.CancellationException; |
9 |
> |
import java.util.concurrent.ExecutionException; |
10 |
> |
import java.util.concurrent.ForkJoinPool; |
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 { |
19 |
|
|
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 |
+ |
try { |
94 |
+ |
assertNull(a.get()); |
95 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
96 |
+ |
try { |
97 |
+ |
assertNull(a.get(5L, SECONDS)); |
98 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
void checkCancelled(RecursiveAction a) { |
102 |
+ |
assertTrue(a.isDone()); |
103 |
+ |
assertTrue(a.isCancelled()); |
104 |
+ |
assertFalse(a.isCompletedNormally()); |
105 |
+ |
assertTrue(a.isCompletedAbnormally()); |
106 |
+ |
assertTrue(a.getException() instanceof CancellationException); |
107 |
+ |
assertNull(a.getRawResult()); |
108 |
+ |
|
109 |
+ |
try { |
110 |
+ |
a.join(); |
111 |
+ |
shouldThrow(); |
112 |
+ |
} catch (CancellationException success) { |
113 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
114 |
+ |
|
115 |
+ |
try { |
116 |
+ |
a.get(); |
117 |
+ |
shouldThrow(); |
118 |
+ |
} catch (CancellationException success) { |
119 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
120 |
+ |
|
121 |
+ |
try { |
122 |
+ |
a.get(5L, SECONDS); |
123 |
+ |
shouldThrow(); |
124 |
+ |
} catch (CancellationException success) { |
125 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
126 |
+ |
} |
127 |
+ |
|
128 |
+ |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
129 |
+ |
assertTrue(a.isDone()); |
130 |
+ |
assertFalse(a.isCancelled()); |
131 |
+ |
assertFalse(a.isCompletedNormally()); |
132 |
+ |
assertTrue(a.isCompletedAbnormally()); |
133 |
+ |
assertSame(t, a.getException()); |
134 |
+ |
assertNull(a.getRawResult()); |
135 |
+ |
|
136 |
+ |
try { |
137 |
+ |
a.join(); |
138 |
+ |
shouldThrow(); |
139 |
+ |
} catch (Throwable expected) { |
140 |
+ |
assertSame(t, expected); |
141 |
+ |
} |
142 |
+ |
|
143 |
+ |
try { |
144 |
+ |
a.get(); |
145 |
+ |
shouldThrow(); |
146 |
+ |
} catch (ExecutionException success) { |
147 |
+ |
assertSame(t, success.getCause()); |
148 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
+ |
|
150 |
+ |
try { |
151 |
+ |
a.get(5L, SECONDS); |
152 |
+ |
shouldThrow(); |
153 |
+ |
} catch (ExecutionException success) { |
154 |
+ |
assertSame(t, success.getCause()); |
155 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
156 |
+ |
} |
157 |
+ |
|
158 |
|
static final class FJException extends RuntimeException { |
159 |
|
FJException() { super(); } |
160 |
|
} |
161 |
|
|
162 |
|
// A simple recursive action for testing |
163 |
< |
static final class FibAction extends RecursiveAction { |
163 |
> |
final class FibAction extends CheckedRecursiveAction { |
164 |
|
final int number; |
165 |
|
int result; |
166 |
|
FibAction(int n) { number = n; } |
167 |
< |
public void compute() { |
167 |
> |
public void realCompute() { |
168 |
|
int n = number; |
169 |
|
if (n <= 1) |
170 |
|
result = n; |
201 |
|
* completed tasks. getRawResult of a RecursiveAction returns null; |
202 |
|
*/ |
203 |
|
public void testInvoke() { |
204 |
< |
RecursiveAction a = new RecursiveAction() { |
205 |
< |
public void compute() { |
204 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
205 |
> |
public void realCompute() { |
206 |
|
FibAction f = new FibAction(8); |
207 |
< |
f.invoke(); |
208 |
< |
threadAssertTrue(f.result == 21); |
209 |
< |
threadAssertTrue(f.isDone()); |
95 |
< |
threadAssertFalse(f.isCancelled()); |
96 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
97 |
< |
threadAssertTrue(f.getRawResult() == null); |
207 |
> |
assertNull(f.invoke()); |
208 |
> |
assertEquals(21, f.result); |
209 |
> |
checkCompletedNormally(f); |
210 |
|
}}; |
211 |
|
testInvokeOnPool(mainPool(), a); |
212 |
|
} |
217 |
|
* completed tasks |
218 |
|
*/ |
219 |
|
public void testQuietlyInvoke() { |
220 |
< |
RecursiveAction a = new RecursiveAction() { |
221 |
< |
public void compute() { |
220 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
221 |
> |
public void realCompute() { |
222 |
|
FibAction f = new FibAction(8); |
223 |
|
f.quietlyInvoke(); |
224 |
< |
threadAssertTrue(f.result == 21); |
225 |
< |
threadAssertTrue(f.isDone()); |
114 |
< |
threadAssertFalse(f.isCancelled()); |
115 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
116 |
< |
threadAssertTrue(f.getRawResult() == null); |
224 |
> |
assertEquals(21, f.result); |
225 |
> |
checkCompletedNormally(f); |
226 |
|
}}; |
227 |
|
testInvokeOnPool(mainPool(), a); |
228 |
|
} |
231 |
|
* join of a forked task returns when task completes |
232 |
|
*/ |
233 |
|
public void testForkJoin() { |
234 |
< |
RecursiveAction a = new RecursiveAction() { |
235 |
< |
public void compute() { |
234 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
235 |
> |
public void realCompute() { |
236 |
|
FibAction f = new FibAction(8); |
237 |
< |
f.fork(); |
238 |
< |
f.join(); |
239 |
< |
threadAssertTrue(f.result == 21); |
240 |
< |
threadAssertTrue(f.isDone()); |
132 |
< |
threadAssertTrue(f.getRawResult() == null); |
237 |
> |
assertSame(f, f.fork()); |
238 |
> |
assertNull(f.join()); |
239 |
> |
assertEquals(21, f.result); |
240 |
> |
checkCompletedNormally(f); |
241 |
|
}}; |
242 |
|
testInvokeOnPool(mainPool(), a); |
243 |
|
} |
246 |
|
* get of a forked task returns when task completes |
247 |
|
*/ |
248 |
|
public void testForkGet() { |
249 |
< |
RecursiveAction a = new RecursiveAction() { |
250 |
< |
public void compute() { |
251 |
< |
try { |
252 |
< |
FibAction f = new FibAction(8); |
253 |
< |
f.fork(); |
254 |
< |
f.get(); |
255 |
< |
threadAssertTrue(f.result == 21); |
148 |
< |
threadAssertTrue(f.isDone()); |
149 |
< |
} catch (Exception ex) { |
150 |
< |
unexpectedException(ex); |
151 |
< |
} |
249 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
250 |
> |
public void realCompute() throws Exception { |
251 |
> |
FibAction f = new FibAction(8); |
252 |
> |
assertSame(f, f.fork()); |
253 |
> |
assertNull(f.get()); |
254 |
> |
assertEquals(21, f.result); |
255 |
> |
checkCompletedNormally(f); |
256 |
|
}}; |
257 |
|
testInvokeOnPool(mainPool(), a); |
258 |
|
} |
261 |
|
* timed get of a forked task returns when task completes |
262 |
|
*/ |
263 |
|
public void testForkTimedGet() { |
264 |
< |
RecursiveAction a = new RecursiveAction() { |
265 |
< |
public void compute() { |
266 |
< |
try { |
267 |
< |
FibAction f = new FibAction(8); |
268 |
< |
f.fork(); |
269 |
< |
f.get(5L, TimeUnit.SECONDS); |
270 |
< |
threadAssertTrue(f.result == 21); |
167 |
< |
threadAssertTrue(f.isDone()); |
168 |
< |
} catch (Exception ex) { |
169 |
< |
unexpectedException(ex); |
170 |
< |
} |
264 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
265 |
> |
public void realCompute() throws Exception { |
266 |
> |
FibAction f = new FibAction(8); |
267 |
> |
assertSame(f, f.fork()); |
268 |
> |
assertNull(f.get(5L, SECONDS)); |
269 |
> |
assertEquals(21, f.result); |
270 |
> |
checkCompletedNormally(f); |
271 |
|
}}; |
272 |
|
testInvokeOnPool(mainPool(), a); |
273 |
|
} |
276 |
|
* timed get with null time unit throws NPE |
277 |
|
*/ |
278 |
|
public void testForkTimedGetNPE() { |
279 |
< |
RecursiveAction a = new RecursiveAction() { |
280 |
< |
public void compute() { |
279 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
280 |
> |
public void realCompute() throws Exception { |
281 |
> |
FibAction f = new FibAction(8); |
282 |
> |
assertSame(f, f.fork()); |
283 |
|
try { |
182 |
– |
FibAction f = new FibAction(8); |
183 |
– |
f.fork(); |
284 |
|
f.get(5L, null); |
285 |
|
shouldThrow(); |
286 |
< |
} catch (NullPointerException success) { |
187 |
< |
} catch (Exception ex) { |
188 |
< |
unexpectedException(ex); |
189 |
< |
} |
286 |
> |
} catch (NullPointerException success) {} |
287 |
|
}}; |
288 |
|
testInvokeOnPool(mainPool(), a); |
289 |
|
} |
292 |
|
* quietlyJoin of a forked task returns when task completes |
293 |
|
*/ |
294 |
|
public void testForkQuietlyJoin() { |
295 |
< |
RecursiveAction a = new RecursiveAction() { |
296 |
< |
public void compute() { |
295 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
296 |
> |
public void realCompute() { |
297 |
|
FibAction f = new FibAction(8); |
298 |
< |
f.fork(); |
298 |
> |
assertSame(f, f.fork()); |
299 |
|
f.quietlyJoin(); |
300 |
< |
threadAssertTrue(f.result == 21); |
301 |
< |
threadAssertTrue(f.isDone()); |
300 |
> |
assertEquals(21, f.result); |
301 |
> |
checkCompletedNormally(f); |
302 |
|
}}; |
303 |
|
testInvokeOnPool(mainPool(), a); |
304 |
|
} |
309 |
|
* getQueuedTaskCount returns 0 when quiescent |
310 |
|
*/ |
311 |
|
public void testForkHelpQuiesce() { |
312 |
< |
RecursiveAction a = new RecursiveAction() { |
313 |
< |
public void compute() { |
312 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
313 |
> |
public void realCompute() { |
314 |
|
FibAction f = new FibAction(8); |
315 |
< |
f.fork(); |
315 |
> |
assertSame(f, f.fork()); |
316 |
|
f.helpQuiesce(); |
317 |
< |
threadAssertTrue(f.result == 21); |
318 |
< |
threadAssertTrue(f.isDone()); |
319 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
317 |
> |
assertEquals(21, f.result); |
318 |
> |
assertEquals(0, getQueuedTaskCount()); |
319 |
> |
checkCompletedNormally(f); |
320 |
|
}}; |
321 |
|
testInvokeOnPool(mainPool(), a); |
322 |
|
} |
326 |
|
* invoke task throws exception when task completes abnormally |
327 |
|
*/ |
328 |
|
public void testAbnormalInvoke() { |
329 |
< |
RecursiveAction a = new RecursiveAction() { |
330 |
< |
public void compute() { |
329 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
330 |
> |
public void realCompute() { |
331 |
> |
FailingFibAction f = new FailingFibAction(8); |
332 |
|
try { |
235 |
– |
FailingFibAction f = new FailingFibAction(8); |
333 |
|
f.invoke(); |
334 |
|
shouldThrow(); |
335 |
|
} catch (FJException success) { |
336 |
+ |
checkCompletedAbnormally(f, success); |
337 |
|
} |
338 |
|
}}; |
339 |
|
testInvokeOnPool(mainPool(), a); |
343 |
|
* quietlyInvoke task returns when task completes abnormally |
344 |
|
*/ |
345 |
|
public void testAbnormalQuietlyInvoke() { |
346 |
< |
RecursiveAction a = new RecursiveAction() { |
347 |
< |
public void compute() { |
346 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
347 |
> |
public void realCompute() { |
348 |
|
FailingFibAction f = new FailingFibAction(8); |
349 |
|
f.quietlyInvoke(); |
350 |
< |
threadAssertTrue(f.isDone()); |
350 |
> |
assertTrue(f.getException() instanceof FJException); |
351 |
> |
checkCompletedAbnormally(f, f.getException()); |
352 |
|
}}; |
353 |
|
testInvokeOnPool(mainPool(), a); |
354 |
|
} |
357 |
|
* join of a forked task throws exception when task completes abnormally |
358 |
|
*/ |
359 |
|
public void testAbnormalForkJoin() { |
360 |
< |
RecursiveAction a = new RecursiveAction() { |
361 |
< |
public void compute() { |
360 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
361 |
> |
public void realCompute() { |
362 |
> |
FailingFibAction f = new FailingFibAction(8); |
363 |
> |
assertSame(f, f.fork()); |
364 |
|
try { |
264 |
– |
FailingFibAction f = new FailingFibAction(8); |
265 |
– |
f.fork(); |
365 |
|
f.join(); |
366 |
|
shouldThrow(); |
367 |
|
} catch (FJException success) { |
368 |
+ |
checkCompletedAbnormally(f, success); |
369 |
|
} |
370 |
|
}}; |
371 |
|
testInvokeOnPool(mainPool(), a); |
375 |
|
* get of a forked task throws exception when task completes abnormally |
376 |
|
*/ |
377 |
|
public void testAbnormalForkGet() { |
378 |
< |
RecursiveAction a = new RecursiveAction() { |
379 |
< |
public void compute() { |
378 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
379 |
> |
public void realCompute() throws Exception { |
380 |
> |
FailingFibAction f = new FailingFibAction(8); |
381 |
> |
assertSame(f, f.fork()); |
382 |
|
try { |
281 |
– |
FailingFibAction f = new FailingFibAction(8); |
282 |
– |
f.fork(); |
383 |
|
f.get(); |
384 |
|
shouldThrow(); |
385 |
|
} catch (ExecutionException success) { |
386 |
< |
} catch (Exception ex) { |
387 |
< |
unexpectedException(ex); |
386 |
> |
Throwable cause = success.getCause(); |
387 |
> |
assertTrue(cause instanceof FJException); |
388 |
> |
checkCompletedAbnormally(f, cause); |
389 |
|
} |
390 |
|
}}; |
391 |
|
testInvokeOnPool(mainPool(), a); |
395 |
|
* timed get of a forked task throws exception when task completes abnormally |
396 |
|
*/ |
397 |
|
public void testAbnormalForkTimedGet() { |
398 |
< |
RecursiveAction a = new RecursiveAction() { |
399 |
< |
public void compute() { |
398 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
399 |
> |
public void realCompute() throws Exception { |
400 |
> |
FailingFibAction f = new FailingFibAction(8); |
401 |
> |
assertSame(f, f.fork()); |
402 |
|
try { |
300 |
– |
FailingFibAction f = new FailingFibAction(8); |
301 |
– |
f.fork(); |
403 |
|
f.get(5L, TimeUnit.SECONDS); |
404 |
|
shouldThrow(); |
405 |
|
} catch (ExecutionException success) { |
406 |
< |
} catch (Exception ex) { |
407 |
< |
unexpectedException(ex); |
406 |
> |
Throwable cause = success.getCause(); |
407 |
> |
assertTrue(cause instanceof FJException); |
408 |
> |
checkCompletedAbnormally(f, cause); |
409 |
|
} |
410 |
|
}}; |
411 |
|
testInvokeOnPool(mainPool(), a); |
415 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
416 |
|
*/ |
417 |
|
public void testAbnormalForkQuietlyJoin() { |
418 |
< |
RecursiveAction a = new RecursiveAction() { |
419 |
< |
public void compute() { |
418 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
419 |
> |
public void realCompute() { |
420 |
|
FailingFibAction f = new FailingFibAction(8); |
421 |
< |
f.fork(); |
421 |
> |
assertSame(f, f.fork()); |
422 |
|
f.quietlyJoin(); |
423 |
< |
threadAssertTrue(f.isDone()); |
424 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
323 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
423 |
> |
assertTrue(f.getException() instanceof FJException); |
424 |
> |
checkCompletedAbnormally(f, f.getException()); |
425 |
|
}}; |
426 |
|
testInvokeOnPool(mainPool(), a); |
427 |
|
} |
430 |
|
* invoke task throws exception when task cancelled |
431 |
|
*/ |
432 |
|
public void testCancelledInvoke() { |
433 |
< |
RecursiveAction a = new RecursiveAction() { |
434 |
< |
public void compute() { |
433 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
434 |
> |
public void realCompute() { |
435 |
> |
FibAction f = new FibAction(8); |
436 |
> |
assertTrue(f.cancel(true)); |
437 |
|
try { |
335 |
– |
FibAction f = new FibAction(8); |
336 |
– |
f.cancel(true); |
438 |
|
f.invoke(); |
439 |
|
shouldThrow(); |
440 |
|
} catch (CancellationException success) { |
441 |
+ |
checkCancelled(f); |
442 |
|
} |
443 |
|
}}; |
444 |
|
testInvokeOnPool(mainPool(), a); |
448 |
|
* join of a forked task throws exception when task cancelled |
449 |
|
*/ |
450 |
|
public void testCancelledForkJoin() { |
451 |
< |
RecursiveAction a = new RecursiveAction() { |
452 |
< |
public void compute() { |
451 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
452 |
> |
public void realCompute() { |
453 |
> |
FibAction f = new FibAction(8); |
454 |
> |
assertTrue(f.cancel(true)); |
455 |
> |
assertSame(f, f.fork()); |
456 |
|
try { |
352 |
– |
FibAction f = new FibAction(8); |
353 |
– |
f.cancel(true); |
354 |
– |
f.fork(); |
457 |
|
f.join(); |
458 |
|
shouldThrow(); |
459 |
|
} catch (CancellationException success) { |
460 |
+ |
checkCancelled(f); |
461 |
|
} |
462 |
|
}}; |
463 |
|
testInvokeOnPool(mainPool(), a); |
467 |
|
* get of a forked task throws exception when task cancelled |
468 |
|
*/ |
469 |
|
public void testCancelledForkGet() { |
470 |
< |
RecursiveAction a = new RecursiveAction() { |
471 |
< |
public void compute() { |
470 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
471 |
> |
public void realCompute() throws Exception { |
472 |
> |
FibAction f = new FibAction(8); |
473 |
> |
assertTrue(f.cancel(true)); |
474 |
> |
assertSame(f, f.fork()); |
475 |
|
try { |
370 |
– |
FibAction f = new FibAction(8); |
371 |
– |
f.cancel(true); |
372 |
– |
f.fork(); |
476 |
|
f.get(); |
477 |
|
shouldThrow(); |
478 |
|
} catch (CancellationException success) { |
479 |
< |
} catch (Exception ex) { |
377 |
< |
unexpectedException(ex); |
479 |
> |
checkCancelled(f); |
480 |
|
} |
481 |
|
}}; |
482 |
|
testInvokeOnPool(mainPool(), a); |
486 |
|
* timed get of a forked task throws exception when task cancelled |
487 |
|
*/ |
488 |
|
public void testCancelledForkTimedGet() { |
489 |
< |
RecursiveAction a = new RecursiveAction() { |
490 |
< |
public void compute() { |
489 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
490 |
> |
public void realCompute() throws Exception { |
491 |
> |
FibAction f = new FibAction(8); |
492 |
> |
assertTrue(f.cancel(true)); |
493 |
> |
assertSame(f, f.fork()); |
494 |
|
try { |
495 |
< |
FibAction f = new FibAction(8); |
391 |
< |
f.cancel(true); |
392 |
< |
f.fork(); |
393 |
< |
f.get(5L, TimeUnit.SECONDS); |
495 |
> |
f.get(5L, SECONDS); |
496 |
|
shouldThrow(); |
497 |
|
} catch (CancellationException success) { |
498 |
< |
} catch (Exception ex) { |
397 |
< |
unexpectedException(ex); |
498 |
> |
checkCancelled(f); |
499 |
|
} |
500 |
|
}}; |
501 |
|
testInvokeOnPool(mainPool(), a); |
505 |
|
* quietlyJoin of a forked task returns when task cancelled |
506 |
|
*/ |
507 |
|
public void testCancelledForkQuietlyJoin() { |
508 |
< |
RecursiveAction a = new RecursiveAction() { |
509 |
< |
public void compute() { |
508 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
509 |
> |
public void realCompute() { |
510 |
|
FibAction f = new FibAction(8); |
511 |
< |
f.cancel(true); |
512 |
< |
f.fork(); |
511 |
> |
assertTrue(f.cancel(true)); |
512 |
> |
assertSame(f, f.fork()); |
513 |
|
f.quietlyJoin(); |
514 |
< |
threadAssertTrue(f.isDone()); |
414 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
415 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
514 |
> |
checkCancelled(f); |
515 |
|
}}; |
516 |
|
testInvokeOnPool(mainPool(), a); |
517 |
|
} |
521 |
|
*/ |
522 |
|
public void testGetPool() { |
523 |
|
final ForkJoinPool mainPool = mainPool(); |
524 |
< |
RecursiveAction a = new RecursiveAction() { |
525 |
< |
public void compute() { |
526 |
< |
threadAssertTrue(getPool() == mainPool); |
524 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
525 |
> |
public void realCompute() { |
526 |
> |
assertSame(mainPool, getPool()); |
527 |
|
}}; |
528 |
|
testInvokeOnPool(mainPool, a); |
529 |
|
} |
532 |
|
* getPool of non-FJ task returns null |
533 |
|
*/ |
534 |
|
public void testGetPool2() { |
535 |
< |
RecursiveAction a = new RecursiveAction() { |
536 |
< |
public void compute() { |
537 |
< |
threadAssertTrue(getPool() == null); |
535 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
536 |
> |
public void realCompute() { |
537 |
> |
assertNull(getPool()); |
538 |
|
}}; |
539 |
< |
a.invoke(); |
539 |
> |
assertNull(a.invoke()); |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
|
* inForkJoinPool of executing task returns true |
544 |
|
*/ |
545 |
|
public void testInForkJoinPool() { |
546 |
< |
RecursiveAction a = new RecursiveAction() { |
547 |
< |
public void compute() { |
548 |
< |
threadAssertTrue(inForkJoinPool()); |
546 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
547 |
> |
public void realCompute() { |
548 |
> |
assertTrue(inForkJoinPool()); |
549 |
|
}}; |
550 |
|
testInvokeOnPool(mainPool(), a); |
551 |
|
} |
554 |
|
* inForkJoinPool of non-FJ task returns false |
555 |
|
*/ |
556 |
|
public void testInForkJoinPool2() { |
557 |
< |
RecursiveAction a = new RecursiveAction() { |
558 |
< |
public void compute() { |
559 |
< |
threadAssertTrue(!inForkJoinPool()); |
557 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
558 |
> |
public void realCompute() { |
559 |
> |
assertFalse(inForkJoinPool()); |
560 |
|
}}; |
561 |
< |
a.invoke(); |
561 |
> |
assertNull(a.invoke()); |
562 |
|
} |
563 |
|
|
564 |
|
/** |
566 |
|
*/ |
567 |
|
public void testWorkerGetPool() { |
568 |
|
final ForkJoinPool mainPool = mainPool(); |
569 |
< |
RecursiveAction a = new RecursiveAction() { |
570 |
< |
public void compute() { |
569 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
570 |
> |
public void realCompute() { |
571 |
|
ForkJoinWorkerThread w = |
572 |
|
(ForkJoinWorkerThread) Thread.currentThread(); |
573 |
< |
threadAssertTrue(w.getPool() == mainPool); |
573 |
> |
assertSame(mainPool, w.getPool()); |
574 |
|
}}; |
575 |
|
testInvokeOnPool(mainPool, a); |
576 |
|
} |
580 |
|
*/ |
581 |
|
public void testWorkerGetPoolIndex() { |
582 |
|
final ForkJoinPool mainPool = mainPool(); |
583 |
< |
RecursiveAction a = new RecursiveAction() { |
584 |
< |
public void compute() { |
583 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
584 |
> |
public void realCompute() { |
585 |
|
ForkJoinWorkerThread w = |
586 |
|
(ForkJoinWorkerThread)(Thread.currentThread()); |
587 |
< |
int idx = w.getPoolIndex(); |
588 |
< |
threadAssertTrue(idx >= 0); |
490 |
< |
threadAssertTrue(idx < mainPool.getPoolSize()); |
587 |
> |
assertTrue(w.getPoolIndex() >= 0); |
588 |
> |
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
589 |
|
}}; |
590 |
|
testInvokeOnPool(mainPool, a); |
591 |
|
} |
595 |
|
* setRawResult(null) succeeds |
596 |
|
*/ |
597 |
|
public void testSetRawResult() { |
598 |
< |
RecursiveAction a = new RecursiveAction() { |
599 |
< |
public void compute() { |
598 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
599 |
> |
public void realCompute() { |
600 |
|
setRawResult(null); |
601 |
|
}}; |
602 |
< |
a.invoke(); |
602 |
> |
assertNull(a.invoke()); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
|
* A reinitialized task may be re-invoked |
607 |
|
*/ |
608 |
|
public void testReinitialize() { |
609 |
< |
RecursiveAction a = new RecursiveAction() { |
610 |
< |
public void compute() { |
609 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
610 |
> |
public void realCompute() { |
611 |
|
FibAction f = new FibAction(8); |
612 |
< |
f.invoke(); |
613 |
< |
threadAssertTrue(f.result == 21); |
614 |
< |
threadAssertTrue(f.isDone()); |
615 |
< |
threadAssertFalse(f.isCancelled()); |
616 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
617 |
< |
f.reinitialize(); |
618 |
< |
f.invoke(); |
619 |
< |
threadAssertTrue(f.result == 21); |
612 |
> |
checkNotDone(f); |
613 |
> |
|
614 |
> |
for (int i = 0; i < 3; i++) { |
615 |
> |
assertNull(f.invoke()); |
616 |
> |
assertEquals(21, f.result); |
617 |
> |
checkCompletedNormally(f); |
618 |
> |
f.reinitialize(); |
619 |
> |
checkNotDone(f); |
620 |
> |
} |
621 |
|
}}; |
622 |
|
testInvokeOnPool(mainPool(), a); |
623 |
|
} |
626 |
|
* invoke task throws exception after invoking completeExceptionally |
627 |
|
*/ |
628 |
|
public void testCompleteExceptionally() { |
629 |
< |
RecursiveAction a = new RecursiveAction() { |
630 |
< |
public void compute() { |
629 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
630 |
> |
public void realCompute() { |
631 |
> |
FibAction f = new FibAction(8); |
632 |
> |
f.completeExceptionally(new FJException()); |
633 |
|
try { |
533 |
– |
FibAction f = new FibAction(8); |
534 |
– |
f.completeExceptionally(new FJException()); |
634 |
|
f.invoke(); |
635 |
|
shouldThrow(); |
636 |
|
} catch (FJException success) { |
637 |
+ |
checkCompletedAbnormally(f, success); |
638 |
|
} |
639 |
|
}}; |
640 |
|
testInvokeOnPool(mainPool(), a); |
644 |
|
* invoke task suppresses execution invoking complete |
645 |
|
*/ |
646 |
|
public void testComplete() { |
647 |
< |
RecursiveAction a = new RecursiveAction() { |
648 |
< |
public void compute() { |
647 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
648 |
> |
public void realCompute() { |
649 |
|
FibAction f = new FibAction(8); |
650 |
|
f.complete(null); |
651 |
< |
f.invoke(); |
652 |
< |
threadAssertTrue(f.isDone()); |
653 |
< |
threadAssertTrue(f.result == 0); |
651 |
> |
assertNull(f.invoke()); |
652 |
> |
assertEquals(0, f.result); |
653 |
> |
checkCompletedNormally(f); |
654 |
|
}}; |
655 |
|
testInvokeOnPool(mainPool(), a); |
656 |
|
} |
659 |
|
* invokeAll(t1, t2) invokes all task arguments |
660 |
|
*/ |
661 |
|
public void testInvokeAll2() { |
662 |
< |
RecursiveAction a = new RecursiveAction() { |
663 |
< |
public void compute() { |
662 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
663 |
> |
public void realCompute() { |
664 |
|
FibAction f = new FibAction(8); |
665 |
|
FibAction g = new FibAction(9); |
666 |
|
invokeAll(f, g); |
667 |
< |
threadAssertTrue(f.isDone()); |
668 |
< |
threadAssertTrue(f.result == 21); |
669 |
< |
threadAssertTrue(g.isDone()); |
670 |
< |
threadAssertTrue(g.result == 34); |
667 |
> |
checkCompletedNormally(f); |
668 |
> |
assertEquals(21, f.result); |
669 |
> |
checkCompletedNormally(g); |
670 |
> |
assertEquals(34, g.result); |
671 |
|
}}; |
672 |
|
testInvokeOnPool(mainPool(), a); |
673 |
|
} |
676 |
|
* invokeAll(tasks) with 1 argument invokes task |
677 |
|
*/ |
678 |
|
public void testInvokeAll1() { |
679 |
< |
RecursiveAction a = new RecursiveAction() { |
680 |
< |
public void compute() { |
679 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
680 |
> |
public void realCompute() { |
681 |
|
FibAction f = new FibAction(8); |
682 |
|
invokeAll(f); |
683 |
< |
threadAssertTrue(f.isDone()); |
684 |
< |
threadAssertTrue(f.result == 21); |
683 |
> |
checkCompletedNormally(f); |
684 |
> |
assertEquals(21, f.result); |
685 |
|
}}; |
686 |
|
testInvokeOnPool(mainPool(), a); |
687 |
|
} |
690 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
691 |
|
*/ |
692 |
|
public void testInvokeAll3() { |
693 |
< |
RecursiveAction a = new RecursiveAction() { |
694 |
< |
public void compute() { |
693 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
694 |
> |
public void realCompute() { |
695 |
|
FibAction f = new FibAction(8); |
696 |
|
FibAction g = new FibAction(9); |
697 |
|
FibAction h = new FibAction(7); |
698 |
|
invokeAll(f, g, h); |
699 |
< |
threadAssertTrue(f.isDone()); |
700 |
< |
threadAssertTrue(f.result == 21); |
701 |
< |
threadAssertTrue(g.isDone()); |
702 |
< |
threadAssertTrue(g.result == 34); |
703 |
< |
threadAssertTrue(h.isDone()); |
704 |
< |
threadAssertTrue(h.result == 13); |
699 |
> |
assertTrue(f.isDone()); |
700 |
> |
assertTrue(g.isDone()); |
701 |
> |
assertTrue(h.isDone()); |
702 |
> |
checkCompletedNormally(f); |
703 |
> |
assertEquals(21, f.result); |
704 |
> |
checkCompletedNormally(g); |
705 |
> |
assertEquals(34, g.result); |
706 |
> |
checkCompletedNormally(g); |
707 |
> |
assertEquals(13, h.result); |
708 |
|
}}; |
709 |
|
testInvokeOnPool(mainPool(), a); |
710 |
|
} |
713 |
|
* invokeAll(collection) invokes all tasks in the collection |
714 |
|
*/ |
715 |
|
public void testInvokeAllCollection() { |
716 |
< |
RecursiveAction a = new RecursiveAction() { |
717 |
< |
public void compute() { |
716 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
717 |
> |
public void realCompute() { |
718 |
|
FibAction f = new FibAction(8); |
719 |
|
FibAction g = new FibAction(9); |
720 |
|
FibAction h = new FibAction(7); |
723 |
|
set.add(g); |
724 |
|
set.add(h); |
725 |
|
invokeAll(set); |
726 |
< |
threadAssertTrue(f.isDone()); |
727 |
< |
threadAssertTrue(f.result == 21); |
728 |
< |
threadAssertTrue(g.isDone()); |
729 |
< |
threadAssertTrue(g.result == 34); |
730 |
< |
threadAssertTrue(h.isDone()); |
731 |
< |
threadAssertTrue(h.result == 13); |
726 |
> |
assertTrue(f.isDone()); |
727 |
> |
assertTrue(g.isDone()); |
728 |
> |
assertTrue(h.isDone()); |
729 |
> |
checkCompletedNormally(f); |
730 |
> |
assertEquals(21, f.result); |
731 |
> |
checkCompletedNormally(g); |
732 |
> |
assertEquals(34, g.result); |
733 |
> |
checkCompletedNormally(g); |
734 |
> |
assertEquals(13, h.result); |
735 |
|
}}; |
736 |
|
testInvokeOnPool(mainPool(), a); |
737 |
|
} |
741 |
|
* invokeAll(tasks) with any null task throws NPE |
742 |
|
*/ |
743 |
|
public void testInvokeAllNPE() { |
744 |
< |
RecursiveAction a = new RecursiveAction() { |
745 |
< |
public void compute() { |
744 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
745 |
> |
public void realCompute() { |
746 |
> |
FibAction f = new FibAction(8); |
747 |
> |
FibAction g = new FibAction(9); |
748 |
> |
FibAction h = null; |
749 |
|
try { |
641 |
– |
FibAction f = new FibAction(8); |
642 |
– |
FibAction g = new FibAction(9); |
643 |
– |
FibAction h = null; |
750 |
|
invokeAll(f, g, h); |
751 |
|
shouldThrow(); |
752 |
< |
} catch (NullPointerException success) { |
647 |
< |
} |
752 |
> |
} catch (NullPointerException success) {} |
753 |
|
}}; |
754 |
|
testInvokeOnPool(mainPool(), a); |
755 |
|
} |
758 |
|
* invokeAll(t1, t2) throw exception if any task does |
759 |
|
*/ |
760 |
|
public void testAbnormalInvokeAll2() { |
761 |
< |
RecursiveAction a = new RecursiveAction() { |
762 |
< |
public void compute() { |
761 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
762 |
> |
public void realCompute() { |
763 |
> |
FibAction f = new FibAction(8); |
764 |
> |
FailingFibAction g = new FailingFibAction(9); |
765 |
|
try { |
659 |
– |
FibAction f = new FibAction(8); |
660 |
– |
FailingFibAction g = new FailingFibAction(9); |
766 |
|
invokeAll(f, g); |
767 |
|
shouldThrow(); |
768 |
|
} catch (FJException success) { |
769 |
+ |
checkCompletedAbnormally(g, success); |
770 |
|
} |
771 |
|
}}; |
772 |
|
testInvokeOnPool(mainPool(), a); |
776 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
777 |
|
*/ |
778 |
|
public void testAbnormalInvokeAll1() { |
779 |
< |
RecursiveAction a = new RecursiveAction() { |
780 |
< |
public void compute() { |
779 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
780 |
> |
public void realCompute() { |
781 |
> |
FailingFibAction g = new FailingFibAction(9); |
782 |
|
try { |
676 |
– |
FailingFibAction g = new FailingFibAction(9); |
783 |
|
invokeAll(g); |
784 |
|
shouldThrow(); |
785 |
|
} catch (FJException success) { |
786 |
+ |
checkCompletedAbnormally(g, success); |
787 |
|
} |
788 |
|
}}; |
789 |
|
testInvokeOnPool(mainPool(), a); |
793 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
794 |
|
*/ |
795 |
|
public void testAbnormalInvokeAll3() { |
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 |
> |
FailingFibAction g = new FailingFibAction(9); |
800 |
> |
FibAction h = new FibAction(7); |
801 |
|
try { |
692 |
– |
FibAction f = new FibAction(8); |
693 |
– |
FailingFibAction g = new FailingFibAction(9); |
694 |
– |
FibAction h = new FibAction(7); |
802 |
|
invokeAll(f, g, h); |
803 |
|
shouldThrow(); |
804 |
|
} catch (FJException success) { |
805 |
+ |
checkCompletedAbnormally(g, success); |
806 |
|
} |
807 |
|
}}; |
808 |
|
testInvokeOnPool(mainPool(), a); |
812 |
|
* invokeAll(collection) throws exception if any task does |
813 |
|
*/ |
814 |
|
public void testAbnormalInvokeAllCollection() { |
815 |
< |
RecursiveAction a = new RecursiveAction() { |
816 |
< |
public void compute() { |
815 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
816 |
> |
public void realCompute() { |
817 |
> |
FailingFibAction f = new FailingFibAction(8); |
818 |
> |
FibAction g = new FibAction(9); |
819 |
> |
FibAction h = new FibAction(7); |
820 |
> |
HashSet set = new HashSet(); |
821 |
> |
set.add(f); |
822 |
> |
set.add(g); |
823 |
> |
set.add(h); |
824 |
|
try { |
710 |
– |
FailingFibAction f = new FailingFibAction(8); |
711 |
– |
FibAction g = new FibAction(9); |
712 |
– |
FibAction h = new FibAction(7); |
713 |
– |
HashSet set = new HashSet(); |
714 |
– |
set.add(f); |
715 |
– |
set.add(g); |
716 |
– |
set.add(h); |
825 |
|
invokeAll(set); |
826 |
|
shouldThrow(); |
827 |
|
} catch (FJException success) { |
828 |
+ |
checkCompletedAbnormally(f, success); |
829 |
|
} |
830 |
|
}}; |
831 |
|
testInvokeOnPool(mainPool(), a); |
836 |
|
* and suppresses execution |
837 |
|
*/ |
838 |
|
public void testTryUnfork() { |
839 |
< |
RecursiveAction a = new RecursiveAction() { |
840 |
< |
public void compute() { |
839 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
840 |
> |
public void realCompute() { |
841 |
|
FibAction g = new FibAction(9); |
842 |
< |
g.fork(); |
842 |
> |
assertSame(g, g.fork()); |
843 |
|
FibAction f = new FibAction(8); |
844 |
< |
f.fork(); |
845 |
< |
threadAssertTrue(f.tryUnfork()); |
844 |
> |
assertSame(f, f.fork()); |
845 |
> |
assertTrue(f.tryUnfork()); |
846 |
|
helpQuiesce(); |
847 |
< |
threadAssertFalse(f.isDone()); |
848 |
< |
threadAssertTrue(g.isDone()); |
847 |
> |
checkNotDone(f); |
848 |
> |
checkCompletedNormally(g); |
849 |
|
}}; |
850 |
|
testInvokeOnPool(singletonPool(), a); |
851 |
|
} |
855 |
|
* there are more tasks than threads |
856 |
|
*/ |
857 |
|
public void testGetSurplusQueuedTaskCount() { |
858 |
< |
RecursiveAction a = new RecursiveAction() { |
859 |
< |
public void compute() { |
858 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
859 |
> |
public void realCompute() { |
860 |
|
FibAction h = new FibAction(7); |
861 |
< |
h.fork(); |
861 |
> |
assertSame(h, h.fork()); |
862 |
|
FibAction g = new FibAction(9); |
863 |
< |
g.fork(); |
863 |
> |
assertSame(g, g.fork()); |
864 |
|
FibAction f = new FibAction(8); |
865 |
< |
f.fork(); |
866 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
865 |
> |
assertSame(f, f.fork()); |
866 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
867 |
|
helpQuiesce(); |
868 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
869 |
+ |
checkCompletedNormally(f); |
870 |
+ |
checkCompletedNormally(g); |
871 |
+ |
checkCompletedNormally(h); |
872 |
|
}}; |
873 |
|
testInvokeOnPool(singletonPool(), a); |
874 |
|
} |
877 |
|
* peekNextLocalTask returns most recent unexecuted task. |
878 |
|
*/ |
879 |
|
public void testPeekNextLocalTask() { |
880 |
< |
RecursiveAction a = new RecursiveAction() { |
881 |
< |
public void compute() { |
880 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
881 |
> |
public void realCompute() { |
882 |
|
FibAction g = new FibAction(9); |
883 |
< |
g.fork(); |
883 |
> |
assertSame(g, g.fork()); |
884 |
|
FibAction f = new FibAction(8); |
885 |
< |
f.fork(); |
886 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
887 |
< |
f.join(); |
888 |
< |
threadAssertTrue(f.isDone()); |
885 |
> |
assertSame(f, f.fork()); |
886 |
> |
assertSame(f, peekNextLocalTask()); |
887 |
> |
assertNull(f.join()); |
888 |
> |
checkCompletedNormally(f); |
889 |
|
helpQuiesce(); |
890 |
+ |
checkCompletedNormally(f); |
891 |
+ |
checkCompletedNormally(g); |
892 |
|
}}; |
893 |
|
testInvokeOnPool(singletonPool(), a); |
894 |
|
} |
898 |
|
* without executing it |
899 |
|
*/ |
900 |
|
public void testPollNextLocalTask() { |
901 |
< |
RecursiveAction a = new RecursiveAction() { |
902 |
< |
public void compute() { |
901 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
902 |
> |
public void realCompute() { |
903 |
|
FibAction g = new FibAction(9); |
904 |
< |
g.fork(); |
904 |
> |
assertSame(g, g.fork()); |
905 |
|
FibAction f = new FibAction(8); |
906 |
< |
f.fork(); |
907 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
906 |
> |
assertSame(f, f.fork()); |
907 |
> |
assertSame(f, pollNextLocalTask()); |
908 |
|
helpQuiesce(); |
909 |
< |
threadAssertFalse(f.isDone()); |
909 |
> |
checkNotDone(f); |
910 |
> |
checkCompletedNormally(g); |
911 |
|
}}; |
912 |
|
testInvokeOnPool(singletonPool(), a); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
< |
* pollTask returns an unexecuted task |
801 |
< |
* without executing it |
916 |
> |
* pollTask returns an unexecuted task without executing it |
917 |
|
*/ |
918 |
|
public void testPollTask() { |
919 |
< |
RecursiveAction a = new RecursiveAction() { |
920 |
< |
public void compute() { |
919 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
920 |
> |
public void realCompute() { |
921 |
|
FibAction g = new FibAction(9); |
922 |
< |
g.fork(); |
922 |
> |
assertSame(g, g.fork()); |
923 |
|
FibAction f = new FibAction(8); |
924 |
< |
f.fork(); |
925 |
< |
threadAssertTrue(pollTask() == f); |
924 |
> |
assertSame(f, f.fork()); |
925 |
> |
assertSame(f, pollTask()); |
926 |
|
helpQuiesce(); |
927 |
< |
threadAssertFalse(f.isDone()); |
928 |
< |
threadAssertTrue(g.isDone()); |
927 |
> |
checkNotDone(f); |
928 |
> |
checkCompletedNormally(g); |
929 |
|
}}; |
930 |
|
testInvokeOnPool(singletonPool(), a); |
931 |
|
} |
934 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
935 |
|
*/ |
936 |
|
public void testPeekNextLocalTaskAsync() { |
937 |
< |
RecursiveAction a = new RecursiveAction() { |
938 |
< |
public void compute() { |
937 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
938 |
> |
public void realCompute() { |
939 |
|
FibAction g = new FibAction(9); |
940 |
< |
g.fork(); |
940 |
> |
assertSame(g, g.fork()); |
941 |
|
FibAction f = new FibAction(8); |
942 |
< |
f.fork(); |
943 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
944 |
< |
f.join(); |
942 |
> |
assertSame(f, f.fork()); |
943 |
> |
assertSame(g, peekNextLocalTask()); |
944 |
> |
assertNull(f.join()); |
945 |
|
helpQuiesce(); |
946 |
< |
threadAssertTrue(f.isDone()); |
946 |
> |
checkCompletedNormally(f); |
947 |
> |
checkCompletedNormally(g); |
948 |
|
}}; |
949 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
950 |
|
} |
951 |
|
|
952 |
|
/** |
953 |
< |
* pollNextLocalTask returns least recent unexecuted task |
954 |
< |
* without executing it, in async mode |
953 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
954 |
> |
* executing it, in async mode |
955 |
|
*/ |
956 |
|
public void testPollNextLocalTaskAsync() { |
957 |
< |
RecursiveAction a = new RecursiveAction() { |
958 |
< |
public void compute() { |
957 |
> |
RecursiveAction a = new CheckedRecursiveAction() { |
958 |
> |
public void realCompute() { |
959 |
|
FibAction g = new FibAction(9); |
960 |
< |
g.fork(); |
960 |
> |
assertSame(g, g.fork()); |
961 |
|
FibAction f = new FibAction(8); |
962 |
< |
f.fork(); |
963 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
962 |
> |
assertSame(f, f.fork()); |
963 |
> |
assertSame(g, pollNextLocalTask()); |
964 |
|
helpQuiesce(); |
965 |
< |
threadAssertTrue(f.isDone()); |
966 |
< |
threadAssertFalse(g.isDone()); |
965 |
> |
checkCompletedNormally(f); |
966 |
> |
checkNotDone(g); |
967 |
|
}}; |
968 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
969 |
|
} |
970 |
|
|
971 |
|
/** |
972 |
< |
* pollTask returns an unexecuted task |
973 |
< |
* without executing it, in async mode |
972 |
> |
* pollTask returns an unexecuted task without executing it, in |
973 |
> |
* async mode |
974 |
|
*/ |
975 |
|
public void testPollTaskAsync() { |
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(pollTask() == g); |
981 |
> |
assertSame(f, f.fork()); |
982 |
> |
assertSame(g, pollTask()); |
983 |
|
helpQuiesce(); |
984 |
< |
threadAssertTrue(f.isDone()); |
985 |
< |
threadAssertFalse(g.isDone()); |
984 |
> |
checkCompletedNormally(f); |
985 |
> |
checkNotDone(g); |
986 |
|
}}; |
987 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
988 |
|
} |