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 |
> |
|
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
|
import java.util.concurrent.Callable; |
16 |
|
import java.util.concurrent.CancellationException; |
17 |
|
import java.util.concurrent.CountDownLatch; |
18 |
|
import java.util.concurrent.ExecutionException; |
19 |
+ |
import java.util.concurrent.Executors; |
20 |
+ |
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.Future; |
22 |
|
import java.util.concurrent.FutureTask; |
23 |
|
import java.util.concurrent.TimeoutException; |
24 |
|
import java.util.concurrent.atomic.AtomicInteger; |
25 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
26 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
27 |
< |
import java.util.*; |
25 |
> |
|
26 |
> |
import junit.framework.Test; |
27 |
> |
import junit.framework.TestSuite; |
28 |
|
|
29 |
|
public class FutureTaskTest extends JSR166TestCase { |
30 |
|
|
31 |
|
public static void main(String[] args) { |
32 |
< |
junit.textui.TestRunner.run(suite()); |
32 |
> |
main(suite(), args); |
33 |
|
} |
34 |
|
public static Test suite() { |
35 |
|
return new TestSuite(FutureTaskTest.class); |
44 |
|
assertEquals(1, pf.doneCount()); |
45 |
|
assertFalse(pf.runAndReset()); |
46 |
|
assertEquals(1, pf.doneCount()); |
47 |
+ |
Object r = null; Object exInfo = null; |
48 |
+ |
try { |
49 |
+ |
r = f.get(); |
50 |
+ |
} catch (CancellationException t) { |
51 |
+ |
exInfo = CancellationException.class; |
52 |
+ |
} catch (ExecutionException t) { |
53 |
+ |
exInfo = t.getCause(); |
54 |
+ |
} catch (Throwable t) { |
55 |
+ |
threadUnexpectedException(t); |
56 |
+ |
} |
57 |
|
|
58 |
|
// Check that run and runAndReset have no effect. |
59 |
|
int savedRunCount = pf.runCount(); |
43 |
– |
int savedSetCount = pf.setCount(); |
44 |
– |
int savedSetExceptionCount = pf.setExceptionCount(); |
60 |
|
pf.run(); |
61 |
|
pf.runAndReset(); |
62 |
|
assertEquals(savedRunCount, pf.runCount()); |
63 |
< |
assertEquals(savedSetCount, pf.setCount()); |
64 |
< |
assertEquals(savedSetExceptionCount, pf.setExceptionCount()); |
63 |
> |
Object r2 = null; |
64 |
> |
try { |
65 |
> |
r2 = 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 |
> |
if (exInfo == null) |
74 |
> |
assertSame(r, r2); |
75 |
|
assertTrue(f.isDone()); |
76 |
|
} |
77 |
|
} |
90 |
|
void checkIsRunning(Future<?> f) { |
91 |
|
checkNotDone(f); |
92 |
|
if (f instanceof FutureTask) { |
93 |
< |
FutureTask ft = (FutureTask<?>) f; |
93 |
> |
FutureTask<?> ft = (FutureTask<?>) f; |
94 |
|
// Check that run methods do nothing |
95 |
|
ft.run(); |
96 |
< |
if (f instanceof PublicFutureTask) |
97 |
< |
assertFalse(((PublicFutureTask) f).runAndReset()); |
96 |
> |
if (f instanceof PublicFutureTask) { |
97 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
98 |
> |
int savedRunCount = pf.runCount(); |
99 |
> |
pf.run(); |
100 |
> |
assertFalse(pf.runAndReset()); |
101 |
> |
assertEquals(savedRunCount, pf.runCount()); |
102 |
> |
} |
103 |
|
checkNotDone(f); |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
< |
<T> void checkCompletedNormally(Future<T> f, T expected) { |
107 |
> |
<T> void checkCompletedNormally(Future<T> f, T expectedValue) { |
108 |
|
checkIsDone(f); |
109 |
|
assertFalse(f.isCancelled()); |
110 |
|
|
111 |
+ |
T v1 = null, v2 = null; |
112 |
|
try { |
113 |
< |
assertSame(expected, f.get()); |
114 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
< |
try { |
85 |
< |
assertSame(expected, f.get(5L, SECONDS)); |
113 |
> |
v1 = f.get(); |
114 |
> |
v2 = f.get(randomTimeout(), randomTimeUnit()); |
115 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
+ |
assertSame(expectedValue, v1); |
117 |
+ |
assertSame(expectedValue, v2); |
118 |
|
} |
119 |
|
|
120 |
|
void checkCancelled(Future<?> f) { |
128 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
129 |
|
|
130 |
|
try { |
131 |
< |
f.get(5L, SECONDS); |
131 |
> |
f.get(randomTimeout(), randomTimeUnit()); |
132 |
|
shouldThrow(); |
133 |
|
} catch (CancellationException success) { |
134 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
138 |
|
pf.set(new Object()); |
139 |
|
pf.setException(new Error()); |
140 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
141 |
< |
pf.cancel(true); |
141 |
> |
pf.cancel(mayInterruptIfRunning); |
142 |
|
} |
143 |
|
} |
144 |
|
|
154 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
|
|
156 |
|
try { |
157 |
< |
f.get(5L, SECONDS); |
157 |
> |
f.get(randomTimeout(), randomTimeUnit()); |
158 |
|
shouldThrow(); |
159 |
|
} catch (ExecutionException success) { |
160 |
|
assertSame(t, success.getCause()); |
164 |
|
/** |
165 |
|
* Subclass to expose protected methods |
166 |
|
*/ |
167 |
< |
static class PublicFutureTask extends FutureTask { |
167 |
> |
static class PublicFutureTask extends FutureTask<Object> { |
168 |
|
private final AtomicInteger runCount; |
169 |
|
private final AtomicInteger doneCount = new AtomicInteger(0); |
170 |
|
private final AtomicInteger runAndResetCount = new AtomicInteger(0); |
191 |
|
}}, result); |
192 |
|
this.runCount = runCount; |
193 |
|
} |
194 |
< |
PublicFutureTask(Callable callable) { |
194 |
> |
PublicFutureTask(Callable<?> callable) { |
195 |
|
this(callable, new AtomicInteger(0)); |
196 |
|
} |
197 |
< |
private PublicFutureTask(final Callable callable, |
197 |
> |
private PublicFutureTask(final Callable<?> callable, |
198 |
|
final AtomicInteger runCount) { |
199 |
< |
super(new Callable() { |
199 |
> |
super(new Callable<Object>() { |
200 |
|
public Object call() throws Exception { |
201 |
|
runCount.getAndIncrement(); |
202 |
|
return callable.call(); |
235 |
|
*/ |
236 |
|
public void testConstructor() { |
237 |
|
try { |
238 |
< |
new FutureTask(null); |
238 |
> |
new FutureTask<Void>(null); |
239 |
|
shouldThrow(); |
240 |
|
} catch (NullPointerException success) {} |
241 |
|
} |
245 |
|
*/ |
246 |
|
public void testConstructor2() { |
247 |
|
try { |
248 |
< |
new FutureTask(null, Boolean.TRUE); |
248 |
> |
new FutureTask<Boolean>(null, Boolean.TRUE); |
249 |
|
shouldThrow(); |
250 |
|
} catch (NullPointerException success) {} |
251 |
|
} |
270 |
|
for (int i = 0; i < 3; i++) { |
271 |
|
assertTrue(task.runAndReset()); |
272 |
|
checkNotDone(task); |
273 |
< |
assertEquals(i+1, task.runCount()); |
274 |
< |
assertEquals(i+1, task.runAndResetCount()); |
273 |
> |
assertEquals(i + 1, task.runCount()); |
274 |
> |
assertEquals(i + 1, task.runAndResetCount()); |
275 |
|
assertEquals(0, task.setCount()); |
276 |
|
assertEquals(0, task.setExceptionCount()); |
277 |
|
} |
287 |
|
for (int i = 0; i < 3; i++) { |
288 |
|
assertFalse(task.runAndReset()); |
289 |
|
assertEquals(0, task.runCount()); |
290 |
< |
assertEquals(i+1, task.runAndResetCount()); |
290 |
> |
assertEquals(i + 1, task.runAndResetCount()); |
291 |
|
assertEquals(0, task.setCount()); |
292 |
|
assertEquals(0, task.setExceptionCount()); |
293 |
|
} |
420 |
|
delay(LONG_DELAY_MS); |
421 |
|
shouldThrow(); |
422 |
|
} catch (InterruptedException success) {} |
423 |
+ |
assertFalse(Thread.interrupted()); |
424 |
|
}}); |
425 |
|
|
426 |
|
Thread t = newStartedThread(task); |
464 |
|
try { |
465 |
|
task.cancel(true); |
466 |
|
shouldThrow(); |
467 |
< |
} catch (SecurityException expected) {} |
467 |
> |
} catch (SecurityException success) {} |
468 |
|
|
469 |
|
// We failed to deliver the interrupt, but the world retains |
470 |
|
// its sanity, as if we had done task.cancel(false) |
490 |
|
final PublicFutureTask task = |
491 |
|
new PublicFutureTask(new Runnable() { |
492 |
|
public void run() { |
493 |
+ |
pleaseCancel.countDown(); |
494 |
|
try { |
462 |
– |
pleaseCancel.countDown(); |
495 |
|
delay(LONG_DELAY_MS); |
496 |
< |
shouldThrow(); |
497 |
< |
} catch (Throwable t) { |
498 |
< |
assertTrue(t instanceof InterruptedException); |
467 |
< |
} |
496 |
> |
threadShouldThrow(); |
497 |
> |
} catch (InterruptedException success) { |
498 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
499 |
|
throw new RuntimeException(); |
500 |
|
}}); |
501 |
|
|
629 |
|
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
630 |
|
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
631 |
|
final CountDownLatch cancelled = new CountDownLatch(1); |
632 |
< |
final Callable<Object> callable = |
602 |
< |
new CheckedCallable<Object>() { |
632 |
> |
final Callable<Object> callable = new CheckedCallable<>() { |
633 |
|
public Object realCall() throws InterruptedException { |
634 |
|
pleaseCancel.countDown(); |
635 |
|
if (mayInterruptIfRunning) { |
676 |
|
*/ |
677 |
|
public void testGet_ExecutionException() throws InterruptedException { |
678 |
|
final ArithmeticException e = new ArithmeticException(); |
679 |
< |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
679 |
> |
final PublicFutureTask task = new PublicFutureTask(new Callable<Object>() { |
680 |
|
public Object call() { |
681 |
|
throw e; |
682 |
|
}}); |
700 |
|
*/ |
701 |
|
public void testTimedGet_ExecutionException2() throws Exception { |
702 |
|
final ArithmeticException e = new ArithmeticException(); |
703 |
< |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
703 |
> |
final PublicFutureTask task = new PublicFutureTask(new Callable<Object>() { |
704 |
|
public Object call() { |
705 |
|
throw e; |
706 |
|
}}); |
719 |
|
/** |
720 |
|
* get is interruptible |
721 |
|
*/ |
722 |
< |
public void testGet_interruptible() { |
722 |
> |
public void testGet_Interruptible() { |
723 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
724 |
< |
final FutureTask task = new FutureTask(new NoOpCallable()); |
724 |
> |
final FutureTask<Object> task = new FutureTask<>(new NoOpCallable()); |
725 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
726 |
|
public void realRun() throws Exception { |
727 |
|
Thread.currentThread().interrupt(); |
748 |
|
/** |
749 |
|
* timed get is interruptible |
750 |
|
*/ |
751 |
< |
public void testTimedGet_interruptible() { |
751 |
> |
public void testTimedGet_Interruptible() { |
752 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
753 |
< |
final FutureTask task = new FutureTask(new NoOpCallable()); |
753 |
> |
final FutureTask<Object> task = new FutureTask<>(new NoOpCallable()); |
754 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
755 |
|
public void realRun() throws Exception { |
756 |
|
Thread.currentThread().interrupt(); |
757 |
|
try { |
758 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
758 |
> |
task.get(randomTimeout(), randomTimeUnit()); |
759 |
|
shouldThrow(); |
760 |
|
} catch (InterruptedException success) {} |
761 |
|
assertFalse(Thread.interrupted()); |
762 |
|
|
763 |
|
pleaseInterrupt.countDown(); |
764 |
|
try { |
765 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
765 |
> |
task.get(LONGER_DELAY_MS, MILLISECONDS); |
766 |
|
shouldThrow(); |
767 |
|
} catch (InterruptedException success) {} |
768 |
|
assertFalse(Thread.interrupted()); |
769 |
|
}}); |
770 |
|
|
771 |
|
await(pleaseInterrupt); |
772 |
+ |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
773 |
|
t.interrupt(); |
774 |
|
awaitTermination(t); |
775 |
|
checkNotDone(task); |
779 |
|
* A timed out timed get throws TimeoutException |
780 |
|
*/ |
781 |
|
public void testGet_TimeoutException() throws Exception { |
782 |
< |
FutureTask task = new FutureTask(new NoOpCallable()); |
782 |
> |
FutureTask<Object> task = new FutureTask<>(new NoOpCallable()); |
783 |
|
long startTime = System.nanoTime(); |
784 |
|
try { |
785 |
|
task.get(timeoutMillis(), MILLISECONDS); |
793 |
|
* timed get with null TimeUnit throws NullPointerException |
794 |
|
*/ |
795 |
|
public void testGet_NullTimeUnit() throws Exception { |
796 |
< |
FutureTask task = new FutureTask(new NoOpCallable()); |
796 |
> |
FutureTask<Object> task = new FutureTask<>(new NoOpCallable()); |
797 |
|
long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; |
798 |
|
|
799 |
|
for (long timeout : timeouts) { |
813 |
|
} |
814 |
|
} |
815 |
|
|
816 |
+ |
/** |
817 |
+ |
* timed get with most negative timeout works correctly (i.e. no |
818 |
+ |
* underflow bug) |
819 |
+ |
*/ |
820 |
+ |
public void testGet_NegativeInfinityTimeout() throws Exception { |
821 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
822 |
+ |
final Runnable nop = new Runnable() { public void run() {}}; |
823 |
+ |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
824 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
825 |
+ |
Runnable r = new Runnable() { public void run() { |
826 |
+ |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
827 |
+ |
try { |
828 |
+ |
task.get(timeout, NANOSECONDS); |
829 |
+ |
shouldThrow(); |
830 |
+ |
} catch (TimeoutException success) { |
831 |
+ |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
832 |
+ |
for (int i = 0; i < 10; i++) |
833 |
+ |
futures.add(pool.submit(r)); |
834 |
+ |
try { |
835 |
+ |
joinPool(pool); |
836 |
+ |
for (Future<?> future : futures) |
837 |
+ |
checkCompletedNormally(future, null); |
838 |
+ |
} finally { |
839 |
+ |
task.run(); // last resort to help terminate |
840 |
+ |
} |
841 |
+ |
} |
842 |
+ |
|
843 |
+ |
/** |
844 |
+ |
* toString indicates current completion state |
845 |
+ |
*/ |
846 |
+ |
public void testToString_incomplete() { |
847 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
848 |
+ |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
849 |
+ |
if (testImplementationDetails) |
850 |
+ |
assertTrue(f.toString().startsWith( |
851 |
+ |
identityString(f) + "[Not completed, task =")); |
852 |
+ |
} |
853 |
+ |
|
854 |
+ |
public void testToString_normal() { |
855 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
856 |
+ |
f.run(); |
857 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
858 |
+ |
if (testImplementationDetails) |
859 |
+ |
assertEquals(identityString(f) + "[Completed normally]", |
860 |
+ |
f.toString()); |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
public void testToString_exception() { |
864 |
+ |
FutureTask<String> f = new FutureTask<>( |
865 |
+ |
() -> { throw new ArithmeticException(); }); |
866 |
+ |
f.run(); |
867 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
868 |
+ |
if (testImplementationDetails) |
869 |
+ |
assertTrue(f.toString().startsWith( |
870 |
+ |
identityString(f) + "[Completed exceptionally: ")); |
871 |
+ |
} |
872 |
+ |
|
873 |
+ |
public void testToString_cancelled() { |
874 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
875 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
876 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
877 |
+ |
assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); |
878 |
+ |
if (testImplementationDetails) |
879 |
+ |
assertEquals(identityString(f) + "[Cancelled]", |
880 |
+ |
f.toString()); |
881 |
+ |
} |
882 |
+ |
} |
883 |
+ |
|
884 |
|
} |