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. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
22 |
|
/** |
23 |
|
* Subclass to expose protected methods |
24 |
|
*/ |
25 |
< |
static class MyFutureTask extends FutureTask { |
26 |
< |
public MyFutureTask(Callable r) { super(r); } |
27 |
< |
public boolean reset() { return super.reset(); } |
27 |
< |
public void setCancelled() { super.setCancelled(); } |
28 |
< |
public void setDone() { super.setDone(); } |
25 |
> |
static class PublicFutureTask extends FutureTask { |
26 |
> |
public PublicFutureTask(Callable r) { super(r); } |
27 |
> |
public boolean runAndReset() { return super.runAndReset(); } |
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 |
> |
* runAndReset of a non-cancelled task succeeds |
68 |
|
*/ |
69 |
< |
public void testReset() { |
70 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
71 |
< |
task.run(); |
72 |
< |
assertTrue(task.isDone()); |
74 |
< |
assertTrue(task.reset()); |
69 |
> |
public void testRunAndReset() { |
70 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
71 |
> |
assertTrue(task.runAndReset()); |
72 |
> |
assertFalse(task.isDone()); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
< |
* |
76 |
> |
* runAndReset after cancellation fails |
77 |
|
*/ |
78 |
|
public void testResetAfterCancel() { |
79 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
79 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
80 |
|
assertTrue(task.cancel(false)); |
81 |
< |
task.run(); |
81 |
> |
assertFalse(task.runAndReset()); |
82 |
|
assertTrue(task.isDone()); |
83 |
|
assertTrue(task.isCancelled()); |
86 |
– |
assertFalse(task.reset()); |
84 |
|
} |
85 |
|
|
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 |
– |
} |
86 |
|
|
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 |
– |
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* |
89 |
> |
* setting value gauses get to return it |
90 |
|
*/ |
91 |
|
public void testSet() { |
92 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
92 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
93 |
|
task.set(one); |
94 |
|
try { |
95 |
|
assertEquals(task.get(), one); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* |
103 |
> |
* setException causes get to throw ExecutionException |
104 |
|
*/ |
105 |
|
public void testSetException() { |
106 |
|
Exception nse = new NoSuchElementException(); |
107 |
< |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
107 |
> |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
108 |
|
task.setException(nse); |
109 |
|
try { |
110 |
|
Object x = task.get(); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* |
123 |
> |
* Cancelling before running succeeds |
124 |
|
*/ |
125 |
|
public void testCancelBeforeRun() { |
126 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* |
134 |
> |
* Cancel(true) before run succeeds |
135 |
|
*/ |
136 |
|
public void testCancelBeforeRun2() { |
137 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* |
145 |
> |
* cancel of a completed task fails |
146 |
|
*/ |
147 |
|
public void testCancelAfterRun() { |
148 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* |
156 |
> |
* cancel(true) interrupts a running task |
157 |
|
*/ |
158 |
|
public void testCancelInterrupt() { |
159 |
|
FutureTask task = new FutureTask( new Callable() { |
181 |
|
|
182 |
|
|
183 |
|
/** |
184 |
< |
* |
184 |
> |
* cancel(false) does not interrupt a running task |
185 |
|
*/ |
186 |
|
public void testCancelNoInterrupt() { |
187 |
|
FutureTask task = new FutureTask( new Callable() { |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* |
212 |
> |
* set in one thread causes get in another thread to retrieve value |
213 |
|
*/ |
214 |
|
public void testGet1() { |
215 |
|
final FutureTask ft = new FutureTask(new Callable() { |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* |
250 |
> |
* set in one thread causes timed get in another thread to retrieve value |
251 |
|
*/ |
252 |
|
public void testTimedGet1() { |
253 |
|
final FutureTask ft = new FutureTask(new Callable() { |
284 |
|
} |
285 |
|
} |
286 |
|
|
309 |
– |
|
287 |
|
/** |
288 |
< |
* |
288 |
> |
* Cancelling a task causes timed get in another thread to throw CancellationException |
289 |
|
*/ |
290 |
< |
public void testGet_Cancellation() { |
290 |
> |
public void testTimedGet_Cancellation() { |
291 |
|
final FutureTask ft = new FutureTask(new Callable() { |
292 |
|
public Object call() { |
293 |
|
try { |
294 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
295 |
< |
} catch(InterruptedException e){ |
296 |
< |
threadUnexpectedException(); |
294 |
> |
Thread.sleep(SMALL_DELAY_MS); |
295 |
> |
threadShouldThrow(); |
296 |
> |
} catch(InterruptedException e) { |
297 |
|
} |
298 |
< |
return Boolean.TRUE; |
298 |
> |
return Boolean.TRUE; |
299 |
|
} |
300 |
|
}); |
301 |
|
try { |
302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
Thread t = new Thread(new Runnable() { |
302 |
> |
Thread t1 = new Thread(new Runnable() { |
303 |
|
public void run() { |
304 |
|
try { |
305 |
< |
ft.get(); |
305 |
> |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
306 |
|
threadShouldThrow(); |
307 |
< |
} catch(CancellationException success){ |
332 |
< |
} |
307 |
> |
} catch(CancellationException success) {} |
308 |
|
catch(Exception e){ |
309 |
|
threadUnexpectedException(); |
310 |
< |
} |
310 |
> |
} |
311 |
|
} |
312 |
|
}); |
313 |
< |
t.start(); |
313 |
> |
Thread t2 = new Thread(ft); |
314 |
> |
t1.start(); |
315 |
> |
t2.start(); |
316 |
> |
Thread.sleep(SHORT_DELAY_MS); |
317 |
|
ft.cancel(true); |
318 |
< |
t.join(); |
319 |
< |
} catch(InterruptedException success){ |
318 |
> |
t1.join(); |
319 |
> |
t2.join(); |
320 |
> |
} catch(InterruptedException ie){ |
321 |
|
unexpectedException(); |
322 |
|
} |
323 |
|
} |
324 |
< |
|
324 |
> |
|
325 |
|
/** |
326 |
< |
* |
326 |
> |
* Cancelling a task causes get in another thread to throw CancellationException |
327 |
|
*/ |
328 |
< |
public void testGet_Cancellation2() { |
328 |
> |
public void testGet_Cancellation() { |
329 |
|
final FutureTask ft = new FutureTask(new Callable() { |
330 |
|
public Object call() { |
331 |
|
try { |
332 |
< |
Thread.sleep(SHORT_DELAY_MS); |
333 |
< |
} catch(InterruptedException e) { |
334 |
< |
threadUnexpectedException(); |
332 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
333 |
> |
threadShouldThrow(); |
334 |
> |
} catch(InterruptedException e){ |
335 |
|
} |
336 |
< |
return Boolean.TRUE; |
336 |
> |
return Boolean.TRUE; |
337 |
|
} |
338 |
|
}); |
339 |
|
try { |
340 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
Thread t = new Thread(new Runnable() { |
340 |
> |
Thread t1 = new Thread(new Runnable() { |
341 |
|
public void run() { |
342 |
|
try { |
343 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
343 |
> |
ft.get(); |
344 |
|
threadShouldThrow(); |
345 |
< |
} catch(CancellationException success) {} |
345 |
> |
} catch(CancellationException success){ |
346 |
> |
} |
347 |
|
catch(Exception e){ |
348 |
|
threadUnexpectedException(); |
349 |
< |
} |
349 |
> |
} |
350 |
|
} |
351 |
|
}); |
352 |
< |
t.start(); |
353 |
< |
Thread.sleep(SHORT_DELAY_MS); |
352 |
> |
Thread t2 = new Thread(ft); |
353 |
> |
t1.start(); |
354 |
> |
t2.start(); |
355 |
> |
Thread.sleep(SHORT_DELAY_MS); |
356 |
|
ft.cancel(true); |
357 |
< |
Thread.sleep(SHORT_DELAY_MS); |
358 |
< |
t.join(); |
359 |
< |
} catch(InterruptedException ie){ |
357 |
> |
t1.join(); |
358 |
> |
t2.join(); |
359 |
> |
} catch(InterruptedException success){ |
360 |
|
unexpectedException(); |
361 |
|
} |
362 |
|
} |
363 |
+ |
|
364 |
|
|
365 |
|
/** |
366 |
< |
* |
366 |
> |
* A runtime exception in task causes get to throw ExecutionException |
367 |
|
*/ |
368 |
|
public void testGet_ExecutionException() { |
369 |
|
final FutureTask ft = new FutureTask(new Callable() { |
384 |
|
} |
385 |
|
|
386 |
|
/** |
387 |
< |
* |
387 |
> |
* A runtime exception in task causes timed get to throw ExecutionException |
388 |
|
*/ |
389 |
|
public void testTimedGet_ExecutionException2() { |
390 |
|
final FutureTask ft = new FutureTask(new Callable() { |
406 |
|
|
407 |
|
|
408 |
|
/** |
409 |
< |
* |
409 |
> |
* Interrupting a waiting get causes it to throw InterruptedException |
410 |
|
*/ |
411 |
|
public void testGet_InterruptedException() { |
412 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
< |
* |
435 |
> |
* Interrupting a waiting timed get causes it to throw InterruptedException |
436 |
|
*/ |
437 |
|
public void testTimedGet_InterruptedException2() { |
438 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* |
461 |
> |
* A timed out timed get throws TimeoutException |
462 |
|
*/ |
463 |
|
public void testGet_TimeoutException() { |
464 |
|
try { |