1 |
/* |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
* Pat Fisher, Mike Judd. |
7 |
*/ |
*/ |
8 |
|
|
9 |
import junit.framework.*; |
import junit.framework.*; |
10 |
import java.util.concurrent.*; |
import java.util.concurrent.*; |
11 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
13 |
import java.util.*; |
import java.util.*; |
14 |
|
|
15 |
public class FutureTaskTest extends JSR166TestCase { |
public class FutureTaskTest extends JSR166TestCase { |
21 |
return new TestSuite(FutureTaskTest.class); |
return new TestSuite(FutureTaskTest.class); |
22 |
} |
} |
23 |
|
|
24 |
|
void checkNotDone(Future<?> f) { |
25 |
|
assertFalse(f.isDone()); |
26 |
|
assertFalse(f.isCancelled()); |
27 |
|
} |
28 |
|
|
29 |
|
<T> void checkCompletedNormally(Future<T> f, T expected) { |
30 |
|
assertTrue(f.isDone()); |
31 |
|
assertFalse(f.isCancelled()); |
32 |
|
|
33 |
|
try { |
34 |
|
assertSame(expected, f.get()); |
35 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
36 |
|
try { |
37 |
|
assertSame(expected, f.get(5L, SECONDS)); |
38 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
39 |
|
|
40 |
|
assertFalse(f.cancel(false)); |
41 |
|
assertFalse(f.cancel(true)); |
42 |
|
} |
43 |
|
|
44 |
|
void checkCancelled(Future<?> f) { |
45 |
|
assertTrue(f.isDone()); |
46 |
|
assertTrue(f.isCancelled()); |
47 |
|
|
48 |
|
try { |
49 |
|
f.get(); |
50 |
|
shouldThrow(); |
51 |
|
} catch (CancellationException success) { |
52 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
53 |
|
|
54 |
|
try { |
55 |
|
f.get(5L, SECONDS); |
56 |
|
shouldThrow(); |
57 |
|
} catch (CancellationException success) { |
58 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
59 |
|
|
60 |
|
assertFalse(f.cancel(false)); |
61 |
|
assertFalse(f.cancel(true)); |
62 |
|
} |
63 |
|
|
64 |
|
void checkCompletedAbnormally(Future<?> f, Throwable t) { |
65 |
|
assertTrue(f.isDone()); |
66 |
|
assertFalse(f.isCancelled()); |
67 |
|
|
68 |
|
try { |
69 |
|
f.get(); |
70 |
|
shouldThrow(); |
71 |
|
} catch (ExecutionException success) { |
72 |
|
assertSame(t, success.getCause()); |
73 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
74 |
|
|
75 |
|
try { |
76 |
|
f.get(5L, SECONDS); |
77 |
|
shouldThrow(); |
78 |
|
} catch (ExecutionException success) { |
79 |
|
assertSame(t, success.getCause()); |
80 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
81 |
|
|
82 |
|
assertFalse(f.cancel(false)); |
83 |
|
assertFalse(f.cancel(true)); |
84 |
|
} |
85 |
|
|
86 |
/** |
/** |
87 |
* Subclass to expose protected methods |
* Subclass to expose protected methods |
88 |
*/ |
*/ |
100 |
try { |
try { |
101 |
FutureTask task = new FutureTask(null); |
FutureTask task = new FutureTask(null); |
102 |
shouldThrow(); |
shouldThrow(); |
103 |
} |
} catch (NullPointerException success) {} |
|
catch(NullPointerException success) { |
|
|
} |
|
104 |
} |
} |
105 |
|
|
106 |
/** |
/** |
110 |
try { |
try { |
111 |
FutureTask task = new FutureTask(null, Boolean.TRUE); |
FutureTask task = new FutureTask(null, Boolean.TRUE); |
112 |
shouldThrow(); |
shouldThrow(); |
113 |
} |
} catch (NullPointerException success) {} |
|
catch(NullPointerException success) { |
|
|
} |
|
114 |
} |
} |
115 |
|
|
116 |
/** |
/** |
120 |
FutureTask task = new FutureTask( new NoOpCallable()); |
FutureTask task = new FutureTask( new NoOpCallable()); |
121 |
task.run(); |
task.run(); |
122 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
123 |
assertFalse(task.isCancelled()); |
checkCompletedNormally(task, Boolean.TRUE); |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
129 |
public void testRunAndReset() { |
public void testRunAndReset() { |
130 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
131 |
assertTrue(task.runAndReset()); |
assertTrue(task.runAndReset()); |
132 |
assertFalse(task.isDone()); |
checkNotDone(task); |
133 |
} |
} |
134 |
|
|
135 |
/** |
/** |
139 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
140 |
assertTrue(task.cancel(false)); |
assertTrue(task.cancel(false)); |
141 |
assertFalse(task.runAndReset()); |
assertFalse(task.runAndReset()); |
142 |
assertTrue(task.isDone()); |
checkCancelled(task); |
|
assertTrue(task.isCancelled()); |
|
143 |
} |
} |
144 |
|
|
145 |
|
|
|
|
|
146 |
/** |
/** |
147 |
* setting value gauses get to return it |
* setting value causes get to return it |
148 |
*/ |
*/ |
149 |
public void testSet() { |
public void testSet() throws Exception { |
150 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
151 |
task.set(one); |
task.set(one); |
152 |
try { |
assertSame(task.get(), one); |
153 |
assertEquals(task.get(), one); |
checkCompletedNormally(task, one); |
|
} |
|
|
catch(Exception e) { |
|
|
unexpectedException(); |
|
|
} |
|
154 |
} |
} |
155 |
|
|
156 |
/** |
/** |
157 |
* setException causes get to throw ExecutionException |
* setException causes get to throw ExecutionException |
158 |
*/ |
*/ |
159 |
public void testSetException() { |
public void testSetException() throws Exception { |
160 |
Exception nse = new NoSuchElementException(); |
Exception nse = new NoSuchElementException(); |
161 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
162 |
task.setException(nse); |
task.setException(nse); |
163 |
try { |
try { |
164 |
Object x = task.get(); |
Object x = task.get(); |
165 |
shouldThrow(); |
shouldThrow(); |
166 |
} |
} catch (ExecutionException success) { |
167 |
catch(ExecutionException ee) { |
assertSame(success.getCause(), nse); |
168 |
Throwable cause = ee.getCause(); |
checkCompletedAbnormally(task, nse); |
|
assertEquals(cause, nse); |
|
|
} |
|
|
catch(Exception e) { |
|
|
unexpectedException(); |
|
169 |
} |
} |
170 |
} |
} |
171 |
|
|
176 |
FutureTask task = new FutureTask( new NoOpCallable()); |
FutureTask task = new FutureTask( new NoOpCallable()); |
177 |
assertTrue(task.cancel(false)); |
assertTrue(task.cancel(false)); |
178 |
task.run(); |
task.run(); |
179 |
assertTrue(task.isDone()); |
checkCancelled(task); |
|
assertTrue(task.isCancelled()); |
|
180 |
} |
} |
181 |
|
|
182 |
/** |
/** |
186 |
FutureTask task = new FutureTask( new NoOpCallable()); |
FutureTask task = new FutureTask( new NoOpCallable()); |
187 |
assertTrue(task.cancel(true)); |
assertTrue(task.cancel(true)); |
188 |
task.run(); |
task.run(); |
189 |
assertTrue(task.isDone()); |
checkCancelled(task); |
|
assertTrue(task.isCancelled()); |
|
190 |
} |
} |
191 |
|
|
192 |
/** |
/** |
196 |
FutureTask task = new FutureTask( new NoOpCallable()); |
FutureTask task = new FutureTask( new NoOpCallable()); |
197 |
task.run(); |
task.run(); |
198 |
assertFalse(task.cancel(false)); |
assertFalse(task.cancel(false)); |
199 |
assertTrue(task.isDone()); |
checkCompletedNormally(task, Boolean.TRUE); |
|
assertFalse(task.isCancelled()); |
|
200 |
} |
} |
201 |
|
|
202 |
/** |
/** |
203 |
* cancel(true) interrupts a running task |
* cancel(true) interrupts a running task |
204 |
*/ |
*/ |
205 |
public void testCancelInterrupt() { |
public void testCancelInterrupt() throws InterruptedException { |
206 |
FutureTask task = new FutureTask( new Callable() { |
final CountDownLatch threadStarted = new CountDownLatch(1); |
207 |
public Object call() { |
final FutureTask task = |
208 |
try { |
new FutureTask(new CheckedCallable<Object>() { |
209 |
Thread.sleep(MEDIUM_DELAY_MS); |
public Object realCall() { |
210 |
threadShouldThrow(); |
threadStarted.countDown(); |
211 |
} |
long t0 = System.nanoTime(); |
212 |
catch (InterruptedException success) {} |
for (;;) { |
213 |
|
if (Thread.interrupted()) |
214 |
return Boolean.TRUE; |
return Boolean.TRUE; |
215 |
|
if (millisElapsedSince(t0) > MEDIUM_DELAY_MS) |
216 |
|
fail("interrupt not delivered"); |
217 |
|
Thread.yield(); |
218 |
|
} |
219 |
} }); |
} }); |
|
Thread t = new Thread(task); |
|
|
t.start(); |
|
220 |
|
|
221 |
try { |
Thread t = newStartedThread(task); |
222 |
Thread.sleep(SHORT_DELAY_MS); |
threadStarted.await(); |
223 |
assertTrue(task.cancel(true)); |
assertTrue(task.cancel(true)); |
224 |
t.join(); |
checkCancelled(task); |
225 |
assertTrue(task.isDone()); |
awaitTermination(t, MEDIUM_DELAY_MS); |
226 |
assertTrue(task.isCancelled()); |
checkCancelled(task); |
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
227 |
} |
} |
|
} |
|
|
|
|
228 |
|
|
229 |
/** |
/** |
230 |
* cancel(false) does not interrupt a running task |
* cancel(false) does not interrupt a running task |
231 |
*/ |
*/ |
232 |
public void testCancelNoInterrupt() { |
public void testCancelNoInterrupt() throws InterruptedException { |
233 |
FutureTask task = new FutureTask( new Callable() { |
final CountDownLatch threadStarted = new CountDownLatch(1); |
234 |
public Object call() { |
final CountDownLatch cancelled = new CountDownLatch(1); |
235 |
try { |
final FutureTask<Boolean> task = |
236 |
Thread.sleep(MEDIUM_DELAY_MS); |
new FutureTask<Boolean>(new CheckedCallable<Boolean>() { |
237 |
} |
public Boolean realCall() throws InterruptedException { |
238 |
catch (InterruptedException success) { |
threadStarted.countDown(); |
239 |
threadFail("should not interrupt"); |
cancelled.await(MEDIUM_DELAY_MS, MILLISECONDS); |
240 |
} |
assertFalse(Thread.interrupted()); |
241 |
return Boolean.TRUE; |
return Boolean.TRUE; |
242 |
} }); |
} }); |
|
Thread t = new Thread(task); |
|
|
t.start(); |
|
243 |
|
|
244 |
try { |
Thread t = newStartedThread(task); |
245 |
Thread.sleep(SHORT_DELAY_MS); |
threadStarted.await(); |
246 |
assertTrue(task.cancel(false)); |
assertTrue(task.cancel(false)); |
247 |
t.join(); |
checkCancelled(task); |
248 |
assertTrue(task.isDone()); |
cancelled.countDown(); |
249 |
assertTrue(task.isCancelled()); |
awaitTermination(t, MEDIUM_DELAY_MS); |
250 |
} catch(InterruptedException e){ |
checkCancelled(task); |
|
unexpectedException(); |
|
251 |
} |
} |
252 |
|
|
253 |
|
/** |
254 |
|
* run in one thread causes get in another thread to retrieve value |
255 |
|
*/ |
256 |
|
public void testGetRun() throws InterruptedException { |
257 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
258 |
|
|
259 |
|
final FutureTask task = |
260 |
|
new FutureTask(new CheckedCallable<Object>() { |
261 |
|
public Object realCall() throws InterruptedException { |
262 |
|
return Boolean.TRUE; |
263 |
|
}}); |
264 |
|
|
265 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
266 |
|
public void realRun() throws Exception { |
267 |
|
threadStarted.countDown(); |
268 |
|
assertSame(Boolean.TRUE, task.get()); |
269 |
|
}}); |
270 |
|
|
271 |
|
threadStarted.await(); |
272 |
|
checkNotDone(task); |
273 |
|
assertTrue(t.isAlive()); |
274 |
|
task.run(); |
275 |
|
checkCompletedNormally(task, Boolean.TRUE); |
276 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
277 |
} |
} |
278 |
|
|
279 |
/** |
/** |
280 |
* set in one thread causes get in another thread to retrieve value |
* set in one thread causes get in another thread to retrieve value |
281 |
*/ |
*/ |
282 |
public void testGet1() { |
public void testGetSet() throws InterruptedException { |
283 |
final FutureTask ft = new FutureTask(new Callable() { |
final CountDownLatch threadStarted = new CountDownLatch(1); |
284 |
public Object call() { |
|
285 |
try { |
final PublicFutureTask task = |
286 |
Thread.sleep(MEDIUM_DELAY_MS); |
new PublicFutureTask(new CheckedCallable<Object>() { |
287 |
} catch(InterruptedException e){ |
public Object realCall() throws InterruptedException { |
|
threadUnexpectedException(); |
|
|
} |
|
288 |
return Boolean.TRUE; |
return Boolean.TRUE; |
289 |
} |
}}); |
|
}); |
|
|
Thread t = new Thread(new Runnable() { |
|
|
public void run() { |
|
|
try { |
|
|
ft.get(); |
|
|
} catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
|
assertFalse(ft.isDone()); |
|
|
assertFalse(ft.isCancelled()); |
|
|
t.start(); |
|
|
Thread.sleep(SHORT_DELAY_MS); |
|
|
ft.run(); |
|
|
t.join(); |
|
|
assertTrue(ft.isDone()); |
|
|
assertFalse(ft.isCancelled()); |
|
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
290 |
|
|
291 |
} |
Thread t = newStartedThread(new CheckedRunnable() { |
292 |
|
public void realRun() throws Exception { |
293 |
|
threadStarted.countDown(); |
294 |
|
assertSame(Boolean.FALSE, task.get()); |
295 |
|
}}); |
296 |
|
|
297 |
|
threadStarted.await(); |
298 |
|
checkNotDone(task); |
299 |
|
assertTrue(t.isAlive()); |
300 |
|
task.set(Boolean.FALSE); |
301 |
|
checkCompletedNormally(task, Boolean.FALSE); |
302 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
303 |
} |
} |
304 |
|
|
305 |
/** |
/** |
306 |
* set in one thread causes timed get in another thread to retrieve value |
* run in one thread causes timed get in another thread to retrieve value |
307 |
*/ |
*/ |
308 |
public void testTimedGet1() { |
public void testTimedGetRun() throws InterruptedException { |
309 |
final FutureTask ft = new FutureTask(new Callable() { |
final CountDownLatch threadStarted = new CountDownLatch(1); |
310 |
public Object call() { |
|
311 |
try { |
final FutureTask task = |
312 |
Thread.sleep(MEDIUM_DELAY_MS); |
new FutureTask(new CheckedCallable<Object>() { |
313 |
} catch(InterruptedException e){ |
public Object realCall() throws InterruptedException { |
|
threadUnexpectedException(); |
|
|
} |
|
314 |
return Boolean.TRUE; |
return Boolean.TRUE; |
315 |
} |
}}); |
|
}); |
|
|
Thread t = new Thread(new Runnable() { |
|
|
public void run() { |
|
|
try { |
|
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
|
|
} catch(TimeoutException success) { |
|
|
} catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
|
assertFalse(ft.isDone()); |
|
|
assertFalse(ft.isCancelled()); |
|
|
t.start(); |
|
|
ft.run(); |
|
|
t.join(); |
|
|
assertTrue(ft.isDone()); |
|
|
assertFalse(ft.isCancelled()); |
|
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
316 |
|
|
317 |
} |
Thread t = newStartedThread(new CheckedRunnable() { |
318 |
|
public void realRun() throws Exception { |
319 |
|
threadStarted.countDown(); |
320 |
|
assertSame(Boolean.TRUE, |
321 |
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
322 |
|
}}); |
323 |
|
|
324 |
|
threadStarted.await(); |
325 |
|
checkNotDone(task); |
326 |
|
assertTrue(t.isAlive()); |
327 |
|
task.run(); |
328 |
|
checkCompletedNormally(task, Boolean.TRUE); |
329 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
330 |
} |
} |
331 |
|
|
332 |
/** |
/** |
333 |
* Cancelling a task causes timed get in another thread to throw CancellationException |
* set in one thread causes timed get in another thread to retrieve value |
334 |
*/ |
*/ |
335 |
public void testTimedGet_Cancellation() { |
public void testTimedGetSet() throws InterruptedException { |
336 |
final FutureTask ft = new FutureTask(new Callable() { |
final CountDownLatch threadStarted = new CountDownLatch(1); |
337 |
public Object call() { |
|
338 |
try { |
final PublicFutureTask task = |
339 |
Thread.sleep(SMALL_DELAY_MS); |
new PublicFutureTask(new CheckedCallable<Object>() { |
340 |
threadShouldThrow(); |
public Object realCall() throws InterruptedException { |
|
} catch(InterruptedException e) { |
|
|
} |
|
341 |
return Boolean.TRUE; |
return Boolean.TRUE; |
342 |
|
}}); |
343 |
|
|
344 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
345 |
|
public void realRun() throws Exception { |
346 |
|
threadStarted.countDown(); |
347 |
|
assertSame(Boolean.FALSE, |
348 |
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
349 |
|
}}); |
350 |
|
|
351 |
|
threadStarted.await(); |
352 |
|
checkNotDone(task); |
353 |
|
assertTrue(t.isAlive()); |
354 |
|
task.set(Boolean.FALSE); |
355 |
|
checkCompletedNormally(task, Boolean.FALSE); |
356 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
357 |
} |
} |
358 |
}); |
|
359 |
try { |
/** |
360 |
Thread t1 = new Thread(new Runnable() { |
* Cancelling a task causes timed get in another thread to throw |
361 |
public void run() { |
* CancellationException |
362 |
try { |
*/ |
363 |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
public void testTimedGet_Cancellation() throws InterruptedException { |
364 |
threadShouldThrow(); |
final CountDownLatch threadStarted = new CountDownLatch(2); |
365 |
} catch(CancellationException success) {} |
final FutureTask task = |
366 |
catch(Exception e){ |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
367 |
threadUnexpectedException(); |
public Object realCall() throws InterruptedException { |
368 |
} |
threadStarted.countDown(); |
369 |
} |
Thread.sleep(LONG_DELAY_MS); |
370 |
}); |
return Boolean.TRUE; |
371 |
Thread t2 = new Thread(ft); |
}}); |
372 |
|
|
373 |
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
374 |
|
public void realRun() throws Exception { |
375 |
|
threadStarted.countDown(); |
376 |
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS); |
377 |
|
}}; |
378 |
|
Thread t2 = new Thread(task); |
379 |
t1.start(); |
t1.start(); |
380 |
t2.start(); |
t2.start(); |
381 |
Thread.sleep(SHORT_DELAY_MS); |
threadStarted.await(); |
382 |
ft.cancel(true); |
task.cancel(true); |
383 |
t1.join(); |
awaitTermination(t1, MEDIUM_DELAY_MS); |
384 |
t2.join(); |
awaitTermination(t2, MEDIUM_DELAY_MS); |
385 |
} catch(InterruptedException ie){ |
checkCancelled(task); |
|
unexpectedException(); |
|
|
} |
|
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
* Cancelling a task causes get in another thread to throw CancellationException |
* Cancelling a task causes get in another thread to throw |
390 |
*/ |
* CancellationException |
391 |
public void testGet_Cancellation() { |
*/ |
392 |
final FutureTask ft = new FutureTask(new Callable() { |
public void testGet_Cancellation() throws InterruptedException { |
393 |
public Object call() { |
final CountDownLatch threadStarted = new CountDownLatch(2); |
394 |
try { |
final FutureTask task = |
395 |
Thread.sleep(MEDIUM_DELAY_MS); |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
396 |
threadShouldThrow(); |
public Object realCall() throws InterruptedException { |
397 |
} catch(InterruptedException e){ |
threadStarted.countDown(); |
398 |
} |
Thread.sleep(LONG_DELAY_MS); |
399 |
return Boolean.TRUE; |
return Boolean.TRUE; |
400 |
} |
}}); |
401 |
}); |
|
402 |
try { |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
403 |
Thread t1 = new Thread(new Runnable() { |
public void realRun() throws Exception { |
404 |
public void run() { |
threadStarted.countDown(); |
405 |
try { |
task.get(); |
406 |
ft.get(); |
}}; |
407 |
threadShouldThrow(); |
Thread t2 = new Thread(task); |
|
} catch(CancellationException success){ |
|
|
} |
|
|
catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
Thread t2 = new Thread(ft); |
|
408 |
t1.start(); |
t1.start(); |
409 |
t2.start(); |
t2.start(); |
410 |
Thread.sleep(SHORT_DELAY_MS); |
threadStarted.await(); |
411 |
ft.cancel(true); |
task.cancel(true); |
412 |
t1.join(); |
awaitTermination(t1, MEDIUM_DELAY_MS); |
413 |
t2.join(); |
awaitTermination(t2, MEDIUM_DELAY_MS); |
414 |
} catch(InterruptedException success){ |
checkCancelled(task); |
|
unexpectedException(); |
|
|
} |
|
415 |
} |
} |
416 |
|
|
417 |
|
|
418 |
/** |
/** |
419 |
* A runtime exception in task causes get to throw ExecutionException |
* A runtime exception in task causes get to throw ExecutionException |
420 |
*/ |
*/ |
421 |
public void testGet_ExecutionException() { |
public void testGet_ExecutionException() throws InterruptedException { |
422 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask task = new FutureTask(new Callable() { |
423 |
public Object call() { |
public Object call() { |
424 |
int i = 5/0; |
return 5/0; |
425 |
return Boolean.TRUE; |
}}); |
426 |
} |
|
427 |
}); |
task.run(); |
428 |
try { |
try { |
429 |
ft.run(); |
task.get(); |
|
ft.get(); |
|
430 |
shouldThrow(); |
shouldThrow(); |
431 |
} catch(ExecutionException success){ |
} catch(ExecutionException success){ |
432 |
} |
assertTrue(success.getCause() instanceof ArithmeticException); |
433 |
catch(Exception e){ |
checkCompletedAbnormally(task, success.getCause()); |
|
unexpectedException(); |
|
434 |
} |
} |
435 |
} |
} |
436 |
|
|
437 |
/** |
/** |
438 |
* A runtime exception in task causes timed get to throw ExecutionException |
* A runtime exception in task causes timed get to throw ExecutionException |
439 |
*/ |
*/ |
440 |
public void testTimedGet_ExecutionException2() { |
public void testTimedGet_ExecutionException2() throws Exception { |
441 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask task = new FutureTask(new Callable() { |
442 |
public Object call() { |
public Object call() { |
443 |
int i = 5/0; |
return 5/0; |
444 |
return Boolean.TRUE; |
}}); |
445 |
} |
|
446 |
}); |
task.run(); |
447 |
try { |
try { |
448 |
ft.run(); |
task.get(SHORT_DELAY_MS, MILLISECONDS); |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
|
449 |
shouldThrow(); |
shouldThrow(); |
450 |
} catch(ExecutionException success) { |
} catch(ExecutionException success) { |
451 |
} catch(TimeoutException success) { } // unlikely but OK |
assertTrue(success.getCause() instanceof ArithmeticException); |
452 |
catch(Exception e){ |
checkCompletedAbnormally(task, success.getCause()); |
|
unexpectedException(); |
|
453 |
} |
} |
454 |
} |
} |
455 |
|
|
457 |
/** |
/** |
458 |
* Interrupting a waiting get causes it to throw InterruptedException |
* Interrupting a waiting get causes it to throw InterruptedException |
459 |
*/ |
*/ |
460 |
public void testGet_InterruptedException() { |
public void testGet_InterruptedException() throws InterruptedException { |
461 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
final CountDownLatch threadStarted = new CountDownLatch(1); |
462 |
Thread t = new Thread(new Runnable() { |
final FutureTask task = new FutureTask(new NoOpCallable()); |
463 |
public void run() { |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
464 |
try { |
public void realRun() throws Exception { |
465 |
ft.get(); |
threadStarted.countDown(); |
466 |
threadShouldThrow(); |
task.get(); |
467 |
} catch(InterruptedException success){ |
}}); |
468 |
} catch(Exception e){ |
|
469 |
threadUnexpectedException(); |
threadStarted.await(); |
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
|
t.start(); |
|
|
Thread.sleep(SHORT_DELAY_MS); |
|
470 |
t.interrupt(); |
t.interrupt(); |
471 |
t.join(); |
awaitTermination(t, MEDIUM_DELAY_MS); |
472 |
} catch(Exception e){ |
checkNotDone(task); |
|
unexpectedException(); |
|
|
} |
|
473 |
} |
} |
474 |
|
|
475 |
/** |
/** |
476 |
* Interrupting a waiting timed get causes it to throw InterruptedException |
* Interrupting a waiting timed get causes it to throw InterruptedException |
477 |
*/ |
*/ |
478 |
public void testTimedGet_InterruptedException2() { |
public void testTimedGet_InterruptedException2() throws InterruptedException { |
479 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
final CountDownLatch threadStarted = new CountDownLatch(1); |
480 |
Thread t = new Thread(new Runnable() { |
final FutureTask task = new FutureTask(new NoOpCallable()); |
481 |
public void run() { |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
482 |
try { |
public void realRun() throws Exception { |
483 |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
threadStarted.countDown(); |
484 |
threadShouldThrow(); |
task.get(LONG_DELAY_MS, MILLISECONDS); |
485 |
} catch(InterruptedException success){} |
}}); |
486 |
catch(Exception e){ |
|
487 |
threadUnexpectedException(); |
threadStarted.await(); |
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
|
t.start(); |
|
|
Thread.sleep(SHORT_DELAY_MS); |
|
488 |
t.interrupt(); |
t.interrupt(); |
489 |
t.join(); |
awaitTermination(t, MEDIUM_DELAY_MS); |
490 |
} catch(Exception e){ |
checkNotDone(task); |
|
unexpectedException(); |
|
|
} |
|
491 |
} |
} |
492 |
|
|
493 |
/** |
/** |
494 |
* A timed out timed get throws TimeoutException |
* A timed out timed get throws TimeoutException |
495 |
*/ |
*/ |
496 |
public void testGet_TimeoutException() { |
public void testGet_TimeoutException() throws Exception { |
497 |
try { |
try { |
498 |
FutureTask ft = new FutureTask(new NoOpCallable()); |
FutureTask task = new FutureTask(new NoOpCallable()); |
499 |
ft.get(1,TimeUnit.MILLISECONDS); |
task.get(1, MILLISECONDS); |
500 |
shouldThrow(); |
shouldThrow(); |
501 |
} catch(TimeoutException success){} |
} catch(TimeoutException success){} |
|
catch(Exception success){ |
|
|
unexpectedException(); |
|
|
} |
|
502 |
} |
} |
503 |
|
|
504 |
} |
} |