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 |
|
/** |
33 |
< |
* |
33 |
> |
* Creating a future with a null callable throws NPE |
34 |
|
*/ |
35 |
|
public void testConstructor() { |
36 |
|
try { |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* |
45 |
> |
* creating a future with null runnable fails |
46 |
|
*/ |
47 |
|
public void testConstructor2() { |
48 |
|
try { |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* |
57 |
> |
* isDone is true when a task completes |
58 |
|
*/ |
59 |
|
public void testIsDone() { |
60 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* |
67 |
> |
* reset of a done task succeeds and changes status to not done |
68 |
|
*/ |
69 |
|
public void testReset() { |
70 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
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 |
< |
* |
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 |
|
|
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 |
– |
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* |
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()); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* |
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); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* |
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(); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* |
136 |
> |
* Cancelling before running succeeds |
137 |
|
*/ |
138 |
|
public void testCancelBeforeRun() { |
139 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* |
147 |
> |
* Cancel(true) before run succeeds |
148 |
|
*/ |
149 |
|
public void testCancelBeforeRun2() { |
150 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
< |
* |
158 |
> |
* cancel of a completed task fails |
159 |
|
*/ |
160 |
|
public void testCancelAfterRun() { |
161 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* |
169 |
> |
* cancel(true) interrupts a running task |
170 |
|
*/ |
171 |
|
public void testCancelInterrupt() { |
172 |
|
FutureTask task = new FutureTask( new Callable() { |
194 |
|
|
195 |
|
|
196 |
|
/** |
197 |
< |
* |
197 |
> |
* cancel(false) does not interrupt a running task |
198 |
|
*/ |
199 |
|
public void testCancelNoInterrupt() { |
200 |
|
FutureTask task = new FutureTask( new Callable() { |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* |
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() { |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
< |
* |
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() { |
297 |
|
} |
298 |
|
} |
299 |
|
|
309 |
– |
|
300 |
|
/** |
301 |
< |
* |
301 |
> |
* Cancelling a task causes timed get in another thread to throw CancellationException |
302 |
|
*/ |
303 |
< |
public void testGet_Cancellation() { |
303 |
> |
public void testTimedGet_Cancellation() { |
304 |
|
final FutureTask ft = new FutureTask(new Callable() { |
305 |
|
public Object call() { |
306 |
|
try { |
307 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
308 |
< |
} catch(InterruptedException e){ |
309 |
< |
threadUnexpectedException(); |
307 |
> |
Thread.sleep(SMALL_DELAY_MS); |
308 |
> |
threadShouldThrow(); |
309 |
> |
} catch(InterruptedException e) { |
310 |
|
} |
311 |
< |
return Boolean.TRUE; |
311 |
> |
return Boolean.TRUE; |
312 |
|
} |
313 |
|
}); |
314 |
|
try { |
315 |
< |
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
Thread t = new Thread(new Runnable() { |
315 |
> |
Thread t1 = new Thread(new Runnable() { |
316 |
|
public void run() { |
317 |
|
try { |
318 |
< |
ft.get(); |
318 |
> |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
319 |
|
threadShouldThrow(); |
320 |
< |
} catch(CancellationException success){ |
332 |
< |
} |
320 |
> |
} catch(CancellationException success) {} |
321 |
|
catch(Exception e){ |
322 |
|
threadUnexpectedException(); |
323 |
< |
} |
323 |
> |
} |
324 |
|
} |
325 |
|
}); |
326 |
< |
t.start(); |
326 |
> |
Thread t2 = new Thread(ft); |
327 |
> |
t1.start(); |
328 |
> |
t2.start(); |
329 |
> |
Thread.sleep(SHORT_DELAY_MS); |
330 |
|
ft.cancel(true); |
331 |
< |
t.join(); |
332 |
< |
} catch(InterruptedException success){ |
331 |
> |
t1.join(); |
332 |
> |
t2.join(); |
333 |
> |
} catch(InterruptedException ie){ |
334 |
|
unexpectedException(); |
335 |
|
} |
336 |
|
} |
337 |
< |
|
337 |
> |
|
338 |
|
/** |
339 |
< |
* |
339 |
> |
* Cancelling a task causes get in another thread to throw CancellationException |
340 |
|
*/ |
341 |
< |
public void testGet_Cancellation2() { |
341 |
> |
public void testGet_Cancellation() { |
342 |
|
final FutureTask ft = new FutureTask(new Callable() { |
343 |
|
public Object call() { |
344 |
|
try { |
345 |
< |
Thread.sleep(SHORT_DELAY_MS); |
346 |
< |
} catch(InterruptedException e) { |
347 |
< |
threadUnexpectedException(); |
345 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
346 |
> |
threadShouldThrow(); |
347 |
> |
} catch(InterruptedException e){ |
348 |
|
} |
349 |
< |
return Boolean.TRUE; |
349 |
> |
return Boolean.TRUE; |
350 |
|
} |
351 |
|
}); |
352 |
|
try { |
353 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
Thread t = new Thread(new Runnable() { |
353 |
> |
Thread t1 = new Thread(new Runnable() { |
354 |
|
public void run() { |
355 |
|
try { |
356 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
356 |
> |
ft.get(); |
357 |
|
threadShouldThrow(); |
358 |
< |
} catch(CancellationException success) {} |
358 |
> |
} catch(CancellationException success){ |
359 |
> |
} |
360 |
|
catch(Exception e){ |
361 |
|
threadUnexpectedException(); |
362 |
< |
} |
362 |
> |
} |
363 |
|
} |
364 |
|
}); |
365 |
< |
t.start(); |
366 |
< |
Thread.sleep(SHORT_DELAY_MS); |
365 |
> |
Thread t2 = new Thread(ft); |
366 |
> |
t1.start(); |
367 |
> |
t2.start(); |
368 |
> |
Thread.sleep(SHORT_DELAY_MS); |
369 |
|
ft.cancel(true); |
370 |
< |
Thread.sleep(SHORT_DELAY_MS); |
371 |
< |
t.join(); |
372 |
< |
} catch(InterruptedException ie){ |
370 |
> |
t1.join(); |
371 |
> |
t2.join(); |
372 |
> |
} catch(InterruptedException success){ |
373 |
|
unexpectedException(); |
374 |
|
} |
375 |
|
} |
376 |
+ |
|
377 |
|
|
378 |
|
/** |
379 |
< |
* |
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() { |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
< |
* |
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() { |
419 |
|
|
420 |
|
|
421 |
|
/** |
422 |
< |
* |
422 |
> |
* Interrupting a waiting get causes it to throw InterruptedException |
423 |
|
*/ |
424 |
|
public void testGet_InterruptedException() { |
425 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
< |
* |
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()); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* |
474 |
> |
* A timed out timed get throws TimeoutException |
475 |
|
*/ |
476 |
|
public void testGet_TimeoutException() { |
477 |
|
try { |