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