37 |
|
assertEquals(1, pf.doneCount()); |
38 |
|
assertFalse(pf.runAndReset()); |
39 |
|
assertEquals(1, pf.doneCount()); |
40 |
+ |
Object r = null; Object exInfo = null; |
41 |
+ |
try { |
42 |
+ |
r = f.get(); |
43 |
+ |
} catch (CancellationException t) { |
44 |
+ |
exInfo = CancellationException.class; |
45 |
+ |
} catch (ExecutionException t) { |
46 |
+ |
exInfo = t.getCause(); |
47 |
+ |
} catch (Throwable t) { |
48 |
+ |
threadUnexpectedException(t); |
49 |
+ |
} |
50 |
|
|
51 |
|
// Check that run and runAndReset have no effect. |
52 |
|
int savedRunCount = pf.runCount(); |
43 |
– |
int savedSetCount = pf.setCount(); |
44 |
– |
int savedSetExceptionCount = pf.setExceptionCount(); |
53 |
|
pf.run(); |
54 |
|
pf.runAndReset(); |
55 |
|
assertEquals(savedRunCount, pf.runCount()); |
56 |
< |
assertEquals(savedSetCount, pf.setCount()); |
57 |
< |
assertEquals(savedSetExceptionCount, pf.setExceptionCount()); |
56 |
> |
try { |
57 |
> |
assertSame(r, f.get()); |
58 |
> |
} catch (CancellationException t) { |
59 |
> |
assertSame(exInfo, CancellationException.class); |
60 |
> |
} catch (ExecutionException t) { |
61 |
> |
assertSame(exInfo, t.getCause()); |
62 |
> |
} catch (Throwable t) { |
63 |
> |
threadUnexpectedException(t); |
64 |
> |
} |
65 |
|
assertTrue(f.isDone()); |
66 |
|
} |
67 |
|
} |
83 |
|
FutureTask ft = (FutureTask<?>) f; |
84 |
|
// Check that run methods do nothing |
85 |
|
ft.run(); |
86 |
< |
if (f instanceof PublicFutureTask) |
87 |
< |
assertFalse(((PublicFutureTask) f).runAndReset()); |
86 |
> |
if (f instanceof PublicFutureTask) { |
87 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
88 |
> |
int savedRunCount = pf.runCount(); |
89 |
> |
pf.run(); |
90 |
> |
assertFalse(pf.runAndReset()); |
91 |
> |
assertEquals(savedRunCount, pf.runCount()); |
92 |
> |
} |
93 |
|
checkNotDone(f); |
94 |
|
} |
95 |
|
} |
339 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
340 |
|
assertTrue(task.cancel(false)); |
341 |
|
task.run(); |
342 |
+ |
assertEquals(0, task.runCount()); |
343 |
|
assertEquals(0, task.setCount()); |
344 |
|
assertEquals(0, task.setExceptionCount()); |
345 |
+ |
assertTrue(task.isCancelled()); |
346 |
+ |
assertTrue(task.isDone()); |
347 |
|
tryToConfuseDoneTask(task); |
325 |
– |
checkCancelled(task); |
348 |
|
assertEquals(0, task.runCount()); |
349 |
+ |
checkCancelled(task); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
356 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
357 |
|
assertTrue(task.cancel(true)); |
358 |
|
task.run(); |
359 |
+ |
assertEquals(0, task.runCount()); |
360 |
|
assertEquals(0, task.setCount()); |
361 |
|
assertEquals(0, task.setExceptionCount()); |
362 |
+ |
assertTrue(task.isCancelled()); |
363 |
+ |
assertTrue(task.isDone()); |
364 |
|
tryToConfuseDoneTask(task); |
339 |
– |
checkCancelled(task); |
365 |
|
assertEquals(0, task.runCount()); |
366 |
+ |
checkCancelled(task); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
373 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
374 |
|
task.run(); |
375 |
|
assertFalse(task.cancel(false)); |
376 |
+ |
assertEquals(1, task.runCount()); |
377 |
|
assertEquals(1, task.setCount()); |
378 |
|
assertEquals(0, task.setExceptionCount()); |
379 |
|
tryToConfuseDoneTask(task); |
388 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
389 |
|
task.run(); |
390 |
|
assertFalse(task.cancel(true)); |
391 |
+ |
assertEquals(1, task.runCount()); |
392 |
|
assertEquals(1, task.setCount()); |
393 |
|
assertEquals(0, task.setExceptionCount()); |
394 |
|
tryToConfuseDoneTask(task); |
415 |
|
await(pleaseCancel); |
416 |
|
assertTrue(task.cancel(true)); |
417 |
|
assertTrue(task.isCancelled()); |
418 |
+ |
assertTrue(task.isDone()); |
419 |
|
awaitTermination(t); |
420 |
|
assertEquals(1, task.runCount()); |
421 |
|
assertEquals(1, task.setCount()); |
425 |
|
} |
426 |
|
|
427 |
|
/** |
428 |
+ |
* cancel(true) tries to interrupt a running task, but |
429 |
+ |
* Thread.interrupt throws (simulating a restrictive security |
430 |
+ |
* manager) |
431 |
+ |
*/ |
432 |
+ |
public void testCancelInterrupt_ThrowsSecurityException() { |
433 |
+ |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
434 |
+ |
final CountDownLatch cancelled = new CountDownLatch(1); |
435 |
+ |
final PublicFutureTask task = |
436 |
+ |
new PublicFutureTask(new CheckedRunnable() { |
437 |
+ |
public void realRun() { |
438 |
+ |
pleaseCancel.countDown(); |
439 |
+ |
await(cancelled); |
440 |
+ |
assertFalse(Thread.interrupted()); |
441 |
+ |
}}); |
442 |
+ |
|
443 |
+ |
final Thread t = new Thread(task) { |
444 |
+ |
// Simulate a restrictive security manager. |
445 |
+ |
@Override public void interrupt() { |
446 |
+ |
throw new SecurityException(); |
447 |
+ |
}}; |
448 |
+ |
t.setDaemon(true); |
449 |
+ |
t.start(); |
450 |
+ |
|
451 |
+ |
await(pleaseCancel); |
452 |
+ |
try { |
453 |
+ |
task.cancel(true); |
454 |
+ |
shouldThrow(); |
455 |
+ |
} catch (SecurityException expected) {} |
456 |
+ |
|
457 |
+ |
// We failed to deliver the interrupt, but the world retains |
458 |
+ |
// its sanity, as if we had done task.cancel(false) |
459 |
+ |
assertTrue(task.isCancelled()); |
460 |
+ |
assertTrue(task.isDone()); |
461 |
+ |
assertEquals(1, task.runCount()); |
462 |
+ |
assertEquals(1, task.doneCount()); |
463 |
+ |
assertEquals(0, task.setCount()); |
464 |
+ |
assertEquals(0, task.setExceptionCount()); |
465 |
+ |
cancelled.countDown(); |
466 |
+ |
awaitTermination(t); |
467 |
+ |
assertEquals(1, task.setCount()); |
468 |
+ |
assertEquals(0, task.setExceptionCount()); |
469 |
+ |
tryToConfuseDoneTask(task); |
470 |
+ |
checkCancelled(task); |
471 |
+ |
} |
472 |
+ |
|
473 |
+ |
/** |
474 |
|
* cancel(true) interrupts a running task that subsequently throws |
475 |
|
*/ |
476 |
|
public void testCancelInterrupt_taskFails() { |
481 |
|
try { |
482 |
|
pleaseCancel.countDown(); |
483 |
|
delay(LONG_DELAY_MS); |
484 |
< |
} finally { throw new RuntimeException(); } |
484 |
> |
threadShouldThrow(); |
485 |
> |
} catch (InterruptedException success) { |
486 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
487 |
> |
throw new RuntimeException(); |
488 |
|
}}); |
489 |
|
|
490 |
|
Thread t = newStartedThread(task); |
609 |
|
* CancellationException |
610 |
|
*/ |
611 |
|
public void testTimedGet_Cancellation() { |
612 |
< |
for (final boolean mayInterruptIfRunning : |
613 |
< |
new boolean[] { true, false }) { |
614 |
< |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
615 |
< |
final CountDownLatch cancelled = new CountDownLatch(1); |
616 |
< |
final PublicFutureTask task = |
617 |
< |
new PublicFutureTask(new CheckedCallable<Object>() { |
618 |
< |
public Object realCall() throws InterruptedException { |
619 |
< |
pleaseCancel.countDown(); |
620 |
< |
if (mayInterruptIfRunning) { |
621 |
< |
try { |
622 |
< |
delay(2*LONG_DELAY_MS); |
623 |
< |
} catch (InterruptedException success) {} |
624 |
< |
} else { |
625 |
< |
await(cancelled); |
626 |
< |
} |
627 |
< |
return two; |
628 |
< |
}}); |
612 |
> |
testTimedGet_Cancellation(false); |
613 |
> |
} |
614 |
> |
public void testTimedGet_Cancellation_interrupt() { |
615 |
> |
testTimedGet_Cancellation(true); |
616 |
> |
} |
617 |
> |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
618 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
619 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
620 |
> |
final Callable<Object> callable = |
621 |
> |
new CheckedCallable<Object>() { |
622 |
> |
public Object realCall() throws InterruptedException { |
623 |
> |
pleaseCancel.countDown(); |
624 |
> |
if (mayInterruptIfRunning) { |
625 |
> |
try { |
626 |
> |
delay(2*LONG_DELAY_MS); |
627 |
> |
} catch (InterruptedException success) {} |
628 |
> |
} else { |
629 |
> |
await(cancelled); |
630 |
> |
} |
631 |
> |
return two; |
632 |
> |
}}; |
633 |
> |
final PublicFutureTask task = new PublicFutureTask(callable); |
634 |
|
|
635 |
< |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
635 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
636 |
|
public void realRun() throws Exception { |
637 |
|
pleaseCancel.countDown(); |
638 |
|
task.get(); |
639 |
|
}}; |
640 |
< |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
640 |
> |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
641 |
|
public void realRun() throws Exception { |
642 |
|
pleaseCancel.countDown(); |
643 |
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
644 |
|
}}; |
645 |
< |
t1.start(); |
646 |
< |
t2.start(); |
647 |
< |
Thread t3 = newStartedThread(task); |
648 |
< |
await(pleaseCancel); |
649 |
< |
checkIsRunning(task); |
650 |
< |
task.cancel(mayInterruptIfRunning); |
651 |
< |
checkCancelled(task); |
652 |
< |
awaitTermination(t1); |
653 |
< |
awaitTermination(t2); |
654 |
< |
cancelled.countDown(); |
655 |
< |
awaitTermination(t3); |
656 |
< |
assertEquals(1, task.runCount()); |
657 |
< |
assertEquals(1, task.setCount()); |
658 |
< |
assertEquals(0, task.setExceptionCount()); |
659 |
< |
tryToConfuseDoneTask(task); |
660 |
< |
checkCancelled(task); |
578 |
< |
} |
645 |
> |
t1.start(); |
646 |
> |
t2.start(); |
647 |
> |
Thread t3 = newStartedThread(task); |
648 |
> |
await(pleaseCancel); |
649 |
> |
checkIsRunning(task); |
650 |
> |
task.cancel(mayInterruptIfRunning); |
651 |
> |
checkCancelled(task); |
652 |
> |
awaitTermination(t1); |
653 |
> |
awaitTermination(t2); |
654 |
> |
cancelled.countDown(); |
655 |
> |
awaitTermination(t3); |
656 |
> |
assertEquals(1, task.runCount()); |
657 |
> |
assertEquals(1, task.setCount()); |
658 |
> |
assertEquals(0, task.setExceptionCount()); |
659 |
> |
tryToConfuseDoneTask(task); |
660 |
> |
checkCancelled(task); |
661 |
|
} |
662 |
|
|
663 |
|
/** |