6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
|
import java.util.concurrent.Callable; |
17 |
|
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
+ |
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.Future; |
23 |
|
import java.util.concurrent.FutureTask; |
24 |
|
import java.util.concurrent.TimeoutException; |
25 |
|
import java.util.concurrent.atomic.AtomicInteger; |
26 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
27 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
28 |
< |
import java.util.*; |
26 |
> |
|
27 |
> |
import junit.framework.Test; |
28 |
> |
import junit.framework.TestSuite; |
29 |
|
|
30 |
|
public class FutureTaskTest extends JSR166TestCase { |
31 |
|
|
45 |
|
assertEquals(1, pf.doneCount()); |
46 |
|
assertFalse(pf.runAndReset()); |
47 |
|
assertEquals(1, pf.doneCount()); |
48 |
+ |
Object r = null; Object exInfo = null; |
49 |
+ |
try { |
50 |
+ |
r = f.get(); |
51 |
+ |
} catch (CancellationException t) { |
52 |
+ |
exInfo = CancellationException.class; |
53 |
+ |
} catch (ExecutionException t) { |
54 |
+ |
exInfo = t.getCause(); |
55 |
+ |
} catch (Throwable t) { |
56 |
+ |
threadUnexpectedException(t); |
57 |
+ |
} |
58 |
|
|
59 |
|
// Check that run and runAndReset have no effect. |
60 |
|
int savedRunCount = pf.runCount(); |
43 |
– |
int savedSetCount = pf.setCount(); |
44 |
– |
int savedSetExceptionCount = pf.setExceptionCount(); |
61 |
|
pf.run(); |
62 |
|
pf.runAndReset(); |
63 |
|
assertEquals(savedRunCount, pf.runCount()); |
64 |
< |
assertEquals(savedSetCount, pf.setCount()); |
65 |
< |
assertEquals(savedSetExceptionCount, pf.setExceptionCount()); |
64 |
> |
try { |
65 |
> |
assertSame(r, f.get()); |
66 |
> |
} catch (CancellationException t) { |
67 |
> |
assertSame(exInfo, CancellationException.class); |
68 |
> |
} catch (ExecutionException t) { |
69 |
> |
assertSame(exInfo, t.getCause()); |
70 |
> |
} catch (Throwable t) { |
71 |
> |
threadUnexpectedException(t); |
72 |
> |
} |
73 |
|
assertTrue(f.isDone()); |
74 |
|
} |
75 |
|
} |
91 |
|
FutureTask ft = (FutureTask<?>) f; |
92 |
|
// Check that run methods do nothing |
93 |
|
ft.run(); |
94 |
< |
if (f instanceof PublicFutureTask) |
95 |
< |
assertFalse(((PublicFutureTask) f).runAndReset()); |
94 |
> |
if (f instanceof PublicFutureTask) { |
95 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
96 |
> |
int savedRunCount = pf.runCount(); |
97 |
> |
pf.run(); |
98 |
> |
assertFalse(pf.runAndReset()); |
99 |
> |
assertEquals(savedRunCount, pf.runCount()); |
100 |
> |
} |
101 |
|
checkNotDone(f); |
102 |
|
} |
103 |
|
} |
135 |
|
pf.set(new Object()); |
136 |
|
pf.setException(new Error()); |
137 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
138 |
< |
pf.cancel(true); |
138 |
> |
pf.cancel(mayInterruptIfRunning); |
139 |
|
} |
140 |
|
} |
141 |
|
|
486 |
|
final PublicFutureTask task = |
487 |
|
new PublicFutureTask(new Runnable() { |
488 |
|
public void run() { |
489 |
+ |
pleaseCancel.countDown(); |
490 |
|
try { |
462 |
– |
pleaseCancel.countDown(); |
491 |
|
delay(LONG_DELAY_MS); |
492 |
< |
shouldThrow(); |
493 |
< |
} catch (Throwable t) { |
494 |
< |
assertTrue(t instanceof InterruptedException); |
467 |
< |
} |
492 |
> |
threadShouldThrow(); |
493 |
> |
} catch (InterruptedException success) { |
494 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
495 |
|
throw new RuntimeException(); |
496 |
|
}}); |
497 |
|
|
617 |
|
* CancellationException |
618 |
|
*/ |
619 |
|
public void testTimedGet_Cancellation() { |
620 |
< |
for (final boolean mayInterruptIfRunning : |
621 |
< |
new boolean[] { true, false }) { |
622 |
< |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
623 |
< |
final CountDownLatch cancelled = new CountDownLatch(1); |
624 |
< |
final PublicFutureTask task = |
625 |
< |
new PublicFutureTask(new CheckedCallable<Object>() { |
626 |
< |
public Object realCall() throws InterruptedException { |
627 |
< |
pleaseCancel.countDown(); |
628 |
< |
if (mayInterruptIfRunning) { |
629 |
< |
try { |
630 |
< |
delay(2*LONG_DELAY_MS); |
631 |
< |
} catch (InterruptedException success) {} |
632 |
< |
} else { |
633 |
< |
await(cancelled); |
634 |
< |
} |
635 |
< |
return two; |
636 |
< |
}}); |
620 |
> |
testTimedGet_Cancellation(false); |
621 |
> |
} |
622 |
> |
public void testTimedGet_Cancellation_interrupt() { |
623 |
> |
testTimedGet_Cancellation(true); |
624 |
> |
} |
625 |
> |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
626 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
627 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
628 |
> |
final Callable<Object> callable = |
629 |
> |
new CheckedCallable<Object>() { |
630 |
> |
public Object realCall() throws InterruptedException { |
631 |
> |
pleaseCancel.countDown(); |
632 |
> |
if (mayInterruptIfRunning) { |
633 |
> |
try { |
634 |
> |
delay(2*LONG_DELAY_MS); |
635 |
> |
} catch (InterruptedException success) {} |
636 |
> |
} else { |
637 |
> |
await(cancelled); |
638 |
> |
} |
639 |
> |
return two; |
640 |
> |
}}; |
641 |
> |
final PublicFutureTask task = new PublicFutureTask(callable); |
642 |
|
|
643 |
< |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
643 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
644 |
|
public void realRun() throws Exception { |
645 |
|
pleaseCancel.countDown(); |
646 |
|
task.get(); |
647 |
|
}}; |
648 |
< |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
648 |
> |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
649 |
|
public void realRun() throws Exception { |
650 |
|
pleaseCancel.countDown(); |
651 |
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
652 |
|
}}; |
653 |
< |
t1.start(); |
654 |
< |
t2.start(); |
655 |
< |
Thread t3 = newStartedThread(task); |
656 |
< |
await(pleaseCancel); |
657 |
< |
checkIsRunning(task); |
658 |
< |
task.cancel(mayInterruptIfRunning); |
659 |
< |
checkCancelled(task); |
660 |
< |
awaitTermination(t1); |
661 |
< |
awaitTermination(t2); |
662 |
< |
cancelled.countDown(); |
663 |
< |
awaitTermination(t3); |
664 |
< |
assertEquals(1, task.runCount()); |
665 |
< |
assertEquals(1, task.setCount()); |
666 |
< |
assertEquals(0, task.setExceptionCount()); |
667 |
< |
tryToConfuseDoneTask(task); |
668 |
< |
checkCancelled(task); |
637 |
< |
} |
653 |
> |
t1.start(); |
654 |
> |
t2.start(); |
655 |
> |
Thread t3 = newStartedThread(task); |
656 |
> |
await(pleaseCancel); |
657 |
> |
checkIsRunning(task); |
658 |
> |
task.cancel(mayInterruptIfRunning); |
659 |
> |
checkCancelled(task); |
660 |
> |
awaitTermination(t1); |
661 |
> |
awaitTermination(t2); |
662 |
> |
cancelled.countDown(); |
663 |
> |
awaitTermination(t3); |
664 |
> |
assertEquals(1, task.runCount()); |
665 |
> |
assertEquals(1, task.setCount()); |
666 |
> |
assertEquals(0, task.setExceptionCount()); |
667 |
> |
tryToConfuseDoneTask(task); |
668 |
> |
checkCancelled(task); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
809 |
|
} |
810 |
|
} |
811 |
|
|
812 |
+ |
/** |
813 |
+ |
* timed get with most negative timeout works correctly (i.e. no |
814 |
+ |
* underflow bug) |
815 |
+ |
*/ |
816 |
+ |
public void testGet_NegativeInfinityTimeout() throws Exception { |
817 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
818 |
+ |
final Runnable nop = new Runnable() { public void run() {}}; |
819 |
+ |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
820 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
821 |
+ |
Runnable r = new Runnable() { public void run() { |
822 |
+ |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
823 |
+ |
try { |
824 |
+ |
task.get(timeout, NANOSECONDS); |
825 |
+ |
shouldThrow(); |
826 |
+ |
} catch (TimeoutException success) { |
827 |
+ |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
828 |
+ |
for (int i = 0; i < 10; i++) |
829 |
+ |
futures.add(pool.submit(r)); |
830 |
+ |
try { |
831 |
+ |
joinPool(pool); |
832 |
+ |
for (Future<?> future : futures) |
833 |
+ |
checkCompletedNormally(future, null); |
834 |
+ |
} finally { |
835 |
+ |
task.run(); // last resort to help terminate |
836 |
+ |
} |
837 |
+ |
} |
838 |
+ |
|
839 |
|
} |