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