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); |
107 |
|
|
108 |
|
try { |
109 |
|
assertSame(expected, f.get()); |
110 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
104 |
< |
try { |
105 |
< |
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 { |
482 |
– |
pleaseCancel.countDown(); |
489 |
|
delay(LONG_DELAY_MS); |
490 |
|
threadShouldThrow(); |
491 |
|
} catch (InterruptedException success) { |
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 |
|
} |