10 |
|
import java.util.concurrent.Callable; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.ExecutorService; |
14 |
+ |
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.ExecutionException; |
16 |
|
import java.util.concurrent.Future; |
17 |
|
import java.util.concurrent.FutureTask; |
18 |
|
import java.util.concurrent.TimeoutException; |
19 |
|
import java.util.concurrent.atomic.AtomicInteger; |
20 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
20 |
> |
import static java.util.concurrent.TimeUnit.*; |
21 |
|
import java.util.*; |
22 |
|
|
23 |
|
public class FutureTaskTest extends JSR166TestCase { |
38 |
|
assertEquals(1, pf.doneCount()); |
39 |
|
assertFalse(pf.runAndReset()); |
40 |
|
assertEquals(1, pf.doneCount()); |
41 |
+ |
Object r = null; Object exInfo = null; |
42 |
+ |
try { |
43 |
+ |
r = f.get(); |
44 |
+ |
} catch (CancellationException t) { |
45 |
+ |
exInfo = CancellationException.class; |
46 |
+ |
} catch (ExecutionException t) { |
47 |
+ |
exInfo = t.getCause(); |
48 |
+ |
} catch (Throwable t) { |
49 |
+ |
threadUnexpectedException(t); |
50 |
+ |
} |
51 |
|
|
52 |
|
// Check that run and runAndReset have no effect. |
53 |
|
int savedRunCount = pf.runCount(); |
43 |
– |
int savedSetCount = pf.setCount(); |
44 |
– |
int savedSetExceptionCount = pf.setExceptionCount(); |
54 |
|
pf.run(); |
55 |
|
pf.runAndReset(); |
56 |
|
assertEquals(savedRunCount, pf.runCount()); |
57 |
< |
assertEquals(savedSetCount, pf.setCount()); |
58 |
< |
assertEquals(savedSetExceptionCount, pf.setExceptionCount()); |
57 |
> |
try { |
58 |
> |
assertSame(r, f.get()); |
59 |
> |
} catch (CancellationException t) { |
60 |
> |
assertSame(exInfo, CancellationException.class); |
61 |
> |
} catch (ExecutionException t) { |
62 |
> |
assertSame(exInfo, t.getCause()); |
63 |
> |
} catch (Throwable t) { |
64 |
> |
threadUnexpectedException(t); |
65 |
> |
} |
66 |
|
assertTrue(f.isDone()); |
67 |
|
} |
68 |
|
} |
84 |
|
FutureTask ft = (FutureTask<?>) f; |
85 |
|
// Check that run methods do nothing |
86 |
|
ft.run(); |
87 |
< |
if (f instanceof PublicFutureTask) |
88 |
< |
assertFalse(((PublicFutureTask) f).runAndReset()); |
87 |
> |
if (f instanceof PublicFutureTask) { |
88 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
89 |
> |
int savedRunCount = pf.runCount(); |
90 |
> |
pf.run(); |
91 |
> |
assertFalse(pf.runAndReset()); |
92 |
> |
assertEquals(savedRunCount, pf.runCount()); |
93 |
> |
} |
94 |
|
checkNotDone(f); |
95 |
|
} |
96 |
|
} |
482 |
|
try { |
483 |
|
pleaseCancel.countDown(); |
484 |
|
delay(LONG_DELAY_MS); |
485 |
< |
shouldThrow(); |
486 |
< |
} catch (Throwable t) { |
487 |
< |
assertTrue(t instanceof InterruptedException); |
467 |
< |
} |
485 |
> |
threadShouldThrow(); |
486 |
> |
} catch (InterruptedException success) { |
487 |
> |
} catch (Throwable t) { threadUnexpectedException(t); } |
488 |
|
throw new RuntimeException(); |
489 |
|
}}); |
490 |
|
|
802 |
|
} |
803 |
|
} |
804 |
|
|
805 |
+ |
/** |
806 |
+ |
* timed get with most negative timeout works correctly (i.e. no |
807 |
+ |
* underflow bug) |
808 |
+ |
*/ |
809 |
+ |
public void testGet_NegativeInfinityTimeout() throws Exception { |
810 |
+ |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
811 |
+ |
final Runnable nop = new Runnable() { public void run() {}}; |
812 |
+ |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
813 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
814 |
+ |
Runnable r = new Runnable() { public void run() { |
815 |
+ |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
816 |
+ |
try { |
817 |
+ |
task.get(timeout, NANOSECONDS); |
818 |
+ |
shouldThrow(); |
819 |
+ |
} catch (TimeoutException success) { |
820 |
+ |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
821 |
+ |
for (int i = 0; i < 10; i++) |
822 |
+ |
futures.add(pool.submit(r)); |
823 |
+ |
try { |
824 |
+ |
joinPool(pool); |
825 |
+ |
for (Future<?> future : futures) |
826 |
+ |
checkCompletedNormally(future, null); |
827 |
+ |
} finally { |
828 |
+ |
task.run(); // last resort to help terminate |
829 |
+ |
} |
830 |
+ |
} |
831 |
+ |
|
832 |
|
} |