7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import java.util.*; |
11 |
|
|
12 |
< |
public class FutureTaskTest extends TestCase { |
12 |
> |
public class FutureTaskTest extends JSR166TestCase { |
13 |
|
|
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
18 |
|
return new TestSuite(FutureTaskTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
private static long SHORT_DELAY_MS = 100; |
22 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
23 |
< |
private static long LONG_DELAY_MS = 10000; |
21 |
> |
/** |
22 |
> |
* Subclass to expose protected methods |
23 |
> |
*/ |
24 |
> |
static class MyFutureTask extends FutureTask { |
25 |
> |
public MyFutureTask(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 |
|
public void testConstructor(){ |
34 |
|
try { |
49 |
|
} |
50 |
|
|
51 |
|
public void testIsDone(){ |
52 |
< |
FutureTask task = new FutureTask( new Callable() { |
53 |
< |
public Object call() { return Boolean.TRUE; } }); |
52 |
> |
FutureTask task = new FutureTask( new NoOpCallable()); |
53 |
> |
task.run(); |
54 |
> |
assertTrue(task.isDone()); |
55 |
> |
assertFalse(task.isCancelled()); |
56 |
> |
} |
57 |
> |
|
58 |
> |
public void testReset(){ |
59 |
> |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
60 |
> |
task.run(); |
61 |
> |
assertTrue(task.isDone()); |
62 |
> |
assertTrue(task.reset()); |
63 |
> |
} |
64 |
> |
|
65 |
> |
public void testResetAfterCancel() { |
66 |
> |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
67 |
> |
assertTrue(task.cancel(false)); |
68 |
|
task.run(); |
69 |
|
assertTrue(task.isDone()); |
70 |
+ |
assertTrue(task.isCancelled()); |
71 |
+ |
assertFalse(task.reset()); |
72 |
+ |
} |
73 |
+ |
|
74 |
+ |
public void testSetDone() { |
75 |
+ |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
76 |
+ |
task.setDone(); |
77 |
+ |
assertTrue(task.isDone()); |
78 |
|
assertFalse(task.isCancelled()); |
79 |
|
} |
80 |
|
|
81 |
+ |
public void testSetCancelled() { |
82 |
+ |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
83 |
+ |
assertTrue(task.cancel(false)); |
84 |
+ |
task.setCancelled(); |
85 |
+ |
assertTrue(task.isDone()); |
86 |
+ |
assertTrue(task.isCancelled()); |
87 |
+ |
} |
88 |
+ |
|
89 |
+ |
public void testSet() { |
90 |
+ |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
91 |
+ |
task.set(one); |
92 |
+ |
try { |
93 |
+ |
assertEquals(task.get(), one); |
94 |
+ |
} |
95 |
+ |
catch(Exception e) { |
96 |
+ |
fail("unexpected exception"); |
97 |
+ |
} |
98 |
+ |
} |
99 |
+ |
|
100 |
+ |
public void testSetException() { |
101 |
+ |
Exception nse = new NoSuchElementException(); |
102 |
+ |
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
103 |
+ |
task.setException(nse); |
104 |
+ |
try { |
105 |
+ |
Object x = task.get(); |
106 |
+ |
fail("should throw"); |
107 |
+ |
} |
108 |
+ |
catch(ExecutionException ee) { |
109 |
+ |
Throwable cause = ee.getCause(); |
110 |
+ |
assertEquals(cause, nse); |
111 |
+ |
} |
112 |
+ |
catch(Exception e) { |
113 |
+ |
fail("unexpected exception"); |
114 |
+ |
} |
115 |
+ |
} |
116 |
+ |
|
117 |
|
public void testCancelBeforeRun() { |
118 |
< |
FutureTask task = new FutureTask( new Callable() { |
52 |
< |
public Object call() { return Boolean.TRUE; } }); |
118 |
> |
FutureTask task = new FutureTask( new NoOpCallable()); |
119 |
|
assertTrue(task.cancel(false)); |
120 |
|
task.run(); |
121 |
|
assertTrue(task.isDone()); |
123 |
|
} |
124 |
|
|
125 |
|
public void testCancelBeforeRun2() { |
126 |
< |
FutureTask task = new FutureTask( new Callable() { |
61 |
< |
public Object call() { return Boolean.TRUE; } }); |
126 |
> |
FutureTask task = new FutureTask( new NoOpCallable()); |
127 |
|
assertTrue(task.cancel(true)); |
128 |
|
task.run(); |
129 |
|
assertTrue(task.isDone()); |
131 |
|
} |
132 |
|
|
133 |
|
public void testCancelAfterRun() { |
134 |
< |
FutureTask task = new FutureTask( new Callable() { |
70 |
< |
public Object call() { return Boolean.TRUE; } }); |
134 |
> |
FutureTask task = new FutureTask( new NoOpCallable()); |
135 |
|
task.run(); |
136 |
|
assertFalse(task.cancel(false)); |
137 |
|
assertTrue(task.isDone()); |
142 |
|
FutureTask task = new FutureTask( new Callable() { |
143 |
|
public Object call() { |
144 |
|
try { |
145 |
< |
Thread.sleep(SHORT_DELAY_MS* 2); |
146 |
< |
fail("should throw"); |
145 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
146 |
> |
threadFail("should throw"); |
147 |
|
} |
148 |
|
catch (InterruptedException success) {} |
149 |
|
return Boolean.TRUE; |
167 |
|
FutureTask task = new FutureTask( new Callable() { |
168 |
|
public Object call() { |
169 |
|
try { |
170 |
< |
Thread.sleep(SHORT_DELAY_MS* 2); |
170 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
171 |
|
} |
172 |
|
catch (InterruptedException success) { |
173 |
< |
fail("should not interrupt"); |
173 |
> |
threadFail("should not interrupt"); |
174 |
|
} |
175 |
|
return Boolean.TRUE; |
176 |
|
} }); |
194 |
|
try{ |
195 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
196 |
|
} catch(InterruptedException e){ |
197 |
< |
fail("unexpected exception"); |
197 |
> |
threadFail("unexpected exception"); |
198 |
|
} |
199 |
|
return Boolean.TRUE; |
200 |
|
} |
204 |
|
try{ |
205 |
|
ft.get(); |
206 |
|
} catch(Exception e){ |
207 |
< |
fail("unexpected exception"); |
207 |
> |
threadFail("unexpected exception"); |
208 |
|
} |
209 |
|
} |
210 |
|
}); |
229 |
|
try{ |
230 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
231 |
|
} catch(InterruptedException e){ |
232 |
< |
fail("unexpected exception"); |
232 |
> |
threadFail("unexpected exception"); |
233 |
|
} |
234 |
|
return Boolean.TRUE; |
235 |
|
} |
240 |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
241 |
|
} catch(TimeoutException success) { |
242 |
|
} catch(Exception e){ |
243 |
< |
fail("unexpected exception"); |
243 |
> |
threadFail("unexpected exception"); |
244 |
|
} |
245 |
|
} |
246 |
|
}); |
265 |
|
try{ |
266 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
267 |
|
} catch(InterruptedException e){ |
268 |
< |
fail("unexpected exception"); |
268 |
> |
threadFail("unexpected exception"); |
269 |
|
} |
270 |
|
return Boolean.TRUE; |
271 |
|
} |
276 |
|
public void run(){ |
277 |
|
try{ |
278 |
|
ft.get(); |
279 |
< |
fail("should throw"); |
279 |
> |
threadFail("should throw"); |
280 |
|
} catch(CancellationException success){ |
281 |
|
} |
282 |
|
catch(Exception e){ |
283 |
< |
fail("unexpected exception"); |
283 |
> |
threadFail("unexpected exception"); |
284 |
|
} |
285 |
|
} |
286 |
|
}); |
298 |
|
try{ |
299 |
|
Thread.sleep(SHORT_DELAY_MS); |
300 |
|
} catch(InterruptedException e) { |
301 |
< |
fail("unexpected exception"); |
301 |
> |
threadFail("unexpected exception"); |
302 |
|
} |
303 |
|
return Boolean.TRUE; |
304 |
|
} |
305 |
|
}); |
306 |
|
try{ |
307 |
< |
Thread.sleep(100); |
307 |
> |
Thread.sleep(SHORT_DELAY_MS); |
308 |
|
Thread t = new Thread(new Runnable(){ |
309 |
|
public void run(){ |
310 |
|
try{ |
311 |
< |
ft.get(3 * SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
312 |
< |
fail("should throw"); |
311 |
> |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
312 |
> |
threadFail("should throw"); |
313 |
|
} catch(CancellationException success) {} |
314 |
|
catch(Exception e){ |
315 |
< |
fail("unexpected exception"); |
315 |
> |
threadFail("unexpected exception"); |
316 |
|
} |
317 |
|
} |
318 |
|
}); |
364 |
|
|
365 |
|
|
366 |
|
public void testGet_InterruptedException(){ |
367 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
304 |
< |
public Object call(){ |
305 |
< |
return new Object(); |
306 |
< |
} |
307 |
< |
}); |
367 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
368 |
|
Thread t = new Thread(new Runnable(){ |
369 |
|
public void run(){ |
370 |
|
try{ |
371 |
|
ft.get(); |
372 |
< |
fail("should throw"); |
372 |
> |
threadFail("should throw"); |
373 |
|
} catch(InterruptedException success){ |
374 |
|
} catch(Exception e){ |
375 |
< |
fail("unexpected exception"); |
375 |
> |
threadFail("unexpected exception"); |
376 |
|
} |
377 |
|
} |
378 |
|
}); |
387 |
|
} |
388 |
|
|
389 |
|
public void testTimedGet_InterruptedException2(){ |
390 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
331 |
< |
public Object call(){ |
332 |
< |
return new Object(); |
333 |
< |
} |
334 |
< |
}); |
390 |
> |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
391 |
|
Thread t = new Thread(new Runnable(){ |
392 |
|
public void run(){ |
393 |
|
try{ |
394 |
< |
ft.get(100,TimeUnit.SECONDS); |
395 |
< |
fail("should throw"); |
394 |
> |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
395 |
> |
threadFail("should throw"); |
396 |
|
} catch(InterruptedException success){} |
397 |
|
catch(Exception e){ |
398 |
< |
fail("unexpected exception"); |
398 |
> |
threadFail("unexpected exception"); |
399 |
|
} |
400 |
|
} |
401 |
|
}); |
410 |
|
} |
411 |
|
|
412 |
|
public void testGet_TimeoutException(){ |
357 |
– |
FutureTask ft = new FutureTask(new Callable(){ |
358 |
– |
public Object call(){ |
359 |
– |
return new Object(); |
360 |
– |
} |
361 |
– |
}); |
413 |
|
try{ |
414 |
+ |
FutureTask ft = new FutureTask(new NoOpCallable()); |
415 |
|
ft.get(1,TimeUnit.MILLISECONDS); |
416 |
|
fail("should throw"); |
417 |
|
} catch(TimeoutException success){} |
418 |
|
catch(Exception success){ |
419 |
|
fail("unexpected exception"); |
420 |
|
} |
369 |
– |
|
370 |
– |
|
421 |
|
} |
422 |
|
|
423 |
|
} |