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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.8 by dl, Mon Dec 22 00:48:55 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines