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