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.ForkJoinWorkerThread; |
91 |
|
assertNull(a.getException()); |
92 |
|
assertNull(a.getRawResult()); |
93 |
|
assertNull(a.join()); |
94 |
+ |
assertFalse(a.cancel(false)); |
95 |
+ |
assertFalse(a.cancel(true)); |
96 |
|
try { |
97 |
|
assertNull(a.get()); |
98 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
128 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
129 |
|
} |
130 |
|
|
131 |
< |
void checkTaskThrew(RecursiveAction a, Throwable t) { |
131 |
> |
void checkCompletedAbnormally(RecursiveAction a, Throwable t) { |
132 |
|
assertTrue(a.isDone()); |
133 |
|
assertFalse(a.isCancelled()); |
134 |
|
assertFalse(a.isCompletedNormally()); |
135 |
|
assertTrue(a.isCompletedAbnormally()); |
136 |
|
assertSame(t, a.getException()); |
137 |
|
assertNull(a.getRawResult()); |
138 |
+ |
assertFalse(a.cancel(false)); |
139 |
+ |
assertFalse(a.cancel(true)); |
140 |
|
|
141 |
|
try { |
142 |
|
a.join(); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
+ |
* join/quietlyJoin of a forked task succeeds in the presence of interrupts |
252 |
+ |
*/ |
253 |
+ |
public void testJoinIgnoresInterrupts() { |
254 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
255 |
+ |
public void realCompute() { |
256 |
+ |
FibAction f = new FibAction(8); |
257 |
+ |
|
258 |
+ |
// test join() |
259 |
+ |
assertSame(f, f.fork()); |
260 |
+ |
Thread.currentThread().interrupt(); |
261 |
+ |
assertNull(f.join()); |
262 |
+ |
Thread.interrupted(); |
263 |
+ |
assertEquals(21, f.result); |
264 |
+ |
checkCompletedNormally(f); |
265 |
+ |
|
266 |
+ |
f.reinitialize(); |
267 |
+ |
f.cancel(true); |
268 |
+ |
assertSame(f, f.fork()); |
269 |
+ |
Thread.currentThread().interrupt(); |
270 |
+ |
try { |
271 |
+ |
f.join(); |
272 |
+ |
shouldThrow(); |
273 |
+ |
} catch (CancellationException success) { |
274 |
+ |
Thread.interrupted(); |
275 |
+ |
checkCancelled(f); |
276 |
+ |
} |
277 |
+ |
|
278 |
+ |
f.reinitialize(); |
279 |
+ |
f.completeExceptionally(new FJException()); |
280 |
+ |
assertSame(f, f.fork()); |
281 |
+ |
Thread.currentThread().interrupt(); |
282 |
+ |
try { |
283 |
+ |
f.join(); |
284 |
+ |
shouldThrow(); |
285 |
+ |
} catch (FJException success) { |
286 |
+ |
Thread.interrupted(); |
287 |
+ |
checkCompletedAbnormally(f, success); |
288 |
+ |
} |
289 |
+ |
|
290 |
+ |
// test quietlyJoin() |
291 |
+ |
f.reinitialize(); |
292 |
+ |
assertSame(f, f.fork()); |
293 |
+ |
Thread.currentThread().interrupt(); |
294 |
+ |
f.quietlyJoin(); |
295 |
+ |
Thread.interrupted(); |
296 |
+ |
assertEquals(21, f.result); |
297 |
+ |
checkCompletedNormally(f); |
298 |
+ |
|
299 |
+ |
f.reinitialize(); |
300 |
+ |
f.cancel(true); |
301 |
+ |
assertSame(f, f.fork()); |
302 |
+ |
Thread.currentThread().interrupt(); |
303 |
+ |
f.quietlyJoin(); |
304 |
+ |
Thread.interrupted(); |
305 |
+ |
checkCancelled(f); |
306 |
+ |
|
307 |
+ |
f.reinitialize(); |
308 |
+ |
f.completeExceptionally(new FJException()); |
309 |
+ |
assertSame(f, f.fork()); |
310 |
+ |
Thread.currentThread().interrupt(); |
311 |
+ |
f.quietlyJoin(); |
312 |
+ |
Thread.interrupted(); |
313 |
+ |
checkCompletedAbnormally(f, f.getException()); |
314 |
+ |
}}; |
315 |
+ |
testInvokeOnPool(mainPool(), a); |
316 |
+ |
a.reinitialize(); |
317 |
+ |
testInvokeOnPool(singletonPool(), a); |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
/** |
321 |
+ |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
322 |
+ |
* succeeds in the presence of interrupts |
323 |
+ |
*/ |
324 |
+ |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
325 |
+ |
final SynchronousQueue<FibAction[]> sq = |
326 |
+ |
new SynchronousQueue<FibAction[]>(); |
327 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
328 |
+ |
public void realCompute() throws InterruptedException { |
329 |
+ |
FibAction[] fibActions = new FibAction[6]; |
330 |
+ |
for (int i = 0; i < fibActions.length; i++) |
331 |
+ |
fibActions[i] = new FibAction(8); |
332 |
+ |
|
333 |
+ |
fibActions[1].cancel(false); |
334 |
+ |
fibActions[2].completeExceptionally(new FJException()); |
335 |
+ |
fibActions[4].cancel(true); |
336 |
+ |
fibActions[5].completeExceptionally(new FJException()); |
337 |
+ |
|
338 |
+ |
for (int i = 0; i < fibActions.length; i++) |
339 |
+ |
fibActions[i].fork(); |
340 |
+ |
|
341 |
+ |
sq.put(fibActions); |
342 |
+ |
|
343 |
+ |
helpQuiesce(); |
344 |
+ |
}}; |
345 |
+ |
|
346 |
+ |
Runnable r = new CheckedRunnable() { |
347 |
+ |
public void realRun() throws InterruptedException { |
348 |
+ |
FibAction[] fibActions = sq.take(); |
349 |
+ |
FibAction f; |
350 |
+ |
|
351 |
+ |
// test join() ------------ |
352 |
+ |
|
353 |
+ |
f = fibActions[0]; |
354 |
+ |
assertFalse(f.inForkJoinPool()); |
355 |
+ |
Thread.currentThread().interrupt(); |
356 |
+ |
assertNull(f.join()); |
357 |
+ |
assertTrue(Thread.interrupted()); |
358 |
+ |
assertEquals(21, f.result); |
359 |
+ |
checkCompletedNormally(f); |
360 |
+ |
|
361 |
+ |
f = fibActions[1]; |
362 |
+ |
Thread.currentThread().interrupt(); |
363 |
+ |
try { |
364 |
+ |
f.join(); |
365 |
+ |
shouldThrow(); |
366 |
+ |
} catch (CancellationException success) { |
367 |
+ |
assertTrue(Thread.interrupted()); |
368 |
+ |
checkCancelled(f); |
369 |
+ |
} |
370 |
+ |
|
371 |
+ |
f = fibActions[2]; |
372 |
+ |
Thread.currentThread().interrupt(); |
373 |
+ |
try { |
374 |
+ |
f.join(); |
375 |
+ |
shouldThrow(); |
376 |
+ |
} catch (FJException success) { |
377 |
+ |
assertTrue(Thread.interrupted()); |
378 |
+ |
checkCompletedAbnormally(f, success); |
379 |
+ |
} |
380 |
+ |
|
381 |
+ |
// test quietlyJoin() --------- |
382 |
+ |
|
383 |
+ |
f = fibActions[3]; |
384 |
+ |
Thread.currentThread().interrupt(); |
385 |
+ |
f.quietlyJoin(); |
386 |
+ |
assertTrue(Thread.interrupted()); |
387 |
+ |
assertEquals(21, f.result); |
388 |
+ |
checkCompletedNormally(f); |
389 |
+ |
|
390 |
+ |
f = fibActions[4]; |
391 |
+ |
Thread.currentThread().interrupt(); |
392 |
+ |
f.quietlyJoin(); |
393 |
+ |
assertTrue(Thread.interrupted()); |
394 |
+ |
checkCancelled(f); |
395 |
+ |
|
396 |
+ |
f = fibActions[5]; |
397 |
+ |
Thread.currentThread().interrupt(); |
398 |
+ |
f.quietlyJoin(); |
399 |
+ |
assertTrue(Thread.interrupted()); |
400 |
+ |
assertTrue(f.getException() instanceof FJException); |
401 |
+ |
checkCompletedAbnormally(f, f.getException()); |
402 |
+ |
}}; |
403 |
+ |
|
404 |
+ |
Thread t; |
405 |
+ |
|
406 |
+ |
t = newStartedThread(r); |
407 |
+ |
testInvokeOnPool(mainPool(), a); |
408 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
409 |
+ |
|
410 |
+ |
a.reinitialize(); |
411 |
+ |
t = newStartedThread(r); |
412 |
+ |
testInvokeOnPool(singletonPool(), a); |
413 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
414 |
+ |
} |
415 |
+ |
|
416 |
+ |
/** |
417 |
|
* get of a forked task returns when task completes |
418 |
|
*/ |
419 |
|
public void testForkGet() { |
504 |
|
f.invoke(); |
505 |
|
shouldThrow(); |
506 |
|
} catch (FJException success) { |
507 |
< |
checkTaskThrew(f, success); |
507 |
> |
checkCompletedAbnormally(f, success); |
508 |
|
} |
509 |
|
}}; |
510 |
|
testInvokeOnPool(mainPool(), a); |
519 |
|
FailingFibAction f = new FailingFibAction(8); |
520 |
|
f.quietlyInvoke(); |
521 |
|
assertTrue(f.getException() instanceof FJException); |
522 |
< |
checkTaskThrew(f, f.getException()); |
522 |
> |
checkCompletedAbnormally(f, f.getException()); |
523 |
|
}}; |
524 |
|
testInvokeOnPool(mainPool(), a); |
525 |
|
} |
536 |
|
f.join(); |
537 |
|
shouldThrow(); |
538 |
|
} catch (FJException success) { |
539 |
< |
checkTaskThrew(f, success); |
539 |
> |
checkCompletedAbnormally(f, success); |
540 |
|
} |
541 |
|
}}; |
542 |
|
testInvokeOnPool(mainPool(), a); |
554 |
|
f.get(); |
555 |
|
shouldThrow(); |
556 |
|
} catch (ExecutionException success) { |
557 |
< |
checkTaskThrew(f, success.getCause()); |
557 |
> |
Throwable cause = success.getCause(); |
558 |
> |
assertTrue(cause instanceof FJException); |
559 |
> |
checkCompletedAbnormally(f, cause); |
560 |
|
} |
561 |
|
}}; |
562 |
|
testInvokeOnPool(mainPool(), a); |
574 |
|
f.get(5L, TimeUnit.SECONDS); |
575 |
|
shouldThrow(); |
576 |
|
} catch (ExecutionException success) { |
577 |
< |
checkTaskThrew(f, success.getCause()); |
577 |
> |
Throwable cause = success.getCause(); |
578 |
> |
assertTrue(cause instanceof FJException); |
579 |
> |
checkCompletedAbnormally(f, cause); |
580 |
|
} |
581 |
|
}}; |
582 |
|
testInvokeOnPool(mainPool(), a); |
592 |
|
assertSame(f, f.fork()); |
593 |
|
f.quietlyJoin(); |
594 |
|
assertTrue(f.getException() instanceof FJException); |
595 |
< |
checkTaskThrew(f, f.getException()); |
595 |
> |
checkCompletedAbnormally(f, f.getException()); |
596 |
|
}}; |
597 |
|
testInvokeOnPool(mainPool(), a); |
598 |
|
} |
769 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
|
public void realCompute() { |
771 |
|
setRawResult(null); |
772 |
+ |
assertNull(getRawResult()); |
773 |
|
}}; |
774 |
|
assertNull(a.invoke()); |
775 |
|
} |
806 |
|
f.invoke(); |
807 |
|
shouldThrow(); |
808 |
|
} catch (FJException success) { |
809 |
< |
checkTaskThrew(f, success); |
809 |
> |
checkCompletedAbnormally(f, success); |
810 |
|
} |
811 |
|
}}; |
812 |
|
testInvokeOnPool(mainPool(), a); |
868 |
|
FibAction g = new FibAction(9); |
869 |
|
FibAction h = new FibAction(7); |
870 |
|
invokeAll(f, g, h); |
871 |
+ |
assertTrue(f.isDone()); |
872 |
+ |
assertTrue(g.isDone()); |
873 |
+ |
assertTrue(h.isDone()); |
874 |
|
checkCompletedNormally(f); |
875 |
|
assertEquals(21, f.result); |
876 |
|
checkCompletedNormally(g); |
895 |
|
set.add(g); |
896 |
|
set.add(h); |
897 |
|
invokeAll(set); |
898 |
+ |
assertTrue(f.isDone()); |
899 |
+ |
assertTrue(g.isDone()); |
900 |
+ |
assertTrue(h.isDone()); |
901 |
|
checkCompletedNormally(f); |
902 |
|
assertEquals(21, f.result); |
903 |
|
checkCompletedNormally(g); |
938 |
|
invokeAll(f, g); |
939 |
|
shouldThrow(); |
940 |
|
} catch (FJException success) { |
941 |
< |
checkTaskThrew(g, success); |
941 |
> |
checkCompletedAbnormally(g, success); |
942 |
|
} |
943 |
|
}}; |
944 |
|
testInvokeOnPool(mainPool(), a); |
955 |
|
invokeAll(g); |
956 |
|
shouldThrow(); |
957 |
|
} catch (FJException success) { |
958 |
< |
checkTaskThrew(g, success); |
958 |
> |
checkCompletedAbnormally(g, success); |
959 |
|
} |
960 |
|
}}; |
961 |
|
testInvokeOnPool(mainPool(), a); |
974 |
|
invokeAll(f, g, h); |
975 |
|
shouldThrow(); |
976 |
|
} catch (FJException success) { |
977 |
< |
checkTaskThrew(g, success); |
977 |
> |
checkCompletedAbnormally(g, success); |
978 |
|
} |
979 |
|
}}; |
980 |
|
testInvokeOnPool(mainPool(), a); |
997 |
|
invokeAll(set); |
998 |
|
shouldThrow(); |
999 |
|
} catch (FJException success) { |
1000 |
< |
checkTaskThrew(f, success); |
1000 |
> |
checkCompletedAbnormally(f, success); |
1001 |
|
} |
1002 |
|
}}; |
1003 |
|
testInvokeOnPool(mainPool(), a); |
1037 |
|
assertSame(f, f.fork()); |
1038 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1039 |
|
helpQuiesce(); |
1040 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
1041 |
|
checkCompletedNormally(f); |
1042 |
|
checkCompletedNormally(g); |
1043 |
|
checkCompletedNormally(h); |