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); |
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 |
|
} |
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); } |
104 |
< |
try { |
105 |
< |
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()); |
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 { |
482 |
– |
pleaseCancel.countDown(); |
495 |
|
delay(LONG_DELAY_MS); |
496 |
< |
shouldThrow(); |
497 |
< |
} catch (Throwable t) { |
498 |
< |
assertTrue(t instanceof InterruptedException); |
487 |
< |
} |
496 |
> |
threadShouldThrow(); |
497 |
> |
} catch (InterruptedException success) { |
498 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
499 |
|
throw new RuntimeException(); |
500 |
|
}}); |
501 |
|
|
754 |
|
final FutureTask task = new FutureTask(new NoOpCallable()); |
755 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
756 |
|
public void realRun() throws Exception { |
757 |
+ |
long startTime = System.nanoTime(); |
758 |
+ |
|
759 |
|
Thread.currentThread().interrupt(); |
760 |
|
try { |
761 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
761 |
> |
task.get(randomTimeout(), randomTimeUnit()); |
762 |
|
shouldThrow(); |
763 |
|
} catch (InterruptedException success) {} |
764 |
|
assertFalse(Thread.interrupted()); |
765 |
|
|
766 |
|
pleaseInterrupt.countDown(); |
767 |
|
try { |
768 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
768 |
> |
task.get(LONG_DELAY_MS, MILLISECONDS); |
769 |
|
shouldThrow(); |
770 |
|
} catch (InterruptedException success) {} |
771 |
|
assertFalse(Thread.interrupted()); |
772 |
+ |
|
773 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
774 |
|
}}); |
775 |
|
|
776 |
|
await(pleaseInterrupt); |
817 |
|
} |
818 |
|
} |
819 |
|
|
820 |
+ |
/** |
821 |
+ |
* timed get with most negative timeout works correctly (i.e. no |
822 |
+ |
* underflow bug) |
823 |
+ |
*/ |
824 |
+ |
public void testGet_NegativeInfinityTimeout() throws Exception { |
825 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
826 |
+ |
final Runnable nop = new Runnable() { public void run() {}}; |
827 |
+ |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
828 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
829 |
+ |
Runnable r = new Runnable() { public void run() { |
830 |
+ |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
831 |
+ |
try { |
832 |
+ |
task.get(timeout, NANOSECONDS); |
833 |
+ |
shouldThrow(); |
834 |
+ |
} catch (TimeoutException success) { |
835 |
+ |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
836 |
+ |
for (int i = 0; i < 10; i++) |
837 |
+ |
futures.add(pool.submit(r)); |
838 |
+ |
try { |
839 |
+ |
joinPool(pool); |
840 |
+ |
for (Future<?> future : futures) |
841 |
+ |
checkCompletedNormally(future, null); |
842 |
+ |
} finally { |
843 |
+ |
task.run(); // last resort to help terminate |
844 |
+ |
} |
845 |
+ |
} |
846 |
+ |
|
847 |
+ |
/** |
848 |
+ |
* toString indicates current completion state |
849 |
+ |
*/ |
850 |
+ |
public void testToString_incomplete() { |
851 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
852 |
+ |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
853 |
+ |
if (testImplementationDetails) |
854 |
+ |
assertTrue(f.toString().startsWith( |
855 |
+ |
identityString(f) + "[Not completed, task =")); |
856 |
+ |
} |
857 |
+ |
|
858 |
+ |
public void testToString_normal() { |
859 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
860 |
+ |
f.run(); |
861 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
862 |
+ |
if (testImplementationDetails) |
863 |
+ |
assertEquals(identityString(f) + "[Completed normally]", |
864 |
+ |
f.toString()); |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
public void testToString_exception() { |
868 |
+ |
FutureTask<String> f = new FutureTask<>( |
869 |
+ |
() -> { throw new ArithmeticException(); }); |
870 |
+ |
f.run(); |
871 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
872 |
+ |
if (testImplementationDetails) |
873 |
+ |
assertTrue(f.toString().startsWith( |
874 |
+ |
identityString(f) + "[Completed exceptionally: ")); |
875 |
+ |
} |
876 |
+ |
|
877 |
+ |
public void testToString_cancelled() { |
878 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
879 |
+ |
FutureTask<String> f = new FutureTask<>(() -> ""); |
880 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
881 |
+ |
assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); |
882 |
+ |
if (testImplementationDetails) |
883 |
+ |
assertEquals(identityString(f) + "[Cancelled]", |
884 |
+ |
f.toString()); |
885 |
+ |
} |
886 |
+ |
} |
887 |
+ |
|
888 |
|
} |