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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines