4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
8 |
> |
|
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
9 |
– |
import java.util.concurrent.SynchronousQueue; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
15 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
16 |
|
import java.util.concurrent.RecursiveAction; |
17 |
+ |
import java.util.concurrent.SynchronousQueue; |
18 |
|
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
19 |
|
import java.util.concurrent.TimeoutException; |
20 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
21 |
< |
import java.util.Arrays; |
22 |
< |
import java.util.HashSet; |
20 |
> |
|
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class RecursiveActionTest extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
|
30 |
|
public static Test suite() { |
46 |
|
} |
47 |
|
|
48 |
|
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { |
49 |
< |
try { |
49 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
50 |
|
checkNotDone(a); |
51 |
|
|
52 |
|
assertNull(pool.invoke(a)); |
53 |
|
|
54 |
|
checkCompletedNormally(a); |
53 |
– |
} finally { |
54 |
– |
joinPool(pool); |
55 |
|
} |
56 |
|
} |
57 |
|
|
258 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
259 |
|
protected void realCompute() { |
260 |
|
FibAction f = new FibAction(8); |
261 |
< |
final Thread myself = Thread.currentThread(); |
261 |
> |
final Thread currentThread = Thread.currentThread(); |
262 |
|
|
263 |
|
// test join() |
264 |
|
assertSame(f, f.fork()); |
265 |
< |
myself.interrupt(); |
266 |
< |
assertTrue(myself.isInterrupted()); |
265 |
> |
currentThread.interrupt(); |
266 |
> |
assertTrue(currentThread.isInterrupted()); |
267 |
|
assertNull(f.join()); |
268 |
|
Thread.interrupted(); |
269 |
|
assertEquals(21, f.result); |
272 |
|
f = new FibAction(8); |
273 |
|
f.cancel(true); |
274 |
|
assertSame(f, f.fork()); |
275 |
< |
myself.interrupt(); |
276 |
< |
assertTrue(myself.isInterrupted()); |
275 |
> |
currentThread.interrupt(); |
276 |
> |
assertTrue(currentThread.isInterrupted()); |
277 |
|
try { |
278 |
|
f.join(); |
279 |
|
shouldThrow(); |
285 |
|
f = new FibAction(8); |
286 |
|
f.completeExceptionally(new FJException()); |
287 |
|
assertSame(f, f.fork()); |
288 |
< |
myself.interrupt(); |
289 |
< |
assertTrue(myself.isInterrupted()); |
288 |
> |
currentThread.interrupt(); |
289 |
> |
assertTrue(currentThread.isInterrupted()); |
290 |
|
try { |
291 |
|
f.join(); |
292 |
|
shouldThrow(); |
298 |
|
// test quietlyJoin() |
299 |
|
f = new FibAction(8); |
300 |
|
assertSame(f, f.fork()); |
301 |
< |
myself.interrupt(); |
302 |
< |
assertTrue(myself.isInterrupted()); |
301 |
> |
currentThread.interrupt(); |
302 |
> |
assertTrue(currentThread.isInterrupted()); |
303 |
|
f.quietlyJoin(); |
304 |
|
Thread.interrupted(); |
305 |
|
assertEquals(21, f.result); |
308 |
|
f = new FibAction(8); |
309 |
|
f.cancel(true); |
310 |
|
assertSame(f, f.fork()); |
311 |
< |
myself.interrupt(); |
312 |
< |
assertTrue(myself.isInterrupted()); |
311 |
> |
currentThread.interrupt(); |
312 |
> |
assertTrue(currentThread.isInterrupted()); |
313 |
|
f.quietlyJoin(); |
314 |
|
Thread.interrupted(); |
315 |
|
checkCancelled(f); |
317 |
|
f = new FibAction(8); |
318 |
|
f.completeExceptionally(new FJException()); |
319 |
|
assertSame(f, f.fork()); |
320 |
< |
myself.interrupt(); |
321 |
< |
assertTrue(myself.isInterrupted()); |
320 |
> |
currentThread.interrupt(); |
321 |
> |
assertTrue(currentThread.isInterrupted()); |
322 |
|
f.quietlyJoin(); |
323 |
|
Thread.interrupted(); |
324 |
|
checkCompletedAbnormally(f, f.getException()); |
358 |
|
public void realRun() throws InterruptedException { |
359 |
|
FibAction[] fibActions = sq.take(); |
360 |
|
FibAction f; |
361 |
< |
final Thread myself = Thread.currentThread(); |
361 |
> |
final Thread currentThread = Thread.currentThread(); |
362 |
|
|
363 |
|
// test join() ------------ |
364 |
|
|
365 |
|
f = fibActions[0]; |
366 |
|
assertFalse(ForkJoinTask.inForkJoinPool()); |
367 |
< |
myself.interrupt(); |
368 |
< |
assertTrue(myself.isInterrupted()); |
367 |
> |
currentThread.interrupt(); |
368 |
> |
assertTrue(currentThread.isInterrupted()); |
369 |
|
assertNull(f.join()); |
370 |
|
assertTrue(Thread.interrupted()); |
371 |
|
assertEquals(21, f.result); |
372 |
|
checkCompletedNormally(f); |
373 |
|
|
374 |
|
f = fibActions[1]; |
375 |
< |
myself.interrupt(); |
376 |
< |
assertTrue(myself.isInterrupted()); |
375 |
> |
currentThread.interrupt(); |
376 |
> |
assertTrue(currentThread.isInterrupted()); |
377 |
|
try { |
378 |
|
f.join(); |
379 |
|
shouldThrow(); |
383 |
|
} |
384 |
|
|
385 |
|
f = fibActions[2]; |
386 |
< |
myself.interrupt(); |
387 |
< |
assertTrue(myself.isInterrupted()); |
386 |
> |
currentThread.interrupt(); |
387 |
> |
assertTrue(currentThread.isInterrupted()); |
388 |
|
try { |
389 |
|
f.join(); |
390 |
|
shouldThrow(); |
396 |
|
// test quietlyJoin() --------- |
397 |
|
|
398 |
|
f = fibActions[3]; |
399 |
< |
myself.interrupt(); |
400 |
< |
assertTrue(myself.isInterrupted()); |
399 |
> |
currentThread.interrupt(); |
400 |
> |
assertTrue(currentThread.isInterrupted()); |
401 |
|
f.quietlyJoin(); |
402 |
|
assertTrue(Thread.interrupted()); |
403 |
|
assertEquals(21, f.result); |
404 |
|
checkCompletedNormally(f); |
405 |
|
|
406 |
|
f = fibActions[4]; |
407 |
< |
myself.interrupt(); |
408 |
< |
assertTrue(myself.isInterrupted()); |
407 |
> |
currentThread.interrupt(); |
408 |
> |
assertTrue(currentThread.isInterrupted()); |
409 |
|
f.quietlyJoin(); |
410 |
|
assertTrue(Thread.interrupted()); |
411 |
|
checkCancelled(f); |
412 |
|
|
413 |
|
f = fibActions[5]; |
414 |
< |
myself.interrupt(); |
415 |
< |
assertTrue(myself.isInterrupted()); |
414 |
> |
currentThread.interrupt(); |
415 |
> |
assertTrue(currentThread.isInterrupted()); |
416 |
|
f.quietlyJoin(); |
417 |
|
assertTrue(Thread.interrupted()); |
418 |
|
assertTrue(f.getException() instanceof FJException); |
423 |
|
|
424 |
|
t = newStartedThread(r); |
425 |
|
testInvokeOnPool(mainPool(), a); |
426 |
< |
awaitTermination(t, LONG_DELAY_MS); |
426 |
> |
awaitTermination(t); |
427 |
|
|
428 |
|
a.reinitialize(); |
429 |
|
t = newStartedThread(r); |
430 |
|
testInvokeOnPool(singletonPool(), a); |
431 |
< |
awaitTermination(t, LONG_DELAY_MS); |
431 |
> |
awaitTermination(t); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
502 |
|
FibAction f = new FibAction(8); |
503 |
|
assertSame(f, f.fork()); |
504 |
|
helpQuiesce(); |
505 |
+ |
while (!f.isDone()) // wait out race |
506 |
+ |
; |
507 |
|
assertEquals(21, f.result); |
508 |
|
assertEquals(0, getQueuedTaskCount()); |
509 |
|
checkCompletedNormally(f); |
589 |
|
FailingFibAction f = new FailingFibAction(8); |
590 |
|
assertSame(f, f.fork()); |
591 |
|
try { |
592 |
< |
f.get(5L, TimeUnit.SECONDS); |
592 |
> |
f.get(5L, SECONDS); |
593 |
|
shouldThrow(); |
594 |
|
} catch (ExecutionException success) { |
595 |
|
Throwable cause = success.getCause(); |