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 |
> |
try { |
64 |
> |
assertSame(r, f.get()); |
65 |
> |
} catch (CancellationException t) { |
66 |
> |
assertSame(exInfo, CancellationException.class); |
67 |
> |
} catch (ExecutionException t) { |
68 |
> |
assertSame(exInfo, t.getCause()); |
69 |
> |
} catch (Throwable t) { |
70 |
> |
threadUnexpectedException(t); |
71 |
> |
} |
72 |
|
assertTrue(f.isDone()); |
73 |
|
} |
74 |
|
} |
90 |
|
FutureTask ft = (FutureTask<?>) f; |
91 |
|
// Check that run methods do nothing |
92 |
|
ft.run(); |
93 |
< |
if (f instanceof PublicFutureTask) |
94 |
< |
assertFalse(((PublicFutureTask) f).runAndReset()); |
93 |
> |
if (f instanceof PublicFutureTask) { |
94 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
95 |
> |
int savedRunCount = pf.runCount(); |
96 |
> |
pf.run(); |
97 |
> |
assertFalse(pf.runAndReset()); |
98 |
> |
assertEquals(savedRunCount, pf.runCount()); |
99 |
> |
} |
100 |
|
checkNotDone(f); |
101 |
|
} |
102 |
|
} |
107 |
|
|
108 |
|
try { |
109 |
|
assertSame(expected, f.get()); |
110 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
< |
try { |
85 |
< |
assertSame(expected, f.get(5L, SECONDS)); |
110 |
> |
assertSame(expected, f.get(randomTimeout(), randomTimeUnit())); |
111 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
112 |
|
} |
113 |
|
|
122 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
|
|
124 |
|
try { |
125 |
< |
f.get(5L, SECONDS); |
125 |
> |
f.get(randomTimeout(), randomTimeUnit()); |
126 |
|
shouldThrow(); |
127 |
|
} catch (CancellationException success) { |
128 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
132 |
|
pf.set(new Object()); |
133 |
|
pf.setException(new Error()); |
134 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
135 |
< |
pf.cancel(true); |
135 |
> |
pf.cancel(mayInterruptIfRunning); |
136 |
|
} |
137 |
|
} |
138 |
|
|
148 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
|
|
150 |
|
try { |
151 |
< |
f.get(5L, SECONDS); |
151 |
> |
f.get(randomTimeout(), randomTimeUnit()); |
152 |
|
shouldThrow(); |
153 |
|
} catch (ExecutionException success) { |
154 |
|
assertSame(t, success.getCause()); |
264 |
|
for (int i = 0; i < 3; i++) { |
265 |
|
assertTrue(task.runAndReset()); |
266 |
|
checkNotDone(task); |
267 |
< |
assertEquals(i+1, task.runCount()); |
268 |
< |
assertEquals(i+1, task.runAndResetCount()); |
267 |
> |
assertEquals(i + 1, task.runCount()); |
268 |
> |
assertEquals(i + 1, task.runAndResetCount()); |
269 |
|
assertEquals(0, task.setCount()); |
270 |
|
assertEquals(0, task.setExceptionCount()); |
271 |
|
} |
281 |
|
for (int i = 0; i < 3; i++) { |
282 |
|
assertFalse(task.runAndReset()); |
283 |
|
assertEquals(0, task.runCount()); |
284 |
< |
assertEquals(i+1, task.runAndResetCount()); |
284 |
> |
assertEquals(i + 1, task.runAndResetCount()); |
285 |
|
assertEquals(0, task.setCount()); |
286 |
|
assertEquals(0, task.setExceptionCount()); |
287 |
|
} |
414 |
|
delay(LONG_DELAY_MS); |
415 |
|
shouldThrow(); |
416 |
|
} catch (InterruptedException success) {} |
417 |
+ |
assertFalse(Thread.interrupted()); |
418 |
|
}}); |
419 |
|
|
420 |
|
Thread t = newStartedThread(task); |
484 |
|
final PublicFutureTask task = |
485 |
|
new PublicFutureTask(new Runnable() { |
486 |
|
public void run() { |
487 |
+ |
pleaseCancel.countDown(); |
488 |
|
try { |
462 |
– |
pleaseCancel.countDown(); |
489 |
|
delay(LONG_DELAY_MS); |
490 |
< |
shouldThrow(); |
491 |
< |
} catch (Throwable t) { |
492 |
< |
assertTrue(t instanceof InterruptedException); |
467 |
< |
} |
490 |
> |
threadShouldThrow(); |
491 |
> |
} catch (InterruptedException success) { |
492 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
493 |
|
throw new RuntimeException(); |
494 |
|
}}); |
495 |
|
|
807 |
|
} |
808 |
|
} |
809 |
|
|
810 |
+ |
/** |
811 |
+ |
* timed get with most negative timeout works correctly (i.e. no |
812 |
+ |
* underflow bug) |
813 |
+ |
*/ |
814 |
+ |
public void testGet_NegativeInfinityTimeout() throws Exception { |
815 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
816 |
+ |
final Runnable nop = new Runnable() { public void run() {}}; |
817 |
+ |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
818 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
819 |
+ |
Runnable r = new Runnable() { public void run() { |
820 |
+ |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
821 |
+ |
try { |
822 |
+ |
task.get(timeout, NANOSECONDS); |
823 |
+ |
shouldThrow(); |
824 |
+ |
} catch (TimeoutException success) { |
825 |
+ |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
826 |
+ |
for (int i = 0; i < 10; i++) |
827 |
+ |
futures.add(pool.submit(r)); |
828 |
+ |
try { |
829 |
+ |
joinPool(pool); |
830 |
+ |
for (Future<?> future : futures) |
831 |
+ |
checkCompletedNormally(future, null); |
832 |
+ |
} finally { |
833 |
+ |
task.run(); // last resort to help terminate |
834 |
+ |
} |
835 |
+ |
} |
836 |
+ |
|
837 |
+ |
/** |
838 |
+ |
* toString indicates current completion state |
839 |
+ |
*/ |
840 |
+ |
public void testToString_incomplete() { |
841 |
+ |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
842 |
+ |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
843 |
+ |
if (testImplementationDetails) |
844 |
+ |
assertTrue(f.toString().startsWith( |
845 |
+ |
identityString(f) + "[Not completed, task =")); |
846 |
+ |
} |
847 |
+ |
|
848 |
+ |
public void testToString_normal() { |
849 |
+ |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
850 |
+ |
f.run(); |
851 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
852 |
+ |
if (testImplementationDetails) |
853 |
+ |
assertEquals(identityString(f) + "[Completed normally]", |
854 |
+ |
f.toString()); |
855 |
+ |
} |
856 |
+ |
|
857 |
+ |
public void testToString_exception() { |
858 |
+ |
FutureTask<String> f = new FutureTask<String>( |
859 |
+ |
() -> { throw new ArithmeticException(); }); |
860 |
+ |
f.run(); |
861 |
+ |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
862 |
+ |
if (testImplementationDetails) |
863 |
+ |
assertTrue(f.toString().startsWith( |
864 |
+ |
identityString(f) + "[Completed exceptionally: ")); |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
public void testToString_cancelled() { |
868 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
869 |
+ |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
870 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
871 |
+ |
assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); |
872 |
+ |
if (testImplementationDetails) |
873 |
+ |
assertEquals(identityString(f) + "[Cancelled]", |
874 |
+ |
f.toString()); |
875 |
+ |
} |
876 |
+ |
} |
877 |
+ |
|
878 |
|
} |