1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.concurrent.*; |
11 |
< |
import java.util.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CancellationException; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.ExecutionException; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.FutureTask; |
24 |
> |
import java.util.concurrent.TimeoutException; |
25 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
26 |
> |
|
27 |
> |
import junit.framework.Test; |
28 |
> |
import junit.framework.TestSuite; |
29 |
|
|
30 |
|
public class FutureTaskTest extends JSR166TestCase { |
31 |
|
|
32 |
|
public static void main(String[] args) { |
33 |
< |
junit.textui.TestRunner.run (suite()); |
33 |
> |
main(suite(), args); |
34 |
|
} |
35 |
|
public static Test suite() { |
36 |
< |
return new TestSuite(FutureTaskTest.class); |
36 |
> |
return new TestSuite(FutureTaskTest.class); |
37 |
> |
} |
38 |
> |
|
39 |
> |
void checkIsDone(Future<?> f) { |
40 |
> |
assertTrue(f.isDone()); |
41 |
> |
assertFalse(f.cancel(false)); |
42 |
> |
assertFalse(f.cancel(true)); |
43 |
> |
if (f instanceof PublicFutureTask) { |
44 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
45 |
> |
assertEquals(1, pf.doneCount()); |
46 |
> |
assertFalse(pf.runAndReset()); |
47 |
> |
assertEquals(1, pf.doneCount()); |
48 |
> |
Object r = null; Object exInfo = null; |
49 |
> |
try { |
50 |
> |
r = f.get(); |
51 |
> |
} catch (CancellationException t) { |
52 |
> |
exInfo = CancellationException.class; |
53 |
> |
} catch (ExecutionException t) { |
54 |
> |
exInfo = t.getCause(); |
55 |
> |
} catch (Throwable t) { |
56 |
> |
threadUnexpectedException(t); |
57 |
> |
} |
58 |
> |
|
59 |
> |
// Check that run and runAndReset have no effect. |
60 |
> |
int savedRunCount = pf.runCount(); |
61 |
> |
pf.run(); |
62 |
> |
pf.runAndReset(); |
63 |
> |
assertEquals(savedRunCount, pf.runCount()); |
64 |
> |
try { |
65 |
> |
assertSame(r, f.get()); |
66 |
> |
} catch (CancellationException t) { |
67 |
> |
assertSame(exInfo, CancellationException.class); |
68 |
> |
} catch (ExecutionException t) { |
69 |
> |
assertSame(exInfo, t.getCause()); |
70 |
> |
} catch (Throwable t) { |
71 |
> |
threadUnexpectedException(t); |
72 |
> |
} |
73 |
> |
assertTrue(f.isDone()); |
74 |
> |
} |
75 |
> |
} |
76 |
> |
|
77 |
> |
void checkNotDone(Future<?> f) { |
78 |
> |
assertFalse(f.isDone()); |
79 |
> |
assertFalse(f.isCancelled()); |
80 |
> |
if (f instanceof PublicFutureTask) { |
81 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
82 |
> |
assertEquals(0, pf.doneCount()); |
83 |
> |
assertEquals(0, pf.setCount()); |
84 |
> |
assertEquals(0, pf.setExceptionCount()); |
85 |
> |
} |
86 |
> |
} |
87 |
> |
|
88 |
> |
void checkIsRunning(Future<?> f) { |
89 |
> |
checkNotDone(f); |
90 |
> |
if (f instanceof FutureTask) { |
91 |
> |
FutureTask ft = (FutureTask<?>) f; |
92 |
> |
// Check that run methods do nothing |
93 |
> |
ft.run(); |
94 |
> |
if (f instanceof PublicFutureTask) { |
95 |
> |
PublicFutureTask pf = (PublicFutureTask) f; |
96 |
> |
int savedRunCount = pf.runCount(); |
97 |
> |
pf.run(); |
98 |
> |
assertFalse(pf.runAndReset()); |
99 |
> |
assertEquals(savedRunCount, pf.runCount()); |
100 |
> |
} |
101 |
> |
checkNotDone(f); |
102 |
> |
} |
103 |
> |
} |
104 |
> |
|
105 |
> |
<T> void checkCompletedNormally(Future<T> f, T expected) { |
106 |
> |
checkIsDone(f); |
107 |
> |
assertFalse(f.isCancelled()); |
108 |
> |
|
109 |
> |
try { |
110 |
> |
assertSame(expected, f.get()); |
111 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
112 |
> |
try { |
113 |
> |
assertSame(expected, f.get(5L, SECONDS)); |
114 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
> |
} |
116 |
> |
|
117 |
> |
void checkCancelled(Future<?> f) { |
118 |
> |
checkIsDone(f); |
119 |
> |
assertTrue(f.isCancelled()); |
120 |
> |
|
121 |
> |
try { |
122 |
> |
f.get(); |
123 |
> |
shouldThrow(); |
124 |
> |
} catch (CancellationException success) { |
125 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
126 |
> |
|
127 |
> |
try { |
128 |
> |
f.get(5L, SECONDS); |
129 |
> |
shouldThrow(); |
130 |
> |
} catch (CancellationException success) { |
131 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
132 |
> |
} |
133 |
> |
|
134 |
> |
void tryToConfuseDoneTask(PublicFutureTask pf) { |
135 |
> |
pf.set(new Object()); |
136 |
> |
pf.setException(new Error()); |
137 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
138 |
> |
pf.cancel(mayInterruptIfRunning); |
139 |
> |
} |
140 |
> |
} |
141 |
> |
|
142 |
> |
void checkCompletedAbnormally(Future<?> f, Throwable t) { |
143 |
> |
checkIsDone(f); |
144 |
> |
assertFalse(f.isCancelled()); |
145 |
> |
|
146 |
> |
try { |
147 |
> |
f.get(); |
148 |
> |
shouldThrow(); |
149 |
> |
} catch (ExecutionException success) { |
150 |
> |
assertSame(t, success.getCause()); |
151 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
152 |
> |
|
153 |
> |
try { |
154 |
> |
f.get(5L, SECONDS); |
155 |
> |
shouldThrow(); |
156 |
> |
} catch (ExecutionException success) { |
157 |
> |
assertSame(t, success.getCause()); |
158 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
|
* Subclass to expose protected methods |
163 |
|
*/ |
164 |
|
static class PublicFutureTask extends FutureTask { |
165 |
< |
public PublicFutureTask(Callable r) { super(r); } |
166 |
< |
public boolean runAndReset() { return super.runAndReset(); } |
167 |
< |
public void set(Object x) { super.set(x); } |
168 |
< |
public void setException(Throwable t) { super.setException(t); } |
165 |
> |
private final AtomicInteger runCount; |
166 |
> |
private final AtomicInteger doneCount = new AtomicInteger(0); |
167 |
> |
private final AtomicInteger runAndResetCount = new AtomicInteger(0); |
168 |
> |
private final AtomicInteger setCount = new AtomicInteger(0); |
169 |
> |
private final AtomicInteger setExceptionCount = new AtomicInteger(0); |
170 |
> |
public int runCount() { return runCount.get(); } |
171 |
> |
public int doneCount() { return doneCount.get(); } |
172 |
> |
public int runAndResetCount() { return runAndResetCount.get(); } |
173 |
> |
public int setCount() { return setCount.get(); } |
174 |
> |
public int setExceptionCount() { return setExceptionCount.get(); } |
175 |
> |
|
176 |
> |
PublicFutureTask(Runnable runnable) { |
177 |
> |
this(runnable, seven); |
178 |
> |
} |
179 |
> |
PublicFutureTask(Runnable runnable, Object result) { |
180 |
> |
this(runnable, result, new AtomicInteger(0)); |
181 |
> |
} |
182 |
> |
private PublicFutureTask(final Runnable runnable, Object result, |
183 |
> |
final AtomicInteger runCount) { |
184 |
> |
super(new Runnable() { |
185 |
> |
public void run() { |
186 |
> |
runCount.getAndIncrement(); |
187 |
> |
runnable.run(); |
188 |
> |
}}, result); |
189 |
> |
this.runCount = runCount; |
190 |
> |
} |
191 |
> |
PublicFutureTask(Callable callable) { |
192 |
> |
this(callable, new AtomicInteger(0)); |
193 |
> |
} |
194 |
> |
private PublicFutureTask(final Callable callable, |
195 |
> |
final AtomicInteger runCount) { |
196 |
> |
super(new Callable() { |
197 |
> |
public Object call() throws Exception { |
198 |
> |
runCount.getAndIncrement(); |
199 |
> |
return callable.call(); |
200 |
> |
}}); |
201 |
> |
this.runCount = runCount; |
202 |
> |
} |
203 |
> |
@Override public void done() { |
204 |
> |
assertTrue(isDone()); |
205 |
> |
doneCount.incrementAndGet(); |
206 |
> |
super.done(); |
207 |
> |
} |
208 |
> |
@Override public boolean runAndReset() { |
209 |
> |
runAndResetCount.incrementAndGet(); |
210 |
> |
return super.runAndReset(); |
211 |
> |
} |
212 |
> |
@Override public void set(Object x) { |
213 |
> |
setCount.incrementAndGet(); |
214 |
> |
super.set(x); |
215 |
> |
} |
216 |
> |
@Override public void setException(Throwable t) { |
217 |
> |
setExceptionCount.incrementAndGet(); |
218 |
> |
super.setException(t); |
219 |
> |
} |
220 |
> |
} |
221 |
> |
|
222 |
> |
class Counter extends CheckedRunnable { |
223 |
> |
final AtomicInteger count = new AtomicInteger(0); |
224 |
> |
public int get() { return count.get(); } |
225 |
> |
public void realRun() { |
226 |
> |
count.getAndIncrement(); |
227 |
> |
} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* Creating a future with a null callable throws NPE |
231 |
> |
* creating a future with a null callable throws NullPointerException |
232 |
|
*/ |
233 |
|
public void testConstructor() { |
234 |
|
try { |
235 |
< |
FutureTask task = new FutureTask(null); |
235 |
> |
new FutureTask(null); |
236 |
|
shouldThrow(); |
237 |
< |
} |
39 |
< |
catch(NullPointerException success) { |
40 |
< |
} |
237 |
> |
} catch (NullPointerException success) {} |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
< |
* creating a future with null runnable fails |
241 |
> |
* creating a future with null runnable throws NullPointerException |
242 |
|
*/ |
243 |
|
public void testConstructor2() { |
244 |
|
try { |
245 |
< |
FutureTask task = new FutureTask(null, Boolean.TRUE); |
245 |
> |
new FutureTask(null, Boolean.TRUE); |
246 |
|
shouldThrow(); |
247 |
< |
} |
51 |
< |
catch(NullPointerException success) { |
52 |
< |
} |
247 |
> |
} catch (NullPointerException success) {} |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
|
* isDone is true when a task completes |
252 |
|
*/ |
253 |
|
public void testIsDone() { |
254 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
255 |
< |
task.run(); |
256 |
< |
assertTrue(task.isDone()); |
257 |
< |
assertFalse(task.isCancelled()); |
254 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
255 |
> |
assertFalse(task.isDone()); |
256 |
> |
task.run(); |
257 |
> |
assertTrue(task.isDone()); |
258 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
259 |
> |
assertEquals(1, task.runCount()); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
264 |
|
*/ |
265 |
|
public void testRunAndReset() { |
266 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
267 |
< |
assertTrue(task.runAndReset()); |
268 |
< |
assertFalse(task.isDone()); |
267 |
> |
for (int i = 0; i < 3; i++) { |
268 |
> |
assertTrue(task.runAndReset()); |
269 |
> |
checkNotDone(task); |
270 |
> |
assertEquals(i + 1, task.runCount()); |
271 |
> |
assertEquals(i + 1, task.runAndResetCount()); |
272 |
> |
assertEquals(0, task.setCount()); |
273 |
> |
assertEquals(0, task.setExceptionCount()); |
274 |
> |
} |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* runAndReset after cancellation fails |
279 |
|
*/ |
280 |
< |
public void testResetAfterCancel() { |
281 |
< |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
282 |
< |
assertTrue(task.cancel(false)); |
283 |
< |
assertFalse(task.runAndReset()); |
284 |
< |
assertTrue(task.isDone()); |
285 |
< |
assertTrue(task.isCancelled()); |
280 |
> |
public void testRunAndResetAfterCancel() { |
281 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
282 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
283 |
> |
assertTrue(task.cancel(mayInterruptIfRunning)); |
284 |
> |
for (int i = 0; i < 3; i++) { |
285 |
> |
assertFalse(task.runAndReset()); |
286 |
> |
assertEquals(0, task.runCount()); |
287 |
> |
assertEquals(i + 1, task.runAndResetCount()); |
288 |
> |
assertEquals(0, task.setCount()); |
289 |
> |
assertEquals(0, task.setExceptionCount()); |
290 |
> |
} |
291 |
> |
tryToConfuseDoneTask(task); |
292 |
> |
checkCancelled(task); |
293 |
> |
} |
294 |
|
} |
295 |
|
|
85 |
– |
|
86 |
– |
|
296 |
|
/** |
297 |
< |
* setting value gauses get to return it |
297 |
> |
* setting value causes get to return it |
298 |
|
*/ |
299 |
< |
public void testSet() { |
299 |
> |
public void testSet() throws Exception { |
300 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
301 |
|
task.set(one); |
302 |
< |
try { |
303 |
< |
assertEquals(task.get(), one); |
304 |
< |
} |
305 |
< |
catch(Exception e) { |
306 |
< |
unexpectedException(); |
307 |
< |
} |
302 |
> |
for (int i = 0; i < 3; i++) { |
303 |
> |
assertSame(one, task.get()); |
304 |
> |
assertSame(one, task.get(LONG_DELAY_MS, MILLISECONDS)); |
305 |
> |
assertEquals(1, task.setCount()); |
306 |
> |
} |
307 |
> |
tryToConfuseDoneTask(task); |
308 |
> |
checkCompletedNormally(task, one); |
309 |
> |
assertEquals(0, task.runCount()); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
|
* setException causes get to throw ExecutionException |
314 |
|
*/ |
315 |
< |
public void testSetException() { |
315 |
> |
public void testSetException_get() throws Exception { |
316 |
|
Exception nse = new NoSuchElementException(); |
317 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
318 |
|
task.setException(nse); |
319 |
+ |
|
320 |
|
try { |
321 |
< |
Object x = task.get(); |
321 |
> |
task.get(); |
322 |
|
shouldThrow(); |
323 |
+ |
} catch (ExecutionException success) { |
324 |
+ |
assertSame(nse, success.getCause()); |
325 |
+ |
checkCompletedAbnormally(task, nse); |
326 |
|
} |
327 |
< |
catch(ExecutionException ee) { |
328 |
< |
Throwable cause = ee.getCause(); |
329 |
< |
assertEquals(cause, nse); |
330 |
< |
} |
331 |
< |
catch(Exception e) { |
332 |
< |
unexpectedException(); |
327 |
> |
|
328 |
> |
try { |
329 |
> |
task.get(LONG_DELAY_MS, MILLISECONDS); |
330 |
> |
shouldThrow(); |
331 |
> |
} catch (ExecutionException success) { |
332 |
> |
assertSame(nse, success.getCause()); |
333 |
> |
checkCompletedAbnormally(task, nse); |
334 |
|
} |
335 |
+ |
|
336 |
+ |
assertEquals(1, task.setExceptionCount()); |
337 |
+ |
assertEquals(0, task.setCount()); |
338 |
+ |
tryToConfuseDoneTask(task); |
339 |
+ |
checkCompletedAbnormally(task, nse); |
340 |
+ |
assertEquals(0, task.runCount()); |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
< |
* Cancelling before running succeeds |
344 |
> |
* cancel(false) before run succeeds |
345 |
|
*/ |
346 |
|
public void testCancelBeforeRun() { |
347 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
347 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
348 |
|
assertTrue(task.cancel(false)); |
349 |
< |
task.run(); |
350 |
< |
assertTrue(task.isDone()); |
351 |
< |
assertTrue(task.isCancelled()); |
349 |
> |
task.run(); |
350 |
> |
assertEquals(0, task.runCount()); |
351 |
> |
assertEquals(0, task.setCount()); |
352 |
> |
assertEquals(0, task.setExceptionCount()); |
353 |
> |
assertTrue(task.isCancelled()); |
354 |
> |
assertTrue(task.isDone()); |
355 |
> |
tryToConfuseDoneTask(task); |
356 |
> |
assertEquals(0, task.runCount()); |
357 |
> |
checkCancelled(task); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
< |
* Cancel(true) before run succeeds |
361 |
> |
* cancel(true) before run succeeds |
362 |
|
*/ |
363 |
|
public void testCancelBeforeRun2() { |
364 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
364 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
365 |
|
assertTrue(task.cancel(true)); |
366 |
< |
task.run(); |
367 |
< |
assertTrue(task.isDone()); |
368 |
< |
assertTrue(task.isCancelled()); |
366 |
> |
task.run(); |
367 |
> |
assertEquals(0, task.runCount()); |
368 |
> |
assertEquals(0, task.setCount()); |
369 |
> |
assertEquals(0, task.setExceptionCount()); |
370 |
> |
assertTrue(task.isCancelled()); |
371 |
> |
assertTrue(task.isDone()); |
372 |
> |
tryToConfuseDoneTask(task); |
373 |
> |
assertEquals(0, task.runCount()); |
374 |
> |
checkCancelled(task); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* cancel of a completed task fails |
378 |
> |
* cancel(false) of a completed task fails |
379 |
|
*/ |
380 |
|
public void testCancelAfterRun() { |
381 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
382 |
< |
task.run(); |
381 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
382 |
> |
task.run(); |
383 |
|
assertFalse(task.cancel(false)); |
384 |
< |
assertTrue(task.isDone()); |
385 |
< |
assertFalse(task.isCancelled()); |
384 |
> |
assertEquals(1, task.runCount()); |
385 |
> |
assertEquals(1, task.setCount()); |
386 |
> |
assertEquals(0, task.setExceptionCount()); |
387 |
> |
tryToConfuseDoneTask(task); |
388 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
389 |
> |
assertEquals(1, task.runCount()); |
390 |
> |
} |
391 |
> |
|
392 |
> |
/** |
393 |
> |
* cancel(true) of a completed task fails |
394 |
> |
*/ |
395 |
> |
public void testCancelAfterRun2() { |
396 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
397 |
> |
task.run(); |
398 |
> |
assertFalse(task.cancel(true)); |
399 |
> |
assertEquals(1, task.runCount()); |
400 |
> |
assertEquals(1, task.setCount()); |
401 |
> |
assertEquals(0, task.setExceptionCount()); |
402 |
> |
tryToConfuseDoneTask(task); |
403 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
404 |
> |
assertEquals(1, task.runCount()); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* cancel(true) interrupts a running task |
408 |
> |
* cancel(true) interrupts a running task that subsequently succeeds |
409 |
|
*/ |
410 |
|
public void testCancelInterrupt() { |
411 |
< |
FutureTask task = new FutureTask( new Callable() { |
412 |
< |
public Object call() { |
411 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
412 |
> |
final PublicFutureTask task = |
413 |
> |
new PublicFutureTask(new CheckedRunnable() { |
414 |
> |
public void realRun() { |
415 |
> |
pleaseCancel.countDown(); |
416 |
|
try { |
417 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
418 |
< |
threadShouldThrow(); |
419 |
< |
} |
420 |
< |
catch (InterruptedException success) {} |
421 |
< |
return Boolean.TRUE; |
422 |
< |
} }); |
423 |
< |
Thread t = new Thread(task); |
417 |
> |
delay(LONG_DELAY_MS); |
418 |
> |
shouldThrow(); |
419 |
> |
} catch (InterruptedException success) {} |
420 |
> |
}}); |
421 |
> |
|
422 |
> |
Thread t = newStartedThread(task); |
423 |
> |
await(pleaseCancel); |
424 |
> |
assertTrue(task.cancel(true)); |
425 |
> |
assertTrue(task.isCancelled()); |
426 |
> |
assertTrue(task.isDone()); |
427 |
> |
awaitTermination(t); |
428 |
> |
assertEquals(1, task.runCount()); |
429 |
> |
assertEquals(1, task.setCount()); |
430 |
> |
assertEquals(0, task.setExceptionCount()); |
431 |
> |
tryToConfuseDoneTask(task); |
432 |
> |
checkCancelled(task); |
433 |
> |
} |
434 |
> |
|
435 |
> |
/** |
436 |
> |
* cancel(true) tries to interrupt a running task, but |
437 |
> |
* Thread.interrupt throws (simulating a restrictive security |
438 |
> |
* manager) |
439 |
> |
*/ |
440 |
> |
public void testCancelInterrupt_ThrowsSecurityException() { |
441 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
442 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
443 |
> |
final PublicFutureTask task = |
444 |
> |
new PublicFutureTask(new CheckedRunnable() { |
445 |
> |
public void realRun() { |
446 |
> |
pleaseCancel.countDown(); |
447 |
> |
await(cancelled); |
448 |
> |
assertFalse(Thread.interrupted()); |
449 |
> |
}}); |
450 |
> |
|
451 |
> |
final Thread t = new Thread(task) { |
452 |
> |
// Simulate a restrictive security manager. |
453 |
> |
@Override public void interrupt() { |
454 |
> |
throw new SecurityException(); |
455 |
> |
}}; |
456 |
> |
t.setDaemon(true); |
457 |
|
t.start(); |
458 |
< |
|
458 |
> |
|
459 |
> |
await(pleaseCancel); |
460 |
|
try { |
461 |
< |
Thread.sleep(SHORT_DELAY_MS); |
462 |
< |
assertTrue(task.cancel(true)); |
463 |
< |
t.join(); |
464 |
< |
assertTrue(task.isDone()); |
465 |
< |
assertTrue(task.isCancelled()); |
466 |
< |
} catch(InterruptedException e){ |
467 |
< |
unexpectedException(); |
468 |
< |
} |
461 |
> |
task.cancel(true); |
462 |
> |
shouldThrow(); |
463 |
> |
} catch (SecurityException expected) {} |
464 |
> |
|
465 |
> |
// We failed to deliver the interrupt, but the world retains |
466 |
> |
// its sanity, as if we had done task.cancel(false) |
467 |
> |
assertTrue(task.isCancelled()); |
468 |
> |
assertTrue(task.isDone()); |
469 |
> |
assertEquals(1, task.runCount()); |
470 |
> |
assertEquals(1, task.doneCount()); |
471 |
> |
assertEquals(0, task.setCount()); |
472 |
> |
assertEquals(0, task.setExceptionCount()); |
473 |
> |
cancelled.countDown(); |
474 |
> |
awaitTermination(t); |
475 |
> |
assertEquals(1, task.setCount()); |
476 |
> |
assertEquals(0, task.setExceptionCount()); |
477 |
> |
tryToConfuseDoneTask(task); |
478 |
> |
checkCancelled(task); |
479 |
|
} |
480 |
|
|
481 |
+ |
/** |
482 |
+ |
* cancel(true) interrupts a running task that subsequently throws |
483 |
+ |
*/ |
484 |
+ |
public void testCancelInterrupt_taskFails() { |
485 |
+ |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
486 |
+ |
final PublicFutureTask task = |
487 |
+ |
new PublicFutureTask(new Runnable() { |
488 |
+ |
public void run() { |
489 |
+ |
pleaseCancel.countDown(); |
490 |
+ |
try { |
491 |
+ |
delay(LONG_DELAY_MS); |
492 |
+ |
threadShouldThrow(); |
493 |
+ |
} catch (InterruptedException success) { |
494 |
+ |
} catch (Throwable t) { threadUnexpectedException(t); } |
495 |
+ |
throw new RuntimeException(); |
496 |
+ |
}}); |
497 |
+ |
|
498 |
+ |
Thread t = newStartedThread(task); |
499 |
+ |
await(pleaseCancel); |
500 |
+ |
assertTrue(task.cancel(true)); |
501 |
+ |
assertTrue(task.isCancelled()); |
502 |
+ |
awaitTermination(t); |
503 |
+ |
assertEquals(1, task.runCount()); |
504 |
+ |
assertEquals(0, task.setCount()); |
505 |
+ |
assertEquals(1, task.setExceptionCount()); |
506 |
+ |
tryToConfuseDoneTask(task); |
507 |
+ |
checkCancelled(task); |
508 |
+ |
} |
509 |
|
|
510 |
|
/** |
511 |
|
* cancel(false) does not interrupt a running task |
512 |
|
*/ |
513 |
|
public void testCancelNoInterrupt() { |
514 |
< |
FutureTask task = new FutureTask( new Callable() { |
515 |
< |
public Object call() { |
516 |
< |
try { |
517 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
518 |
< |
} |
519 |
< |
catch (InterruptedException success) { |
520 |
< |
threadFail("should not interrupt"); |
521 |
< |
} |
514 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
515 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
516 |
> |
final PublicFutureTask task = |
517 |
> |
new PublicFutureTask(new CheckedCallable<Boolean>() { |
518 |
> |
public Boolean realCall() { |
519 |
> |
pleaseCancel.countDown(); |
520 |
> |
await(cancelled); |
521 |
> |
assertFalse(Thread.interrupted()); |
522 |
|
return Boolean.TRUE; |
523 |
< |
} }); |
524 |
< |
Thread t = new Thread(task); |
525 |
< |
t.start(); |
526 |
< |
|
527 |
< |
try { |
528 |
< |
Thread.sleep(SHORT_DELAY_MS); |
529 |
< |
assertTrue(task.cancel(false)); |
530 |
< |
t.join(); |
531 |
< |
assertTrue(task.isDone()); |
532 |
< |
assertTrue(task.isCancelled()); |
533 |
< |
} catch(InterruptedException e){ |
534 |
< |
unexpectedException(); |
535 |
< |
} |
523 |
> |
}}); |
524 |
> |
|
525 |
> |
Thread t = newStartedThread(task); |
526 |
> |
await(pleaseCancel); |
527 |
> |
assertTrue(task.cancel(false)); |
528 |
> |
assertTrue(task.isCancelled()); |
529 |
> |
cancelled.countDown(); |
530 |
> |
awaitTermination(t); |
531 |
> |
assertEquals(1, task.runCount()); |
532 |
> |
assertEquals(1, task.setCount()); |
533 |
> |
assertEquals(0, task.setExceptionCount()); |
534 |
> |
tryToConfuseDoneTask(task); |
535 |
> |
checkCancelled(task); |
536 |
> |
} |
537 |
> |
|
538 |
> |
/** |
539 |
> |
* run in one thread causes get in another thread to retrieve value |
540 |
> |
*/ |
541 |
> |
public void testGetRun() { |
542 |
> |
final CountDownLatch pleaseRun = new CountDownLatch(2); |
543 |
> |
|
544 |
> |
final PublicFutureTask task = |
545 |
> |
new PublicFutureTask(new CheckedCallable<Object>() { |
546 |
> |
public Object realCall() { |
547 |
> |
return two; |
548 |
> |
}}); |
549 |
> |
|
550 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
551 |
> |
public void realRun() throws Exception { |
552 |
> |
pleaseRun.countDown(); |
553 |
> |
assertSame(two, task.get()); |
554 |
> |
}}); |
555 |
> |
|
556 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
557 |
> |
public void realRun() throws Exception { |
558 |
> |
pleaseRun.countDown(); |
559 |
> |
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
560 |
> |
}}); |
561 |
> |
|
562 |
> |
await(pleaseRun); |
563 |
> |
checkNotDone(task); |
564 |
> |
assertTrue(t1.isAlive()); |
565 |
> |
assertTrue(t2.isAlive()); |
566 |
> |
task.run(); |
567 |
> |
checkCompletedNormally(task, two); |
568 |
> |
assertEquals(1, task.runCount()); |
569 |
> |
assertEquals(1, task.setCount()); |
570 |
> |
assertEquals(0, task.setExceptionCount()); |
571 |
> |
awaitTermination(t1); |
572 |
> |
awaitTermination(t2); |
573 |
> |
tryToConfuseDoneTask(task); |
574 |
> |
checkCompletedNormally(task, two); |
575 |
|
} |
576 |
|
|
577 |
|
/** |
578 |
|
* set in one thread causes get in another thread to retrieve value |
579 |
|
*/ |
580 |
< |
public void testGet1() { |
581 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
582 |
< |
public Object call() { |
583 |
< |
try { |
584 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
585 |
< |
} catch(InterruptedException e){ |
586 |
< |
threadUnexpectedException(); |
587 |
< |
} |
588 |
< |
return Boolean.TRUE; |
589 |
< |
} |
590 |
< |
}); |
591 |
< |
Thread t = new Thread(new Runnable() { |
592 |
< |
public void run() { |
593 |
< |
try { |
594 |
< |
ft.get(); |
595 |
< |
} catch(Exception e){ |
596 |
< |
threadUnexpectedException(); |
597 |
< |
} |
598 |
< |
} |
599 |
< |
}); |
600 |
< |
try { |
601 |
< |
assertFalse(ft.isDone()); |
602 |
< |
assertFalse(ft.isCancelled()); |
603 |
< |
t.start(); |
604 |
< |
Thread.sleep(SHORT_DELAY_MS); |
605 |
< |
ft.run(); |
606 |
< |
t.join(); |
607 |
< |
assertTrue(ft.isDone()); |
608 |
< |
assertFalse(ft.isCancelled()); |
609 |
< |
} catch(InterruptedException e){ |
610 |
< |
unexpectedException(); |
611 |
< |
|
612 |
< |
} |
580 |
> |
public void testGetSet() { |
581 |
> |
final CountDownLatch pleaseSet = new CountDownLatch(2); |
582 |
> |
|
583 |
> |
final PublicFutureTask task = |
584 |
> |
new PublicFutureTask(new CheckedCallable<Object>() { |
585 |
> |
public Object realCall() throws InterruptedException { |
586 |
> |
return two; |
587 |
> |
}}); |
588 |
> |
|
589 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
590 |
> |
public void realRun() throws Exception { |
591 |
> |
pleaseSet.countDown(); |
592 |
> |
assertSame(two, task.get()); |
593 |
> |
}}); |
594 |
> |
|
595 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
596 |
> |
public void realRun() throws Exception { |
597 |
> |
pleaseSet.countDown(); |
598 |
> |
assertSame(two, task.get(2*LONG_DELAY_MS, MILLISECONDS)); |
599 |
> |
}}); |
600 |
> |
|
601 |
> |
await(pleaseSet); |
602 |
> |
checkNotDone(task); |
603 |
> |
assertTrue(t1.isAlive()); |
604 |
> |
assertTrue(t2.isAlive()); |
605 |
> |
task.set(two); |
606 |
> |
assertEquals(0, task.runCount()); |
607 |
> |
assertEquals(1, task.setCount()); |
608 |
> |
assertEquals(0, task.setExceptionCount()); |
609 |
> |
tryToConfuseDoneTask(task); |
610 |
> |
checkCompletedNormally(task, two); |
611 |
> |
awaitTermination(t1); |
612 |
> |
awaitTermination(t2); |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
< |
* set in one thread causes timed get in another thread to retrieve value |
617 |
< |
*/ |
618 |
< |
public void testTimedGet1() { |
619 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
620 |
< |
public Object call() { |
621 |
< |
try { |
622 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
623 |
< |
} catch(InterruptedException e){ |
624 |
< |
threadUnexpectedException(); |
625 |
< |
} |
626 |
< |
return Boolean.TRUE; |
627 |
< |
} |
628 |
< |
}); |
629 |
< |
Thread t = new Thread(new Runnable() { |
630 |
< |
public void run() { |
631 |
< |
try { |
632 |
< |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
633 |
< |
} catch(TimeoutException success) { |
634 |
< |
} catch(Exception e){ |
635 |
< |
threadUnexpectedException(); |
636 |
< |
} |
637 |
< |
} |
638 |
< |
}); |
639 |
< |
try { |
640 |
< |
assertFalse(ft.isDone()); |
641 |
< |
assertFalse(ft.isCancelled()); |
642 |
< |
t.start(); |
643 |
< |
ft.run(); |
644 |
< |
t.join(); |
645 |
< |
assertTrue(ft.isDone()); |
646 |
< |
assertFalse(ft.isCancelled()); |
647 |
< |
} catch(InterruptedException e){ |
648 |
< |
unexpectedException(); |
649 |
< |
|
650 |
< |
} |
616 |
> |
* Cancelling a task causes timed get in another thread to throw |
617 |
> |
* CancellationException |
618 |
> |
*/ |
619 |
> |
public void testTimedGet_Cancellation() { |
620 |
> |
testTimedGet_Cancellation(false); |
621 |
> |
} |
622 |
> |
public void testTimedGet_Cancellation_interrupt() { |
623 |
> |
testTimedGet_Cancellation(true); |
624 |
> |
} |
625 |
> |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
626 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
627 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
628 |
> |
final Callable<Object> callable = |
629 |
> |
new CheckedCallable<Object>() { |
630 |
> |
public Object realCall() throws InterruptedException { |
631 |
> |
pleaseCancel.countDown(); |
632 |
> |
if (mayInterruptIfRunning) { |
633 |
> |
try { |
634 |
> |
delay(2*LONG_DELAY_MS); |
635 |
> |
} catch (InterruptedException success) {} |
636 |
> |
} else { |
637 |
> |
await(cancelled); |
638 |
> |
} |
639 |
> |
return two; |
640 |
> |
}}; |
641 |
> |
final PublicFutureTask task = new PublicFutureTask(callable); |
642 |
> |
|
643 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
644 |
> |
public void realRun() throws Exception { |
645 |
> |
pleaseCancel.countDown(); |
646 |
> |
task.get(); |
647 |
> |
}}; |
648 |
> |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
649 |
> |
public void realRun() throws Exception { |
650 |
> |
pleaseCancel.countDown(); |
651 |
> |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
652 |
> |
}}; |
653 |
> |
t1.start(); |
654 |
> |
t2.start(); |
655 |
> |
Thread t3 = newStartedThread(task); |
656 |
> |
await(pleaseCancel); |
657 |
> |
checkIsRunning(task); |
658 |
> |
task.cancel(mayInterruptIfRunning); |
659 |
> |
checkCancelled(task); |
660 |
> |
awaitTermination(t1); |
661 |
> |
awaitTermination(t2); |
662 |
> |
cancelled.countDown(); |
663 |
> |
awaitTermination(t3); |
664 |
> |
assertEquals(1, task.runCount()); |
665 |
> |
assertEquals(1, task.setCount()); |
666 |
> |
assertEquals(0, task.setExceptionCount()); |
667 |
> |
tryToConfuseDoneTask(task); |
668 |
> |
checkCancelled(task); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
< |
* Cancelling a task causes timed get in another thread to throw CancellationException |
672 |
> |
* A runtime exception in task causes get to throw ExecutionException |
673 |
|
*/ |
674 |
< |
public void testTimedGet_Cancellation() { |
675 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
676 |
< |
public Object call() { |
677 |
< |
try { |
678 |
< |
Thread.sleep(SMALL_DELAY_MS); |
679 |
< |
threadShouldThrow(); |
680 |
< |
} catch(InterruptedException e) { |
681 |
< |
} |
682 |
< |
return Boolean.TRUE; |
683 |
< |
} |
684 |
< |
}); |
685 |
< |
try { |
686 |
< |
Thread t1 = new Thread(new Runnable() { |
687 |
< |
public void run() { |
688 |
< |
try { |
689 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
690 |
< |
threadShouldThrow(); |
691 |
< |
} catch(CancellationException success) {} |
307 |
< |
catch(Exception e){ |
308 |
< |
threadUnexpectedException(); |
309 |
< |
} |
310 |
< |
} |
311 |
< |
}); |
312 |
< |
Thread t2 = new Thread(ft); |
313 |
< |
t1.start(); |
314 |
< |
t2.start(); |
315 |
< |
Thread.sleep(SHORT_DELAY_MS); |
316 |
< |
ft.cancel(true); |
317 |
< |
t1.join(); |
318 |
< |
t2.join(); |
319 |
< |
} catch(InterruptedException ie){ |
320 |
< |
unexpectedException(); |
674 |
> |
public void testGet_ExecutionException() throws InterruptedException { |
675 |
> |
final ArithmeticException e = new ArithmeticException(); |
676 |
> |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
677 |
> |
public Object call() { |
678 |
> |
throw e; |
679 |
> |
}}); |
680 |
> |
|
681 |
> |
task.run(); |
682 |
> |
assertEquals(1, task.runCount()); |
683 |
> |
assertEquals(0, task.setCount()); |
684 |
> |
assertEquals(1, task.setExceptionCount()); |
685 |
> |
try { |
686 |
> |
task.get(); |
687 |
> |
shouldThrow(); |
688 |
> |
} catch (ExecutionException success) { |
689 |
> |
assertSame(e, success.getCause()); |
690 |
> |
tryToConfuseDoneTask(task); |
691 |
> |
checkCompletedAbnormally(task, success.getCause()); |
692 |
|
} |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* Cancelling a task causes get in another thread to throw CancellationException |
697 |
< |
*/ |
698 |
< |
public void testGet_Cancellation() { |
699 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
700 |
< |
public Object call() { |
701 |
< |
try { |
702 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
703 |
< |
threadShouldThrow(); |
704 |
< |
} catch(InterruptedException e){ |
705 |
< |
} |
706 |
< |
return Boolean.TRUE; |
707 |
< |
} |
708 |
< |
}); |
709 |
< |
try { |
710 |
< |
Thread t1 = new Thread(new Runnable() { |
711 |
< |
public void run() { |
712 |
< |
try { |
342 |
< |
ft.get(); |
343 |
< |
threadShouldThrow(); |
344 |
< |
} catch(CancellationException success){ |
345 |
< |
} |
346 |
< |
catch(Exception e){ |
347 |
< |
threadUnexpectedException(); |
348 |
< |
} |
349 |
< |
} |
350 |
< |
}); |
351 |
< |
Thread t2 = new Thread(ft); |
352 |
< |
t1.start(); |
353 |
< |
t2.start(); |
354 |
< |
Thread.sleep(SHORT_DELAY_MS); |
355 |
< |
ft.cancel(true); |
356 |
< |
t1.join(); |
357 |
< |
t2.join(); |
358 |
< |
} catch(InterruptedException success){ |
359 |
< |
unexpectedException(); |
696 |
> |
* A runtime exception in task causes timed get to throw ExecutionException |
697 |
> |
*/ |
698 |
> |
public void testTimedGet_ExecutionException2() throws Exception { |
699 |
> |
final ArithmeticException e = new ArithmeticException(); |
700 |
> |
final PublicFutureTask task = new PublicFutureTask(new Callable() { |
701 |
> |
public Object call() { |
702 |
> |
throw e; |
703 |
> |
}}); |
704 |
> |
|
705 |
> |
task.run(); |
706 |
> |
try { |
707 |
> |
task.get(LONG_DELAY_MS, MILLISECONDS); |
708 |
> |
shouldThrow(); |
709 |
> |
} catch (ExecutionException success) { |
710 |
> |
assertSame(e, success.getCause()); |
711 |
> |
tryToConfuseDoneTask(task); |
712 |
> |
checkCompletedAbnormally(task, success.getCause()); |
713 |
|
} |
714 |
|
} |
362 |
– |
|
715 |
|
|
716 |
|
/** |
717 |
< |
* A runtime exception in task causes get to throw ExecutionException |
717 |
> |
* get is interruptible |
718 |
> |
*/ |
719 |
> |
public void testGet_interruptible() { |
720 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
721 |
> |
final FutureTask task = new FutureTask(new NoOpCallable()); |
722 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
723 |
> |
public void realRun() throws Exception { |
724 |
> |
Thread.currentThread().interrupt(); |
725 |
> |
try { |
726 |
> |
task.get(); |
727 |
> |
shouldThrow(); |
728 |
> |
} catch (InterruptedException success) {} |
729 |
> |
assertFalse(Thread.interrupted()); |
730 |
> |
|
731 |
> |
pleaseInterrupt.countDown(); |
732 |
> |
try { |
733 |
> |
task.get(); |
734 |
> |
shouldThrow(); |
735 |
> |
} catch (InterruptedException success) {} |
736 |
> |
assertFalse(Thread.interrupted()); |
737 |
> |
}}); |
738 |
> |
|
739 |
> |
await(pleaseInterrupt); |
740 |
> |
t.interrupt(); |
741 |
> |
awaitTermination(t); |
742 |
> |
checkNotDone(task); |
743 |
> |
} |
744 |
> |
|
745 |
> |
/** |
746 |
> |
* timed get is interruptible |
747 |
> |
*/ |
748 |
> |
public void testTimedGet_interruptible() { |
749 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
750 |
> |
final FutureTask task = new FutureTask(new NoOpCallable()); |
751 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
752 |
> |
public void realRun() throws Exception { |
753 |
> |
Thread.currentThread().interrupt(); |
754 |
> |
try { |
755 |
> |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
756 |
> |
shouldThrow(); |
757 |
> |
} catch (InterruptedException success) {} |
758 |
> |
assertFalse(Thread.interrupted()); |
759 |
> |
|
760 |
> |
pleaseInterrupt.countDown(); |
761 |
> |
try { |
762 |
> |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
763 |
> |
shouldThrow(); |
764 |
> |
} catch (InterruptedException success) {} |
765 |
> |
assertFalse(Thread.interrupted()); |
766 |
> |
}}); |
767 |
> |
|
768 |
> |
await(pleaseInterrupt); |
769 |
> |
t.interrupt(); |
770 |
> |
awaitTermination(t); |
771 |
> |
checkNotDone(task); |
772 |
> |
} |
773 |
> |
|
774 |
> |
/** |
775 |
> |
* A timed out timed get throws TimeoutException |
776 |
|
*/ |
777 |
< |
public void testGet_ExecutionException() { |
778 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
779 |
< |
public Object call() { |
780 |
< |
int i = 5/0; |
781 |
< |
return Boolean.TRUE; |
782 |
< |
} |
783 |
< |
}); |
784 |
< |
try { |
375 |
< |
ft.run(); |
376 |
< |
ft.get(); |
377 |
< |
shouldThrow(); |
378 |
< |
} catch(ExecutionException success){ |
379 |
< |
} |
380 |
< |
catch(Exception e){ |
381 |
< |
unexpectedException(); |
382 |
< |
} |
383 |
< |
} |
384 |
< |
|
385 |
< |
/** |
386 |
< |
* A runtime exception in task causes timed get to throw ExecutionException |
387 |
< |
*/ |
388 |
< |
public void testTimedGet_ExecutionException2() { |
389 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
390 |
< |
public Object call() { |
391 |
< |
int i = 5/0; |
392 |
< |
return Boolean.TRUE; |
393 |
< |
} |
394 |
< |
}); |
395 |
< |
try { |
396 |
< |
ft.run(); |
397 |
< |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
398 |
< |
shouldThrow(); |
399 |
< |
} catch(ExecutionException success) { |
400 |
< |
} catch(TimeoutException success) { } // unlikely but OK |
401 |
< |
catch(Exception e){ |
402 |
< |
unexpectedException(); |
403 |
< |
} |
404 |
< |
} |
405 |
< |
|
406 |
< |
|
407 |
< |
/** |
408 |
< |
* Interrupting a waiting get causes it to throw InterruptedException |
409 |
< |
*/ |
410 |
< |
public void testGet_InterruptedException() { |
411 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
412 |
< |
Thread t = new Thread(new Runnable() { |
413 |
< |
public void run() { |
414 |
< |
try { |
415 |
< |
ft.get(); |
416 |
< |
threadShouldThrow(); |
417 |
< |
} catch(InterruptedException success){ |
418 |
< |
} catch(Exception e){ |
419 |
< |
threadUnexpectedException(); |
420 |
< |
} |
421 |
< |
} |
422 |
< |
}); |
423 |
< |
try { |
424 |
< |
t.start(); |
425 |
< |
Thread.sleep(SHORT_DELAY_MS); |
426 |
< |
t.interrupt(); |
427 |
< |
t.join(); |
428 |
< |
} catch(Exception e){ |
429 |
< |
unexpectedException(); |
430 |
< |
} |
431 |
< |
} |
432 |
< |
|
433 |
< |
/** |
434 |
< |
* Interrupting a waiting timed get causes it to throw InterruptedException |
435 |
< |
*/ |
436 |
< |
public void testTimedGet_InterruptedException2() { |
437 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
438 |
< |
Thread t = new Thread(new Runnable() { |
439 |
< |
public void run() { |
440 |
< |
try { |
441 |
< |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
442 |
< |
threadShouldThrow(); |
443 |
< |
} catch(InterruptedException success){} |
444 |
< |
catch(Exception e){ |
445 |
< |
threadUnexpectedException(); |
446 |
< |
} |
447 |
< |
} |
448 |
< |
}); |
449 |
< |
try { |
450 |
< |
t.start(); |
451 |
< |
Thread.sleep(SHORT_DELAY_MS); |
452 |
< |
t.interrupt(); |
453 |
< |
t.join(); |
454 |
< |
} catch(Exception e){ |
455 |
< |
unexpectedException(); |
777 |
> |
public void testGet_TimeoutException() throws Exception { |
778 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
779 |
> |
long startTime = System.nanoTime(); |
780 |
> |
try { |
781 |
> |
task.get(timeoutMillis(), MILLISECONDS); |
782 |
> |
shouldThrow(); |
783 |
> |
} catch (TimeoutException success) { |
784 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
785 |
|
} |
786 |
|
} |
787 |
< |
|
787 |
> |
|
788 |
|
/** |
789 |
< |
* A timed out timed get throws TimeoutException |
789 |
> |
* timed get with null TimeUnit throws NullPointerException |
790 |
|
*/ |
791 |
< |
public void testGet_TimeoutException() { |
792 |
< |
try { |
793 |
< |
FutureTask ft = new FutureTask(new NoOpCallable()); |
794 |
< |
ft.get(1,TimeUnit.MILLISECONDS); |
795 |
< |
shouldThrow(); |
796 |
< |
} catch(TimeoutException success){} |
797 |
< |
catch(Exception success){ |
798 |
< |
unexpectedException(); |
799 |
< |
} |
791 |
> |
public void testGet_NullTimeUnit() throws Exception { |
792 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
793 |
> |
long[] timeouts = { Long.MIN_VALUE, 0L, Long.MAX_VALUE }; |
794 |
> |
|
795 |
> |
for (long timeout : timeouts) { |
796 |
> |
try { |
797 |
> |
task.get(timeout, null); |
798 |
> |
shouldThrow(); |
799 |
> |
} catch (NullPointerException success) {} |
800 |
> |
} |
801 |
> |
|
802 |
> |
task.run(); |
803 |
> |
|
804 |
> |
for (long timeout : timeouts) { |
805 |
> |
try { |
806 |
> |
task.get(timeout, null); |
807 |
> |
shouldThrow(); |
808 |
> |
} catch (NullPointerException success) {} |
809 |
> |
} |
810 |
|
} |
811 |
< |
|
811 |
> |
|
812 |
> |
/** |
813 |
> |
* timed get with most negative timeout works correctly (i.e. no |
814 |
> |
* underflow bug) |
815 |
> |
*/ |
816 |
> |
public void testGet_NegativeInfinityTimeout() throws Exception { |
817 |
> |
final ExecutorService pool = Executors.newFixedThreadPool(10); |
818 |
> |
final Runnable nop = new Runnable() { public void run() {}}; |
819 |
> |
final FutureTask<Void> task = new FutureTask<>(nop, null); |
820 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
821 |
> |
Runnable r = new Runnable() { public void run() { |
822 |
> |
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) { |
823 |
> |
try { |
824 |
> |
task.get(timeout, NANOSECONDS); |
825 |
> |
shouldThrow(); |
826 |
> |
} catch (TimeoutException success) { |
827 |
> |
} catch (Throwable fail) {threadUnexpectedException(fail);}}}}; |
828 |
> |
for (int i = 0; i < 10; i++) |
829 |
> |
futures.add(pool.submit(r)); |
830 |
> |
try { |
831 |
> |
joinPool(pool); |
832 |
> |
for (Future<?> future : futures) |
833 |
> |
checkCompletedNormally(future, null); |
834 |
> |
} finally { |
835 |
> |
task.run(); // last resort to help terminate |
836 |
> |
} |
837 |
> |
} |
838 |
> |
|
839 |
|
} |