ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
Revision: 1.12
Committed: Mon Nov 2 20:28:31 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +18 -18 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.10 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 jsr166 1.12 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.concurrent.*;
11 dl 1.4 import java.util.*;
12 dl 1.1
13 dl 1.4 public class FutureTaskTest extends JSR166TestCase {
14 dl 1.1
15     public static void main(String[] args) {
16 jsr166 1.12 junit.textui.TestRunner.run (suite());
17 dl 1.1 }
18     public static Test suite() {
19     return new TestSuite(FutureTaskTest.class);
20     }
21    
22 dl 1.4 /**
23     * Subclass to expose protected methods
24     */
25 dl 1.7 static class PublicFutureTask extends FutureTask {
26     public PublicFutureTask(Callable r) { super(r); }
27 dl 1.9 public boolean runAndReset() { return super.runAndReset(); }
28 dl 1.4 public void set(Object x) { super.set(x); }
29     public void setException(Throwable t) { super.setException(t); }
30     }
31 dl 1.1
32 dl 1.5 /**
33 dl 1.6 * Creating a future with a null callable throws NPE
34 dl 1.5 */
35     public void testConstructor() {
36 dl 1.3 try {
37     FutureTask task = new FutureTask(null);
38 dl 1.5 shouldThrow();
39 dl 1.3 }
40     catch(NullPointerException success) {
41     }
42     }
43    
44 dl 1.5 /**
45 dl 1.6 * creating a future with null runnable fails
46 dl 1.5 */
47     public void testConstructor2() {
48 dl 1.3 try {
49     FutureTask task = new FutureTask(null, Boolean.TRUE);
50 dl 1.5 shouldThrow();
51 dl 1.3 }
52     catch(NullPointerException success) {
53     }
54     }
55    
56 dl 1.5 /**
57 dl 1.6 * isDone is true when a task completes
58 dl 1.5 */
59     public void testIsDone() {
60 dl 1.4 FutureTask task = new FutureTask( new NoOpCallable());
61     task.run();
62     assertTrue(task.isDone());
63     assertFalse(task.isCancelled());
64     }
65    
66 dl 1.5 /**
67 dl 1.9 * runAndReset of a non-cancelled task succeeds
68 dl 1.5 */
69 dl 1.9 public void testRunAndReset() {
70 dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
71 dl 1.9 assertTrue(task.runAndReset());
72 dl 1.6 assertFalse(task.isDone());
73 dl 1.4 }
74    
75 dl 1.5 /**
76 dl 1.9 * runAndReset after cancellation fails
77 dl 1.5 */
78 dl 1.4 public void testResetAfterCancel() {
79 dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
80 dl 1.4 assertTrue(task.cancel(false));
81 dl 1.9 assertFalse(task.runAndReset());
82 dl 1.1 assertTrue(task.isDone());
83 dl 1.4 assertTrue(task.isCancelled());
84     }
85    
86 dl 1.1
87 dl 1.4
88 dl 1.5 /**
89 dl 1.11 * setting value causes get to return it
90 dl 1.5 */
91 dl 1.4 public void testSet() {
92 dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
93 dl 1.4 task.set(one);
94     try {
95     assertEquals(task.get(), one);
96     }
97     catch(Exception e) {
98 dl 1.5 unexpectedException();
99 dl 1.4 }
100     }
101    
102 dl 1.5 /**
103 dl 1.6 * setException causes get to throw ExecutionException
104 dl 1.5 */
105 dl 1.4 public void testSetException() {
106     Exception nse = new NoSuchElementException();
107 dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
108 dl 1.4 task.setException(nse);
109     try {
110     Object x = task.get();
111 dl 1.5 shouldThrow();
112 dl 1.4 }
113     catch(ExecutionException ee) {
114     Throwable cause = ee.getCause();
115     assertEquals(cause, nse);
116     }
117     catch(Exception e) {
118 dl 1.5 unexpectedException();
119 dl 1.4 }
120     }
121    
122 dl 1.5 /**
123 dl 1.6 * Cancelling before running succeeds
124 dl 1.5 */
125 dl 1.1 public void testCancelBeforeRun() {
126 dl 1.4 FutureTask task = new FutureTask( new NoOpCallable());
127 dl 1.1 assertTrue(task.cancel(false));
128     task.run();
129     assertTrue(task.isDone());
130     assertTrue(task.isCancelled());
131     }
132    
133 dl 1.5 /**
134 dl 1.6 * Cancel(true) before run succeeds
135 dl 1.5 */
136 dl 1.1 public void testCancelBeforeRun2() {
137 dl 1.4 FutureTask task = new FutureTask( new NoOpCallable());
138 dl 1.1 assertTrue(task.cancel(true));
139     task.run();
140     assertTrue(task.isDone());
141     assertTrue(task.isCancelled());
142     }
143    
144 dl 1.5 /**
145 dl 1.6 * cancel of a completed task fails
146 dl 1.5 */
147 dl 1.1 public void testCancelAfterRun() {
148 dl 1.4 FutureTask task = new FutureTask( new NoOpCallable());
149 dl 1.1 task.run();
150     assertFalse(task.cancel(false));
151     assertTrue(task.isDone());
152     assertFalse(task.isCancelled());
153     }
154    
155 dl 1.5 /**
156 dl 1.6 * cancel(true) interrupts a running task
157 dl 1.5 */
158     public void testCancelInterrupt() {
159 dl 1.1 FutureTask task = new FutureTask( new Callable() {
160     public Object call() {
161     try {
162 dl 1.4 Thread.sleep(MEDIUM_DELAY_MS);
163 dl 1.5 threadShouldThrow();
164 dl 1.1 }
165     catch (InterruptedException success) {}
166     return Boolean.TRUE;
167     } });
168     Thread t = new Thread(task);
169     t.start();
170 jsr166 1.12
171 dl 1.5 try {
172 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
173     assertTrue(task.cancel(true));
174     t.join();
175     assertTrue(task.isDone());
176     assertTrue(task.isCancelled());
177     } catch(InterruptedException e){
178 dl 1.5 unexpectedException();
179 dl 1.1 }
180     }
181    
182    
183 dl 1.5 /**
184 dl 1.6 * cancel(false) does not interrupt a running task
185 dl 1.5 */
186     public void testCancelNoInterrupt() {
187 dl 1.1 FutureTask task = new FutureTask( new Callable() {
188     public Object call() {
189     try {
190 dl 1.4 Thread.sleep(MEDIUM_DELAY_MS);
191 dl 1.1 }
192     catch (InterruptedException success) {
193 dl 1.4 threadFail("should not interrupt");
194 dl 1.1 }
195     return Boolean.TRUE;
196     } });
197     Thread t = new Thread(task);
198     t.start();
199 jsr166 1.12
200 dl 1.5 try {
201 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
202     assertTrue(task.cancel(false));
203     t.join();
204     assertTrue(task.isDone());
205     assertTrue(task.isCancelled());
206     } catch(InterruptedException e){
207 dl 1.5 unexpectedException();
208 dl 1.1 }
209     }
210    
211 dl 1.5 /**
212 dl 1.6 * set in one thread causes get in another thread to retrieve value
213 dl 1.5 */
214 dl 1.1 public void testGet1() {
215 dl 1.5 final FutureTask ft = new FutureTask(new Callable() {
216     public Object call() {
217     try {
218 dl 1.1 Thread.sleep(MEDIUM_DELAY_MS);
219 dl 1.2 } catch(InterruptedException e){
220 dl 1.5 threadUnexpectedException();
221 dl 1.1 }
222     return Boolean.TRUE;
223     }
224     });
225 dl 1.5 Thread t = new Thread(new Runnable() {
226     public void run() {
227     try {
228 dl 1.1 ft.get();
229 dl 1.2 } catch(Exception e){
230 dl 1.5 threadUnexpectedException();
231 dl 1.1 }
232     }
233     });
234 dl 1.5 try {
235 dl 1.1 assertFalse(ft.isDone());
236     assertFalse(ft.isCancelled());
237     t.start();
238     Thread.sleep(SHORT_DELAY_MS);
239     ft.run();
240     t.join();
241     assertTrue(ft.isDone());
242     assertFalse(ft.isCancelled());
243     } catch(InterruptedException e){
244 dl 1.5 unexpectedException();
245 dl 1.1
246 jsr166 1.12 }
247 dl 1.1 }
248    
249 dl 1.5 /**
250 dl 1.6 * set in one thread causes timed get in another thread to retrieve value
251 dl 1.5 */
252 dl 1.1 public void testTimedGet1() {
253 dl 1.5 final FutureTask ft = new FutureTask(new Callable() {
254     public Object call() {
255     try {
256 dl 1.1 Thread.sleep(MEDIUM_DELAY_MS);
257 dl 1.2 } catch(InterruptedException e){
258 dl 1.5 threadUnexpectedException();
259 dl 1.1 }
260     return Boolean.TRUE;
261     }
262     });
263 dl 1.5 Thread t = new Thread(new Runnable() {
264     public void run() {
265     try {
266 dl 1.1 ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
267     } catch(TimeoutException success) {
268     } catch(Exception e){
269 dl 1.5 threadUnexpectedException();
270 dl 1.1 }
271     }
272     });
273 dl 1.5 try {
274 dl 1.1 assertFalse(ft.isDone());
275     assertFalse(ft.isCancelled());
276     t.start();
277     ft.run();
278     t.join();
279     assertTrue(ft.isDone());
280     assertFalse(ft.isCancelled());
281     } catch(InterruptedException e){
282 dl 1.5 unexpectedException();
283 jsr166 1.12
284     }
285 dl 1.1 }
286    
287 dl 1.5 /**
288 dl 1.6 * Cancelling a task causes timed get in another thread to throw CancellationException
289 dl 1.5 */
290 dl 1.6 public void testTimedGet_Cancellation() {
291 dl 1.5 final FutureTask ft = new FutureTask(new Callable() {
292     public Object call() {
293     try {
294 dl 1.6 Thread.sleep(SMALL_DELAY_MS);
295     threadShouldThrow();
296     } catch(InterruptedException e) {
297 dl 1.1 }
298 dl 1.6 return Boolean.TRUE;
299 dl 1.1 }
300     });
301     try {
302 dl 1.6 Thread t1 = new Thread(new Runnable() {
303 dl 1.5 public void run() {
304     try {
305 dl 1.6 ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
306 dl 1.5 threadShouldThrow();
307 dl 1.6 } catch(CancellationException success) {}
308 dl 1.1 catch(Exception e){
309 dl 1.5 threadUnexpectedException();
310 dl 1.6 }
311 dl 1.1 }
312     });
313 dl 1.6 Thread t2 = new Thread(ft);
314 jsr166 1.12 t1.start();
315 dl 1.6 t2.start();
316     Thread.sleep(SHORT_DELAY_MS);
317 dl 1.1 ft.cancel(true);
318 dl 1.6 t1.join();
319     t2.join();
320     } catch(InterruptedException ie){
321 dl 1.5 unexpectedException();
322 dl 1.1 }
323     }
324 dl 1.6
325 dl 1.5 /**
326 dl 1.6 * Cancelling a task causes get in another thread to throw CancellationException
327 dl 1.5 */
328 dl 1.6 public void testGet_Cancellation() {
329 dl 1.5 final FutureTask ft = new FutureTask(new Callable() {
330     public Object call() {
331     try {
332 dl 1.6 Thread.sleep(MEDIUM_DELAY_MS);
333     threadShouldThrow();
334     } catch(InterruptedException e){
335 dl 1.1 }
336 dl 1.6 return Boolean.TRUE;
337 dl 1.1 }
338     });
339 dl 1.5 try {
340 dl 1.6 Thread t1 = new Thread(new Runnable() {
341 dl 1.5 public void run() {
342     try {
343 dl 1.6 ft.get();
344 dl 1.5 threadShouldThrow();
345 dl 1.6 } catch(CancellationException success){
346     }
347 dl 1.1 catch(Exception e){
348 dl 1.5 threadUnexpectedException();
349 dl 1.6 }
350 dl 1.1 }
351     });
352 dl 1.6 Thread t2 = new Thread(ft);
353 jsr166 1.12 t1.start();
354 dl 1.6 t2.start();
355     Thread.sleep(SHORT_DELAY_MS);
356 dl 1.1 ft.cancel(true);
357 dl 1.6 t1.join();
358     t2.join();
359     } catch(InterruptedException success){
360 dl 1.5 unexpectedException();
361 dl 1.1 }
362     }
363 jsr166 1.12
364 dl 1.1
365 dl 1.5 /**
366 dl 1.6 * A runtime exception in task causes get to throw ExecutionException
367 dl 1.5 */
368     public void testGet_ExecutionException() {
369     final FutureTask ft = new FutureTask(new Callable() {
370     public Object call() {
371 dl 1.1 int i = 5/0;
372     return Boolean.TRUE;
373     }
374     });
375 dl 1.5 try {
376 dl 1.1 ft.run();
377     ft.get();
378 dl 1.5 shouldThrow();
379 dl 1.2 } catch(ExecutionException success){
380 dl 1.1 }
381     catch(Exception e){
382 dl 1.5 unexpectedException();
383 dl 1.1 }
384     }
385 jsr166 1.12
386 dl 1.5 /**
387 dl 1.6 * A runtime exception in task causes timed get to throw ExecutionException
388 dl 1.5 */
389     public void testTimedGet_ExecutionException2() {
390     final FutureTask ft = new FutureTask(new Callable() {
391     public Object call() {
392 dl 1.1 int i = 5/0;
393     return Boolean.TRUE;
394     }
395     });
396 dl 1.5 try {
397 dl 1.1 ft.run();
398     ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
399 dl 1.5 shouldThrow();
400 jsr166 1.12 } catch(ExecutionException success) {
401 dl 1.2 } catch(TimeoutException success) { } // unlikely but OK
402 dl 1.1 catch(Exception e){
403 dl 1.5 unexpectedException();
404 dl 1.1 }
405     }
406 jsr166 1.12
407 dl 1.1
408 dl 1.5 /**
409 dl 1.6 * Interrupting a waiting get causes it to throw InterruptedException
410 dl 1.5 */
411     public void testGet_InterruptedException() {
412 dl 1.4 final FutureTask ft = new FutureTask(new NoOpCallable());
413 dl 1.5 Thread t = new Thread(new Runnable() {
414 jsr166 1.12 public void run() {
415 dl 1.5 try {
416 dl 1.1 ft.get();
417 dl 1.5 threadShouldThrow();
418 dl 1.1 } catch(InterruptedException success){
419     } catch(Exception e){
420 dl 1.5 threadUnexpectedException();
421 dl 1.1 }
422     }
423     });
424     try {
425     t.start();
426     Thread.sleep(SHORT_DELAY_MS);
427     t.interrupt();
428     t.join();
429     } catch(Exception e){
430 dl 1.5 unexpectedException();
431 dl 1.1 }
432     }
433    
434 dl 1.5 /**
435 dl 1.6 * Interrupting a waiting timed get causes it to throw InterruptedException
436 dl 1.5 */
437     public void testTimedGet_InterruptedException2() {
438 dl 1.4 final FutureTask ft = new FutureTask(new NoOpCallable());
439 dl 1.5 Thread t = new Thread(new Runnable() {
440 jsr166 1.12 public void run() {
441 dl 1.5 try {
442 dl 1.4 ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS);
443 dl 1.5 threadShouldThrow();
444 dl 1.2 } catch(InterruptedException success){}
445 dl 1.1 catch(Exception e){
446 dl 1.5 threadUnexpectedException();
447 dl 1.1 }
448     }
449     });
450     try {
451     t.start();
452     Thread.sleep(SHORT_DELAY_MS);
453     t.interrupt();
454     t.join();
455     } catch(Exception e){
456 dl 1.5 unexpectedException();
457 dl 1.1 }
458     }
459 jsr166 1.12
460 dl 1.5 /**
461 dl 1.6 * A timed out timed get throws TimeoutException
462 dl 1.5 */
463     public void testGet_TimeoutException() {
464     try {
465 dl 1.4 FutureTask ft = new FutureTask(new NoOpCallable());
466 dl 1.1 ft.get(1,TimeUnit.MILLISECONDS);
467 dl 1.5 shouldThrow();
468 dl 1.2 } catch(TimeoutException success){}
469     catch(Exception success){
470 dl 1.5 unexpectedException();
471 dl 1.1 }
472     }
473 jsr166 1.12
474 dl 1.1 }