ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
(Generate patch)

Comparing jsr166/src/test/tck/FutureTaskTest.java (file contents):
Revision 1.5 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.19 by jsr166, Wed Aug 25 00:07:03 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines