21 |
|
/** |
22 |
|
* Subclass to expose protected methods |
23 |
|
*/ |
24 |
< |
static class MyFutureTask extends FutureTask { |
25 |
< |
public MyFutureTask(Callable r) { super(r); } |
26 |
< |
public boolean reset() { return super.reset(); } |
27 |
< |
public void setCancelled() { super.setCancelled(); } |
28 |
< |
public void setDone() { super.setDone(); } |
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 |
< |
* |
32 |
> |
* Creating a future with a null callable throws NPE |
33 |
|
*/ |
34 |
|
public void testConstructor() { |
35 |
|
try { |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* |
44 |
> |
* creating a future with null runnable fails |
45 |
|
*/ |
46 |
|
public void testConstructor2() { |
47 |
|
try { |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* |
56 |
> |
* isDone is true when a task completes |
57 |
|
*/ |
58 |
|
public void testIsDone() { |
59 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* |
66 |
> |
* runAndReset of a non-cancelled task succeeds |
67 |
|
*/ |
68 |
< |
public void testReset() { |
69 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
70 |
< |
task.run(); |
71 |
< |
assertTrue(task.isDone()); |
74 |
< |
assertTrue(task.reset()); |
68 |
> |
public void testRunAndReset() { |
69 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
70 |
> |
assertTrue(task.runAndReset()); |
71 |
> |
assertFalse(task.isDone()); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* |
75 |
> |
* runAndReset after cancellation fails |
76 |
|
*/ |
77 |
|
public void testResetAfterCancel() { |
78 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
78 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
79 |
|
assertTrue(task.cancel(false)); |
80 |
< |
task.run(); |
80 |
> |
assertFalse(task.runAndReset()); |
81 |
|
assertTrue(task.isDone()); |
82 |
|
assertTrue(task.isCancelled()); |
86 |
– |
assertFalse(task.reset()); |
83 |
|
} |
84 |
|
|
89 |
– |
/** |
90 |
– |
* |
91 |
– |
*/ |
92 |
– |
public void testSetDone() { |
93 |
– |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
94 |
– |
task.setDone(); |
95 |
– |
assertTrue(task.isDone()); |
96 |
– |
assertFalse(task.isCancelled()); |
97 |
– |
} |
85 |
|
|
99 |
– |
/** |
100 |
– |
* |
101 |
– |
*/ |
102 |
– |
public void testSetCancelled() { |
103 |
– |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
104 |
– |
assertTrue(task.cancel(false)); |
105 |
– |
task.setCancelled(); |
106 |
– |
assertTrue(task.isDone()); |
107 |
– |
assertTrue(task.isCancelled()); |
108 |
– |
} |
86 |
|
|
87 |
|
/** |
88 |
< |
* |
88 |
> |
* setting value gauses get to return it |
89 |
|
*/ |
90 |
|
public void testSet() { |
91 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
91 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
92 |
|
task.set(one); |
93 |
|
try { |
94 |
|
assertEquals(task.get(), one); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* |
102 |
> |
* setException causes get to throw ExecutionException |
103 |
|
*/ |
104 |
|
public void testSetException() { |
105 |
|
Exception nse = new NoSuchElementException(); |
106 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
106 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
107 |
|
task.setException(nse); |
108 |
|
try { |
109 |
|
Object x = task.get(); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
< |
* |
122 |
> |
* Cancelling before running succeeds |
123 |
|
*/ |
124 |
|
public void testCancelBeforeRun() { |
125 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* |
133 |
> |
* Cancel(true) before run succeeds |
134 |
|
*/ |
135 |
|
public void testCancelBeforeRun2() { |
136 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* |
144 |
> |
* cancel of a completed task fails |
145 |
|
*/ |
146 |
|
public void testCancelAfterRun() { |
147 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* |
155 |
> |
* cancel(true) interrupts a running task |
156 |
|
*/ |
157 |
|
public void testCancelInterrupt() { |
158 |
|
FutureTask task = new FutureTask( new Callable() { |
180 |
|
|
181 |
|
|
182 |
|
/** |
183 |
< |
* |
183 |
> |
* cancel(false) does not interrupt a running task |
184 |
|
*/ |
185 |
|
public void testCancelNoInterrupt() { |
186 |
|
FutureTask task = new FutureTask( new Callable() { |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
< |
* |
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() { |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
< |
* |
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() { |
283 |
|
} |
284 |
|
} |
285 |
|
|
309 |
– |
|
286 |
|
/** |
287 |
< |
* |
287 |
> |
* Cancelling a task causes timed get in another thread to throw CancellationException |
288 |
|
*/ |
289 |
< |
public void testGet_Cancellation() { |
289 |
> |
public void testTimedGet_Cancellation() { |
290 |
|
final FutureTask ft = new FutureTask(new Callable() { |
291 |
|
public Object call() { |
292 |
|
try { |
293 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
294 |
< |
} catch(InterruptedException e){ |
295 |
< |
threadUnexpectedException(); |
293 |
> |
Thread.sleep(SMALL_DELAY_MS); |
294 |
> |
threadShouldThrow(); |
295 |
> |
} catch(InterruptedException e) { |
296 |
|
} |
297 |
< |
return Boolean.TRUE; |
297 |
> |
return Boolean.TRUE; |
298 |
|
} |
299 |
|
}); |
300 |
|
try { |
301 |
< |
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
Thread t = new Thread(new Runnable() { |
301 |
> |
Thread t1 = new Thread(new Runnable() { |
302 |
|
public void run() { |
303 |
|
try { |
304 |
< |
ft.get(); |
304 |
> |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
305 |
|
threadShouldThrow(); |
306 |
< |
} catch(CancellationException success){ |
332 |
< |
} |
306 |
> |
} catch(CancellationException success) {} |
307 |
|
catch(Exception e){ |
308 |
|
threadUnexpectedException(); |
309 |
< |
} |
309 |
> |
} |
310 |
|
} |
311 |
|
}); |
312 |
< |
t.start(); |
312 |
> |
Thread t2 = new Thread(ft); |
313 |
> |
t1.start(); |
314 |
> |
t2.start(); |
315 |
> |
Thread.sleep(SHORT_DELAY_MS); |
316 |
|
ft.cancel(true); |
317 |
< |
t.join(); |
318 |
< |
} catch(InterruptedException success){ |
317 |
> |
t1.join(); |
318 |
> |
t2.join(); |
319 |
> |
} catch(InterruptedException ie){ |
320 |
|
unexpectedException(); |
321 |
|
} |
322 |
|
} |
323 |
< |
|
323 |
> |
|
324 |
|
/** |
325 |
< |
* |
325 |
> |
* Cancelling a task causes get in another thread to throw CancellationException |
326 |
|
*/ |
327 |
< |
public void testGet_Cancellation2() { |
327 |
> |
public void testGet_Cancellation() { |
328 |
|
final FutureTask ft = new FutureTask(new Callable() { |
329 |
|
public Object call() { |
330 |
|
try { |
331 |
< |
Thread.sleep(SHORT_DELAY_MS); |
332 |
< |
} catch(InterruptedException e) { |
333 |
< |
threadUnexpectedException(); |
331 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
332 |
> |
threadShouldThrow(); |
333 |
> |
} catch(InterruptedException e){ |
334 |
|
} |
335 |
< |
return Boolean.TRUE; |
335 |
> |
return Boolean.TRUE; |
336 |
|
} |
337 |
|
}); |
338 |
|
try { |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
Thread t = new Thread(new Runnable() { |
339 |
> |
Thread t1 = new Thread(new Runnable() { |
340 |
|
public void run() { |
341 |
|
try { |
342 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
342 |
> |
ft.get(); |
343 |
|
threadShouldThrow(); |
344 |
< |
} catch(CancellationException success) {} |
344 |
> |
} catch(CancellationException success){ |
345 |
> |
} |
346 |
|
catch(Exception e){ |
347 |
|
threadUnexpectedException(); |
348 |
< |
} |
348 |
> |
} |
349 |
|
} |
350 |
|
}); |
351 |
< |
t.start(); |
352 |
< |
Thread.sleep(SHORT_DELAY_MS); |
351 |
> |
Thread t2 = new Thread(ft); |
352 |
> |
t1.start(); |
353 |
> |
t2.start(); |
354 |
> |
Thread.sleep(SHORT_DELAY_MS); |
355 |
|
ft.cancel(true); |
356 |
< |
Thread.sleep(SHORT_DELAY_MS); |
357 |
< |
t.join(); |
358 |
< |
} catch(InterruptedException ie){ |
356 |
> |
t1.join(); |
357 |
> |
t2.join(); |
358 |
> |
} catch(InterruptedException success){ |
359 |
|
unexpectedException(); |
360 |
|
} |
361 |
|
} |
362 |
+ |
|
363 |
|
|
364 |
|
/** |
365 |
< |
* |
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() { |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* |
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() { |
405 |
|
|
406 |
|
|
407 |
|
/** |
408 |
< |
* |
408 |
> |
* Interrupting a waiting get causes it to throw InterruptedException |
409 |
|
*/ |
410 |
|
public void testGet_InterruptedException() { |
411 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
< |
* |
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()); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
< |
* |
460 |
> |
* A timed out timed get throws TimeoutException |
461 |
|
*/ |
462 |
|
public void testGet_TimeoutException() { |
463 |
|
try { |