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/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
import java.util.*; |
13 |
|
|
14 |
|
public class FutureTaskTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(FutureTaskTest.class); |
20 |
> |
return new TestSuite(FutureTaskTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
37 |
|
try { |
38 |
|
FutureTask task = new FutureTask(null); |
39 |
|
shouldThrow(); |
40 |
< |
} |
40 |
< |
catch(NullPointerException success) { |
41 |
< |
} |
40 |
> |
} catch (NullPointerException success) {} |
41 |
|
} |
42 |
|
|
43 |
|
/** |
47 |
|
try { |
48 |
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
49 |
|
shouldThrow(); |
50 |
< |
} |
52 |
< |
catch(NullPointerException success) { |
53 |
< |
} |
50 |
> |
} catch (NullPointerException success) {} |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
|
* isDone is true when a task completes |
55 |
|
*/ |
56 |
|
public void testIsDone() { |
57 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
58 |
< |
task.run(); |
59 |
< |
assertTrue(task.isDone()); |
60 |
< |
assertFalse(task.isCancelled()); |
57 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
58 |
> |
task.run(); |
59 |
> |
assertTrue(task.isDone()); |
60 |
> |
assertFalse(task.isCancelled()); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
65 |
|
*/ |
66 |
|
public void testRunAndReset() { |
67 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
68 |
< |
assertTrue(task.runAndReset()); |
68 |
> |
assertTrue(task.runAndReset()); |
69 |
|
assertFalse(task.isDone()); |
70 |
|
} |
71 |
|
|
75 |
|
public void testResetAfterCancel() { |
76 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
77 |
|
assertTrue(task.cancel(false)); |
78 |
< |
assertFalse(task.runAndReset()); |
79 |
< |
assertTrue(task.isDone()); |
80 |
< |
assertTrue(task.isCancelled()); |
78 |
> |
assertFalse(task.runAndReset()); |
79 |
> |
assertTrue(task.isDone()); |
80 |
> |
assertTrue(task.isCancelled()); |
81 |
|
} |
82 |
|
|
83 |
|
|
85 |
|
/** |
86 |
|
* setting value causes get to return it |
87 |
|
*/ |
88 |
< |
public void testSet() { |
88 |
> |
public void testSet() throws Exception { |
89 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
90 |
|
task.set(one); |
91 |
< |
try { |
95 |
< |
assertEquals(task.get(), one); |
96 |
< |
} |
97 |
< |
catch(Exception e) { |
98 |
< |
unexpectedException(); |
99 |
< |
} |
91 |
> |
assertSame(task.get(), one); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
|
* setException causes get to throw ExecutionException |
96 |
|
*/ |
97 |
< |
public void testSetException() { |
97 |
> |
public void testSetException() throws Exception { |
98 |
|
Exception nse = new NoSuchElementException(); |
99 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
100 |
|
task.setException(nse); |
101 |
|
try { |
102 |
|
Object x = task.get(); |
103 |
|
shouldThrow(); |
104 |
< |
} |
105 |
< |
catch(ExecutionException ee) { |
114 |
< |
Throwable cause = ee.getCause(); |
115 |
< |
assertEquals(cause, nse); |
116 |
< |
} |
117 |
< |
catch(Exception e) { |
118 |
< |
unexpectedException(); |
104 |
> |
} catch (ExecutionException success) { |
105 |
> |
assertSame(success.getCause(), nse); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* Cancelling before running succeeds |
110 |
> |
* Cancelling before running succeeds |
111 |
|
*/ |
112 |
|
public void testCancelBeforeRun() { |
113 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
113 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
114 |
|
assertTrue(task.cancel(false)); |
115 |
< |
task.run(); |
116 |
< |
assertTrue(task.isDone()); |
117 |
< |
assertTrue(task.isCancelled()); |
115 |
> |
task.run(); |
116 |
> |
assertTrue(task.isDone()); |
117 |
> |
assertTrue(task.isCancelled()); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
|
* Cancel(true) before run succeeds |
122 |
|
*/ |
123 |
|
public void testCancelBeforeRun2() { |
124 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
124 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
125 |
|
assertTrue(task.cancel(true)); |
126 |
< |
task.run(); |
127 |
< |
assertTrue(task.isDone()); |
128 |
< |
assertTrue(task.isCancelled()); |
126 |
> |
task.run(); |
127 |
> |
assertTrue(task.isDone()); |
128 |
> |
assertTrue(task.isCancelled()); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* cancel of a completed task fails |
133 |
|
*/ |
134 |
|
public void testCancelAfterRun() { |
135 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
136 |
< |
task.run(); |
135 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
136 |
> |
task.run(); |
137 |
|
assertFalse(task.cancel(false)); |
138 |
< |
assertTrue(task.isDone()); |
139 |
< |
assertFalse(task.isCancelled()); |
138 |
> |
assertTrue(task.isDone()); |
139 |
> |
assertFalse(task.isCancelled()); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
|
* cancel(true) interrupts a running task |
144 |
|
*/ |
145 |
< |
public void testCancelInterrupt() { |
146 |
< |
FutureTask task = new FutureTask( new Callable() { |
147 |
< |
public Object call() { |
148 |
< |
try { |
149 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
163 |
< |
threadShouldThrow(); |
164 |
< |
} |
165 |
< |
catch (InterruptedException success) {} |
145 |
> |
public void testCancelInterrupt() throws InterruptedException { |
146 |
> |
final FutureTask task = |
147 |
> |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
148 |
> |
public Object realCall() throws InterruptedException { |
149 |
> |
Thread.sleep(SMALL_DELAY_MS); |
150 |
|
return Boolean.TRUE; |
151 |
< |
} }); |
152 |
< |
Thread t = new Thread(task); |
151 |
> |
}}); |
152 |
> |
|
153 |
> |
Thread t = new Thread(task); |
154 |
|
t.start(); |
155 |
< |
|
156 |
< |
try { |
157 |
< |
Thread.sleep(SHORT_DELAY_MS); |
158 |
< |
assertTrue(task.cancel(true)); |
159 |
< |
t.join(); |
175 |
< |
assertTrue(task.isDone()); |
176 |
< |
assertTrue(task.isCancelled()); |
177 |
< |
} catch(InterruptedException e){ |
178 |
< |
unexpectedException(); |
179 |
< |
} |
155 |
> |
Thread.sleep(SHORT_DELAY_MS); |
156 |
> |
assertTrue(task.cancel(true)); |
157 |
> |
t.join(); |
158 |
> |
assertTrue(task.isDone()); |
159 |
> |
assertTrue(task.isCancelled()); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
/** |
164 |
|
* cancel(false) does not interrupt a running task |
165 |
|
*/ |
166 |
< |
public void testCancelNoInterrupt() { |
167 |
< |
FutureTask task = new FutureTask( new Callable() { |
168 |
< |
public Object call() { |
169 |
< |
try { |
170 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
191 |
< |
} |
192 |
< |
catch (InterruptedException success) { |
193 |
< |
threadFail("should not interrupt"); |
194 |
< |
} |
166 |
> |
public void testCancelNoInterrupt() throws InterruptedException { |
167 |
> |
final FutureTask task = |
168 |
> |
new FutureTask(new CheckedCallable<Object>() { |
169 |
> |
public Object realCall() throws InterruptedException { |
170 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
171 |
|
return Boolean.TRUE; |
172 |
< |
} }); |
173 |
< |
Thread t = new Thread(task); |
172 |
> |
}}); |
173 |
> |
|
174 |
> |
Thread t = new Thread(task); |
175 |
|
t.start(); |
176 |
< |
|
177 |
< |
try { |
178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
179 |
< |
assertTrue(task.cancel(false)); |
180 |
< |
t.join(); |
204 |
< |
assertTrue(task.isDone()); |
205 |
< |
assertTrue(task.isCancelled()); |
206 |
< |
} catch(InterruptedException e){ |
207 |
< |
unexpectedException(); |
208 |
< |
} |
176 |
> |
Thread.sleep(SHORT_DELAY_MS); |
177 |
> |
assertTrue(task.cancel(false)); |
178 |
> |
t.join(); |
179 |
> |
assertTrue(task.isDone()); |
180 |
> |
assertTrue(task.isCancelled()); |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
|
* set in one thread causes get in another thread to retrieve value |
185 |
|
*/ |
186 |
< |
public void testGet1() { |
187 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
188 |
< |
public Object call() { |
189 |
< |
try { |
218 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
219 |
< |
} catch(InterruptedException e){ |
220 |
< |
threadUnexpectedException(); |
221 |
< |
} |
186 |
> |
public void testGet1() throws InterruptedException { |
187 |
> |
final FutureTask ft = |
188 |
> |
new FutureTask(new CheckedCallable<Object>() { |
189 |
> |
public Object realCall() throws InterruptedException { |
190 |
|
return Boolean.TRUE; |
191 |
< |
} |
192 |
< |
}); |
193 |
< |
Thread t = new Thread(new Runnable() { |
194 |
< |
public void run() { |
195 |
< |
try { |
228 |
< |
ft.get(); |
229 |
< |
} catch(Exception e){ |
230 |
< |
threadUnexpectedException(); |
231 |
< |
} |
232 |
< |
} |
233 |
< |
}); |
234 |
< |
try { |
235 |
< |
assertFalse(ft.isDone()); |
236 |
< |
assertFalse(ft.isCancelled()); |
237 |
< |
t.start(); |
238 |
< |
Thread.sleep(SHORT_DELAY_MS); |
239 |
< |
ft.run(); |
240 |
< |
t.join(); |
241 |
< |
assertTrue(ft.isDone()); |
242 |
< |
assertFalse(ft.isCancelled()); |
243 |
< |
} catch(InterruptedException e){ |
244 |
< |
unexpectedException(); |
191 |
> |
}}); |
192 |
> |
Thread t = new Thread(new CheckedRunnable() { |
193 |
> |
public void realRun() throws Exception { |
194 |
> |
assertSame(Boolean.TRUE, ft.get()); |
195 |
> |
}}); |
196 |
|
|
197 |
< |
} |
197 |
> |
assertFalse(ft.isDone()); |
198 |
> |
assertFalse(ft.isCancelled()); |
199 |
> |
t.start(); |
200 |
> |
Thread.sleep(SHORT_DELAY_MS); |
201 |
> |
ft.run(); |
202 |
> |
t.join(); |
203 |
> |
assertTrue(ft.isDone()); |
204 |
> |
assertFalse(ft.isCancelled()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* set in one thread causes timed get in another thread to retrieve value |
209 |
|
*/ |
210 |
< |
public void testTimedGet1() { |
211 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
212 |
< |
public Object call() { |
213 |
< |
try { |
256 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
257 |
< |
} catch(InterruptedException e){ |
258 |
< |
threadUnexpectedException(); |
259 |
< |
} |
210 |
> |
public void testTimedGet1() throws InterruptedException { |
211 |
> |
final FutureTask ft = |
212 |
> |
new FutureTask(new CheckedCallable<Object>() { |
213 |
> |
public Object realCall() throws InterruptedException { |
214 |
|
return Boolean.TRUE; |
215 |
< |
} |
216 |
< |
}); |
217 |
< |
Thread t = new Thread(new Runnable() { |
218 |
< |
public void run() { |
219 |
< |
try { |
220 |
< |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
221 |
< |
} catch(TimeoutException success) { |
222 |
< |
} catch(Exception e){ |
223 |
< |
threadUnexpectedException(); |
224 |
< |
} |
225 |
< |
} |
226 |
< |
}); |
227 |
< |
try { |
228 |
< |
assertFalse(ft.isDone()); |
275 |
< |
assertFalse(ft.isCancelled()); |
276 |
< |
t.start(); |
277 |
< |
ft.run(); |
278 |
< |
t.join(); |
279 |
< |
assertTrue(ft.isDone()); |
280 |
< |
assertFalse(ft.isCancelled()); |
281 |
< |
} catch(InterruptedException e){ |
282 |
< |
unexpectedException(); |
283 |
< |
|
284 |
< |
} |
215 |
> |
}}); |
216 |
> |
Thread t = new Thread(new CheckedRunnable() { |
217 |
> |
public void realRun() throws Exception { |
218 |
> |
assertSame(Boolean.TRUE, ft.get(SMALL_DELAY_MS, MILLISECONDS)); |
219 |
> |
}}); |
220 |
> |
|
221 |
> |
assertFalse(ft.isDone()); |
222 |
> |
assertFalse(ft.isCancelled()); |
223 |
> |
t.start(); |
224 |
> |
Thread.sleep(SHORT_DELAY_MS); |
225 |
> |
ft.run(); |
226 |
> |
t.join(); |
227 |
> |
assertTrue(ft.isDone()); |
228 |
> |
assertFalse(ft.isCancelled()); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
< |
* Cancelling a task causes timed get in another thread to throw CancellationException |
233 |
< |
*/ |
234 |
< |
public void testTimedGet_Cancellation() { |
235 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
236 |
< |
public Object call() { |
237 |
< |
try { |
238 |
< |
Thread.sleep(SMALL_DELAY_MS); |
239 |
< |
threadShouldThrow(); |
240 |
< |
} catch(InterruptedException e) { |
241 |
< |
} |
242 |
< |
return Boolean.TRUE; |
243 |
< |
} |
244 |
< |
}); |
245 |
< |
try { |
246 |
< |
Thread t1 = new Thread(new Runnable() { |
247 |
< |
public void run() { |
248 |
< |
try { |
249 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
250 |
< |
threadShouldThrow(); |
251 |
< |
} catch(CancellationException success) {} |
252 |
< |
catch(Exception e){ |
253 |
< |
threadUnexpectedException(); |
310 |
< |
} |
311 |
< |
} |
312 |
< |
}); |
313 |
< |
Thread t2 = new Thread(ft); |
314 |
< |
t1.start(); |
315 |
< |
t2.start(); |
316 |
< |
Thread.sleep(SHORT_DELAY_MS); |
317 |
< |
ft.cancel(true); |
318 |
< |
t1.join(); |
319 |
< |
t2.join(); |
320 |
< |
} catch(InterruptedException ie){ |
321 |
< |
unexpectedException(); |
322 |
< |
} |
232 |
> |
* Cancelling a task causes timed get in another thread to throw |
233 |
> |
* CancellationException |
234 |
> |
*/ |
235 |
> |
public void testTimedGet_Cancellation() throws InterruptedException { |
236 |
> |
final FutureTask ft = |
237 |
> |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
238 |
> |
public Object realCall() throws InterruptedException { |
239 |
> |
Thread.sleep(SMALL_DELAY_MS); |
240 |
> |
return Boolean.TRUE; |
241 |
> |
}}); |
242 |
> |
|
243 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
244 |
> |
public void realRun() throws Exception { |
245 |
> |
ft.get(MEDIUM_DELAY_MS, MILLISECONDS); |
246 |
> |
}}; |
247 |
> |
Thread t2 = new Thread(ft); |
248 |
> |
t1.start(); |
249 |
> |
t2.start(); |
250 |
> |
Thread.sleep(SHORT_DELAY_MS); |
251 |
> |
ft.cancel(true); |
252 |
> |
t1.join(); |
253 |
> |
t2.join(); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* Cancelling a task causes get in another thread to throw CancellationException |
258 |
< |
*/ |
259 |
< |
public void testGet_Cancellation() { |
260 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
261 |
< |
public Object call() { |
262 |
< |
try { |
263 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
264 |
< |
threadShouldThrow(); |
334 |
< |
} catch(InterruptedException e){ |
335 |
< |
} |
257 |
> |
* Cancelling a task causes get in another thread to throw |
258 |
> |
* CancellationException |
259 |
> |
*/ |
260 |
> |
public void testGet_Cancellation() throws InterruptedException { |
261 |
> |
final FutureTask ft = |
262 |
> |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
263 |
> |
public Object realCall() throws InterruptedException { |
264 |
> |
Thread.sleep(SMALL_DELAY_MS); |
265 |
|
return Boolean.TRUE; |
266 |
< |
} |
267 |
< |
}); |
268 |
< |
try { |
269 |
< |
Thread t1 = new Thread(new Runnable() { |
270 |
< |
public void run() { |
271 |
< |
try { |
272 |
< |
ft.get(); |
273 |
< |
threadShouldThrow(); |
274 |
< |
} catch(CancellationException success){ |
275 |
< |
} |
276 |
< |
catch(Exception e){ |
277 |
< |
threadUnexpectedException(); |
278 |
< |
} |
350 |
< |
} |
351 |
< |
}); |
352 |
< |
Thread t2 = new Thread(ft); |
353 |
< |
t1.start(); |
354 |
< |
t2.start(); |
355 |
< |
Thread.sleep(SHORT_DELAY_MS); |
356 |
< |
ft.cancel(true); |
357 |
< |
t1.join(); |
358 |
< |
t2.join(); |
359 |
< |
} catch(InterruptedException success){ |
360 |
< |
unexpectedException(); |
361 |
< |
} |
266 |
> |
}}); |
267 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
268 |
> |
public void realRun() throws Exception { |
269 |
> |
ft.get(); |
270 |
> |
}}; |
271 |
> |
|
272 |
> |
Thread t2 = new Thread(ft); |
273 |
> |
t1.start(); |
274 |
> |
t2.start(); |
275 |
> |
Thread.sleep(SHORT_DELAY_MS); |
276 |
> |
ft.cancel(true); |
277 |
> |
t1.join(); |
278 |
> |
t2.join(); |
279 |
|
} |
280 |
< |
|
280 |
> |
|
281 |
|
|
282 |
|
/** |
283 |
|
* A runtime exception in task causes get to throw ExecutionException |
284 |
|
*/ |
285 |
< |
public void testGet_ExecutionException() { |
286 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
287 |
< |
public Object call() { |
288 |
< |
int i = 5/0; |
289 |
< |
return Boolean.TRUE; |
290 |
< |
} |
291 |
< |
}); |
292 |
< |
try { |
293 |
< |
ft.run(); |
294 |
< |
ft.get(); |
295 |
< |
shouldThrow(); |
296 |
< |
} catch(ExecutionException success){ |
285 |
> |
public void testGet_ExecutionException() throws InterruptedException { |
286 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
287 |
> |
public Object call() { |
288 |
> |
return 5/0; |
289 |
> |
}}); |
290 |
> |
|
291 |
> |
ft.run(); |
292 |
> |
try { |
293 |
> |
ft.get(); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (ExecutionException success) { |
296 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
297 |
|
} |
381 |
– |
catch(Exception e){ |
382 |
– |
unexpectedException(); |
383 |
– |
} |
384 |
– |
} |
385 |
– |
|
386 |
– |
/** |
387 |
– |
* A runtime exception in task causes timed get to throw ExecutionException |
388 |
– |
*/ |
389 |
– |
public void testTimedGet_ExecutionException2() { |
390 |
– |
final FutureTask ft = new FutureTask(new Callable() { |
391 |
– |
public Object call() { |
392 |
– |
int i = 5/0; |
393 |
– |
return Boolean.TRUE; |
394 |
– |
} |
395 |
– |
}); |
396 |
– |
try { |
397 |
– |
ft.run(); |
398 |
– |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
399 |
– |
shouldThrow(); |
400 |
– |
} catch(ExecutionException success) { |
401 |
– |
} catch(TimeoutException success) { } // unlikely but OK |
402 |
– |
catch(Exception e){ |
403 |
– |
unexpectedException(); |
404 |
– |
} |
298 |
|
} |
406 |
– |
|
299 |
|
|
300 |
|
/** |
301 |
< |
* Interrupting a waiting get causes it to throw InterruptedException |
301 |
> |
* A runtime exception in task causes timed get to throw ExecutionException |
302 |
|
*/ |
303 |
< |
public void testGet_InterruptedException() { |
304 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
305 |
< |
Thread t = new Thread(new Runnable() { |
306 |
< |
public void run() { |
307 |
< |
try { |
308 |
< |
ft.get(); |
309 |
< |
threadShouldThrow(); |
418 |
< |
} catch(InterruptedException success){ |
419 |
< |
} catch(Exception e){ |
420 |
< |
threadUnexpectedException(); |
421 |
< |
} |
422 |
< |
} |
423 |
< |
}); |
303 |
> |
public void testTimedGet_ExecutionException2() throws Exception { |
304 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
305 |
> |
public Object call() { |
306 |
> |
return 5/0; |
307 |
> |
}}); |
308 |
> |
|
309 |
> |
ft.run(); |
310 |
|
try { |
311 |
< |
t.start(); |
312 |
< |
Thread.sleep(SHORT_DELAY_MS); |
313 |
< |
t.interrupt(); |
314 |
< |
t.join(); |
429 |
< |
} catch(Exception e){ |
430 |
< |
unexpectedException(); |
311 |
> |
ft.get(SHORT_DELAY_MS, MILLISECONDS); |
312 |
> |
shouldThrow(); |
313 |
> |
} catch (ExecutionException success) { |
314 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
315 |
|
} |
316 |
|
} |
317 |
|
|
318 |
+ |
|
319 |
|
/** |
320 |
< |
* Interrupting a waiting timed get causes it to throw InterruptedException |
320 |
> |
* Interrupting a waiting get causes it to throw InterruptedException |
321 |
|
*/ |
322 |
< |
public void testTimedGet_InterruptedException2() { |
323 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
324 |
< |
Thread t = new Thread(new Runnable() { |
325 |
< |
public void run() { |
326 |
< |
try { |
327 |
< |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
328 |
< |
threadShouldThrow(); |
329 |
< |
} catch(InterruptedException success){} |
330 |
< |
catch(Exception e){ |
331 |
< |
threadUnexpectedException(); |
332 |
< |
} |
333 |
< |
} |
334 |
< |
}); |
335 |
< |
try { |
336 |
< |
t.start(); |
337 |
< |
Thread.sleep(SHORT_DELAY_MS); |
338 |
< |
t.interrupt(); |
339 |
< |
t.join(); |
340 |
< |
} catch(Exception e){ |
341 |
< |
unexpectedException(); |
342 |
< |
} |
322 |
> |
public void testGet_InterruptedException() throws InterruptedException { |
323 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
324 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
325 |
> |
public void realRun() throws Exception { |
326 |
> |
ft.get(); |
327 |
> |
}}); |
328 |
> |
|
329 |
> |
t.start(); |
330 |
> |
Thread.sleep(SHORT_DELAY_MS); |
331 |
> |
t.interrupt(); |
332 |
> |
t.join(); |
333 |
> |
} |
334 |
> |
|
335 |
> |
/** |
336 |
> |
* Interrupting a waiting timed get causes it to throw InterruptedException |
337 |
> |
*/ |
338 |
> |
public void testTimedGet_InterruptedException2() throws InterruptedException { |
339 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
340 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
341 |
> |
public void realRun() throws Exception { |
342 |
> |
ft.get(LONG_DELAY_MS,MILLISECONDS); |
343 |
> |
}}); |
344 |
> |
|
345 |
> |
t.start(); |
346 |
> |
Thread.sleep(SHORT_DELAY_MS); |
347 |
> |
t.interrupt(); |
348 |
> |
t.join(); |
349 |
|
} |
350 |
< |
|
350 |
> |
|
351 |
|
/** |
352 |
|
* A timed out timed get throws TimeoutException |
353 |
|
*/ |
354 |
< |
public void testGet_TimeoutException() { |
355 |
< |
try { |
354 |
> |
public void testGet_TimeoutException() throws Exception { |
355 |
> |
try { |
356 |
|
FutureTask ft = new FutureTask(new NoOpCallable()); |
357 |
< |
ft.get(1,TimeUnit.MILLISECONDS); |
358 |
< |
shouldThrow(); |
359 |
< |
} catch(TimeoutException success){} |
469 |
< |
catch(Exception success){ |
470 |
< |
unexpectedException(); |
471 |
< |
} |
357 |
> |
ft.get(1,MILLISECONDS); |
358 |
> |
shouldThrow(); |
359 |
> |
} catch (TimeoutException success) {} |
360 |
|
} |
361 |
< |
|
361 |
> |
|
362 |
|
} |