7 |
|
|
8 |
import junit.framework.*; |
import junit.framework.*; |
9 |
import java.util.concurrent.*; |
import java.util.concurrent.*; |
10 |
|
import java.util.*; |
11 |
|
|
12 |
public class FutureTaskTest extends TestCase { |
public class FutureTaskTest extends JSR166TestCase { |
13 |
|
|
14 |
public static void main(String[] args) { |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
junit.textui.TestRunner.run (suite()); |
18 |
return new TestSuite(FutureTaskTest.class); |
return new TestSuite(FutureTaskTest.class); |
19 |
} |
} |
20 |
|
|
21 |
private static long SHORT_DELAY_MS = 100; |
/** |
22 |
private static long MEDIUM_DELAY_MS = 1000; |
* Subclass to expose protected methods |
23 |
private static long LONG_DELAY_MS = 10000; |
*/ |
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(); } |
29 |
|
public void set(Object x) { super.set(x); } |
30 |
|
public void setException(Throwable t) { super.setException(t); } |
31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
|
* Creating a future with a null callable throws NPE |
35 |
|
*/ |
36 |
|
public void testConstructor() { |
37 |
|
try { |
38 |
|
FutureTask task = new FutureTask(null); |
39 |
|
shouldThrow(); |
40 |
|
} |
41 |
|
catch(NullPointerException success) { |
42 |
|
} |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
|
* creating a future with null runnable fails |
47 |
|
*/ |
48 |
|
public void testConstructor2() { |
49 |
|
try { |
50 |
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
51 |
|
shouldThrow(); |
52 |
|
} |
53 |
|
catch(NullPointerException success) { |
54 |
|
} |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* isDone is true when a task completes |
59 |
|
*/ |
60 |
public void testIsDone(){ |
public void testIsDone(){ |
61 |
FutureTask task = new FutureTask( new Callable() { |
FutureTask task = new FutureTask( new NoOpCallable()); |
|
public Object call() { return Boolean.TRUE; } }); |
|
62 |
task.run(); |
task.run(); |
63 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
64 |
assertFalse(task.isCancelled()); |
assertFalse(task.isCancelled()); |
65 |
} |
} |
66 |
|
|
67 |
|
/** |
68 |
|
* reset of a done task succeeds and changes status to not done |
69 |
|
*/ |
70 |
|
public void testReset() { |
71 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
72 |
|
task.run(); |
73 |
|
assertTrue(task.isDone()); |
74 |
|
assertTrue(task.reset()); |
75 |
|
assertFalse(task.isDone()); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
|
* Resetting after cancellation fails |
80 |
|
*/ |
81 |
|
public void testResetAfterCancel() { |
82 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
83 |
|
assertTrue(task.cancel(false)); |
84 |
|
task.run(); |
85 |
|
assertTrue(task.isDone()); |
86 |
|
assertTrue(task.isCancelled()); |
87 |
|
assertFalse(task.reset()); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
|
* setDone of new task causes isDone to be true |
92 |
|
*/ |
93 |
|
public void testSetDone() { |
94 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
95 |
|
task.setDone(); |
96 |
|
assertTrue(task.isDone()); |
97 |
|
assertFalse(task.isCancelled()); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
|
* setCancelled of a new task causes isCancelled to be true |
102 |
|
*/ |
103 |
|
public void testSetCancelled() { |
104 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
105 |
|
assertTrue(task.cancel(false)); |
106 |
|
task.setCancelled(); |
107 |
|
assertTrue(task.isDone()); |
108 |
|
assertTrue(task.isCancelled()); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* setting value gauses get to return it |
113 |
|
*/ |
114 |
|
public void testSet() { |
115 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
116 |
|
task.set(one); |
117 |
|
try { |
118 |
|
assertEquals(task.get(), one); |
119 |
|
} |
120 |
|
catch(Exception e) { |
121 |
|
unexpectedException(); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
|
* setException causes get to throw ExecutionException |
127 |
|
*/ |
128 |
|
public void testSetException() { |
129 |
|
Exception nse = new NoSuchElementException(); |
130 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
131 |
|
task.setException(nse); |
132 |
|
try { |
133 |
|
Object x = task.get(); |
134 |
|
shouldThrow(); |
135 |
|
} |
136 |
|
catch(ExecutionException ee) { |
137 |
|
Throwable cause = ee.getCause(); |
138 |
|
assertEquals(cause, nse); |
139 |
|
} |
140 |
|
catch(Exception e) { |
141 |
|
unexpectedException(); |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* Cancelling before running succeeds |
147 |
|
*/ |
148 |
public void testCancelBeforeRun() { |
public void testCancelBeforeRun() { |
149 |
FutureTask task = new FutureTask( new Callable() { |
FutureTask task = new FutureTask( new NoOpCallable()); |
|
public Object call() { return Boolean.TRUE; } }); |
|
150 |
assertTrue(task.cancel(false)); |
assertTrue(task.cancel(false)); |
151 |
task.run(); |
task.run(); |
152 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
153 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
154 |
} |
} |
155 |
|
|
156 |
|
/** |
157 |
|
* Cancel(true) before run succeeds |
158 |
|
*/ |
159 |
public void testCancelBeforeRun2() { |
public void testCancelBeforeRun2() { |
160 |
FutureTask task = new FutureTask( new Callable() { |
FutureTask task = new FutureTask( new NoOpCallable()); |
|
public Object call() { return Boolean.TRUE; } }); |
|
161 |
assertTrue(task.cancel(true)); |
assertTrue(task.cancel(true)); |
162 |
task.run(); |
task.run(); |
163 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
164 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
165 |
} |
} |
166 |
|
|
167 |
|
/** |
168 |
|
* cancel of a completed task fails |
169 |
|
*/ |
170 |
public void testCancelAfterRun() { |
public void testCancelAfterRun() { |
171 |
FutureTask task = new FutureTask( new Callable() { |
FutureTask task = new FutureTask( new NoOpCallable()); |
|
public Object call() { return Boolean.TRUE; } }); |
|
172 |
task.run(); |
task.run(); |
173 |
assertFalse(task.cancel(false)); |
assertFalse(task.cancel(false)); |
174 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
175 |
assertFalse(task.isCancelled()); |
assertFalse(task.isCancelled()); |
176 |
} |
} |
177 |
|
|
178 |
|
/** |
179 |
|
* cancel(true) interrupts a running task |
180 |
|
*/ |
181 |
public void testCancelInterrupt(){ |
public void testCancelInterrupt(){ |
182 |
FutureTask task = new FutureTask( new Callable() { |
FutureTask task = new FutureTask( new Callable() { |
183 |
public Object call() { |
public Object call() { |
184 |
try { |
try { |
185 |
Thread.sleep(SHORT_DELAY_MS* 2); |
Thread.sleep(MEDIUM_DELAY_MS); |
186 |
fail("should throw"); |
threadShouldThrow(); |
187 |
} |
} |
188 |
catch (InterruptedException success) {} |
catch (InterruptedException success) {} |
189 |
return Boolean.TRUE; |
return Boolean.TRUE; |
198 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
199 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
200 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
201 |
fail("unexpected exception"); |
unexpectedException(); |
202 |
} |
} |
203 |
} |
} |
204 |
|
|
205 |
|
|
206 |
|
/** |
207 |
|
* cancel(false) does not interrupt a running task |
208 |
|
*/ |
209 |
public void testCancelNoInterrupt(){ |
public void testCancelNoInterrupt(){ |
210 |
FutureTask task = new FutureTask( new Callable() { |
FutureTask task = new FutureTask( new Callable() { |
211 |
public Object call() { |
public Object call() { |
212 |
try { |
try { |
213 |
Thread.sleep(SHORT_DELAY_MS* 2); |
Thread.sleep(MEDIUM_DELAY_MS); |
214 |
} |
} |
215 |
catch (InterruptedException success) { |
catch (InterruptedException success) { |
216 |
fail("should not interrupt"); |
threadFail("should not interrupt"); |
217 |
} |
} |
218 |
return Boolean.TRUE; |
return Boolean.TRUE; |
219 |
} }); |
} }); |
227 |
assertTrue(task.isDone()); |
assertTrue(task.isDone()); |
228 |
assertTrue(task.isCancelled()); |
assertTrue(task.isCancelled()); |
229 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
230 |
fail("unexpected exception"); |
unexpectedException(); |
231 |
} |
} |
232 |
} |
} |
233 |
|
|
234 |
|
/** |
235 |
|
* set in one thread causes get in another thread to retrieve value |
236 |
|
*/ |
237 |
public void testGet1() { |
public void testGet1() { |
238 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new Callable(){ |
239 |
public Object call(){ |
public Object call(){ |
240 |
try{ |
try{ |
241 |
Thread.sleep(MEDIUM_DELAY_MS); |
Thread.sleep(MEDIUM_DELAY_MS); |
242 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
243 |
fail("unexpected exception"); |
threadUnexpectedException(); |
244 |
} |
} |
245 |
return Boolean.TRUE; |
return Boolean.TRUE; |
246 |
} |
} |
250 |
try{ |
try{ |
251 |
ft.get(); |
ft.get(); |
252 |
} catch(Exception e){ |
} catch(Exception e){ |
253 |
fail("unexpected exception"); |
threadUnexpectedException(); |
254 |
} |
} |
255 |
} |
} |
256 |
}); |
}); |
264 |
assertTrue(ft.isDone()); |
assertTrue(ft.isDone()); |
265 |
assertFalse(ft.isCancelled()); |
assertFalse(ft.isCancelled()); |
266 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
267 |
fail("unexpected exception"); |
unexpectedException(); |
268 |
|
|
269 |
} |
} |
270 |
} |
} |
271 |
|
|
272 |
|
/** |
273 |
|
* set in one thread causes timed get in another thread to retrieve value |
274 |
|
*/ |
275 |
public void testTimedGet1() { |
public void testTimedGet1() { |
276 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new Callable(){ |
277 |
public Object call(){ |
public Object call(){ |
278 |
try{ |
try{ |
279 |
Thread.sleep(MEDIUM_DELAY_MS); |
Thread.sleep(MEDIUM_DELAY_MS); |
280 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
281 |
fail("unexpected exception"); |
threadUnexpectedException(); |
282 |
} |
} |
283 |
return Boolean.TRUE; |
return Boolean.TRUE; |
284 |
} |
} |
289 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
290 |
} catch(TimeoutException success) { |
} catch(TimeoutException success) { |
291 |
} catch(Exception e){ |
} catch(Exception e){ |
292 |
fail("unexpected exception"); |
threadUnexpectedException(); |
293 |
} |
} |
294 |
} |
} |
295 |
}); |
}); |
302 |
assertTrue(ft.isDone()); |
assertTrue(ft.isDone()); |
303 |
assertFalse(ft.isCancelled()); |
assertFalse(ft.isCancelled()); |
304 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
305 |
fail("unexpected exception"); |
unexpectedException(); |
306 |
|
|
307 |
} |
} |
308 |
} |
} |
309 |
|
|
310 |
|
/** |
311 |
public void testGet_Cancellation(){ |
* Cancelling a task causes timed get in another thread to throw CancellationException |
312 |
|
*/ |
313 |
|
public void testTimedGet_Cancellation() { |
314 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new Callable(){ |
315 |
public Object call(){ |
public Object call(){ |
316 |
try{ |
try{ |
317 |
Thread.sleep(MEDIUM_DELAY_MS); |
Thread.sleep(SMALL_DELAY_MS); |
318 |
|
threadShouldThrow(); |
319 |
} catch(InterruptedException e){ |
} catch(InterruptedException e){ |
|
fail("unexpected exception"); |
|
320 |
} |
} |
321 |
return Boolean.TRUE; |
return Boolean.TRUE; |
322 |
} |
} |
323 |
}); |
}); |
324 |
try { |
try { |
325 |
Thread.sleep(SHORT_DELAY_MS); |
Thread t1 = new Thread(new Runnable() { |
|
Thread t = new Thread(new Runnable(){ |
|
326 |
public void run(){ |
public void run(){ |
327 |
try{ |
try{ |
328 |
ft.get(); |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
329 |
fail("should throw"); |
threadShouldThrow(); |
330 |
} catch(CancellationException success){ |
} catch(CancellationException success) {} |
|
} |
|
331 |
catch(Exception e){ |
catch(Exception e){ |
332 |
fail("unexpected exception"); |
threadUnexpectedException(); |
333 |
} |
} |
334 |
} |
} |
335 |
}); |
}); |
336 |
t.start(); |
Thread t2 = new Thread(ft); |
337 |
|
t1.start(); |
338 |
|
t2.start(); |
339 |
|
Thread.sleep(SHORT_DELAY_MS); |
340 |
ft.cancel(true); |
ft.cancel(true); |
341 |
t.join(); |
t1.join(); |
342 |
} catch(InterruptedException success){ |
t2.join(); |
343 |
fail("unexpected exception"); |
} catch(InterruptedException ie){ |
344 |
|
unexpectedException(); |
345 |
} |
} |
346 |
} |
} |
347 |
|
|
348 |
public void testGet_Cancellation2(){ |
/** |
349 |
|
* Cancelling a task causes get in another thread to throw CancellationException |
350 |
|
*/ |
351 |
|
public void testGet_Cancellation() { |
352 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new Callable(){ |
353 |
public Object call(){ |
public Object call(){ |
354 |
try{ |
try{ |
355 |
Thread.sleep(SHORT_DELAY_MS); |
Thread.sleep(MEDIUM_DELAY_MS); |
356 |
|
threadShouldThrow(); |
357 |
} catch(InterruptedException e) { |
} catch(InterruptedException e) { |
|
fail("unexpected exception"); |
|
358 |
} |
} |
359 |
return Boolean.TRUE; |
return Boolean.TRUE; |
360 |
} |
} |
361 |
}); |
}); |
362 |
try{ |
try{ |
363 |
Thread.sleep(100); |
Thread t1 = new Thread(new Runnable() { |
|
Thread t = new Thread(new Runnable(){ |
|
364 |
public void run(){ |
public void run(){ |
365 |
try{ |
try{ |
366 |
ft.get(3 * SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
ft.get(); |
367 |
fail("should throw"); |
threadShouldThrow(); |
368 |
} catch(CancellationException success) {} |
} catch(CancellationException success){ |
369 |
|
} |
370 |
catch(Exception e){ |
catch(Exception e){ |
371 |
fail("unexpected exception"); |
threadUnexpectedException(); |
372 |
} |
} |
373 |
} |
} |
374 |
}); |
}); |
375 |
t.start(); |
Thread t2 = new Thread(ft); |
376 |
|
t1.start(); |
377 |
|
t2.start(); |
378 |
Thread.sleep(SHORT_DELAY_MS); |
Thread.sleep(SHORT_DELAY_MS); |
379 |
ft.cancel(true); |
ft.cancel(true); |
380 |
Thread.sleep(SHORT_DELAY_MS); |
t1.join(); |
381 |
t.join(); |
t2.join(); |
382 |
} catch(InterruptedException ie){ |
} catch(InterruptedException success){ |
383 |
fail("unexpected exception"); |
unexpectedException(); |
384 |
} |
} |
385 |
} |
} |
386 |
|
|
387 |
|
|
388 |
|
/** |
389 |
|
* A runtime exception in task causes get to throw ExecutionException |
390 |
|
*/ |
391 |
public void testGet_ExecutionException(){ |
public void testGet_ExecutionException(){ |
392 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new Callable(){ |
393 |
public Object call(){ |
public Object call(){ |
398 |
try{ |
try{ |
399 |
ft.run(); |
ft.run(); |
400 |
ft.get(); |
ft.get(); |
401 |
fail("should throw"); |
shouldThrow(); |
402 |
} catch(ExecutionException success){ |
} catch(ExecutionException success){ |
403 |
} |
} |
404 |
catch(Exception e){ |
catch(Exception e){ |
405 |
fail("unexpected exception"); |
unexpectedException(); |
406 |
} |
} |
407 |
} |
} |
408 |
|
|
409 |
|
/** |
410 |
|
* A runtime exception in task causes timed get to throw ExecutionException |
411 |
|
*/ |
412 |
public void testTimedGet_ExecutionException2(){ |
public void testTimedGet_ExecutionException2(){ |
413 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new Callable(){ |
414 |
public Object call(){ |
public Object call(){ |
419 |
try{ |
try{ |
420 |
ft.run(); |
ft.run(); |
421 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
422 |
fail("should throw"); |
shouldThrow(); |
423 |
} catch(ExecutionException success) { |
} catch(ExecutionException success) { |
424 |
} catch(TimeoutException success) { } // unlikely but OK |
} catch(TimeoutException success) { } // unlikely but OK |
425 |
catch(Exception e){ |
catch(Exception e){ |
426 |
fail("unexpected exception"); |
unexpectedException(); |
427 |
} |
} |
428 |
} |
} |
429 |
|
|
430 |
|
|
431 |
|
/** |
432 |
|
* Interrupting a waiting get causes it to throw InterruptedException |
433 |
|
*/ |
434 |
public void testGet_InterruptedException(){ |
public void testGet_InterruptedException(){ |
435 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
|
public Object call(){ |
|
|
return new Object(); |
|
|
} |
|
|
}); |
|
436 |
Thread t = new Thread(new Runnable(){ |
Thread t = new Thread(new Runnable(){ |
437 |
public void run(){ |
public void run(){ |
438 |
try{ |
try{ |
439 |
ft.get(); |
ft.get(); |
440 |
fail("should throw"); |
threadShouldThrow(); |
441 |
} catch(InterruptedException success){ |
} catch(InterruptedException success){ |
442 |
} catch(Exception e){ |
} catch(Exception e){ |
443 |
fail("unexpected exception"); |
threadUnexpectedException(); |
444 |
} |
} |
445 |
} |
} |
446 |
}); |
}); |
450 |
t.interrupt(); |
t.interrupt(); |
451 |
t.join(); |
t.join(); |
452 |
} catch(Exception e){ |
} catch(Exception e){ |
453 |
fail("unexpected exception"); |
unexpectedException(); |
454 |
} |
} |
455 |
} |
} |
456 |
|
|
457 |
|
/** |
458 |
|
* Interrupting a waiting timed get causes it to throw InterruptedException |
459 |
|
*/ |
460 |
public void testTimedGet_InterruptedException2(){ |
public void testTimedGet_InterruptedException2(){ |
461 |
final FutureTask ft = new FutureTask(new Callable(){ |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
|
public Object call(){ |
|
|
return new Object(); |
|
|
} |
|
|
}); |
|
462 |
Thread t = new Thread(new Runnable(){ |
Thread t = new Thread(new Runnable(){ |
463 |
public void run(){ |
public void run(){ |
464 |
try{ |
try{ |
465 |
ft.get(100,TimeUnit.SECONDS); |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
466 |
fail("should throw"); |
threadShouldThrow(); |
467 |
} catch(InterruptedException success){} |
} catch(InterruptedException success){} |
468 |
catch(Exception e){ |
catch(Exception e){ |
469 |
fail("unexpected exception"); |
threadUnexpectedException(); |
470 |
} |
} |
471 |
} |
} |
472 |
}); |
}); |
476 |
t.interrupt(); |
t.interrupt(); |
477 |
t.join(); |
t.join(); |
478 |
} catch(Exception e){ |
} catch(Exception e){ |
479 |
fail("unexpected exception"); |
unexpectedException(); |
480 |
} |
} |
481 |
} |
} |
482 |
|
|
483 |
|
/** |
484 |
|
* A timed out timed get throws TimeoutException |
485 |
|
*/ |
486 |
public void testGet_TimeoutException(){ |
public void testGet_TimeoutException(){ |
|
FutureTask ft = new FutureTask(new Callable(){ |
|
|
public Object call(){ |
|
|
return new Object(); |
|
|
} |
|
|
}); |
|
487 |
try{ |
try{ |
488 |
|
FutureTask ft = new FutureTask(new NoOpCallable()); |
489 |
ft.get(1,TimeUnit.MILLISECONDS); |
ft.get(1,TimeUnit.MILLISECONDS); |
490 |
fail("should throw"); |
shouldThrow(); |
491 |
} catch(TimeoutException success){} |
} catch(TimeoutException success){} |
492 |
catch(Exception success){ |
catch(Exception success){ |
493 |
fail("unexpected exception"); |
unexpectedException(); |
494 |
} |
} |
|
|
|
|
|
|
495 |
} |
} |
496 |
|
|
497 |
} |
} |