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.6 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 30 | Line 30 | public class FutureTaskTest extends JSR1
30          public void setException(Throwable t) { super.setException(t); }
31      }
32  
33 <    public void testConstructor(){
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 <            fail("should throw");
39 >            shouldThrow();
40          }
41          catch(NullPointerException success) {
42          }
43      }
44  
45 <    public void testConstructor2(){
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 <            fail("should throw");
51 >            shouldThrow();
52          }
53          catch(NullPointerException success) {
54          }
55      }
56  
57 <    public void testIsDone(){
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 <    public void testReset(){
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());
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));
# Line 71 | Line 87 | public class FutureTaskTest extends JSR1
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();
# Line 78 | Line 97 | public class FutureTaskTest extends JSR1
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));
# Line 86 | Line 108 | public class FutureTaskTest extends JSR1
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);
# Line 93 | Line 118 | public class FutureTaskTest extends JSR1
118              assertEquals(task.get(), one);
119          }
120          catch(Exception e) {
121 <            fail("unexpected exception");
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 <            fail("should throw");
134 >            shouldThrow();
135          }
136          catch(ExecutionException ee) {
137              Throwable cause = ee.getCause();
138              assertEquals(cause, nse);
139          }
140          catch(Exception e) {
141 <            fail("unexpected exception");
141 >            unexpectedException();
142          }
143      }
144  
145 +    /**
146 +     *  Cancelling before running succeeds
147 +     */
148      public void testCancelBeforeRun() {
149          FutureTask task = new FutureTask( new NoOpCallable());
150          assertTrue(task.cancel(false));
# Line 122 | Line 153 | public class FutureTaskTest extends JSR1
153          assertTrue(task.isCancelled());
154      }
155  
156 +    /**
157 +     * Cancel(true) before run succeeds
158 +     */
159      public void testCancelBeforeRun2() {
160          FutureTask task = new FutureTask( new NoOpCallable());
161          assertTrue(task.cancel(true));
# Line 130 | Line 164 | public class FutureTaskTest extends JSR1
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 NoOpCallable());
172          task.run();
# Line 138 | Line 175 | public class FutureTaskTest extends JSR1
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(MEDIUM_DELAY_MS);
186 <                        threadFail("should throw");
186 >                        threadShouldThrow();
187                      }
188                      catch (InterruptedException success) {}
189                      return Boolean.TRUE;
# Line 151 | Line 191 | public class FutureTaskTest extends JSR1
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 {
# Line 177 | Line 220 | public class FutureTaskTest extends JSR1
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 <                        threadFail("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 <                        threadFail("unexpected exception");
253 >                        threadUnexpectedException();
254                      }
255                  }
256              });
257 <        try{
257 >        try {
258              assertFalse(ft.isDone());
259              assertFalse(ft.isCancelled());
260              t.start();
# Line 218 | Line 264 | public class FutureTaskTest extends JSR1
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 <                        threadFail("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 <                        threadFail("unexpected exception");
292 >                        threadUnexpectedException();
293                      }
294                  }
295              });
296 <        try{
296 >        try {
297              assertFalse(ft.isDone());
298              assertFalse(ft.isCancelled());
299              t.start();
# Line 253 | Line 302 | public class FutureTaskTest extends JSR1
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){
268                        threadFail("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(){
277 <                        try{
363 >            Thread t1 = new Thread(new Runnable() {
364 >                    public void run() {
365 >                        try {
366                              ft.get();
367 <                            threadFail("should throw");
367 >                            threadShouldThrow();
368                          } catch(CancellationException success){
369                          }
370                          catch(Exception e){
371 <                            threadFail("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      
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    }
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(){
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{
436 >        Thread t = new Thread(new Runnable() {
437 >                public void run() {                
438 >                    try {
439                          ft.get();
440 <                        threadFail("should throw");
440 >                        threadShouldThrow();
441                      } catch(InterruptedException success){
442                      } catch(Exception e){
443 <                        threadFail("unexpected exception");
443 >                        threadUnexpectedException();
444                      }
445                  }
446              });
# Line 382 | Line 450 | public class FutureTaskTest extends JSR1
450              t.interrupt();
451              t.join();
452          } catch(Exception e){
453 <            fail("unexpected exception");
453 >            unexpectedException();
454          }
455      }
456  
457 <    public void testTimedGet_InterruptedException2(){
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{
462 >        Thread t = new Thread(new Runnable() {
463 >                public void run() {                
464 >                    try {
465                          ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS);
466 <                        threadFail("should throw");
466 >                        threadShouldThrow();
467                      } catch(InterruptedException success){}
468                      catch(Exception e){
469 <                        threadFail("unexpected exception");
469 >                        threadUnexpectedException();
470                      }
471                  }
472              });
# Line 405 | Line 476 | public class FutureTaskTest extends JSR1
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 <        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          }
495      }
496      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines