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 |
+ |
final Thread myself = Thread.currentThread(); |
259 |
+ |
|
260 |
+ |
// test join() |
261 |
+ |
assertSame(f, f.fork()); |
262 |
+ |
myself.interrupt(); |
263 |
+ |
assertTrue(myself.isInterrupted()); |
264 |
+ |
assertNull(f.join()); |
265 |
+ |
Thread.interrupted(); |
266 |
+ |
assertEquals(21, f.result); |
267 |
+ |
checkCompletedNormally(f); |
268 |
+ |
|
269 |
+ |
f.reinitialize(); |
270 |
+ |
f.cancel(true); |
271 |
+ |
assertSame(f, f.fork()); |
272 |
+ |
myself.interrupt(); |
273 |
+ |
assertTrue(myself.isInterrupted()); |
274 |
+ |
try { |
275 |
+ |
f.join(); |
276 |
+ |
shouldThrow(); |
277 |
+ |
} catch (CancellationException success) { |
278 |
+ |
Thread.interrupted(); |
279 |
+ |
checkCancelled(f); |
280 |
+ |
} |
281 |
+ |
|
282 |
+ |
f.reinitialize(); |
283 |
+ |
f.completeExceptionally(new FJException()); |
284 |
+ |
assertSame(f, f.fork()); |
285 |
+ |
myself.interrupt(); |
286 |
+ |
assertTrue(myself.isInterrupted()); |
287 |
+ |
try { |
288 |
+ |
f.join(); |
289 |
+ |
shouldThrow(); |
290 |
+ |
} catch (FJException success) { |
291 |
+ |
Thread.interrupted(); |
292 |
+ |
checkCompletedAbnormally(f, success); |
293 |
+ |
} |
294 |
+ |
|
295 |
+ |
// test quietlyJoin() |
296 |
+ |
f.reinitialize(); |
297 |
+ |
assertSame(f, f.fork()); |
298 |
+ |
myself.interrupt(); |
299 |
+ |
assertTrue(myself.isInterrupted()); |
300 |
+ |
f.quietlyJoin(); |
301 |
+ |
Thread.interrupted(); |
302 |
+ |
assertEquals(21, f.result); |
303 |
+ |
checkCompletedNormally(f); |
304 |
+ |
|
305 |
+ |
f.reinitialize(); |
306 |
+ |
f.cancel(true); |
307 |
+ |
assertSame(f, f.fork()); |
308 |
+ |
myself.interrupt(); |
309 |
+ |
assertTrue(myself.isInterrupted()); |
310 |
+ |
f.quietlyJoin(); |
311 |
+ |
Thread.interrupted(); |
312 |
+ |
checkCancelled(f); |
313 |
+ |
|
314 |
+ |
f.reinitialize(); |
315 |
+ |
f.completeExceptionally(new FJException()); |
316 |
+ |
assertSame(f, f.fork()); |
317 |
+ |
myself.interrupt(); |
318 |
+ |
assertTrue(myself.isInterrupted()); |
319 |
+ |
f.quietlyJoin(); |
320 |
+ |
Thread.interrupted(); |
321 |
+ |
checkCompletedAbnormally(f, f.getException()); |
322 |
+ |
}}; |
323 |
+ |
testInvokeOnPool(mainPool(), a); |
324 |
+ |
a.reinitialize(); |
325 |
+ |
testInvokeOnPool(singletonPool(), a); |
326 |
+ |
} |
327 |
+ |
|
328 |
+ |
/** |
329 |
+ |
* join/quietlyJoin of a forked task when not in ForkJoinPool |
330 |
+ |
* succeeds in the presence of interrupts |
331 |
+ |
*/ |
332 |
+ |
public void testJoinIgnoresInterruptsOutsideForkJoinPool() { |
333 |
+ |
final SynchronousQueue<FibAction[]> sq = |
334 |
+ |
new SynchronousQueue<FibAction[]>(); |
335 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
336 |
+ |
public void realCompute() throws InterruptedException { |
337 |
+ |
FibAction[] fibActions = new FibAction[6]; |
338 |
+ |
for (int i = 0; i < fibActions.length; i++) |
339 |
+ |
fibActions[i] = new FibAction(8); |
340 |
+ |
|
341 |
+ |
fibActions[1].cancel(false); |
342 |
+ |
fibActions[2].completeExceptionally(new FJException()); |
343 |
+ |
fibActions[4].cancel(true); |
344 |
+ |
fibActions[5].completeExceptionally(new FJException()); |
345 |
+ |
|
346 |
+ |
for (int i = 0; i < fibActions.length; i++) |
347 |
+ |
fibActions[i].fork(); |
348 |
+ |
|
349 |
+ |
sq.put(fibActions); |
350 |
+ |
|
351 |
+ |
helpQuiesce(); |
352 |
+ |
}}; |
353 |
+ |
|
354 |
+ |
Runnable r = new CheckedRunnable() { |
355 |
+ |
public void realRun() throws InterruptedException { |
356 |
+ |
FibAction[] fibActions = sq.take(); |
357 |
+ |
FibAction f; |
358 |
+ |
final Thread myself = Thread.currentThread(); |
359 |
+ |
|
360 |
+ |
// test join() ------------ |
361 |
+ |
|
362 |
+ |
f = fibActions[0]; |
363 |
+ |
assertFalse(ForkJoinTask.inForkJoinPool()); |
364 |
+ |
myself.interrupt(); |
365 |
+ |
assertTrue(myself.isInterrupted()); |
366 |
+ |
assertNull(f.join()); |
367 |
+ |
assertTrue(Thread.interrupted()); |
368 |
+ |
assertEquals(21, f.result); |
369 |
+ |
checkCompletedNormally(f); |
370 |
+ |
|
371 |
+ |
f = fibActions[1]; |
372 |
+ |
myself.interrupt(); |
373 |
+ |
assertTrue(myself.isInterrupted()); |
374 |
+ |
try { |
375 |
+ |
f.join(); |
376 |
+ |
shouldThrow(); |
377 |
+ |
} catch (CancellationException success) { |
378 |
+ |
assertTrue(Thread.interrupted()); |
379 |
+ |
checkCancelled(f); |
380 |
+ |
} |
381 |
+ |
|
382 |
+ |
f = fibActions[2]; |
383 |
+ |
myself.interrupt(); |
384 |
+ |
assertTrue(myself.isInterrupted()); |
385 |
+ |
try { |
386 |
+ |
f.join(); |
387 |
+ |
shouldThrow(); |
388 |
+ |
} catch (FJException success) { |
389 |
+ |
assertTrue(Thread.interrupted()); |
390 |
+ |
checkCompletedAbnormally(f, success); |
391 |
+ |
} |
392 |
+ |
|
393 |
+ |
// test quietlyJoin() --------- |
394 |
+ |
|
395 |
+ |
f = fibActions[3]; |
396 |
+ |
myself.interrupt(); |
397 |
+ |
assertTrue(myself.isInterrupted()); |
398 |
+ |
f.quietlyJoin(); |
399 |
+ |
assertTrue(Thread.interrupted()); |
400 |
+ |
assertEquals(21, f.result); |
401 |
+ |
checkCompletedNormally(f); |
402 |
+ |
|
403 |
+ |
f = fibActions[4]; |
404 |
+ |
myself.interrupt(); |
405 |
+ |
assertTrue(myself.isInterrupted()); |
406 |
+ |
f.quietlyJoin(); |
407 |
+ |
assertTrue(Thread.interrupted()); |
408 |
+ |
checkCancelled(f); |
409 |
+ |
|
410 |
+ |
f = fibActions[5]; |
411 |
+ |
myself.interrupt(); |
412 |
+ |
assertTrue(myself.isInterrupted()); |
413 |
+ |
f.quietlyJoin(); |
414 |
+ |
assertTrue(Thread.interrupted()); |
415 |
+ |
assertTrue(f.getException() instanceof FJException); |
416 |
+ |
checkCompletedAbnormally(f, f.getException()); |
417 |
+ |
}}; |
418 |
+ |
|
419 |
+ |
Thread t; |
420 |
+ |
|
421 |
+ |
t = newStartedThread(r); |
422 |
+ |
testInvokeOnPool(mainPool(), a); |
423 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
424 |
+ |
|
425 |
+ |
a.reinitialize(); |
426 |
+ |
t = newStartedThread(r); |
427 |
+ |
testInvokeOnPool(singletonPool(), a); |
428 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
429 |
+ |
} |
430 |
+ |
|
431 |
+ |
/** |
432 |
|
* get of a forked task returns when task completes |
433 |
|
*/ |
434 |
|
public void testForkGet() { |
519 |
|
f.invoke(); |
520 |
|
shouldThrow(); |
521 |
|
} catch (FJException success) { |
522 |
< |
checkTaskThrew(f, success); |
522 |
> |
checkCompletedAbnormally(f, success); |
523 |
|
} |
524 |
|
}}; |
525 |
|
testInvokeOnPool(mainPool(), a); |
534 |
|
FailingFibAction f = new FailingFibAction(8); |
535 |
|
f.quietlyInvoke(); |
536 |
|
assertTrue(f.getException() instanceof FJException); |
537 |
< |
checkTaskThrew(f, f.getException()); |
537 |
> |
checkCompletedAbnormally(f, f.getException()); |
538 |
|
}}; |
539 |
|
testInvokeOnPool(mainPool(), a); |
540 |
|
} |
551 |
|
f.join(); |
552 |
|
shouldThrow(); |
553 |
|
} catch (FJException success) { |
554 |
< |
checkTaskThrew(f, success); |
554 |
> |
checkCompletedAbnormally(f, success); |
555 |
|
} |
556 |
|
}}; |
557 |
|
testInvokeOnPool(mainPool(), a); |
569 |
|
f.get(); |
570 |
|
shouldThrow(); |
571 |
|
} catch (ExecutionException success) { |
572 |
< |
checkTaskThrew(f, success.getCause()); |
572 |
> |
Throwable cause = success.getCause(); |
573 |
> |
assertTrue(cause instanceof FJException); |
574 |
> |
checkCompletedAbnormally(f, cause); |
575 |
|
} |
576 |
|
}}; |
577 |
|
testInvokeOnPool(mainPool(), a); |
589 |
|
f.get(5L, TimeUnit.SECONDS); |
590 |
|
shouldThrow(); |
591 |
|
} catch (ExecutionException success) { |
592 |
< |
checkTaskThrew(f, success.getCause()); |
592 |
> |
Throwable cause = success.getCause(); |
593 |
> |
assertTrue(cause instanceof FJException); |
594 |
> |
checkCompletedAbnormally(f, cause); |
595 |
|
} |
596 |
|
}}; |
597 |
|
testInvokeOnPool(mainPool(), a); |
607 |
|
assertSame(f, f.fork()); |
608 |
|
f.quietlyJoin(); |
609 |
|
assertTrue(f.getException() instanceof FJException); |
610 |
< |
checkTaskThrew(f, f.getException()); |
610 |
> |
checkCompletedAbnormally(f, f.getException()); |
611 |
|
}}; |
612 |
|
testInvokeOnPool(mainPool(), a); |
613 |
|
} |
769 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
770 |
|
public void realCompute() { |
771 |
|
ForkJoinWorkerThread w = |
772 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
772 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
773 |
|
assertTrue(w.getPoolIndex() >= 0); |
774 |
|
assertTrue(w.getPoolIndex() < mainPool.getPoolSize()); |
775 |
|
}}; |
784 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
785 |
|
public void realCompute() { |
786 |
|
setRawResult(null); |
787 |
+ |
assertNull(getRawResult()); |
788 |
|
}}; |
789 |
|
assertNull(a.invoke()); |
790 |
|
} |
821 |
|
f.invoke(); |
822 |
|
shouldThrow(); |
823 |
|
} catch (FJException success) { |
824 |
< |
checkTaskThrew(f, success); |
824 |
> |
checkCompletedAbnormally(f, success); |
825 |
|
} |
826 |
|
}}; |
827 |
|
testInvokeOnPool(mainPool(), a); |
883 |
|
FibAction g = new FibAction(9); |
884 |
|
FibAction h = new FibAction(7); |
885 |
|
invokeAll(f, g, h); |
886 |
+ |
assertTrue(f.isDone()); |
887 |
+ |
assertTrue(g.isDone()); |
888 |
+ |
assertTrue(h.isDone()); |
889 |
|
checkCompletedNormally(f); |
890 |
|
assertEquals(21, f.result); |
891 |
|
checkCompletedNormally(g); |
910 |
|
set.add(g); |
911 |
|
set.add(h); |
912 |
|
invokeAll(set); |
913 |
+ |
assertTrue(f.isDone()); |
914 |
+ |
assertTrue(g.isDone()); |
915 |
+ |
assertTrue(h.isDone()); |
916 |
|
checkCompletedNormally(f); |
917 |
|
assertEquals(21, f.result); |
918 |
|
checkCompletedNormally(g); |
953 |
|
invokeAll(f, g); |
954 |
|
shouldThrow(); |
955 |
|
} catch (FJException success) { |
956 |
< |
checkTaskThrew(g, success); |
956 |
> |
checkCompletedAbnormally(g, success); |
957 |
|
} |
958 |
|
}}; |
959 |
|
testInvokeOnPool(mainPool(), a); |
970 |
|
invokeAll(g); |
971 |
|
shouldThrow(); |
972 |
|
} catch (FJException success) { |
973 |
< |
checkTaskThrew(g, success); |
973 |
> |
checkCompletedAbnormally(g, success); |
974 |
|
} |
975 |
|
}}; |
976 |
|
testInvokeOnPool(mainPool(), a); |
989 |
|
invokeAll(f, g, h); |
990 |
|
shouldThrow(); |
991 |
|
} catch (FJException success) { |
992 |
< |
checkTaskThrew(g, success); |
992 |
> |
checkCompletedAbnormally(g, success); |
993 |
|
} |
994 |
|
}}; |
995 |
|
testInvokeOnPool(mainPool(), a); |
1012 |
|
invokeAll(set); |
1013 |
|
shouldThrow(); |
1014 |
|
} catch (FJException success) { |
1015 |
< |
checkTaskThrew(f, success); |
1015 |
> |
checkCompletedAbnormally(f, success); |
1016 |
|
} |
1017 |
|
}}; |
1018 |
|
testInvokeOnPool(mainPool(), a); |
1052 |
|
assertSame(f, f.fork()); |
1053 |
|
assertTrue(getSurplusQueuedTaskCount() > 0); |
1054 |
|
helpQuiesce(); |
1055 |
+ |
assertEquals(0, getSurplusQueuedTaskCount()); |
1056 |
|
checkCompletedNormally(f); |
1057 |
|
checkCompletedNormally(g); |
1058 |
|
checkCompletedNormally(h); |