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.14 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.15 by jsr166, Fri Nov 20 00:58:01 2009 UTC

# Line 8 | Line 8
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 {
# Line 35 | Line 36 | public class FutureTaskTest extends JSR1
36      public void testConstructor() {
37          try {
38              FutureTask task = new FutureTask(null);
39 <            shouldThrow();
40 <        }
40 <        catch (NullPointerException success) {
41 <        }
39 >            shouldThrow("NullPointerException");
40 >        } catch (NullPointerException success) {}
41      }
42  
43      /**
# Line 47 | Line 46 | public class FutureTaskTest extends JSR1
46      public void testConstructor2() {
47          try {
48              FutureTask task = new FutureTask(null, Boolean.TRUE);
49 <            shouldThrow();
50 <        }
52 <        catch (NullPointerException success) {
53 <        }
49 >            shouldThrow("NullPointerException");
50 >        } catch (NullPointerException success) {}
51      }
52  
53      /**
54       * isDone is true when a task completes
55       */
56      public void testIsDone() {
57 <        FutureTask task = new FutureTask( new NoOpCallable());
57 >        FutureTask task = new FutureTask(new NoOpCallable());
58          task.run();
59          assertTrue(task.isDone());
60          assertFalse(task.isCancelled());
# Line 88 | Line 85 | public class FutureTaskTest extends JSR1
85      /**
86       * setting value causes get to return it
87       */
88 <    public void testSet() {
88 >    public void testSet() throws Exception {
89          PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
90          task.set(one);
91 <        try {
95 <            assertEquals(task.get(), one);
96 <        }
97 <        catch (Exception e) {
98 <            unexpectedException();
99 <        }
91 >        assertEquals(task.get(), one);
92      }
93  
94      /**
95       * setException causes get to throw ExecutionException
96       */
97 <    public void testSetException() {
97 >    public void testSetException() throws Exception {
98          Exception nse = new NoSuchElementException();
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) {
114 <            Throwable cause = ee.getCause();
115 <            assertEquals(cause, nse);
116 <        }
117 <        catch (Exception e) {
118 <            unexpectedException();
103 >            shouldThrow("ExecutionException");
104 >        } catch (ExecutionException success) {
105 >            assertSame(success.getCause(), nse);
106          }
107      }
108  
# Line 123 | Line 110 | public class FutureTaskTest extends JSR1
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());
# Line 134 | Line 121 | public class FutureTaskTest extends JSR1
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());
# Line 145 | Line 132 | public class FutureTaskTest extends JSR1
132       * cancel of a completed task fails
133       */
134      public void testCancelAfterRun() {
135 <        FutureTask task = new FutureTask( new NoOpCallable());
135 >        FutureTask task = new FutureTask(new NoOpCallable());
136          task.run();
137          assertFalse(task.cancel(false));
138          assertTrue(task.isDone());
# Line 155 | Line 142 | public class FutureTaskTest extends JSR1
142      /**
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);
150 <                        threadShouldThrow();
151 <                    }
165 <                    catch (InterruptedException success) {}
166 <                    return Boolean.TRUE;
167 <                } });
168 <        Thread t = new  Thread(task);
169 <        t.start();
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  
153 <        try {
154 <            Thread.sleep(SHORT_DELAY_MS);
155 <            assertTrue(task.cancel(true));
156 <            t.join();
157 <            assertTrue(task.isDone());
158 <            assertTrue(task.isCancelled());
159 <        } catch (InterruptedException e) {
178 <            unexpectedException();
179 <        }
153 >        Thread t = new Thread(task);
154 >        t.start();
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       * 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);
191 <                    }
192 <                    catch (InterruptedException success) {
193 <                        threadFail("should not interrupt");
194 <                    }
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 <                } });
197 <        Thread t = new  Thread(task);
198 <        t.start();
172 >                }});
173  
174 <        try {
175 <            Thread.sleep(SHORT_DELAY_MS);
176 <            assertTrue(task.cancel(false));
177 <            t.join();
178 <            assertTrue(task.isDone());
179 <            assertTrue(task.isCancelled());
180 <        } catch (InterruptedException e) {
207 <            unexpectedException();
208 <        }
174 >        Thread t = new Thread(task);
175 >        t.start();
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       * 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 {
218 <                        Thread.sleep(MEDIUM_DELAY_MS);
219 <                    } catch (InterruptedException e) {
220 <                        threadUnexpectedException();
221 <                    }
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 {
228 <                        ft.get();
229 <                    } catch (Exception e) {
230 <                        threadUnexpectedException();
231 <                    }
232 <                }
233 <            });
234 <        try {
235 <            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 <            unexpectedException();
191 >                }});
192 >        Thread t = new Thread(new CheckedRunnable() {
193 >            public void realRun() throws Exception {
194 >                assertSame(Boolean.TRUE, ft.get());
195 >            }});
196  
197 <        }
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       * set in one thread causes timed get in another thread to retrieve value
209       */
210 <    public void testTimedGet1() {
211 <        final FutureTask ft = new FutureTask(new Callable() {
212 <                public Object call() {
213 <                    try {
256 <                        Thread.sleep(MEDIUM_DELAY_MS);
257 <                    } catch (InterruptedException e) {
258 <                        threadUnexpectedException();
259 <                    }
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 {
266 <                        ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
267 <                    } catch (TimeoutException success) {
268 <                    } catch (Exception e) {
269 <                        threadUnexpectedException();
270 <                    }
271 <                }
272 <            });
273 <        try {
274 <            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 <            unexpectedException();
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 <        }
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() {
235 <        final FutureTask ft = new FutureTask(new Callable() {
236 <                public Object call() {
237 <                    try {
238 <                        Thread.sleep(SMALL_DELAY_MS);
295 <                        threadShouldThrow();
296 <                    } catch (InterruptedException e) {
297 <                    }
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 <        try {
243 <            Thread t1 = new Thread(new Runnable() {
244 <                    public void run() {
245 <                        try {
246 <                            ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
247 <                            threadShouldThrow();
248 <                        } catch (CancellationException success) {}
249 <                        catch (Exception e) {
250 <                            threadUnexpectedException();
251 <                        }
252 <                    }
312 <                });
313 <            Thread t2 = new Thread(ft);
314 <            t1.start();
315 <            t2.start();
316 <            Thread.sleep(SHORT_DELAY_MS);
317 <            ft.cancel(true);
318 <            t1.join();
319 <            t2.join();
320 <        } catch (InterruptedException ie) {
321 <            unexpectedException();
322 <        }
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       * Cancelling a task causes get in another thread to throw CancellationException
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);
263 <                        threadShouldThrow();
264 <                    } catch (InterruptedException e) {
265 <                    }
266 <                    return Boolean.TRUE;
267 <                }
268 <            });
269 <        try {
270 <            Thread t1 = new Thread(new Runnable() {
271 <                    public void run() {
272 <                        try {
273 <                            ft.get();
274 <                            threadShouldThrow();
275 <                        } catch (CancellationException success) {
276 <                        }
347 <                        catch (Exception e) {
348 <                            threadUnexpectedException();
349 <                        }
350 <                    }
351 <                });
352 <            Thread t2 = new Thread(ft);
353 <            t1.start();
354 <            t2.start();
355 <            Thread.sleep(SHORT_DELAY_MS);
356 <            ft.cancel(true);
357 <            t1.join();
358 <            t2.join();
359 <        } catch (InterruptedException success) {
360 <            unexpectedException();
361 <        }
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 >        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  
279  
280      /**
281       * A runtime exception in task causes get to throw ExecutionException
282       */
283 <    public void testGet_ExecutionException() {
283 >    public void testGet_ExecutionException() throws InterruptedException {
284          final FutureTask ft = new FutureTask(new Callable() {
285 <                public Object call() {
286 <                    int i = 5/0;
287 <                    return Boolean.TRUE;
288 <                }
289 <            });
290 <        try {
376 <            ft.run();
285 >            public Object call() {
286 >                return 5/0;
287 >            }});
288 >
289 >        ft.run();
290 >        try {
291              ft.get();
292 <            shouldThrow();
292 >            shouldThrow("ExecutionException");
293          } catch (ExecutionException success) {
294 +            assertTrue(success.getCause() instanceof ArithmeticException);
295          }
381        catch (Exception e) {
382            unexpectedException();
383        }
296      }
297  
298      /**
299       *  A runtime exception in task causes timed get to throw ExecutionException
300       */
301 <    public void testTimedGet_ExecutionException2() {
301 >    public void testTimedGet_ExecutionException2() throws Exception {
302          final FutureTask ft = new FutureTask(new Callable() {
303 <                public Object call() {
304 <                    int i = 5/0;
305 <                    return Boolean.TRUE;
306 <                }
307 <            });
303 >            public Object call() {
304 >                return 5/0;
305 >            }});
306 >
307 >        ft.run();
308          try {
309 <            ft.run();
310 <            ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
399 <            shouldThrow();
309 >            ft.get(SHORT_DELAY_MS, MILLISECONDS);
310 >            shouldThrow("ExecutionException");
311          } catch (ExecutionException success) {
312 <        } catch (TimeoutException success) { } // unlikely but OK
313 <        catch (Exception e) {
403 <            unexpectedException();
404 <        }
312 >            assertTrue(success.getCause() instanceof ArithmeticException);
313 >        }
314      }
315  
316  
317      /**
318       * Interrupting a waiting get causes it to throw InterruptedException
319       */
320 <    public void testGet_InterruptedException() {
320 >    public void testGet_InterruptedException() throws InterruptedException {
321          final FutureTask ft = new FutureTask(new NoOpCallable());
322 <        Thread t = new Thread(new Runnable() {
323 <                public void run() {
324 <                    try {
325 <                        ft.get();
326 <                        threadShouldThrow();
327 <                    } catch (InterruptedException success) {
328 <                    } catch (Exception e) {
329 <                        threadUnexpectedException();
330 <                    }
422 <                }
423 <            });
424 <        try {
425 <            t.start();
426 <            Thread.sleep(SHORT_DELAY_MS);
427 <            t.interrupt();
428 <            t.join();
429 <        } catch (Exception e) {
430 <            unexpectedException();
431 <        }
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() {
336 >    public void testTimedGet_InterruptedException2() throws InterruptedException {
337          final FutureTask ft = new FutureTask(new NoOpCallable());
338 <        Thread t = new Thread(new Runnable() {
339 <                public void run() {
340 <                    try {
341 <                        ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS);
342 <                        threadShouldThrow();
343 <                    } catch (InterruptedException success) {}
344 <                    catch (Exception e) {
345 <                        threadUnexpectedException();
346 <                    }
448 <                }
449 <            });
450 <        try {
451 <            t.start();
452 <            Thread.sleep(SHORT_DELAY_MS);
453 <            t.interrupt();
454 <            t.join();
455 <        } catch (Exception e) {
456 <            unexpectedException();
457 <        }
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  
349      /**
350       * A timed out timed get throws TimeoutException
351       */
352 <    public void testGet_TimeoutException() {
352 >    public void testGet_TimeoutException() throws Exception {
353          try {
354              FutureTask ft = new FutureTask(new NoOpCallable());
355 <            ft.get(1,TimeUnit.MILLISECONDS);
356 <            shouldThrow();
355 >            ft.get(1,MILLISECONDS);
356 >            shouldThrow("TimeoutException");
357          } catch (TimeoutException success) {}
469        catch (Exception success) {
470            unexpectedException();
471        }
358      }
359  
360   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines