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 |
|
/** |
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 |
|
|
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 |
|
}}); |
232 |
|
* Cancelling a task causes timed get in another thread to throw CancellationException |
233 |
|
*/ |
234 |
|
public void testTimedGet_Cancellation() throws InterruptedException { |
235 |
< |
final FutureTask ft = |
235 |
> |
final FutureTask ft = |
236 |
|
new FutureTask(new CheckedInterruptedCallable<Object>() { |
237 |
< |
public Object realCall() throws InterruptedException { |
237 |
> |
public Object realCall() throws InterruptedException { |
238 |
|
Thread.sleep(SMALL_DELAY_MS); |
239 |
< |
return Boolean.TRUE; |
239 |
> |
return Boolean.TRUE; |
240 |
|
}}); |
241 |
|
|
242 |
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
256 |
|
* Cancelling a task causes get in another thread to throw CancellationException |
257 |
|
*/ |
258 |
|
public void testGet_Cancellation() throws InterruptedException { |
259 |
< |
final FutureTask ft = |
259 |
> |
final FutureTask ft = |
260 |
|
new FutureTask(new CheckedInterruptedCallable<Object>() { |
261 |
< |
public Object realCall() throws InterruptedException { |
261 |
> |
public Object realCall() throws InterruptedException { |
262 |
|
Thread.sleep(SMALL_DELAY_MS); |
263 |
< |
return Boolean.TRUE; |
263 |
> |
return Boolean.TRUE; |
264 |
|
}}); |
265 |
|
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
266 |
|
public void realRun() throws Exception { |
281 |
|
* A runtime exception in task causes get to throw ExecutionException |
282 |
|
*/ |
283 |
|
public void testGet_ExecutionException() throws InterruptedException { |
284 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
285 |
< |
public Object call() { |
284 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
285 |
> |
public Object call() { |
286 |
|
return 5/0; |
287 |
|
}}); |
288 |
|
|
289 |
|
ft.run(); |
290 |
|
try { |
291 |
< |
ft.get(); |
292 |
< |
shouldThrow("ExecutionException"); |
293 |
< |
} catch (ExecutionException success) { |
291 |
> |
ft.get(); |
292 |
> |
shouldThrow("ExecutionException"); |
293 |
> |
} catch (ExecutionException success) { |
294 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
295 |
|
} |
296 |
|
} |
299 |
|
* A runtime exception in task causes timed get to throw ExecutionException |
300 |
|
*/ |
301 |
|
public void testTimedGet_ExecutionException2() throws Exception { |
302 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
303 |
< |
public Object call() { |
302 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
303 |
> |
public Object call() { |
304 |
|
return 5/0; |
305 |
|
}}); |
306 |
|
|
307 |
|
ft.run(); |
308 |
< |
try { |
309 |
< |
ft.get(SHORT_DELAY_MS, MILLISECONDS); |
310 |
< |
shouldThrow("ExecutionException"); |
311 |
< |
} catch (ExecutionException success) { |
308 |
> |
try { |
309 |
> |
ft.get(SHORT_DELAY_MS, MILLISECONDS); |
310 |
> |
shouldThrow("ExecutionException"); |
311 |
> |
} catch (ExecutionException success) { |
312 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
313 |
|
} |
314 |
|
} |
318 |
|
* Interrupting a waiting get causes it to throw InterruptedException |
319 |
|
*/ |
320 |
|
public void testGet_InterruptedException() throws InterruptedException { |
321 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
322 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
321 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
322 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
323 |
|
public void realRun() throws Exception { |
324 |
|
ft.get(); |
325 |
|
}}); |
334 |
|
* Interrupting a waiting timed get causes it to throw InterruptedException |
335 |
|
*/ |
336 |
|
public void testTimedGet_InterruptedException2() throws InterruptedException { |
337 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
338 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
337 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
338 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
339 |
|
public void realRun() throws Exception { |
340 |
|
ft.get(LONG_DELAY_MS,MILLISECONDS); |
341 |
|
}}); |
350 |
|
* A timed out timed get throws TimeoutException |
351 |
|
*/ |
352 |
|
public void testGet_TimeoutException() throws Exception { |
353 |
< |
try { |
353 |
> |
try { |
354 |
|
FutureTask ft = new FutureTask(new NoOpCallable()); |
355 |
< |
ft.get(1,MILLISECONDS); |
356 |
< |
shouldThrow("TimeoutException"); |
357 |
< |
} catch (TimeoutException success) {} |
355 |
> |
ft.get(1,MILLISECONDS); |
356 |
> |
shouldThrow("TimeoutException"); |
357 |
> |
} catch (TimeoutException success) {} |
358 |
|
} |
359 |
|
|
360 |
|
} |