1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.concurrent.*; |
10 |
dl |
1.4 |
import java.util.*; |
11 |
dl |
1.1 |
|
12 |
dl |
1.4 |
public class FutureTaskTest extends JSR166TestCase { |
13 |
dl |
1.1 |
|
14 |
|
|
public static void main(String[] args) { |
15 |
|
|
junit.textui.TestRunner.run (suite()); |
16 |
|
|
} |
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(FutureTaskTest.class); |
19 |
|
|
} |
20 |
|
|
|
21 |
dl |
1.4 |
/** |
22 |
|
|
* Subclass to expose protected methods |
23 |
|
|
*/ |
24 |
dl |
1.7 |
static class PublicFutureTask extends FutureTask { |
25 |
|
|
public PublicFutureTask(Callable r) { super(r); } |
26 |
dl |
1.9 |
public boolean runAndReset() { return super.runAndReset(); } |
27 |
dl |
1.4 |
public void set(Object x) { super.set(x); } |
28 |
|
|
public void setException(Throwable t) { super.setException(t); } |
29 |
|
|
} |
30 |
dl |
1.1 |
|
31 |
dl |
1.5 |
/** |
32 |
dl |
1.6 |
* Creating a future with a null callable throws NPE |
33 |
dl |
1.5 |
*/ |
34 |
|
|
public void testConstructor() { |
35 |
dl |
1.3 |
try { |
36 |
|
|
FutureTask task = new FutureTask(null); |
37 |
dl |
1.5 |
shouldThrow(); |
38 |
dl |
1.3 |
} |
39 |
|
|
catch(NullPointerException success) { |
40 |
|
|
} |
41 |
|
|
} |
42 |
|
|
|
43 |
dl |
1.5 |
/** |
44 |
dl |
1.6 |
* creating a future with null runnable fails |
45 |
dl |
1.5 |
*/ |
46 |
|
|
public void testConstructor2() { |
47 |
dl |
1.3 |
try { |
48 |
|
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
49 |
dl |
1.5 |
shouldThrow(); |
50 |
dl |
1.3 |
} |
51 |
|
|
catch(NullPointerException success) { |
52 |
|
|
} |
53 |
|
|
} |
54 |
|
|
|
55 |
dl |
1.5 |
/** |
56 |
dl |
1.6 |
* isDone is true when a task completes |
57 |
dl |
1.5 |
*/ |
58 |
|
|
public void testIsDone() { |
59 |
dl |
1.4 |
FutureTask task = new FutureTask( new NoOpCallable()); |
60 |
|
|
task.run(); |
61 |
|
|
assertTrue(task.isDone()); |
62 |
|
|
assertFalse(task.isCancelled()); |
63 |
|
|
} |
64 |
|
|
|
65 |
dl |
1.5 |
/** |
66 |
dl |
1.9 |
* runAndReset of a non-cancelled task succeeds |
67 |
dl |
1.5 |
*/ |
68 |
dl |
1.9 |
public void testRunAndReset() { |
69 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
70 |
dl |
1.9 |
assertTrue(task.runAndReset()); |
71 |
dl |
1.6 |
assertFalse(task.isDone()); |
72 |
dl |
1.4 |
} |
73 |
|
|
|
74 |
dl |
1.5 |
/** |
75 |
dl |
1.9 |
* runAndReset after cancellation fails |
76 |
dl |
1.5 |
*/ |
77 |
dl |
1.4 |
public void testResetAfterCancel() { |
78 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
79 |
dl |
1.4 |
assertTrue(task.cancel(false)); |
80 |
dl |
1.9 |
assertFalse(task.runAndReset()); |
81 |
dl |
1.1 |
assertTrue(task.isDone()); |
82 |
dl |
1.4 |
assertTrue(task.isCancelled()); |
83 |
|
|
} |
84 |
|
|
|
85 |
dl |
1.1 |
|
86 |
dl |
1.4 |
|
87 |
dl |
1.5 |
/** |
88 |
dl |
1.6 |
* setting value gauses get to return it |
89 |
dl |
1.5 |
*/ |
90 |
dl |
1.4 |
public void testSet() { |
91 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
92 |
dl |
1.4 |
task.set(one); |
93 |
|
|
try { |
94 |
|
|
assertEquals(task.get(), one); |
95 |
|
|
} |
96 |
|
|
catch(Exception e) { |
97 |
dl |
1.5 |
unexpectedException(); |
98 |
dl |
1.4 |
} |
99 |
|
|
} |
100 |
|
|
|
101 |
dl |
1.5 |
/** |
102 |
dl |
1.6 |
* setException causes get to throw ExecutionException |
103 |
dl |
1.5 |
*/ |
104 |
dl |
1.4 |
public void testSetException() { |
105 |
|
|
Exception nse = new NoSuchElementException(); |
106 |
dl |
1.7 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
107 |
dl |
1.4 |
task.setException(nse); |
108 |
|
|
try { |
109 |
|
|
Object x = task.get(); |
110 |
dl |
1.5 |
shouldThrow(); |
111 |
dl |
1.4 |
} |
112 |
|
|
catch(ExecutionException ee) { |
113 |
|
|
Throwable cause = ee.getCause(); |
114 |
|
|
assertEquals(cause, nse); |
115 |
|
|
} |
116 |
|
|
catch(Exception e) { |
117 |
dl |
1.5 |
unexpectedException(); |
118 |
dl |
1.4 |
} |
119 |
|
|
} |
120 |
|
|
|
121 |
dl |
1.5 |
/** |
122 |
dl |
1.6 |
* Cancelling before running succeeds |
123 |
dl |
1.5 |
*/ |
124 |
dl |
1.1 |
public void testCancelBeforeRun() { |
125 |
dl |
1.4 |
FutureTask task = new FutureTask( new NoOpCallable()); |
126 |
dl |
1.1 |
assertTrue(task.cancel(false)); |
127 |
|
|
task.run(); |
128 |
|
|
assertTrue(task.isDone()); |
129 |
|
|
assertTrue(task.isCancelled()); |
130 |
|
|
} |
131 |
|
|
|
132 |
dl |
1.5 |
/** |
133 |
dl |
1.6 |
* Cancel(true) before run succeeds |
134 |
dl |
1.5 |
*/ |
135 |
dl |
1.1 |
public void testCancelBeforeRun2() { |
136 |
dl |
1.4 |
FutureTask task = new FutureTask( new NoOpCallable()); |
137 |
dl |
1.1 |
assertTrue(task.cancel(true)); |
138 |
|
|
task.run(); |
139 |
|
|
assertTrue(task.isDone()); |
140 |
|
|
assertTrue(task.isCancelled()); |
141 |
|
|
} |
142 |
|
|
|
143 |
dl |
1.5 |
/** |
144 |
dl |
1.6 |
* cancel of a completed task fails |
145 |
dl |
1.5 |
*/ |
146 |
dl |
1.1 |
public void testCancelAfterRun() { |
147 |
dl |
1.4 |
FutureTask task = new FutureTask( new NoOpCallable()); |
148 |
dl |
1.1 |
task.run(); |
149 |
|
|
assertFalse(task.cancel(false)); |
150 |
|
|
assertTrue(task.isDone()); |
151 |
|
|
assertFalse(task.isCancelled()); |
152 |
|
|
} |
153 |
|
|
|
154 |
dl |
1.5 |
/** |
155 |
dl |
1.6 |
* cancel(true) interrupts a running task |
156 |
dl |
1.5 |
*/ |
157 |
|
|
public void testCancelInterrupt() { |
158 |
dl |
1.1 |
FutureTask task = new FutureTask( new Callable() { |
159 |
|
|
public Object call() { |
160 |
|
|
try { |
161 |
dl |
1.4 |
Thread.sleep(MEDIUM_DELAY_MS); |
162 |
dl |
1.5 |
threadShouldThrow(); |
163 |
dl |
1.1 |
} |
164 |
|
|
catch (InterruptedException success) {} |
165 |
|
|
return Boolean.TRUE; |
166 |
|
|
} }); |
167 |
|
|
Thread t = new Thread(task); |
168 |
|
|
t.start(); |
169 |
|
|
|
170 |
dl |
1.5 |
try { |
171 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
172 |
|
|
assertTrue(task.cancel(true)); |
173 |
|
|
t.join(); |
174 |
|
|
assertTrue(task.isDone()); |
175 |
|
|
assertTrue(task.isCancelled()); |
176 |
|
|
} catch(InterruptedException e){ |
177 |
dl |
1.5 |
unexpectedException(); |
178 |
dl |
1.1 |
} |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
dl |
1.5 |
/** |
183 |
dl |
1.6 |
* cancel(false) does not interrupt a running task |
184 |
dl |
1.5 |
*/ |
185 |
|
|
public void testCancelNoInterrupt() { |
186 |
dl |
1.1 |
FutureTask task = new FutureTask( new Callable() { |
187 |
|
|
public Object call() { |
188 |
|
|
try { |
189 |
dl |
1.4 |
Thread.sleep(MEDIUM_DELAY_MS); |
190 |
dl |
1.1 |
} |
191 |
|
|
catch (InterruptedException success) { |
192 |
dl |
1.4 |
threadFail("should not interrupt"); |
193 |
dl |
1.1 |
} |
194 |
|
|
return Boolean.TRUE; |
195 |
|
|
} }); |
196 |
|
|
Thread t = new Thread(task); |
197 |
|
|
t.start(); |
198 |
|
|
|
199 |
dl |
1.5 |
try { |
200 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
201 |
|
|
assertTrue(task.cancel(false)); |
202 |
|
|
t.join(); |
203 |
|
|
assertTrue(task.isDone()); |
204 |
|
|
assertTrue(task.isCancelled()); |
205 |
|
|
} catch(InterruptedException e){ |
206 |
dl |
1.5 |
unexpectedException(); |
207 |
dl |
1.1 |
} |
208 |
|
|
} |
209 |
|
|
|
210 |
dl |
1.5 |
/** |
211 |
dl |
1.6 |
* set in one thread causes get in another thread to retrieve value |
212 |
dl |
1.5 |
*/ |
213 |
dl |
1.1 |
public void testGet1() { |
214 |
dl |
1.5 |
final FutureTask ft = new FutureTask(new Callable() { |
215 |
|
|
public Object call() { |
216 |
|
|
try { |
217 |
dl |
1.1 |
Thread.sleep(MEDIUM_DELAY_MS); |
218 |
dl |
1.2 |
} catch(InterruptedException e){ |
219 |
dl |
1.5 |
threadUnexpectedException(); |
220 |
dl |
1.1 |
} |
221 |
|
|
return Boolean.TRUE; |
222 |
|
|
} |
223 |
|
|
}); |
224 |
dl |
1.5 |
Thread t = new Thread(new Runnable() { |
225 |
|
|
public void run() { |
226 |
|
|
try { |
227 |
dl |
1.1 |
ft.get(); |
228 |
dl |
1.2 |
} catch(Exception e){ |
229 |
dl |
1.5 |
threadUnexpectedException(); |
230 |
dl |
1.1 |
} |
231 |
|
|
} |
232 |
|
|
}); |
233 |
dl |
1.5 |
try { |
234 |
dl |
1.1 |
assertFalse(ft.isDone()); |
235 |
|
|
assertFalse(ft.isCancelled()); |
236 |
|
|
t.start(); |
237 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
238 |
|
|
ft.run(); |
239 |
|
|
t.join(); |
240 |
|
|
assertTrue(ft.isDone()); |
241 |
|
|
assertFalse(ft.isCancelled()); |
242 |
|
|
} catch(InterruptedException e){ |
243 |
dl |
1.5 |
unexpectedException(); |
244 |
dl |
1.1 |
|
245 |
|
|
} |
246 |
|
|
} |
247 |
|
|
|
248 |
dl |
1.5 |
/** |
249 |
dl |
1.6 |
* set in one thread causes timed get in another thread to retrieve value |
250 |
dl |
1.5 |
*/ |
251 |
dl |
1.1 |
public void testTimedGet1() { |
252 |
dl |
1.5 |
final FutureTask ft = new FutureTask(new Callable() { |
253 |
|
|
public Object call() { |
254 |
|
|
try { |
255 |
dl |
1.1 |
Thread.sleep(MEDIUM_DELAY_MS); |
256 |
dl |
1.2 |
} catch(InterruptedException e){ |
257 |
dl |
1.5 |
threadUnexpectedException(); |
258 |
dl |
1.1 |
} |
259 |
|
|
return Boolean.TRUE; |
260 |
|
|
} |
261 |
|
|
}); |
262 |
dl |
1.5 |
Thread t = new Thread(new Runnable() { |
263 |
|
|
public void run() { |
264 |
|
|
try { |
265 |
dl |
1.1 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
266 |
|
|
} catch(TimeoutException success) { |
267 |
|
|
} catch(Exception e){ |
268 |
dl |
1.5 |
threadUnexpectedException(); |
269 |
dl |
1.1 |
} |
270 |
|
|
} |
271 |
|
|
}); |
272 |
dl |
1.5 |
try { |
273 |
dl |
1.1 |
assertFalse(ft.isDone()); |
274 |
|
|
assertFalse(ft.isCancelled()); |
275 |
|
|
t.start(); |
276 |
|
|
ft.run(); |
277 |
|
|
t.join(); |
278 |
|
|
assertTrue(ft.isDone()); |
279 |
|
|
assertFalse(ft.isCancelled()); |
280 |
|
|
} catch(InterruptedException e){ |
281 |
dl |
1.5 |
unexpectedException(); |
282 |
dl |
1.1 |
|
283 |
|
|
} |
284 |
|
|
} |
285 |
|
|
|
286 |
dl |
1.5 |
/** |
287 |
dl |
1.6 |
* Cancelling a task causes timed get in another thread to throw CancellationException |
288 |
dl |
1.5 |
*/ |
289 |
dl |
1.6 |
public void testTimedGet_Cancellation() { |
290 |
dl |
1.5 |
final FutureTask ft = new FutureTask(new Callable() { |
291 |
|
|
public Object call() { |
292 |
|
|
try { |
293 |
dl |
1.6 |
Thread.sleep(SMALL_DELAY_MS); |
294 |
|
|
threadShouldThrow(); |
295 |
|
|
} catch(InterruptedException e) { |
296 |
dl |
1.1 |
} |
297 |
dl |
1.6 |
return Boolean.TRUE; |
298 |
dl |
1.1 |
} |
299 |
|
|
}); |
300 |
|
|
try { |
301 |
dl |
1.6 |
Thread t1 = new Thread(new Runnable() { |
302 |
dl |
1.5 |
public void run() { |
303 |
|
|
try { |
304 |
dl |
1.6 |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
305 |
dl |
1.5 |
threadShouldThrow(); |
306 |
dl |
1.6 |
} catch(CancellationException success) {} |
307 |
dl |
1.1 |
catch(Exception e){ |
308 |
dl |
1.5 |
threadUnexpectedException(); |
309 |
dl |
1.6 |
} |
310 |
dl |
1.1 |
} |
311 |
|
|
}); |
312 |
dl |
1.6 |
Thread t2 = new Thread(ft); |
313 |
|
|
t1.start(); |
314 |
|
|
t2.start(); |
315 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
316 |
dl |
1.1 |
ft.cancel(true); |
317 |
dl |
1.6 |
t1.join(); |
318 |
|
|
t2.join(); |
319 |
|
|
} catch(InterruptedException ie){ |
320 |
dl |
1.5 |
unexpectedException(); |
321 |
dl |
1.1 |
} |
322 |
|
|
} |
323 |
dl |
1.6 |
|
324 |
dl |
1.5 |
/** |
325 |
dl |
1.6 |
* Cancelling a task causes get in another thread to throw CancellationException |
326 |
dl |
1.5 |
*/ |
327 |
dl |
1.6 |
public void testGet_Cancellation() { |
328 |
dl |
1.5 |
final FutureTask ft = new FutureTask(new Callable() { |
329 |
|
|
public Object call() { |
330 |
|
|
try { |
331 |
dl |
1.6 |
Thread.sleep(MEDIUM_DELAY_MS); |
332 |
|
|
threadShouldThrow(); |
333 |
|
|
} catch(InterruptedException e){ |
334 |
dl |
1.1 |
} |
335 |
dl |
1.6 |
return Boolean.TRUE; |
336 |
dl |
1.1 |
} |
337 |
|
|
}); |
338 |
dl |
1.5 |
try { |
339 |
dl |
1.6 |
Thread t1 = new Thread(new Runnable() { |
340 |
dl |
1.5 |
public void run() { |
341 |
|
|
try { |
342 |
dl |
1.6 |
ft.get(); |
343 |
dl |
1.5 |
threadShouldThrow(); |
344 |
dl |
1.6 |
} catch(CancellationException success){ |
345 |
|
|
} |
346 |
dl |
1.1 |
catch(Exception e){ |
347 |
dl |
1.5 |
threadUnexpectedException(); |
348 |
dl |
1.6 |
} |
349 |
dl |
1.1 |
} |
350 |
|
|
}); |
351 |
dl |
1.6 |
Thread t2 = new Thread(ft); |
352 |
|
|
t1.start(); |
353 |
|
|
t2.start(); |
354 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
355 |
dl |
1.1 |
ft.cancel(true); |
356 |
dl |
1.6 |
t1.join(); |
357 |
|
|
t2.join(); |
358 |
|
|
} catch(InterruptedException success){ |
359 |
dl |
1.5 |
unexpectedException(); |
360 |
dl |
1.1 |
} |
361 |
|
|
} |
362 |
dl |
1.6 |
|
363 |
dl |
1.1 |
|
364 |
dl |
1.5 |
/** |
365 |
dl |
1.6 |
* A runtime exception in task causes get to throw ExecutionException |
366 |
dl |
1.5 |
*/ |
367 |
|
|
public void testGet_ExecutionException() { |
368 |
|
|
final FutureTask ft = new FutureTask(new Callable() { |
369 |
|
|
public Object call() { |
370 |
dl |
1.1 |
int i = 5/0; |
371 |
|
|
return Boolean.TRUE; |
372 |
|
|
} |
373 |
|
|
}); |
374 |
dl |
1.5 |
try { |
375 |
dl |
1.1 |
ft.run(); |
376 |
|
|
ft.get(); |
377 |
dl |
1.5 |
shouldThrow(); |
378 |
dl |
1.2 |
} catch(ExecutionException success){ |
379 |
dl |
1.1 |
} |
380 |
|
|
catch(Exception e){ |
381 |
dl |
1.5 |
unexpectedException(); |
382 |
dl |
1.1 |
} |
383 |
|
|
} |
384 |
|
|
|
385 |
dl |
1.5 |
/** |
386 |
dl |
1.6 |
* A runtime exception in task causes timed get to throw ExecutionException |
387 |
dl |
1.5 |
*/ |
388 |
|
|
public void testTimedGet_ExecutionException2() { |
389 |
|
|
final FutureTask ft = new FutureTask(new Callable() { |
390 |
|
|
public Object call() { |
391 |
dl |
1.1 |
int i = 5/0; |
392 |
|
|
return Boolean.TRUE; |
393 |
|
|
} |
394 |
|
|
}); |
395 |
dl |
1.5 |
try { |
396 |
dl |
1.1 |
ft.run(); |
397 |
|
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
398 |
dl |
1.5 |
shouldThrow(); |
399 |
dl |
1.2 |
} catch(ExecutionException success) { |
400 |
|
|
} catch(TimeoutException success) { } // unlikely but OK |
401 |
dl |
1.1 |
catch(Exception e){ |
402 |
dl |
1.5 |
unexpectedException(); |
403 |
dl |
1.1 |
} |
404 |
|
|
} |
405 |
|
|
|
406 |
|
|
|
407 |
dl |
1.5 |
/** |
408 |
dl |
1.6 |
* Interrupting a waiting get causes it to throw InterruptedException |
409 |
dl |
1.5 |
*/ |
410 |
|
|
public void testGet_InterruptedException() { |
411 |
dl |
1.4 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
412 |
dl |
1.5 |
Thread t = new Thread(new Runnable() { |
413 |
|
|
public void run() { |
414 |
|
|
try { |
415 |
dl |
1.1 |
ft.get(); |
416 |
dl |
1.5 |
threadShouldThrow(); |
417 |
dl |
1.1 |
} catch(InterruptedException success){ |
418 |
|
|
} catch(Exception e){ |
419 |
dl |
1.5 |
threadUnexpectedException(); |
420 |
dl |
1.1 |
} |
421 |
|
|
} |
422 |
|
|
}); |
423 |
|
|
try { |
424 |
|
|
t.start(); |
425 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
426 |
|
|
t.interrupt(); |
427 |
|
|
t.join(); |
428 |
|
|
} catch(Exception e){ |
429 |
dl |
1.5 |
unexpectedException(); |
430 |
dl |
1.1 |
} |
431 |
|
|
} |
432 |
|
|
|
433 |
dl |
1.5 |
/** |
434 |
dl |
1.6 |
* Interrupting a waiting timed get causes it to throw InterruptedException |
435 |
dl |
1.5 |
*/ |
436 |
|
|
public void testTimedGet_InterruptedException2() { |
437 |
dl |
1.4 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
438 |
dl |
1.5 |
Thread t = new Thread(new Runnable() { |
439 |
|
|
public void run() { |
440 |
|
|
try { |
441 |
dl |
1.4 |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
442 |
dl |
1.5 |
threadShouldThrow(); |
443 |
dl |
1.2 |
} catch(InterruptedException success){} |
444 |
dl |
1.1 |
catch(Exception e){ |
445 |
dl |
1.5 |
threadUnexpectedException(); |
446 |
dl |
1.1 |
} |
447 |
|
|
} |
448 |
|
|
}); |
449 |
|
|
try { |
450 |
|
|
t.start(); |
451 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
452 |
|
|
t.interrupt(); |
453 |
|
|
t.join(); |
454 |
|
|
} catch(Exception e){ |
455 |
dl |
1.5 |
unexpectedException(); |
456 |
dl |
1.1 |
} |
457 |
|
|
} |
458 |
|
|
|
459 |
dl |
1.5 |
/** |
460 |
dl |
1.6 |
* A timed out timed get throws TimeoutException |
461 |
dl |
1.5 |
*/ |
462 |
|
|
public void testGet_TimeoutException() { |
463 |
|
|
try { |
464 |
dl |
1.4 |
FutureTask ft = new FutureTask(new NoOpCallable()); |
465 |
dl |
1.1 |
ft.get(1,TimeUnit.MILLISECONDS); |
466 |
dl |
1.5 |
shouldThrow(); |
467 |
dl |
1.2 |
} catch(TimeoutException success){} |
468 |
|
|
catch(Exception success){ |
469 |
dl |
1.5 |
unexpectedException(); |
470 |
dl |
1.1 |
} |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
} |