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