7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
– |
import java.security.Permission; |
10 |
|
import java.util.concurrent.Callable; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
|
import java.util.concurrent.CountDownLatch; |
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(); |
44 |
– |
int savedSetCount = pf.setCount(); |
45 |
– |
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 |
|
} |
425 |
|
} |
426 |
|
|
427 |
|
/** |
428 |
< |
* cancel(true) interrupts a running task that subsequently |
429 |
< |
* succeeds, with a security manager that does not permit |
430 |
< |
* Thread.interrupt |
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() { |
414 |
– |
if (System.getSecurityManager() != null) |
415 |
– |
return; |
416 |
– |
|
433 |
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
434 |
|
final CountDownLatch cancelled = new CountDownLatch(1); |
435 |
|
final PublicFutureTask task = |
440 |
|
assertFalse(Thread.interrupted()); |
441 |
|
}}); |
442 |
|
|
443 |
< |
final Thread t = newStartedThread(task); |
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); |
429 |
– |
System.setSecurityManager(new SecurityManager() { |
430 |
– |
public void checkAccess(Thread t) { throw new SecurityException(); } |
431 |
– |
public void checkPermission(Permission p) {}}); |
452 |
|
try { |
453 |
< |
try { |
454 |
< |
task.cancel(true); |
455 |
< |
shouldThrow(); |
456 |
< |
} |
457 |
< |
catch (SecurityException expected) {} |
458 |
< |
} finally { |
439 |
< |
System.setSecurityManager(null); |
440 |
< |
} |
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()); |
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); |
635 |
< |
} |
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 |
|
/** |