14 |
|
public class FutureTaskTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(FutureTaskTest.class); |
20 |
> |
return new TestSuite(FutureTaskTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
36 |
|
public void testConstructor() { |
37 |
|
try { |
38 |
|
FutureTask task = new FutureTask(null); |
39 |
< |
shouldThrow("NullPointerException"); |
39 |
> |
shouldThrow(); |
40 |
|
} catch (NullPointerException success) {} |
41 |
|
} |
42 |
|
|
46 |
|
public void testConstructor2() { |
47 |
|
try { |
48 |
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
49 |
< |
shouldThrow("NullPointerException"); |
49 |
> |
shouldThrow(); |
50 |
|
} catch (NullPointerException success) {} |
51 |
|
} |
52 |
|
|
55 |
|
*/ |
56 |
|
public void testIsDone() { |
57 |
|
FutureTask task = new FutureTask(new NoOpCallable()); |
58 |
< |
task.run(); |
59 |
< |
assertTrue(task.isDone()); |
60 |
< |
assertFalse(task.isCancelled()); |
58 |
> |
task.run(); |
59 |
> |
assertTrue(task.isDone()); |
60 |
> |
assertFalse(task.isCancelled()); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
65 |
|
*/ |
66 |
|
public void testRunAndReset() { |
67 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
68 |
< |
assertTrue(task.runAndReset()); |
68 |
> |
assertTrue(task.runAndReset()); |
69 |
|
assertFalse(task.isDone()); |
70 |
|
} |
71 |
|
|
75 |
|
public void testResetAfterCancel() { |
76 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
77 |
|
assertTrue(task.cancel(false)); |
78 |
< |
assertFalse(task.runAndReset()); |
79 |
< |
assertTrue(task.isDone()); |
80 |
< |
assertTrue(task.isCancelled()); |
78 |
> |
assertFalse(task.runAndReset()); |
79 |
> |
assertTrue(task.isDone()); |
80 |
> |
assertTrue(task.isCancelled()); |
81 |
|
} |
82 |
|
|
83 |
|
|
88 |
|
public void testSet() throws Exception { |
89 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
90 |
|
task.set(one); |
91 |
< |
assertEquals(task.get(), one); |
91 |
> |
assertSame(task.get(), one); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
100 |
|
task.setException(nse); |
101 |
|
try { |
102 |
|
Object x = task.get(); |
103 |
< |
shouldThrow("ExecutionException"); |
103 |
> |
shouldThrow(); |
104 |
|
} catch (ExecutionException success) { |
105 |
|
assertSame(success.getCause(), nse); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* Cancelling before running succeeds |
110 |
> |
* Cancelling before running succeeds |
111 |
|
*/ |
112 |
|
public void testCancelBeforeRun() { |
113 |
|
FutureTask task = new FutureTask(new NoOpCallable()); |
114 |
|
assertTrue(task.cancel(false)); |
115 |
< |
task.run(); |
116 |
< |
assertTrue(task.isDone()); |
117 |
< |
assertTrue(task.isCancelled()); |
115 |
> |
task.run(); |
116 |
> |
assertTrue(task.isDone()); |
117 |
> |
assertTrue(task.isCancelled()); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
123 |
|
public void testCancelBeforeRun2() { |
124 |
|
FutureTask task = new FutureTask(new NoOpCallable()); |
125 |
|
assertTrue(task.cancel(true)); |
126 |
< |
task.run(); |
127 |
< |
assertTrue(task.isDone()); |
128 |
< |
assertTrue(task.isCancelled()); |
126 |
> |
task.run(); |
127 |
> |
assertTrue(task.isDone()); |
128 |
> |
assertTrue(task.isCancelled()); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
133 |
|
*/ |
134 |
|
public void testCancelAfterRun() { |
135 |
|
FutureTask task = new FutureTask(new NoOpCallable()); |
136 |
< |
task.run(); |
136 |
> |
task.run(); |
137 |
|
assertFalse(task.cancel(false)); |
138 |
< |
assertTrue(task.isDone()); |
139 |
< |
assertFalse(task.isCancelled()); |
138 |
> |
assertTrue(task.isDone()); |
139 |
> |
assertFalse(task.isCancelled()); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
|
* cancel(true) interrupts a running task |
144 |
|
*/ |
145 |
|
public void testCancelInterrupt() throws InterruptedException { |
146 |
< |
final FutureTask task = |
146 |
> |
final FutureTask task = |
147 |
|
new FutureTask(new CheckedInterruptedCallable<Object>() { |
148 |
< |
public Object realCall() throws InterruptedException { |
148 |
> |
public Object realCall() throws InterruptedException { |
149 |
|
Thread.sleep(SMALL_DELAY_MS); |
150 |
< |
return Boolean.TRUE; |
150 |
> |
return Boolean.TRUE; |
151 |
|
}}); |
152 |
|
|
153 |
|
Thread t = new Thread(task); |
164 |
|
* cancel(false) does not interrupt a running task |
165 |
|
*/ |
166 |
|
public void testCancelNoInterrupt() throws InterruptedException { |
167 |
< |
final FutureTask task = |
167 |
> |
final FutureTask task = |
168 |
|
new FutureTask(new CheckedCallable<Object>() { |
169 |
< |
public Object realCall() throws InterruptedException { |
169 |
> |
public Object realCall() throws InterruptedException { |
170 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
171 |
|
return Boolean.TRUE; |
172 |
|
}}); |
184 |
|
* set in one thread causes get in another thread to retrieve value |
185 |
|
*/ |
186 |
|
public void testGet1() throws InterruptedException { |
187 |
< |
final FutureTask ft = |
187 |
> |
final FutureTask ft = |
188 |
|
new FutureTask(new CheckedCallable<Object>() { |
189 |
< |
public Object realCall() throws InterruptedException { |
189 |
> |
public Object realCall() throws InterruptedException { |
190 |
|
return Boolean.TRUE; |
191 |
|
}}); |
192 |
< |
Thread t = new Thread(new CheckedRunnable() { |
192 |
> |
Thread t = new Thread(new CheckedRunnable() { |
193 |
|
public void realRun() throws Exception { |
194 |
|
assertSame(Boolean.TRUE, ft.get()); |
195 |
|
}}); |
208 |
|
* set in one thread causes timed get in another thread to retrieve value |
209 |
|
*/ |
210 |
|
public void testTimedGet1() throws InterruptedException { |
211 |
< |
final FutureTask ft = |
211 |
> |
final FutureTask ft = |
212 |
|
new FutureTask(new CheckedCallable<Object>() { |
213 |
< |
public Object realCall() throws InterruptedException { |
213 |
> |
public Object realCall() throws InterruptedException { |
214 |
|
return Boolean.TRUE; |
215 |
|
}}); |
216 |
< |
Thread t = new Thread(new CheckedRunnable() { |
216 |
> |
Thread t = new Thread(new CheckedRunnable() { |
217 |
|
public void realRun() throws Exception { |
218 |
|
assertSame(Boolean.TRUE, ft.get(SMALL_DELAY_MS, MILLISECONDS)); |
219 |
|
}}); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
< |
* Cancelling a task causes timed get in another thread to throw CancellationException |
232 |
> |
* Cancelling a task causes timed get in another thread to throw |
233 |
> |
* CancellationException |
234 |
|
*/ |
235 |
|
public void testTimedGet_Cancellation() throws InterruptedException { |
236 |
< |
final FutureTask ft = |
236 |
> |
final FutureTask ft = |
237 |
|
new FutureTask(new CheckedInterruptedCallable<Object>() { |
238 |
< |
public Object realCall() throws InterruptedException { |
238 |
> |
public Object realCall() throws InterruptedException { |
239 |
|
Thread.sleep(SMALL_DELAY_MS); |
240 |
< |
return Boolean.TRUE; |
240 |
> |
return Boolean.TRUE; |
241 |
|
}}); |
242 |
|
|
243 |
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* Cancelling a task causes get in another thread to throw CancellationException |
257 |
> |
* Cancelling a task causes get in another thread to throw |
258 |
> |
* CancellationException |
259 |
|
*/ |
260 |
|
public void testGet_Cancellation() throws InterruptedException { |
261 |
< |
final FutureTask ft = |
261 |
> |
final FutureTask ft = |
262 |
|
new FutureTask(new CheckedInterruptedCallable<Object>() { |
263 |
< |
public Object realCall() throws InterruptedException { |
263 |
> |
public Object realCall() throws InterruptedException { |
264 |
|
Thread.sleep(SMALL_DELAY_MS); |
265 |
< |
return Boolean.TRUE; |
265 |
> |
return Boolean.TRUE; |
266 |
|
}}); |
267 |
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
268 |
|
public void realRun() throws Exception { |
283 |
|
* A runtime exception in task causes get to throw ExecutionException |
284 |
|
*/ |
285 |
|
public void testGet_ExecutionException() throws InterruptedException { |
286 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
287 |
< |
public Object call() { |
286 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
287 |
> |
public Object call() { |
288 |
|
return 5/0; |
289 |
|
}}); |
290 |
|
|
291 |
|
ft.run(); |
292 |
|
try { |
293 |
< |
ft.get(); |
294 |
< |
shouldThrow("ExecutionException"); |
295 |
< |
} catch (ExecutionException success) { |
293 |
> |
ft.get(); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (ExecutionException success) { |
296 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
< |
* A runtime exception in task causes timed get to throw ExecutionException |
301 |
> |
* A runtime exception in task causes timed get to throw ExecutionException |
302 |
|
*/ |
303 |
|
public void testTimedGet_ExecutionException2() throws Exception { |
304 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
305 |
< |
public Object call() { |
304 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
305 |
> |
public Object call() { |
306 |
|
return 5/0; |
307 |
|
}}); |
308 |
|
|
309 |
|
ft.run(); |
310 |
< |
try { |
311 |
< |
ft.get(SHORT_DELAY_MS, MILLISECONDS); |
312 |
< |
shouldThrow("ExecutionException"); |
313 |
< |
} catch (ExecutionException success) { |
310 |
> |
try { |
311 |
> |
ft.get(SHORT_DELAY_MS, MILLISECONDS); |
312 |
> |
shouldThrow(); |
313 |
> |
} catch (ExecutionException success) { |
314 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
315 |
|
} |
316 |
|
} |
320 |
|
* Interrupting a waiting get causes it to throw InterruptedException |
321 |
|
*/ |
322 |
|
public void testGet_InterruptedException() throws InterruptedException { |
323 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
324 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
323 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
324 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
325 |
|
public void realRun() throws Exception { |
326 |
|
ft.get(); |
327 |
|
}}); |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
< |
* Interrupting a waiting timed get causes it to throw InterruptedException |
336 |
> |
* Interrupting a waiting timed get causes it to throw InterruptedException |
337 |
|
*/ |
338 |
|
public void testTimedGet_InterruptedException2() throws InterruptedException { |
339 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
340 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
339 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
340 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
341 |
|
public void realRun() throws Exception { |
342 |
|
ft.get(LONG_DELAY_MS,MILLISECONDS); |
343 |
|
}}); |
352 |
|
* A timed out timed get throws TimeoutException |
353 |
|
*/ |
354 |
|
public void testGet_TimeoutException() throws Exception { |
355 |
< |
try { |
355 |
> |
try { |
356 |
|
FutureTask ft = new FutureTask(new NoOpCallable()); |
357 |
< |
ft.get(1,MILLISECONDS); |
358 |
< |
shouldThrow("TimeoutException"); |
359 |
< |
} catch (TimeoutException success) {} |
357 |
> |
ft.get(1,MILLISECONDS); |
358 |
> |
shouldThrow(); |
359 |
> |
} catch (TimeoutException success) {} |
360 |
|
} |
361 |
|
|
362 |
|
} |