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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines