8 |
|
|
9 |
import junit.framework.*; |
import junit.framework.*; |
10 |
import java.util.concurrent.*; |
import java.util.concurrent.*; |
11 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
import java.util.*; |
import java.util.*; |
13 |
|
|
14 |
public class FutureTaskTest extends JSR166TestCase { |
public class FutureTaskTest extends JSR166TestCase { |
36 |
public void testConstructor() { |
public void testConstructor() { |
37 |
try { |
try { |
38 |
FutureTask task = new FutureTask(null); |
FutureTask task = new FutureTask(null); |
39 |
shouldThrow(); |
shouldThrow("NullPointerException"); |
40 |
} |
} catch (NullPointerException success) {} |
|
catch(NullPointerException success) { |
|
|
} |
|
41 |
} |
} |
42 |
|
|
43 |
/** |
/** |
46 |
public void testConstructor2() { |
public void testConstructor2() { |
47 |
try { |
try { |
48 |
FutureTask task = new FutureTask(null, Boolean.TRUE); |
FutureTask task = new FutureTask(null, Boolean.TRUE); |
49 |
shouldThrow(); |
shouldThrow("NullPointerException"); |
50 |
} |
} catch (NullPointerException success) {} |
|
catch(NullPointerException success) { |
|
|
} |
|
51 |
} |
} |
52 |
|
|
53 |
/** |
/** |
83 |
|
|
84 |
|
|
85 |
/** |
/** |
86 |
* setting value gauses get to return it |
* setting value causes get to return it |
87 |
*/ |
*/ |
88 |
public void testSet() { |
public void testSet() throws Exception { |
89 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
90 |
task.set(one); |
task.set(one); |
|
try { |
|
91 |
assertEquals(task.get(), one); |
assertEquals(task.get(), one); |
92 |
} |
} |
|
catch(Exception e) { |
|
|
unexpectedException(); |
|
|
} |
|
|
} |
|
93 |
|
|
94 |
/** |
/** |
95 |
* setException causes get to throw ExecutionException |
* setException causes get to throw ExecutionException |
96 |
*/ |
*/ |
97 |
public void testSetException() { |
public void testSetException() throws Exception { |
98 |
Exception nse = new NoSuchElementException(); |
Exception nse = new NoSuchElementException(); |
99 |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
100 |
task.setException(nse); |
task.setException(nse); |
101 |
try { |
try { |
102 |
Object x = task.get(); |
Object x = task.get(); |
103 |
shouldThrow(); |
shouldThrow("ExecutionException"); |
104 |
} |
} catch (ExecutionException success) { |
105 |
catch(ExecutionException ee) { |
assertSame(success.getCause(), nse); |
|
Throwable cause = ee.getCause(); |
|
|
assertEquals(cause, nse); |
|
|
} |
|
|
catch(Exception e) { |
|
|
unexpectedException(); |
|
106 |
} |
} |
107 |
} |
} |
108 |
|
|
142 |
/** |
/** |
143 |
* cancel(true) interrupts a running task |
* cancel(true) interrupts a running task |
144 |
*/ |
*/ |
145 |
public void testCancelInterrupt() { |
public void testCancelInterrupt() throws InterruptedException { |
146 |
FutureTask task = new FutureTask( new Callable() { |
final FutureTask task = |
147 |
public Object call() { |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
148 |
try { |
public Object realCall() throws InterruptedException { |
149 |
Thread.sleep(MEDIUM_DELAY_MS); |
Thread.sleep(SMALL_DELAY_MS); |
|
threadShouldThrow(); |
|
|
} |
|
|
catch (InterruptedException success) {} |
|
150 |
return Boolean.TRUE; |
return Boolean.TRUE; |
151 |
} }); |
} }); |
152 |
|
|
153 |
Thread t = new Thread(task); |
Thread t = new Thread(task); |
154 |
t.start(); |
t.start(); |
|
|
|
|
try { |
|
155 |
Thread.sleep(SHORT_DELAY_MS); |
Thread.sleep(SHORT_DELAY_MS); |
156 |
assertTrue(task.cancel(true)); |
assertTrue(task.cancel(true)); |
157 |
t.join(); |
t.join(); |
158 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
159 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
|
} |
|
160 |
} |
} |
161 |
|
|
162 |
|
|
163 |
/** |
/** |
164 |
* cancel(false) does not interrupt a running task |
* cancel(false) does not interrupt a running task |
165 |
*/ |
*/ |
166 |
public void testCancelNoInterrupt() { |
public void testCancelNoInterrupt() throws InterruptedException { |
167 |
FutureTask task = new FutureTask( new Callable() { |
final FutureTask task = |
168 |
public Object call() { |
new FutureTask(new CheckedCallable<Object>() { |
169 |
try { |
public Object realCall() throws InterruptedException { |
170 |
Thread.sleep(MEDIUM_DELAY_MS); |
Thread.sleep(MEDIUM_DELAY_MS); |
|
} |
|
|
catch (InterruptedException success) { |
|
|
threadFail("should not interrupt"); |
|
|
} |
|
171 |
return Boolean.TRUE; |
return Boolean.TRUE; |
172 |
} }); |
} }); |
173 |
|
|
174 |
Thread t = new Thread(task); |
Thread t = new Thread(task); |
175 |
t.start(); |
t.start(); |
|
|
|
|
try { |
|
176 |
Thread.sleep(SHORT_DELAY_MS); |
Thread.sleep(SHORT_DELAY_MS); |
177 |
assertTrue(task.cancel(false)); |
assertTrue(task.cancel(false)); |
178 |
t.join(); |
t.join(); |
179 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
180 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
|
} |
|
181 |
} |
} |
182 |
|
|
183 |
/** |
/** |
184 |
* set in one thread causes get in another thread to retrieve value |
* set in one thread causes get in another thread to retrieve value |
185 |
*/ |
*/ |
186 |
public void testGet1() { |
public void testGet1() throws InterruptedException { |
187 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask ft = |
188 |
public Object call() { |
new FutureTask(new CheckedCallable<Object>() { |
189 |
try { |
public Object realCall() throws InterruptedException { |
|
Thread.sleep(MEDIUM_DELAY_MS); |
|
|
} catch(InterruptedException e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
190 |
return Boolean.TRUE; |
return Boolean.TRUE; |
191 |
} |
}}); |
192 |
}); |
Thread t = new Thread(new CheckedRunnable() { |
193 |
Thread t = new Thread(new Runnable() { |
public void realRun() throws Exception { |
194 |
public void run() { |
assertSame(Boolean.TRUE, ft.get()); |
195 |
try { |
}}); |
196 |
ft.get(); |
|
|
} catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
197 |
assertFalse(ft.isDone()); |
assertFalse(ft.isDone()); |
198 |
assertFalse(ft.isCancelled()); |
assertFalse(ft.isCancelled()); |
199 |
t.start(); |
t.start(); |
202 |
t.join(); |
t.join(); |
203 |
assertTrue(ft.isDone()); |
assertTrue(ft.isDone()); |
204 |
assertFalse(ft.isCancelled()); |
assertFalse(ft.isCancelled()); |
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
|
|
|
|
} |
|
205 |
} |
} |
206 |
|
|
207 |
/** |
/** |
208 |
* set in one thread causes timed get in another thread to retrieve value |
* set in one thread causes timed get in another thread to retrieve value |
209 |
*/ |
*/ |
210 |
public void testTimedGet1() { |
public void testTimedGet1() throws InterruptedException { |
211 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask ft = |
212 |
public Object call() { |
new FutureTask(new CheckedCallable<Object>() { |
213 |
try { |
public Object realCall() throws InterruptedException { |
|
Thread.sleep(MEDIUM_DELAY_MS); |
|
|
} catch(InterruptedException e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
214 |
return Boolean.TRUE; |
return Boolean.TRUE; |
215 |
} |
}}); |
216 |
}); |
Thread t = new Thread(new CheckedRunnable() { |
217 |
Thread t = new Thread(new Runnable() { |
public void realRun() throws Exception { |
218 |
public void run() { |
assertSame(Boolean.TRUE, ft.get(SMALL_DELAY_MS, MILLISECONDS)); |
219 |
try { |
}}); |
220 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
|
|
} catch(TimeoutException success) { |
|
|
} catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
221 |
assertFalse(ft.isDone()); |
assertFalse(ft.isDone()); |
222 |
assertFalse(ft.isCancelled()); |
assertFalse(ft.isCancelled()); |
223 |
t.start(); |
t.start(); |
224 |
|
Thread.sleep(SHORT_DELAY_MS); |
225 |
ft.run(); |
ft.run(); |
226 |
t.join(); |
t.join(); |
227 |
assertTrue(ft.isDone()); |
assertTrue(ft.isDone()); |
228 |
assertFalse(ft.isCancelled()); |
assertFalse(ft.isCancelled()); |
|
} catch(InterruptedException e){ |
|
|
unexpectedException(); |
|
|
|
|
|
} |
|
229 |
} |
} |
230 |
|
|
231 |
/** |
/** |
232 |
* Cancelling a task causes timed get in another thread to throw CancellationException |
* Cancelling a task causes timed get in another thread to throw CancellationException |
233 |
*/ |
*/ |
234 |
public void testTimedGet_Cancellation() { |
public void testTimedGet_Cancellation() throws InterruptedException { |
235 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask ft = |
236 |
public Object call() { |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
237 |
try { |
public Object realCall() throws InterruptedException { |
238 |
Thread.sleep(SMALL_DELAY_MS); |
Thread.sleep(SMALL_DELAY_MS); |
|
threadShouldThrow(); |
|
|
} catch(InterruptedException e) { |
|
|
} |
|
239 |
return Boolean.TRUE; |
return Boolean.TRUE; |
240 |
} |
}}); |
241 |
}); |
|
242 |
try { |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
243 |
Thread t1 = new Thread(new Runnable() { |
public void realRun() throws Exception { |
244 |
public void run() { |
ft.get(MEDIUM_DELAY_MS, MILLISECONDS); |
245 |
try { |
}}; |
|
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
|
|
threadShouldThrow(); |
|
|
} catch(CancellationException success) {} |
|
|
catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
246 |
Thread t2 = new Thread(ft); |
Thread t2 = new Thread(ft); |
247 |
t1.start(); |
t1.start(); |
248 |
t2.start(); |
t2.start(); |
250 |
ft.cancel(true); |
ft.cancel(true); |
251 |
t1.join(); |
t1.join(); |
252 |
t2.join(); |
t2.join(); |
|
} catch(InterruptedException ie){ |
|
|
unexpectedException(); |
|
|
} |
|
253 |
} |
} |
254 |
|
|
255 |
/** |
/** |
256 |
* Cancelling a task causes get in another thread to throw CancellationException |
* Cancelling a task causes get in another thread to throw CancellationException |
257 |
*/ |
*/ |
258 |
public void testGet_Cancellation() { |
public void testGet_Cancellation() throws InterruptedException { |
259 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask ft = |
260 |
public Object call() { |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
261 |
try { |
public Object realCall() throws InterruptedException { |
262 |
Thread.sleep(MEDIUM_DELAY_MS); |
Thread.sleep(SMALL_DELAY_MS); |
|
threadShouldThrow(); |
|
|
} catch(InterruptedException e){ |
|
|
} |
|
263 |
return Boolean.TRUE; |
return Boolean.TRUE; |
264 |
} |
}}); |
265 |
}); |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
266 |
try { |
public void realRun() throws Exception { |
|
Thread t1 = new Thread(new Runnable() { |
|
|
public void run() { |
|
|
try { |
|
267 |
ft.get(); |
ft.get(); |
268 |
threadShouldThrow(); |
}}; |
269 |
} catch(CancellationException success){ |
|
|
} |
|
|
catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
270 |
Thread t2 = new Thread(ft); |
Thread t2 = new Thread(ft); |
271 |
t1.start(); |
t1.start(); |
272 |
t2.start(); |
t2.start(); |
274 |
ft.cancel(true); |
ft.cancel(true); |
275 |
t1.join(); |
t1.join(); |
276 |
t2.join(); |
t2.join(); |
|
} catch(InterruptedException success){ |
|
|
unexpectedException(); |
|
|
} |
|
277 |
} |
} |
278 |
|
|
279 |
|
|
280 |
/** |
/** |
281 |
* A runtime exception in task causes get to throw ExecutionException |
* A runtime exception in task causes get to throw ExecutionException |
282 |
*/ |
*/ |
283 |
public void testGet_ExecutionException() { |
public void testGet_ExecutionException() throws InterruptedException { |
284 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask ft = new FutureTask(new Callable() { |
285 |
public Object call() { |
public Object call() { |
286 |
int i = 5/0; |
return 5/0; |
287 |
return Boolean.TRUE; |
}}); |
288 |
} |
|
|
}); |
|
|
try { |
|
289 |
ft.run(); |
ft.run(); |
290 |
|
try { |
291 |
ft.get(); |
ft.get(); |
292 |
shouldThrow(); |
shouldThrow("ExecutionException"); |
293 |
} catch(ExecutionException success){ |
} catch(ExecutionException success){ |
294 |
} |
assertTrue(success.getCause() instanceof ArithmeticException); |
|
catch(Exception e){ |
|
|
unexpectedException(); |
|
295 |
} |
} |
296 |
} |
} |
297 |
|
|
298 |
/** |
/** |
299 |
* A runtime exception in task causes timed get to throw ExecutionException |
* A runtime exception in task causes timed get to throw ExecutionException |
300 |
*/ |
*/ |
301 |
public void testTimedGet_ExecutionException2() { |
public void testTimedGet_ExecutionException2() throws Exception { |
302 |
final FutureTask ft = new FutureTask(new Callable() { |
final FutureTask ft = new FutureTask(new Callable() { |
303 |
public Object call() { |
public Object call() { |
304 |
int i = 5/0; |
return 5/0; |
305 |
return Boolean.TRUE; |
}}); |
306 |
} |
|
|
}); |
|
|
try { |
|
307 |
ft.run(); |
ft.run(); |
308 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
try { |
309 |
shouldThrow(); |
ft.get(SHORT_DELAY_MS, MILLISECONDS); |
310 |
|
shouldThrow("ExecutionException"); |
311 |
} catch(ExecutionException success) { |
} catch(ExecutionException success) { |
312 |
} catch(TimeoutException success) { } // unlikely but OK |
assertTrue(success.getCause() instanceof ArithmeticException); |
|
catch(Exception e){ |
|
|
unexpectedException(); |
|
313 |
} |
} |
314 |
} |
} |
315 |
|
|
317 |
/** |
/** |
318 |
* Interrupting a waiting get causes it to throw InterruptedException |
* Interrupting a waiting get causes it to throw InterruptedException |
319 |
*/ |
*/ |
320 |
public void testGet_InterruptedException() { |
public void testGet_InterruptedException() throws InterruptedException { |
321 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
322 |
Thread t = new Thread(new Runnable() { |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
323 |
public void run() { |
public void realRun() throws Exception { |
|
try { |
|
324 |
ft.get(); |
ft.get(); |
325 |
threadShouldThrow(); |
}}); |
326 |
} catch(InterruptedException success){ |
|
|
} catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
327 |
t.start(); |
t.start(); |
328 |
Thread.sleep(SHORT_DELAY_MS); |
Thread.sleep(SHORT_DELAY_MS); |
329 |
t.interrupt(); |
t.interrupt(); |
330 |
t.join(); |
t.join(); |
|
} catch(Exception e){ |
|
|
unexpectedException(); |
|
|
} |
|
331 |
} |
} |
332 |
|
|
333 |
/** |
/** |
334 |
* Interrupting a waiting timed get causes it to throw InterruptedException |
* Interrupting a waiting timed get causes it to throw InterruptedException |
335 |
*/ |
*/ |
336 |
public void testTimedGet_InterruptedException2() { |
public void testTimedGet_InterruptedException2() throws InterruptedException { |
337 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
338 |
Thread t = new Thread(new Runnable() { |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
339 |
public void run() { |
public void realRun() throws Exception { |
340 |
try { |
ft.get(LONG_DELAY_MS,MILLISECONDS); |
341 |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
}}); |
342 |
threadShouldThrow(); |
|
|
} catch(InterruptedException success){} |
|
|
catch(Exception e){ |
|
|
threadUnexpectedException(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
try { |
|
343 |
t.start(); |
t.start(); |
344 |
Thread.sleep(SHORT_DELAY_MS); |
Thread.sleep(SHORT_DELAY_MS); |
345 |
t.interrupt(); |
t.interrupt(); |
346 |
t.join(); |
t.join(); |
|
} catch(Exception e){ |
|
|
unexpectedException(); |
|
|
} |
|
347 |
} |
} |
348 |
|
|
349 |
/** |
/** |
350 |
* A timed out timed get throws TimeoutException |
* A timed out timed get throws TimeoutException |
351 |
*/ |
*/ |
352 |
public void testGet_TimeoutException() { |
public void testGet_TimeoutException() throws Exception { |
353 |
try { |
try { |
354 |
FutureTask ft = new FutureTask(new NoOpCallable()); |
FutureTask ft = new FutureTask(new NoOpCallable()); |
355 |
ft.get(1,TimeUnit.MILLISECONDS); |
ft.get(1,MILLISECONDS); |
356 |
shouldThrow(); |
shouldThrow("TimeoutException"); |
357 |
} catch(TimeoutException success){} |
} catch(TimeoutException success){} |
|
catch(Exception success){ |
|
|
unexpectedException(); |
|
|
} |
|
358 |
} |
} |
359 |
|
|
360 |
} |
} |