1 |
dl |
1.1 |
/* |
2 |
dl |
1.10 |
* 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 |
jsr166 |
1.24 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.12 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
jsr166 |
1.26 |
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 |
jsr166 |
1.15 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
jsr166 |
1.22 |
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
dl |
1.4 |
import java.util.*; |
20 |
dl |
1.1 |
|
21 |
dl |
1.4 |
public class FutureTaskTest extends JSR166TestCase { |
22 |
dl |
1.1 |
|
23 |
|
|
public static void main(String[] args) { |
24 |
jsr166 |
1.19 |
junit.textui.TestRunner.run(suite()); |
25 |
dl |
1.1 |
} |
26 |
|
|
public static Test suite() { |
27 |
jsr166 |
1.16 |
return new TestSuite(FutureTaskTest.class); |
28 |
dl |
1.1 |
} |
29 |
|
|
|
30 |
jsr166 |
1.22 |
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 |
dl |
1.4 |
/** |
93 |
|
|
* Subclass to expose protected methods |
94 |
|
|
*/ |
95 |
dl |
1.7 |
static class PublicFutureTask extends FutureTask { |
96 |
|
|
public PublicFutureTask(Callable r) { super(r); } |
97 |
dl |
1.9 |
public boolean runAndReset() { return super.runAndReset(); } |
98 |
dl |
1.4 |
public void set(Object x) { super.set(x); } |
99 |
|
|
public void setException(Throwable t) { super.setException(t); } |
100 |
|
|
} |
101 |
dl |
1.1 |
|
102 |
dl |
1.5 |
/** |
103 |
dl |
1.6 |
* Creating a future with a null callable throws NPE |
104 |
dl |
1.5 |
*/ |
105 |
|
|
public void testConstructor() { |
106 |
dl |
1.3 |
try { |
107 |
|
|
FutureTask task = new FutureTask(null); |
108 |
jsr166 |
1.17 |
shouldThrow(); |
109 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
110 |
dl |
1.3 |
} |
111 |
|
|
|
112 |
dl |
1.5 |
/** |
113 |
dl |
1.6 |
* creating a future with null runnable fails |
114 |
dl |
1.5 |
*/ |
115 |
|
|
public void testConstructor2() { |
116 |
dl |
1.3 |
try { |
117 |
|
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
118 |
jsr166 |
1.17 |
shouldThrow(); |
119 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
120 |
dl |
1.3 |
} |
121 |
|
|
|
122 |
dl |
1.5 |
/** |
123 |
dl |
1.6 |
* isDone is true when a task completes |
124 |
dl |
1.5 |
*/ |
125 |
|
|
public void testIsDone() { |
126 |
jsr166 |
1.15 |
FutureTask task = new FutureTask(new NoOpCallable()); |
127 |
jsr166 |
1.16 |
task.run(); |
128 |
|
|
assertTrue(task.isDone()); |
129 |
jsr166 |
1.22 |
checkCompletedNormally(task, Boolean.TRUE); |
130 |
dl |
1.4 |
} |
131 |
|
|
|
132 |
dl |
1.5 |
/** |
133 |
dl |
1.9 |
* runAndReset of a non-cancelled task succeeds |
134 |
dl |
1.5 |
*/ |
135 |
dl |
1.9 |
public void testRunAndReset() { |
136 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
137 |
jsr166 |
1.16 |
assertTrue(task.runAndReset()); |
138 |
jsr166 |
1.22 |
checkNotDone(task); |
139 |
dl |
1.4 |
} |
140 |
|
|
|
141 |
dl |
1.5 |
/** |
142 |
dl |
1.9 |
* runAndReset after cancellation fails |
143 |
dl |
1.5 |
*/ |
144 |
dl |
1.4 |
public void testResetAfterCancel() { |
145 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
146 |
dl |
1.4 |
assertTrue(task.cancel(false)); |
147 |
jsr166 |
1.16 |
assertFalse(task.runAndReset()); |
148 |
jsr166 |
1.22 |
checkCancelled(task); |
149 |
dl |
1.4 |
} |
150 |
|
|
|
151 |
dl |
1.1 |
|
152 |
dl |
1.5 |
/** |
153 |
dl |
1.11 |
* setting value causes get to return it |
154 |
dl |
1.5 |
*/ |
155 |
jsr166 |
1.15 |
public void testSet() throws Exception { |
156 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
157 |
dl |
1.4 |
task.set(one); |
158 |
jsr166 |
1.18 |
assertSame(task.get(), one); |
159 |
jsr166 |
1.22 |
checkCompletedNormally(task, one); |
160 |
dl |
1.4 |
} |
161 |
|
|
|
162 |
dl |
1.5 |
/** |
163 |
dl |
1.6 |
* setException causes get to throw ExecutionException |
164 |
dl |
1.5 |
*/ |
165 |
jsr166 |
1.15 |
public void testSetException() throws Exception { |
166 |
dl |
1.4 |
Exception nse = new NoSuchElementException(); |
167 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
168 |
dl |
1.4 |
task.setException(nse); |
169 |
|
|
try { |
170 |
|
|
Object x = task.get(); |
171 |
jsr166 |
1.17 |
shouldThrow(); |
172 |
jsr166 |
1.15 |
} catch (ExecutionException success) { |
173 |
|
|
assertSame(success.getCause(), nse); |
174 |
jsr166 |
1.22 |
checkCompletedAbnormally(task, nse); |
175 |
dl |
1.4 |
} |
176 |
|
|
} |
177 |
|
|
|
178 |
dl |
1.5 |
/** |
179 |
jsr166 |
1.21 |
* Cancelling before running succeeds |
180 |
dl |
1.5 |
*/ |
181 |
dl |
1.1 |
public void testCancelBeforeRun() { |
182 |
jsr166 |
1.15 |
FutureTask task = new FutureTask(new NoOpCallable()); |
183 |
dl |
1.1 |
assertTrue(task.cancel(false)); |
184 |
jsr166 |
1.16 |
task.run(); |
185 |
jsr166 |
1.22 |
checkCancelled(task); |
186 |
dl |
1.1 |
} |
187 |
|
|
|
188 |
dl |
1.5 |
/** |
189 |
dl |
1.6 |
* Cancel(true) before run succeeds |
190 |
dl |
1.5 |
*/ |
191 |
dl |
1.1 |
public void testCancelBeforeRun2() { |
192 |
jsr166 |
1.15 |
FutureTask task = new FutureTask(new NoOpCallable()); |
193 |
dl |
1.1 |
assertTrue(task.cancel(true)); |
194 |
jsr166 |
1.16 |
task.run(); |
195 |
jsr166 |
1.22 |
checkCancelled(task); |
196 |
dl |
1.1 |
} |
197 |
|
|
|
198 |
dl |
1.5 |
/** |
199 |
dl |
1.6 |
* cancel of a completed task fails |
200 |
dl |
1.5 |
*/ |
201 |
dl |
1.1 |
public void testCancelAfterRun() { |
202 |
jsr166 |
1.15 |
FutureTask task = new FutureTask(new NoOpCallable()); |
203 |
jsr166 |
1.16 |
task.run(); |
204 |
dl |
1.1 |
assertFalse(task.cancel(false)); |
205 |
jsr166 |
1.22 |
checkCompletedNormally(task, Boolean.TRUE); |
206 |
dl |
1.1 |
} |
207 |
|
|
|
208 |
dl |
1.5 |
/** |
209 |
dl |
1.6 |
* cancel(true) interrupts a running task |
210 |
dl |
1.5 |
*/ |
211 |
jsr166 |
1.15 |
public void testCancelInterrupt() throws InterruptedException { |
212 |
jsr166 |
1.22 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
213 |
jsr166 |
1.16 |
final FutureTask task = |
214 |
jsr166 |
1.22 |
new FutureTask(new CheckedCallable<Object>() { |
215 |
|
|
public Object realCall() { |
216 |
|
|
threadStarted.countDown(); |
217 |
|
|
long t0 = System.nanoTime(); |
218 |
|
|
for (;;) { |
219 |
|
|
if (Thread.interrupted()) |
220 |
|
|
return Boolean.TRUE; |
221 |
|
|
if (millisElapsedSince(t0) > MEDIUM_DELAY_MS) |
222 |
|
|
fail("interrupt not delivered"); |
223 |
|
|
Thread.yield(); |
224 |
|
|
} |
225 |
jsr166 |
1.15 |
}}); |
226 |
|
|
|
227 |
jsr166 |
1.22 |
Thread t = newStartedThread(task); |
228 |
|
|
threadStarted.await(); |
229 |
jsr166 |
1.15 |
assertTrue(task.cancel(true)); |
230 |
jsr166 |
1.22 |
checkCancelled(task); |
231 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
232 |
|
|
checkCancelled(task); |
233 |
dl |
1.1 |
} |
234 |
|
|
|
235 |
dl |
1.5 |
/** |
236 |
dl |
1.6 |
* cancel(false) does not interrupt a running task |
237 |
dl |
1.5 |
*/ |
238 |
jsr166 |
1.15 |
public void testCancelNoInterrupt() throws InterruptedException { |
239 |
jsr166 |
1.22 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
240 |
|
|
final CountDownLatch cancelled = new CountDownLatch(1); |
241 |
|
|
final FutureTask<Boolean> task = |
242 |
|
|
new FutureTask<Boolean>(new CheckedCallable<Boolean>() { |
243 |
|
|
public Boolean realCall() throws InterruptedException { |
244 |
|
|
threadStarted.countDown(); |
245 |
|
|
cancelled.await(MEDIUM_DELAY_MS, MILLISECONDS); |
246 |
|
|
assertFalse(Thread.interrupted()); |
247 |
dl |
1.1 |
return Boolean.TRUE; |
248 |
jsr166 |
1.15 |
}}); |
249 |
|
|
|
250 |
jsr166 |
1.22 |
Thread t = newStartedThread(task); |
251 |
|
|
threadStarted.await(); |
252 |
jsr166 |
1.15 |
assertTrue(task.cancel(false)); |
253 |
jsr166 |
1.22 |
checkCancelled(task); |
254 |
|
|
cancelled.countDown(); |
255 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
256 |
|
|
checkCancelled(task); |
257 |
dl |
1.1 |
} |
258 |
|
|
|
259 |
dl |
1.5 |
/** |
260 |
jsr166 |
1.23 |
* run in one thread causes get in another thread to retrieve value |
261 |
dl |
1.5 |
*/ |
262 |
jsr166 |
1.23 |
public void testGetRun() throws InterruptedException { |
263 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
264 |
|
|
|
265 |
jsr166 |
1.22 |
final FutureTask task = |
266 |
jsr166 |
1.15 |
new FutureTask(new CheckedCallable<Object>() { |
267 |
jsr166 |
1.16 |
public Object realCall() throws InterruptedException { |
268 |
dl |
1.1 |
return Boolean.TRUE; |
269 |
jsr166 |
1.15 |
}}); |
270 |
jsr166 |
1.22 |
|
271 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
272 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
273 |
jsr166 |
1.23 |
threadStarted.countDown(); |
274 |
jsr166 |
1.22 |
assertSame(Boolean.TRUE, task.get()); |
275 |
jsr166 |
1.15 |
}}); |
276 |
dl |
1.1 |
|
277 |
jsr166 |
1.23 |
threadStarted.await(); |
278 |
|
|
checkNotDone(task); |
279 |
|
|
assertTrue(t.isAlive()); |
280 |
jsr166 |
1.22 |
task.run(); |
281 |
|
|
checkCompletedNormally(task, Boolean.TRUE); |
282 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
283 |
dl |
1.1 |
} |
284 |
|
|
|
285 |
dl |
1.5 |
/** |
286 |
jsr166 |
1.23 |
* set in one thread causes get in another thread to retrieve value |
287 |
|
|
*/ |
288 |
|
|
public void testGetSet() throws InterruptedException { |
289 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
290 |
|
|
|
291 |
|
|
final PublicFutureTask task = |
292 |
|
|
new PublicFutureTask(new CheckedCallable<Object>() { |
293 |
|
|
public Object realCall() throws InterruptedException { |
294 |
|
|
return Boolean.TRUE; |
295 |
|
|
}}); |
296 |
|
|
|
297 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
298 |
|
|
public void realRun() throws Exception { |
299 |
|
|
threadStarted.countDown(); |
300 |
|
|
assertSame(Boolean.FALSE, task.get()); |
301 |
|
|
}}); |
302 |
|
|
|
303 |
|
|
threadStarted.await(); |
304 |
|
|
checkNotDone(task); |
305 |
|
|
assertTrue(t.isAlive()); |
306 |
|
|
task.set(Boolean.FALSE); |
307 |
|
|
checkCompletedNormally(task, Boolean.FALSE); |
308 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
/** |
312 |
|
|
* run in one thread causes timed get in another thread to retrieve value |
313 |
dl |
1.5 |
*/ |
314 |
jsr166 |
1.23 |
public void testTimedGetRun() throws InterruptedException { |
315 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
316 |
|
|
|
317 |
jsr166 |
1.22 |
final FutureTask task = |
318 |
jsr166 |
1.15 |
new FutureTask(new CheckedCallable<Object>() { |
319 |
jsr166 |
1.16 |
public Object realCall() throws InterruptedException { |
320 |
dl |
1.1 |
return Boolean.TRUE; |
321 |
jsr166 |
1.15 |
}}); |
322 |
jsr166 |
1.22 |
|
323 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
324 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
325 |
jsr166 |
1.23 |
threadStarted.countDown(); |
326 |
|
|
assertSame(Boolean.TRUE, |
327 |
|
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
328 |
jsr166 |
1.15 |
}}); |
329 |
jsr166 |
1.12 |
|
330 |
jsr166 |
1.23 |
threadStarted.await(); |
331 |
|
|
checkNotDone(task); |
332 |
|
|
assertTrue(t.isAlive()); |
333 |
jsr166 |
1.22 |
task.run(); |
334 |
|
|
checkCompletedNormally(task, Boolean.TRUE); |
335 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
336 |
dl |
1.1 |
} |
337 |
|
|
|
338 |
dl |
1.5 |
/** |
339 |
jsr166 |
1.23 |
* set in one thread causes timed get in another thread to retrieve value |
340 |
|
|
*/ |
341 |
|
|
public void testTimedGetSet() throws InterruptedException { |
342 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
343 |
|
|
|
344 |
|
|
final PublicFutureTask task = |
345 |
|
|
new PublicFutureTask(new CheckedCallable<Object>() { |
346 |
|
|
public Object realCall() throws InterruptedException { |
347 |
|
|
return Boolean.TRUE; |
348 |
|
|
}}); |
349 |
|
|
|
350 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
351 |
|
|
public void realRun() throws Exception { |
352 |
|
|
threadStarted.countDown(); |
353 |
|
|
assertSame(Boolean.FALSE, |
354 |
|
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
355 |
|
|
}}); |
356 |
|
|
|
357 |
|
|
threadStarted.await(); |
358 |
|
|
checkNotDone(task); |
359 |
|
|
assertTrue(t.isAlive()); |
360 |
|
|
task.set(Boolean.FALSE); |
361 |
|
|
checkCompletedNormally(task, Boolean.FALSE); |
362 |
|
|
awaitTermination(t, MEDIUM_DELAY_MS); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
/** |
366 |
jsr166 |
1.21 |
* Cancelling a task causes timed get in another thread to throw |
367 |
|
|
* CancellationException |
368 |
dl |
1.5 |
*/ |
369 |
jsr166 |
1.15 |
public void testTimedGet_Cancellation() throws InterruptedException { |
370 |
jsr166 |
1.22 |
final CountDownLatch threadStarted = new CountDownLatch(2); |
371 |
|
|
final FutureTask task = |
372 |
jsr166 |
1.15 |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
373 |
jsr166 |
1.16 |
public Object realCall() throws InterruptedException { |
374 |
jsr166 |
1.22 |
threadStarted.countDown(); |
375 |
dl |
1.25 |
delay(LONG_DELAY_MS); |
376 |
jsr166 |
1.16 |
return Boolean.TRUE; |
377 |
jsr166 |
1.15 |
}}); |
378 |
|
|
|
379 |
|
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
380 |
|
|
public void realRun() throws Exception { |
381 |
jsr166 |
1.22 |
threadStarted.countDown(); |
382 |
|
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS); |
383 |
jsr166 |
1.15 |
}}; |
384 |
jsr166 |
1.22 |
Thread t2 = new Thread(task); |
385 |
jsr166 |
1.15 |
t1.start(); |
386 |
|
|
t2.start(); |
387 |
jsr166 |
1.22 |
threadStarted.await(); |
388 |
|
|
task.cancel(true); |
389 |
|
|
awaitTermination(t1, MEDIUM_DELAY_MS); |
390 |
|
|
awaitTermination(t2, MEDIUM_DELAY_MS); |
391 |
|
|
checkCancelled(task); |
392 |
dl |
1.1 |
} |
393 |
dl |
1.6 |
|
394 |
dl |
1.5 |
/** |
395 |
jsr166 |
1.20 |
* Cancelling a task causes get in another thread to throw |
396 |
|
|
* CancellationException |
397 |
dl |
1.5 |
*/ |
398 |
jsr166 |
1.15 |
public void testGet_Cancellation() throws InterruptedException { |
399 |
jsr166 |
1.22 |
final CountDownLatch threadStarted = new CountDownLatch(2); |
400 |
|
|
final FutureTask task = |
401 |
jsr166 |
1.15 |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
402 |
jsr166 |
1.16 |
public Object realCall() throws InterruptedException { |
403 |
jsr166 |
1.22 |
threadStarted.countDown(); |
404 |
dl |
1.25 |
delay(LONG_DELAY_MS); |
405 |
jsr166 |
1.16 |
return Boolean.TRUE; |
406 |
jsr166 |
1.15 |
}}); |
407 |
jsr166 |
1.22 |
|
408 |
jsr166 |
1.15 |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
409 |
|
|
public void realRun() throws Exception { |
410 |
jsr166 |
1.22 |
threadStarted.countDown(); |
411 |
|
|
task.get(); |
412 |
jsr166 |
1.15 |
}}; |
413 |
jsr166 |
1.22 |
Thread t2 = new Thread(task); |
414 |
jsr166 |
1.15 |
t1.start(); |
415 |
|
|
t2.start(); |
416 |
jsr166 |
1.22 |
threadStarted.await(); |
417 |
|
|
task.cancel(true); |
418 |
|
|
awaitTermination(t1, MEDIUM_DELAY_MS); |
419 |
|
|
awaitTermination(t2, MEDIUM_DELAY_MS); |
420 |
|
|
checkCancelled(task); |
421 |
dl |
1.1 |
} |
422 |
jsr166 |
1.12 |
|
423 |
dl |
1.1 |
|
424 |
dl |
1.5 |
/** |
425 |
dl |
1.6 |
* A runtime exception in task causes get to throw ExecutionException |
426 |
dl |
1.5 |
*/ |
427 |
jsr166 |
1.15 |
public void testGet_ExecutionException() throws InterruptedException { |
428 |
jsr166 |
1.22 |
final FutureTask task = new FutureTask(new Callable() { |
429 |
jsr166 |
1.16 |
public Object call() { |
430 |
jsr166 |
1.15 |
return 5/0; |
431 |
|
|
}}); |
432 |
|
|
|
433 |
jsr166 |
1.22 |
task.run(); |
434 |
jsr166 |
1.15 |
try { |
435 |
jsr166 |
1.22 |
task.get(); |
436 |
jsr166 |
1.17 |
shouldThrow(); |
437 |
jsr166 |
1.16 |
} catch (ExecutionException success) { |
438 |
jsr166 |
1.15 |
assertTrue(success.getCause() instanceof ArithmeticException); |
439 |
jsr166 |
1.22 |
checkCompletedAbnormally(task, success.getCause()); |
440 |
dl |
1.1 |
} |
441 |
|
|
} |
442 |
jsr166 |
1.12 |
|
443 |
dl |
1.5 |
/** |
444 |
jsr166 |
1.20 |
* A runtime exception in task causes timed get to throw ExecutionException |
445 |
dl |
1.5 |
*/ |
446 |
jsr166 |
1.15 |
public void testTimedGet_ExecutionException2() throws Exception { |
447 |
jsr166 |
1.22 |
final FutureTask task = new FutureTask(new Callable() { |
448 |
jsr166 |
1.16 |
public Object call() { |
449 |
jsr166 |
1.15 |
return 5/0; |
450 |
|
|
}}); |
451 |
|
|
|
452 |
jsr166 |
1.22 |
task.run(); |
453 |
jsr166 |
1.16 |
try { |
454 |
jsr166 |
1.22 |
task.get(SHORT_DELAY_MS, MILLISECONDS); |
455 |
jsr166 |
1.17 |
shouldThrow(); |
456 |
jsr166 |
1.16 |
} catch (ExecutionException success) { |
457 |
jsr166 |
1.15 |
assertTrue(success.getCause() instanceof ArithmeticException); |
458 |
jsr166 |
1.22 |
checkCompletedAbnormally(task, success.getCause()); |
459 |
jsr166 |
1.15 |
} |
460 |
dl |
1.1 |
} |
461 |
jsr166 |
1.12 |
|
462 |
dl |
1.1 |
|
463 |
dl |
1.5 |
/** |
464 |
dl |
1.6 |
* Interrupting a waiting get causes it to throw InterruptedException |
465 |
dl |
1.5 |
*/ |
466 |
jsr166 |
1.15 |
public void testGet_InterruptedException() throws InterruptedException { |
467 |
jsr166 |
1.22 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
468 |
|
|
final FutureTask task = new FutureTask(new NoOpCallable()); |
469 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
470 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
471 |
jsr166 |
1.22 |
threadStarted.countDown(); |
472 |
|
|
task.get(); |
473 |
jsr166 |
1.15 |
}}); |
474 |
|
|
|
475 |
jsr166 |
1.22 |
threadStarted.await(); |
476 |
jsr166 |
1.15 |
t.interrupt(); |
477 |
jsr166 |
1.22 |
awaitTermination(t, MEDIUM_DELAY_MS); |
478 |
|
|
checkNotDone(task); |
479 |
dl |
1.1 |
} |
480 |
|
|
|
481 |
dl |
1.5 |
/** |
482 |
jsr166 |
1.20 |
* Interrupting a waiting timed get causes it to throw InterruptedException |
483 |
dl |
1.5 |
*/ |
484 |
jsr166 |
1.15 |
public void testTimedGet_InterruptedException2() throws InterruptedException { |
485 |
jsr166 |
1.22 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
486 |
|
|
final FutureTask task = new FutureTask(new NoOpCallable()); |
487 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
488 |
jsr166 |
1.15 |
public void realRun() throws Exception { |
489 |
jsr166 |
1.22 |
threadStarted.countDown(); |
490 |
|
|
task.get(LONG_DELAY_MS, MILLISECONDS); |
491 |
jsr166 |
1.15 |
}}); |
492 |
|
|
|
493 |
jsr166 |
1.22 |
threadStarted.await(); |
494 |
jsr166 |
1.15 |
t.interrupt(); |
495 |
jsr166 |
1.22 |
awaitTermination(t, MEDIUM_DELAY_MS); |
496 |
|
|
checkNotDone(task); |
497 |
dl |
1.1 |
} |
498 |
jsr166 |
1.12 |
|
499 |
dl |
1.5 |
/** |
500 |
dl |
1.6 |
* A timed out timed get throws TimeoutException |
501 |
dl |
1.5 |
*/ |
502 |
jsr166 |
1.15 |
public void testGet_TimeoutException() throws Exception { |
503 |
jsr166 |
1.16 |
try { |
504 |
jsr166 |
1.22 |
FutureTask task = new FutureTask(new NoOpCallable()); |
505 |
|
|
task.get(1, MILLISECONDS); |
506 |
jsr166 |
1.17 |
shouldThrow(); |
507 |
jsr166 |
1.16 |
} catch (TimeoutException success) {} |
508 |
dl |
1.1 |
} |
509 |
jsr166 |
1.12 |
|
510 |
dl |
1.1 |
} |