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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines