10 |
|
import java.util.concurrent.Callable; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.ExecutorService; |
14 |
+ |
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.ExecutionException; |
16 |
|
import java.util.concurrent.Future; |
17 |
|
import java.util.concurrent.FutureTask; |
18 |
|
import java.util.concurrent.TimeoutException; |
19 |
|
import java.util.concurrent.atomic.AtomicInteger; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
20 |
> |
import static java.util.concurrent.TimeUnit.*; |
21 |
|
import java.util.*; |
22 |
|
|
23 |
|
public class FutureTaskTest extends JSR166TestCase { |
38 |
|
assertEquals(1, pf.doneCount()); |
39 |
|
assertFalse(pf.runAndReset()); |
40 |
|
assertEquals(1, pf.doneCount()); |
41 |
+ |
Object r = null; Object exInfo = null; |
42 |
+ |
try { |
43 |
+ |
r = f.get(); |
44 |
+ |
} catch (CancellationException t) { |
45 |
+ |
exInfo = CancellationException.class; |
46 |
+ |
} catch (ExecutionException t) { |
47 |
+ |
exInfo = t.getCause(); |
48 |
+ |
} catch (Throwable t) { |
49 |
+ |
threadUnexpectedException(t); |
50 |
+ |
} |
51 |
|
|
52 |
|
// Check that run and runAndReset have no effect. |
53 |
|
int savedRunCount = pf.runCount(); |
43 |
– |
int savedSetCount = pf.setCount(); |
44 |
– |
int savedSetExceptionCount = pf.setExceptionCount(); |
54 |
|
pf.run(); |
55 |
|
pf.runAndReset(); |
56 |
|
assertEquals(savedRunCount, pf.runCount()); |
57 |
< |
assertEquals(savedSetCount, pf.setCount()); |
58 |
< |
assertEquals(savedSetExceptionCount, pf.setExceptionCount()); |
57 |
> |
try { |
58 |
> |
assertSame(r, f.get()); |
59 |
> |
} catch (CancellationException t) { |
60 |
> |
assertSame(exInfo, CancellationException.class); |
61 |
> |
} catch (ExecutionException t) { |
62 |
> |
assertSame(exInfo, t.getCause()); |
63 |
> |
} catch (Throwable t) { |
64 |
> |
threadUnexpectedException(t); |
65 |
> |
} |
66 |
|
assertTrue(f.isDone()); |
67 |
|
} |
68 |
|
} |
84 |
|
FutureTask ft = (FutureTask<?>) f; |
85 |
|
// Check that run methods do nothing |
86 |
|
ft.run(); |
87 |
< |
if (f instanceof PublicFutureTask) |
88 |
< |
assertFalse(((PublicFutureTask) f).runAndReset()); |
87 |
> |
if (f instanceof PublicFutureTask) { |
88 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
89 |
> |
int savedRunCount = pf.runCount(); |
90 |
> |
pf.run(); |
91 |
> |
assertFalse(pf.runAndReset()); |
92 |
> |
assertEquals(savedRunCount, pf.runCount()); |
93 |
> |
} |
94 |
|
checkNotDone(f); |
95 |
|
} |
96 |
|
} |
482 |
|
try { |
483 |
|
pleaseCancel.countDown(); |
484 |
|
delay(LONG_DELAY_MS); |
485 |
< |
} finally { throw new RuntimeException(); } |
485 |
> |
threadShouldThrow(); |
486 |
> |
} catch (InterruptedException success) { |
487 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
488 |
> |
throw new RuntimeException(); |
489 |
|
}}); |
490 |
|
|
491 |
|
Thread t = newStartedThread(task); |
610 |
|
* CancellationException |
611 |
|
*/ |
612 |
|
public void testTimedGet_Cancellation() { |
613 |
< |
for (final boolean mayInterruptIfRunning : |
614 |
< |
new boolean[] { true, false }) { |
615 |
< |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
616 |
< |
final CountDownLatch cancelled = new CountDownLatch(1); |
617 |
< |
final PublicFutureTask task = |
618 |
< |
new PublicFutureTask(new CheckedCallable<Object>() { |
619 |
< |
public Object realCall() throws InterruptedException { |
620 |
< |
pleaseCancel.countDown(); |
621 |
< |
if (mayInterruptIfRunning) { |
622 |
< |
try { |
623 |
< |
delay(2*LONG_DELAY_MS); |
624 |
< |
} catch (InterruptedException success) {} |
625 |
< |
} else { |
626 |
< |
await(cancelled); |
627 |
< |
} |
628 |
< |
return two; |
629 |
< |
}}); |
613 |
> |
testTimedGet_Cancellation(false); |
614 |
> |
} |
615 |
> |
public void testTimedGet_Cancellation_interrupt() { |
616 |
> |
testTimedGet_Cancellation(true); |
617 |
> |
} |
618 |
> |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
619 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
620 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
621 |
> |
final Callable<Object> callable = |
622 |
> |
new CheckedCallable<Object>() { |
623 |
> |
public Object realCall() throws InterruptedException { |
624 |
> |
pleaseCancel.countDown(); |
625 |
> |
if (mayInterruptIfRunning) { |
626 |
> |
try { |
627 |
> |
delay(2*LONG_DELAY_MS); |
628 |
> |
} catch (InterruptedException success) {} |
629 |
> |
} else { |
630 |
> |
await(cancelled); |
631 |
> |
} |
632 |
> |
return two; |
633 |
> |
}}; |
634 |
> |
final PublicFutureTask task = new PublicFutureTask(callable); |
635 |
|
|
636 |
< |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
636 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
637 |
|
public void realRun() throws Exception { |
638 |
|
pleaseCancel.countDown(); |
639 |
|
task.get(); |
640 |
|
}}; |
641 |
< |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
641 |
> |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
642 |
|
public void realRun() throws Exception { |
643 |
|
pleaseCancel.countDown(); |
644 |
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
645 |
|
}}; |
646 |
< |
t1.start(); |
647 |
< |
t2.start(); |
648 |
< |
Thread t3 = newStartedThread(task); |
649 |
< |
await(pleaseCancel); |
650 |
< |
checkIsRunning(task); |
651 |
< |
task.cancel(mayInterruptIfRunning); |
652 |
< |
checkCancelled(task); |
653 |
< |
awaitTermination(t1); |
654 |
< |
awaitTermination(t2); |
655 |
< |
cancelled.countDown(); |
656 |
< |
awaitTermination(t3); |
657 |
< |
assertEquals(1, task.runCount()); |
658 |
< |
assertEquals(1, task.setCount()); |
659 |
< |
assertEquals(0, task.setExceptionCount()); |
660 |
< |
tryToConfuseDoneTask(task); |
661 |
< |
checkCancelled(task); |
633 |
< |
} |
646 |
> |
t1.start(); |
647 |
> |
t2.start(); |
648 |
> |
Thread t3 = newStartedThread(task); |
649 |
> |
await(pleaseCancel); |
650 |
> |
checkIsRunning(task); |
651 |
> |
task.cancel(mayInterruptIfRunning); |
652 |
> |
checkCancelled(task); |
653 |
> |
awaitTermination(t1); |
654 |
> |
awaitTermination(t2); |
655 |
> |
cancelled.countDown(); |
656 |
> |
awaitTermination(t3); |
657 |
> |
assertEquals(1, task.runCount()); |
658 |
> |
assertEquals(1, task.setCount()); |
659 |
> |
assertEquals(0, task.setExceptionCount()); |
660 |
> |
tryToConfuseDoneTask(task); |
661 |
> |
checkCancelled(task); |
662 |
|
} |
663 |
|
|
664 |
|
/** |
802 |
|
} |
803 |
|
} |
804 |
|
|
805 |
+ |
/** |
806 |
+ |
* timed get with most negative timeout works correctly (i.e. no |
807 |
+ |
* underflow bug) |
808 |
+ |
*/ |
809 |
+ |
public void testGet_NegativeInfinityTimeout() throws Exception { |
810 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
811 |
+ |
final Runnable nop = new Runnable() { public void run() {}}; |
812 |
+ |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
813 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
814 |
+ |
Runnable r = new Runnable() { public void run() { |
815 |
+ |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
816 |
+ |
try { |
817 |
+ |
task.get(timeout, NANOSECONDS); |
818 |
+ |
shouldThrow(); |
819 |
+ |
} catch (TimeoutException success) { |
820 |
+ |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
821 |
+ |
for (int i = 0; i < 10; i++) |
822 |
+ |
futures.add(pool.submit(r)); |
823 |
+ |
try { |
824 |
+ |
joinPool(pool); |
825 |
+ |
for (Future<?> future : futures) |
826 |
+ |
checkCompletedNormally(future, null); |
827 |
+ |
} finally { |
828 |
+ |
task.run(); // last resort to help terminate |
829 |
+ |
} |
830 |
+ |
} |
831 |
+ |
|
832 |
|
} |