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.4 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.9 by dl, Mon Dec 22 16:25:38 2003 UTC

# Line 21 | Line 21 | public class FutureTaskTest extends JSR1
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(); }
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 testConstructor(){
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 <            fail("should throw");
37 >            shouldThrow();
38          }
39          catch(NullPointerException success) {
40          }
41      }
42  
43 <    public void testConstructor2(){
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 <            fail("should throw");
49 >            shouldThrow();
50          }
51          catch(NullPointerException success) {
52          }
53      }
54  
55 <    public void testIsDone(){
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 <    public void testReset(){
66 <        MyFutureTask task = new MyFutureTask(new NoOpCallable());
67 <        task.run();
68 <        assertTrue(task.isDone());
69 <        assertTrue(task.reset());
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 <        MyFutureTask task = new MyFutureTask(new NoOpCallable());
78 >        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
79          assertTrue(task.cancel(false));
80 <        task.run();
80 >        assertFalse(task.runAndReset());
81          assertTrue(task.isDone());
82          assertTrue(task.isCancelled());
71        assertFalse(task.reset());
83      }
84  
74    public void testSetDone() {
75        MyFutureTask task = new MyFutureTask(new NoOpCallable());
76        task.setDone();
77        assertTrue(task.isDone());
78        assertFalse(task.isCancelled());
79    }
85  
81    public void testSetCancelled() {
82        MyFutureTask task = new MyFutureTask(new NoOpCallable());
83        assertTrue(task.cancel(false));
84        task.setCancelled();
85        assertTrue(task.isDone());
86        assertTrue(task.isCancelled());
87    }
86  
87 +    /**
88 +     * setting value gauses get to return it
89 +     */
90      public void testSet() {
91 <        MyFutureTask task = new MyFutureTask(new NoOpCallable());
91 >        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
92          task.set(one);
93          try {
94              assertEquals(task.get(), one);
95          }
96          catch(Exception e) {
97 <            fail("unexpected exception");
97 >            unexpectedException();
98          }
99      }
100  
101 +    /**
102 +     * setException causes get to throw ExecutionException
103 +     */
104      public void testSetException() {
105          Exception nse = new NoSuchElementException();
106 <        MyFutureTask task = new MyFutureTask(new NoOpCallable());
106 >        PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
107          task.setException(nse);
108          try {
109              Object x = task.get();
110 <            fail("should throw");
110 >            shouldThrow();
111          }
112          catch(ExecutionException ee) {
113              Throwable cause = ee.getCause();
114              assertEquals(cause, nse);
115          }
116          catch(Exception e) {
117 <            fail("unexpected exception");
117 >            unexpectedException();
118          }
119      }
120  
121 +    /**
122 +     *  Cancelling before running succeeds
123 +     */
124      public void testCancelBeforeRun() {
125          FutureTask task = new FutureTask( new NoOpCallable());
126          assertTrue(task.cancel(false));
# Line 122 | Line 129 | public class FutureTaskTest extends JSR1
129          assertTrue(task.isCancelled());
130      }
131  
132 +    /**
133 +     * Cancel(true) before run succeeds
134 +     */
135      public void testCancelBeforeRun2() {
136          FutureTask task = new FutureTask( new NoOpCallable());
137          assertTrue(task.cancel(true));
# Line 130 | Line 140 | public class FutureTaskTest extends JSR1
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 NoOpCallable());
148          task.run();
# Line 138 | Line 151 | public class FutureTaskTest extends JSR1
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(MEDIUM_DELAY_MS);
162 <                        threadFail("should throw");
162 >                        threadShouldThrow();
163                      }
164                      catch (InterruptedException success) {}
165                      return Boolean.TRUE;
# Line 151 | Line 167 | public class FutureTaskTest extends JSR1
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 {
# Line 177 | Line 196 | public class FutureTaskTest extends JSR1
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 <                        threadFail("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 <                        threadFail("unexpected exception");
229 >                        threadUnexpectedException();
230                      }
231                  }
232              });
233 <        try{
233 >        try {
234              assertFalse(ft.isDone());
235              assertFalse(ft.isCancelled());
236              t.start();
# Line 218 | Line 240 | public class FutureTaskTest extends JSR1
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 <                        threadFail("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 <                        threadFail("unexpected exception");
268 >                        threadUnexpectedException();
269                      }
270                  }
271              });
272 <        try{
272 >        try {
273              assertFalse(ft.isDone());
274              assertFalse(ft.isCancelled());
275              t.start();
# Line 253 | Line 278 | public class FutureTaskTest extends JSR1
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){
268                        threadFail("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(){
277 <                        try{
339 >            Thread t1 = new Thread(new Runnable() {
340 >                    public void run() {
341 >                        try {
342                              ft.get();
343 <                            threadFail("should throw");
343 >                            threadShouldThrow();
344                          } catch(CancellationException success){
345                          }
346                          catch(Exception e){
347 <                            threadFail("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      
295    public void testGet_Cancellation2(){
296        final FutureTask ft = new FutureTask(new Callable(){
297                public Object call(){
298                    try{
299                        Thread.sleep(SHORT_DELAY_MS);
300                    } catch(InterruptedException e) {
301                        threadFail("unexpected exception");
302                    }
303                    return Boolean.TRUE;
304                }
305            });
306        try{
307            Thread.sleep(SHORT_DELAY_MS);
308            Thread t = new Thread(new Runnable(){
309                    public void run(){
310                        try{
311                            ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
312                            threadFail("should throw");
313                        } catch(CancellationException success) {}
314                        catch(Exception e){
315                            threadFail("unexpected exception");
316                        }
317                    }
318                });
319            t.start();
320            Thread.sleep(SHORT_DELAY_MS);
321            ft.cancel(true);
322            Thread.sleep(SHORT_DELAY_MS);
323            t.join();
324        } catch(InterruptedException ie){
325            fail("unexpected exception");
326        }
327    }
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(){
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{
412 >        Thread t = new Thread(new Runnable() {
413 >                public void run() {                
414 >                    try {
415                          ft.get();
416 <                        threadFail("should throw");
416 >                        threadShouldThrow();
417                      } catch(InterruptedException success){
418                      } catch(Exception e){
419 <                        threadFail("unexpected exception");
419 >                        threadUnexpectedException();
420                      }
421                  }
422              });
# Line 382 | Line 426 | public class FutureTaskTest extends JSR1
426              t.interrupt();
427              t.join();
428          } catch(Exception e){
429 <            fail("unexpected exception");
429 >            unexpectedException();
430          }
431      }
432  
433 <    public void testTimedGet_InterruptedException2(){
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{
438 >        Thread t = new Thread(new Runnable() {
439 >                public void run() {                
440 >                    try {
441                          ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS);
442 <                        threadFail("should throw");
442 >                        threadShouldThrow();
443                      } catch(InterruptedException success){}
444                      catch(Exception e){
445 <                        threadFail("unexpected exception");
445 >                        threadUnexpectedException();
446                      }
447                  }
448              });
# Line 405 | Line 452 | public class FutureTaskTest extends JSR1
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 <        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          }
471      }
472      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines