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