31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
< |
* |
34 |
> |
* Creating a future with a null callable throws NPE |
35 |
|
*/ |
36 |
|
public void testConstructor() { |
37 |
|
try { |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
46 |
> |
* creating a future with null runnable fails |
47 |
|
*/ |
48 |
|
public void testConstructor2() { |
49 |
|
try { |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* |
58 |
> |
* isDone is true when a task completes |
59 |
|
*/ |
60 |
|
public void testIsDone() { |
61 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* |
68 |
> |
* reset of a done task succeeds and changes status to not done |
69 |
|
*/ |
70 |
|
public void testReset() { |
71 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
72 |
|
task.run(); |
73 |
|
assertTrue(task.isDone()); |
74 |
< |
assertTrue(task.reset()); |
74 |
> |
assertTrue(task.reset()); |
75 |
> |
assertFalse(task.isDone()); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
< |
* |
79 |
> |
* Resetting after cancellation fails |
80 |
|
*/ |
81 |
|
public void testResetAfterCancel() { |
82 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* |
91 |
> |
* setDone of new task causes isDone to be true |
92 |
|
*/ |
93 |
|
public void testSetDone() { |
94 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
< |
* |
101 |
> |
* setCancelled of a new task causes isCancelled to be true |
102 |
|
*/ |
103 |
|
public void testSetCancelled() { |
104 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* |
112 |
> |
* setting value gauses get to return it |
113 |
|
*/ |
114 |
|
public void testSet() { |
115 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
< |
* |
126 |
> |
* setException causes get to throw ExecutionException |
127 |
|
*/ |
128 |
|
public void testSetException() { |
129 |
|
Exception nse = new NoSuchElementException(); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* |
146 |
> |
* Cancelling before running succeeds |
147 |
|
*/ |
148 |
|
public void testCancelBeforeRun() { |
149 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* |
157 |
> |
* Cancel(true) before run succeeds |
158 |
|
*/ |
159 |
|
public void testCancelBeforeRun2() { |
160 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* |
168 |
> |
* cancel of a completed task fails |
169 |
|
*/ |
170 |
|
public void testCancelAfterRun() { |
171 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* |
179 |
> |
* cancel(true) interrupts a running task |
180 |
|
*/ |
181 |
|
public void testCancelInterrupt() { |
182 |
|
FutureTask task = new FutureTask( new Callable() { |
204 |
|
|
205 |
|
|
206 |
|
/** |
207 |
< |
* |
207 |
> |
* cancel(false) does not interrupt a running task |
208 |
|
*/ |
209 |
|
public void testCancelNoInterrupt() { |
210 |
|
FutureTask task = new FutureTask( new Callable() { |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* |
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() { |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
< |
* |
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() { |
307 |
|
} |
308 |
|
} |
309 |
|
|
309 |
– |
|
310 |
|
/** |
311 |
< |
* |
311 |
> |
* Cancelling a task causes timed get in another thread to throw CancellationException |
312 |
|
*/ |
313 |
< |
public void testGet_Cancellation() { |
313 |
> |
public void testTimedGet_Cancellation() { |
314 |
|
final FutureTask ft = new FutureTask(new Callable() { |
315 |
|
public Object call() { |
316 |
|
try { |
317 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
318 |
< |
} catch(InterruptedException e){ |
319 |
< |
threadUnexpectedException(); |
317 |
> |
Thread.sleep(SMALL_DELAY_MS); |
318 |
> |
threadShouldThrow(); |
319 |
> |
} catch(InterruptedException e) { |
320 |
|
} |
321 |
< |
return Boolean.TRUE; |
321 |
> |
return Boolean.TRUE; |
322 |
|
} |
323 |
|
}); |
324 |
|
try { |
325 |
< |
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
Thread t = new Thread(new Runnable() { |
325 |
> |
Thread t1 = new Thread(new Runnable() { |
326 |
|
public void run() { |
327 |
|
try { |
328 |
< |
ft.get(); |
328 |
> |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
329 |
|
threadShouldThrow(); |
330 |
< |
} catch(CancellationException success){ |
332 |
< |
} |
330 |
> |
} catch(CancellationException success) {} |
331 |
|
catch(Exception e){ |
332 |
|
threadUnexpectedException(); |
333 |
< |
} |
333 |
> |
} |
334 |
|
} |
335 |
|
}); |
336 |
< |
t.start(); |
336 |
> |
Thread t2 = new Thread(ft); |
337 |
> |
t1.start(); |
338 |
> |
t2.start(); |
339 |
> |
Thread.sleep(SHORT_DELAY_MS); |
340 |
|
ft.cancel(true); |
341 |
< |
t.join(); |
342 |
< |
} catch(InterruptedException success){ |
341 |
> |
t1.join(); |
342 |
> |
t2.join(); |
343 |
> |
} catch(InterruptedException ie){ |
344 |
|
unexpectedException(); |
345 |
|
} |
346 |
|
} |
347 |
< |
|
347 |
> |
|
348 |
|
/** |
349 |
< |
* |
349 |
> |
* Cancelling a task causes get in another thread to throw CancellationException |
350 |
|
*/ |
351 |
< |
public void testGet_Cancellation2() { |
351 |
> |
public void testGet_Cancellation() { |
352 |
|
final FutureTask ft = new FutureTask(new Callable() { |
353 |
|
public Object call() { |
354 |
|
try { |
355 |
< |
Thread.sleep(SHORT_DELAY_MS); |
356 |
< |
} catch(InterruptedException e) { |
357 |
< |
threadUnexpectedException(); |
355 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
356 |
> |
threadShouldThrow(); |
357 |
> |
} catch(InterruptedException e){ |
358 |
|
} |
359 |
< |
return Boolean.TRUE; |
359 |
> |
return Boolean.TRUE; |
360 |
|
} |
361 |
|
}); |
362 |
|
try { |
363 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
Thread t = new Thread(new Runnable() { |
363 |
> |
Thread t1 = new Thread(new Runnable() { |
364 |
|
public void run() { |
365 |
|
try { |
366 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
366 |
> |
ft.get(); |
367 |
|
threadShouldThrow(); |
368 |
< |
} catch(CancellationException success) {} |
368 |
> |
} catch(CancellationException success){ |
369 |
> |
} |
370 |
|
catch(Exception e){ |
371 |
|
threadUnexpectedException(); |
372 |
< |
} |
372 |
> |
} |
373 |
|
} |
374 |
|
}); |
375 |
< |
t.start(); |
376 |
< |
Thread.sleep(SHORT_DELAY_MS); |
375 |
> |
Thread t2 = new Thread(ft); |
376 |
> |
t1.start(); |
377 |
> |
t2.start(); |
378 |
> |
Thread.sleep(SHORT_DELAY_MS); |
379 |
|
ft.cancel(true); |
380 |
< |
Thread.sleep(SHORT_DELAY_MS); |
381 |
< |
t.join(); |
382 |
< |
} catch(InterruptedException ie){ |
380 |
> |
t1.join(); |
381 |
> |
t2.join(); |
382 |
> |
} catch(InterruptedException success){ |
383 |
|
unexpectedException(); |
384 |
|
} |
385 |
|
} |
386 |
+ |
|
387 |
|
|
388 |
|
/** |
389 |
< |
* |
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() { |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* |
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() { |
429 |
|
|
430 |
|
|
431 |
|
/** |
432 |
< |
* |
432 |
> |
* Interrupting a waiting get causes it to throw InterruptedException |
433 |
|
*/ |
434 |
|
public void testGet_InterruptedException() { |
435 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
458 |
< |
* |
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()); |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
< |
* |
484 |
> |
* A timed out timed get throws TimeoutException |
485 |
|
*/ |
486 |
|
public void testGet_TimeoutException() { |
487 |
|
try { |