1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.concurrent.*; |
10 |
import java.util.*; |
11 |
|
12 |
public class FutureTaskTest extends JSR166TestCase { |
13 |
|
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(FutureTaskTest.class); |
19 |
} |
20 |
|
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 { |
35 |
FutureTask task = new FutureTask(null); |
36 |
fail("should throw"); |
37 |
} |
38 |
catch(NullPointerException success) { |
39 |
} |
40 |
} |
41 |
|
42 |
public void testConstructor2(){ |
43 |
try { |
44 |
FutureTask task = new FutureTask(null, Boolean.TRUE); |
45 |
fail("should throw"); |
46 |
} |
47 |
catch(NullPointerException success) { |
48 |
} |
49 |
} |
50 |
|
51 |
public void testIsDone(){ |
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 NoOpCallable()); |
119 |
assertTrue(task.cancel(false)); |
120 |
task.run(); |
121 |
assertTrue(task.isDone()); |
122 |
assertTrue(task.isCancelled()); |
123 |
} |
124 |
|
125 |
public void testCancelBeforeRun2() { |
126 |
FutureTask task = new FutureTask( new NoOpCallable()); |
127 |
assertTrue(task.cancel(true)); |
128 |
task.run(); |
129 |
assertTrue(task.isDone()); |
130 |
assertTrue(task.isCancelled()); |
131 |
} |
132 |
|
133 |
public void testCancelAfterRun() { |
134 |
FutureTask task = new FutureTask( new NoOpCallable()); |
135 |
task.run(); |
136 |
assertFalse(task.cancel(false)); |
137 |
assertTrue(task.isDone()); |
138 |
assertFalse(task.isCancelled()); |
139 |
} |
140 |
|
141 |
public void testCancelInterrupt(){ |
142 |
FutureTask task = new FutureTask( new Callable() { |
143 |
public Object call() { |
144 |
try { |
145 |
Thread.sleep(MEDIUM_DELAY_MS); |
146 |
threadFail("should throw"); |
147 |
} |
148 |
catch (InterruptedException success) {} |
149 |
return Boolean.TRUE; |
150 |
} }); |
151 |
Thread t = new Thread(task); |
152 |
t.start(); |
153 |
|
154 |
try{ |
155 |
Thread.sleep(SHORT_DELAY_MS); |
156 |
assertTrue(task.cancel(true)); |
157 |
t.join(); |
158 |
assertTrue(task.isDone()); |
159 |
assertTrue(task.isCancelled()); |
160 |
} catch(InterruptedException e){ |
161 |
fail("unexpected exception"); |
162 |
} |
163 |
} |
164 |
|
165 |
|
166 |
public void testCancelNoInterrupt(){ |
167 |
FutureTask task = new FutureTask( new Callable() { |
168 |
public Object call() { |
169 |
try { |
170 |
Thread.sleep(MEDIUM_DELAY_MS); |
171 |
} |
172 |
catch (InterruptedException success) { |
173 |
threadFail("should not interrupt"); |
174 |
} |
175 |
return Boolean.TRUE; |
176 |
} }); |
177 |
Thread t = new Thread(task); |
178 |
t.start(); |
179 |
|
180 |
try{ |
181 |
Thread.sleep(SHORT_DELAY_MS); |
182 |
assertTrue(task.cancel(false)); |
183 |
t.join(); |
184 |
assertTrue(task.isDone()); |
185 |
assertTrue(task.isCancelled()); |
186 |
} catch(InterruptedException e){ |
187 |
fail("unexpected exception"); |
188 |
} |
189 |
} |
190 |
|
191 |
public void testGet1() { |
192 |
final FutureTask ft = new FutureTask(new Callable(){ |
193 |
public Object call(){ |
194 |
try{ |
195 |
Thread.sleep(MEDIUM_DELAY_MS); |
196 |
} catch(InterruptedException e){ |
197 |
threadFail("unexpected exception"); |
198 |
} |
199 |
return Boolean.TRUE; |
200 |
} |
201 |
}); |
202 |
Thread t = new Thread(new Runnable(){ |
203 |
public void run(){ |
204 |
try{ |
205 |
ft.get(); |
206 |
} catch(Exception e){ |
207 |
threadFail("unexpected exception"); |
208 |
} |
209 |
} |
210 |
}); |
211 |
try{ |
212 |
assertFalse(ft.isDone()); |
213 |
assertFalse(ft.isCancelled()); |
214 |
t.start(); |
215 |
Thread.sleep(SHORT_DELAY_MS); |
216 |
ft.run(); |
217 |
t.join(); |
218 |
assertTrue(ft.isDone()); |
219 |
assertFalse(ft.isCancelled()); |
220 |
} catch(InterruptedException e){ |
221 |
fail("unexpected exception"); |
222 |
|
223 |
} |
224 |
} |
225 |
|
226 |
public void testTimedGet1() { |
227 |
final FutureTask ft = new FutureTask(new Callable(){ |
228 |
public Object call(){ |
229 |
try{ |
230 |
Thread.sleep(MEDIUM_DELAY_MS); |
231 |
} catch(InterruptedException e){ |
232 |
threadFail("unexpected exception"); |
233 |
} |
234 |
return Boolean.TRUE; |
235 |
} |
236 |
}); |
237 |
Thread t = new Thread(new Runnable(){ |
238 |
public void run(){ |
239 |
try{ |
240 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
241 |
} catch(TimeoutException success) { |
242 |
} catch(Exception e){ |
243 |
threadFail("unexpected exception"); |
244 |
} |
245 |
} |
246 |
}); |
247 |
try{ |
248 |
assertFalse(ft.isDone()); |
249 |
assertFalse(ft.isCancelled()); |
250 |
t.start(); |
251 |
ft.run(); |
252 |
t.join(); |
253 |
assertTrue(ft.isDone()); |
254 |
assertFalse(ft.isCancelled()); |
255 |
} catch(InterruptedException e){ |
256 |
fail("unexpected exception"); |
257 |
|
258 |
} |
259 |
} |
260 |
|
261 |
|
262 |
public void testGet_Cancellation(){ |
263 |
final FutureTask ft = new FutureTask(new Callable(){ |
264 |
public Object call(){ |
265 |
try{ |
266 |
Thread.sleep(MEDIUM_DELAY_MS); |
267 |
} catch(InterruptedException e){ |
268 |
threadFail("unexpected exception"); |
269 |
} |
270 |
return Boolean.TRUE; |
271 |
} |
272 |
}); |
273 |
try { |
274 |
Thread.sleep(SHORT_DELAY_MS); |
275 |
Thread t = new Thread(new Runnable(){ |
276 |
public void run(){ |
277 |
try{ |
278 |
ft.get(); |
279 |
threadFail("should throw"); |
280 |
} catch(CancellationException success){ |
281 |
} |
282 |
catch(Exception e){ |
283 |
threadFail("unexpected exception"); |
284 |
} |
285 |
} |
286 |
}); |
287 |
t.start(); |
288 |
ft.cancel(true); |
289 |
t.join(); |
290 |
} catch(InterruptedException success){ |
291 |
fail("unexpected exception"); |
292 |
} |
293 |
} |
294 |
|
295 |
public void testGet_Cancellation2(){ |
296 |
final FutureTask ft = new FutureTask(new Callable(){ |
297 |
public Object call(){ |
298 |
try{ |
299 |
Thread.sleep(SHORT_DELAY_MS); |
300 |
} catch(InterruptedException e) { |
301 |
threadFail("unexpected exception"); |
302 |
} |
303 |
return Boolean.TRUE; |
304 |
} |
305 |
}); |
306 |
try{ |
307 |
Thread.sleep(SHORT_DELAY_MS); |
308 |
Thread t = new Thread(new Runnable(){ |
309 |
public void run(){ |
310 |
try{ |
311 |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
312 |
threadFail("should throw"); |
313 |
} catch(CancellationException success) {} |
314 |
catch(Exception e){ |
315 |
threadFail("unexpected exception"); |
316 |
} |
317 |
} |
318 |
}); |
319 |
t.start(); |
320 |
Thread.sleep(SHORT_DELAY_MS); |
321 |
ft.cancel(true); |
322 |
Thread.sleep(SHORT_DELAY_MS); |
323 |
t.join(); |
324 |
} catch(InterruptedException ie){ |
325 |
fail("unexpected exception"); |
326 |
} |
327 |
} |
328 |
|
329 |
public void testGet_ExecutionException(){ |
330 |
final FutureTask ft = new FutureTask(new Callable(){ |
331 |
public Object call(){ |
332 |
int i = 5/0; |
333 |
return Boolean.TRUE; |
334 |
} |
335 |
}); |
336 |
try{ |
337 |
ft.run(); |
338 |
ft.get(); |
339 |
fail("should throw"); |
340 |
} catch(ExecutionException success){ |
341 |
} |
342 |
catch(Exception e){ |
343 |
fail("unexpected exception"); |
344 |
} |
345 |
} |
346 |
|
347 |
public void testTimedGet_ExecutionException2(){ |
348 |
final FutureTask ft = new FutureTask(new Callable(){ |
349 |
public Object call(){ |
350 |
int i = 5/0; |
351 |
return Boolean.TRUE; |
352 |
} |
353 |
}); |
354 |
try{ |
355 |
ft.run(); |
356 |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
357 |
fail("should throw"); |
358 |
} catch(ExecutionException success) { |
359 |
} catch(TimeoutException success) { } // unlikely but OK |
360 |
catch(Exception e){ |
361 |
fail("unexpected exception"); |
362 |
} |
363 |
} |
364 |
|
365 |
|
366 |
public void testGet_InterruptedException(){ |
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 |
threadFail("should throw"); |
373 |
} catch(InterruptedException success){ |
374 |
} catch(Exception e){ |
375 |
threadFail("unexpected exception"); |
376 |
} |
377 |
} |
378 |
}); |
379 |
try { |
380 |
t.start(); |
381 |
Thread.sleep(SHORT_DELAY_MS); |
382 |
t.interrupt(); |
383 |
t.join(); |
384 |
} catch(Exception e){ |
385 |
fail("unexpected exception"); |
386 |
} |
387 |
} |
388 |
|
389 |
public void testTimedGet_InterruptedException2(){ |
390 |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
391 |
Thread t = new Thread(new Runnable(){ |
392 |
public void run(){ |
393 |
try{ |
394 |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
395 |
threadFail("should throw"); |
396 |
} catch(InterruptedException success){} |
397 |
catch(Exception e){ |
398 |
threadFail("unexpected exception"); |
399 |
} |
400 |
} |
401 |
}); |
402 |
try { |
403 |
t.start(); |
404 |
Thread.sleep(SHORT_DELAY_MS); |
405 |
t.interrupt(); |
406 |
t.join(); |
407 |
} catch(Exception e){ |
408 |
fail("unexpected exception"); |
409 |
} |
410 |
} |
411 |
|
412 |
public void testGet_TimeoutException(){ |
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 |
} |
421 |
} |
422 |
|
423 |
} |