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.13 by jsr166, Mon Nov 16 04:57:10 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines