8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.List; |
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); |
60 |
|
pf.run(); |
61 |
|
pf.runAndReset(); |
62 |
|
assertEquals(savedRunCount, pf.runCount()); |
63 |
+ |
Object r2 = null; |
64 |
|
try { |
65 |
< |
assertSame(r, f.get()); |
65 |
> |
r2 = f.get(); |
66 |
|
} catch (CancellationException t) { |
67 |
|
assertSame(exInfo, CancellationException.class); |
68 |
|
} catch (ExecutionException t) { |
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) { |
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); } |
112 |
< |
try { |
113 |
< |
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); } |
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) |
677 |
|
*/ |
678 |
|
public void testGet_ExecutionException() throws InterruptedException { |
679 |
|
final ArithmeticException e = new ArithmeticException(); |
680 |
< |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
680 |
> |
final PublicFutureTask task = new PublicFutureTask(new Callable<Object>() { |
681 |
|
public Object call() { |
682 |
|
throw e; |
683 |
|
}}); |
701 |
|
*/ |
702 |
|
public void testTimedGet_ExecutionException2() throws Exception { |
703 |
|
final ArithmeticException e = new ArithmeticException(); |
704 |
< |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
704 |
> |
final PublicFutureTask task = new PublicFutureTask(new Callable<Object>() { |
705 |
|
public Object call() { |
706 |
|
throw e; |
707 |
|
}}); |
720 |
|
/** |
721 |
|
* get is interruptible |
722 |
|
*/ |
723 |
< |
public void testGet_interruptible() { |
723 |
> |
public void testGet_Interruptible() { |
724 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
725 |
< |
final FutureTask task = new FutureTask(new NoOpCallable()); |
725 |
> |
final FutureTask<Object> task = new FutureTask<Object>(new NoOpCallable()); |
726 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
727 |
|
public void realRun() throws Exception { |
728 |
|
Thread.currentThread().interrupt(); |
749 |
|
/** |
750 |
|
* timed get is interruptible |
751 |
|
*/ |
752 |
< |
public void testTimedGet_interruptible() { |
752 |
> |
public void testTimedGet_Interruptible() { |
753 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
754 |
< |
final FutureTask task = new FutureTask(new NoOpCallable()); |
754 |
> |
final FutureTask<Object> task = new FutureTask<Object>(new NoOpCallable()); |
755 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
756 |
|
public void realRun() throws Exception { |
757 |
|
Thread.currentThread().interrupt(); |
758 |
|
try { |
759 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
759 |
> |
task.get(randomTimeout(), randomTimeUnit()); |
760 |
|
shouldThrow(); |
761 |
|
} catch (InterruptedException success) {} |
762 |
|
assertFalse(Thread.interrupted()); |
763 |
|
|
764 |
|
pleaseInterrupt.countDown(); |
765 |
|
try { |
766 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
766 |
> |
task.get(LONGER_DELAY_MS, MILLISECONDS); |
767 |
|
shouldThrow(); |
768 |
|
} catch (InterruptedException success) {} |
769 |
|
assertFalse(Thread.interrupted()); |
770 |
|
}}); |
771 |
|
|
772 |
|
await(pleaseInterrupt); |
773 |
+ |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
774 |
|
t.interrupt(); |
775 |
|
awaitTermination(t); |
776 |
|
checkNotDone(task); |
780 |
|
* A timed out timed get throws TimeoutException |
781 |
|
*/ |
782 |
|
public void testGet_TimeoutException() throws Exception { |
783 |
< |
FutureTask task = new FutureTask(new NoOpCallable()); |
783 |
> |
FutureTask<Object> task = new FutureTask<Object>(new NoOpCallable()); |
784 |
|
long startTime = System.nanoTime(); |
785 |
|
try { |
786 |
|
task.get(timeoutMillis(), MILLISECONDS); |
794 |
|
* timed get with null TimeUnit throws NullPointerException |
795 |
|
*/ |
796 |
|
public void testGet_NullTimeUnit() throws Exception { |
797 |
< |
FutureTask task = new FutureTask(new NoOpCallable()); |
797 |
> |
FutureTask<Object> task = new FutureTask<Object>(new NoOpCallable()); |
798 |
|
long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; |
799 |
|
|
800 |
|
for (long timeout : timeouts) { |
841 |
|
} |
842 |
|
} |
843 |
|
|
844 |
+ |
/** |
845 |
+ |
* toString indicates current completion state |
846 |
+ |
*/ |
847 |
+ |
public void testToString_incomplete() { |
848 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
849 |
+ |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
850 |
+ |
if (testImplementationDetails) |
851 |
+ |
assertTrue(f.toString().startsWith( |
852 |
+ |
identityString(f) + "[Not completed, task =")); |
853 |
+ |
} |
854 |
+ |
|
855 |
+ |
public void testToString_normal() { |
856 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
857 |
+ |
f.run(); |
858 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
859 |
+ |
if (testImplementationDetails) |
860 |
+ |
assertEquals(identityString(f) + "[Completed normally]", |
861 |
+ |
f.toString()); |
862 |
+ |
} |
863 |
+ |
|
864 |
+ |
public void testToString_exception() { |
865 |
+ |
FutureTask<String> f = new FutureTask<>( |
866 |
+ |
() -> { throw new ArithmeticException(); }); |
867 |
+ |
f.run(); |
868 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
869 |
+ |
if (testImplementationDetails) |
870 |
+ |
assertTrue(f.toString().startsWith( |
871 |
+ |
identityString(f) + "[Completed exceptionally: ")); |
872 |
+ |
} |
873 |
+ |
|
874 |
+ |
public void testToString_cancelled() { |
875 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
876 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
877 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
878 |
+ |
assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); |
879 |
+ |
if (testImplementationDetails) |
880 |
+ |
assertEquals(identityString(f) + "[Cancelled]", |
881 |
+ |
f.toString()); |
882 |
+ |
} |
883 |
+ |
} |
884 |
+ |
|
885 |
|
} |