ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC

# Line 12 | Line 12 | import java.util.concurrent.*;
12   import java.math.BigInteger;
13  
14   public class ExecutorsTest extends JSR166TestCase{
15 <    
15 >  
16      public static void main(String[] args) {
17          junit.textui.TestRunner.run (suite());  
18      }
19    
20
19      public static Test suite() {
20          return new TestSuite(ExecutorsTest.class);
21      }
22  
23 +    private static final String TEST_STRING = "a test string";
24 +
25 +    private static class MyTask implements Runnable {
26 +        public void run() { completed = true; }
27 +        public boolean isCompleted() { return completed; }
28 +        public void reset() { completed = false; }
29 +        private boolean completed = false;
30 +    }
31 +
32 +    private static class StringTask implements Callable<String> {
33 +        public String call() { return TEST_STRING; }
34 +    }
35 +
36 +    static class DirectExecutor implements Executor {
37 +        public void execute(Runnable r) {
38 +            r.run();
39 +        }
40 +    }
41 +
42 +    static class TimedCallable<T> implements Callable<T> {
43 +        private final Executor exec;
44 +        private final Callable<T> func;
45 +        private final long msecs;
46 +        
47 +        TimedCallable(Executor exec, Callable<T> func, long msecs) {
48 +            this.exec = exec;
49 +            this.func = func;
50 +            this.msecs = msecs;
51 +        }
52 +        
53 +        public T call() throws Exception {
54 +            Future<T> ftask = Executors.execute(exec, func);
55 +            try {
56 +                return ftask.get(msecs, TimeUnit.MILLISECONDS);
57 +            } finally {
58 +                ftask.cancel(true);
59 +            }
60 +        }
61 +    }
62 +
63 +
64 +    private static class Fib implements Callable<BigInteger> {
65 +        private final BigInteger n;
66 +        Fib(long n) {
67 +            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
68 +            this.n = BigInteger.valueOf(n);
69 +        }
70 +        public BigInteger call() {
71 +            BigInteger f1 = BigInteger.ONE;
72 +            BigInteger f2 = f1;
73 +            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
74 +                BigInteger t = f1.add(f2);
75 +                f1 = f2;
76 +                f2 = t;
77 +            }
78 +            return f1;
79 +        }
80 +    };
81 +
82 +
83 +
84      /**
85       *   execute(Executor, Runnable) will throw
86       *  RejectedExecutionException Attempting to execute a runnable on
# Line 29 | Line 88 | public class ExecutorsTest extends JSR16
88       *  runnables are attempted on a pool capable on handling one
89       *  until it throws an exception
90       */
91 <    public void testExecute1(){
91 >    public void testExecute1() {
92          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
93 <        try{
93 >        try {
94              
95              for(int i = 0; i < 5; ++i){
96                  Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
97              }
98 <            fail("should throw");
98 >            shouldThrow();
99          } catch(RejectedExecutionException success){}
100          joinPool(p);
101      }
# Line 48 | Line 107 | public class ExecutorsTest extends JSR16
107       *  runnables are attempted on a pool capable on handling one
108       *  until it throws an exception
109       */
110 <    public void testExecute2(){
110 >    public void testExecute2() {
111           ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
112 <        try{
112 >        try {
113              for(int i = 0; i < 5; ++i) {
114                  Executors.execute(p, new SmallCallable());
115              }
116 <            fail("should throw");
117 <        }catch(RejectedExecutionException e){}
116 >            shouldThrow();
117 >        } catch(RejectedExecutionException e){}
118          joinPool(p);
119      }
120  
# Line 65 | Line 124 | public class ExecutorsTest extends JSR16
124       *  A single use of invoke starts that will wait long enough
125       *  for the invoking thread to be interrupted
126       */
127 <    public void testInvoke2(){
127 >    public void testInvoke2() {
128          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
129          Thread t = new Thread(new Runnable() {
130 <                public void run(){
131 <                    try{
132 <                        Executors.invoke(p,new Runnable(){
133 <                                public void run(){
134 <                                    try{
130 >                public void run() {
131 >                    try {
132 >                        Executors.invoke(p,new Runnable() {
133 >                                public void run() {
134 >                                    try {
135                                          Thread.sleep(MEDIUM_DELAY_MS);
136 <                                        fail("should throw");
137 <                                    }catch(InterruptedException e){
136 >                                        shouldThrow();
137 >                                    } catch(InterruptedException e){
138                                      }
139                                  }
140                              });
141                      } catch(InterruptedException success){
142                      } catch(Exception e) {
143 <                        fail("unexpected exception");
143 >                        unexpectedException();
144                      }
145                      
146                  }
147              });
148 <        try{
148 >        try {
149              t.start();
150              Thread.sleep(SHORT_DELAY_MS);
151              t.interrupt();
152 <        }catch(Exception e){
153 <            fail("unexpected exception");
152 >        } catch(Exception e){
153 >            unexpectedException();
154          }
155          joinPool(p);
156      }
# Line 102 | Line 161 | public class ExecutorsTest extends JSR16
161       *  underlying Runnable throws an exception, here the
162       *  DivideByZeroException will cause an ExecutionException
163       */
164 <    public void testInvoke3(){
164 >    public void testInvoke3() {
165          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
166 <        try{
167 <            Runnable r = new Runnable(){
168 <                    public void run(){
166 >        try {
167 >            Runnable r = new Runnable() {
168 >                    public void run() {
169                          int i = 5/0;
170                      }
171                  };
# Line 115 | Line 174 | public class ExecutorsTest extends JSR16
174                  Executors.invoke(p,r);
175              }
176              
177 <            fail("should throw");
177 >            shouldThrow();
178          } catch(ExecutionException success){
179          } catch(Exception e){
180 <            fail("should throw EE");
180 >            unexpectedException();
181          }
182          joinPool(p);
183      }
# Line 130 | Line 189 | public class ExecutorsTest extends JSR16
189       *  InterruptedException A single use of invoke starts that will
190       *  wait long enough for the invoking thread to be interrupted
191       */
192 <    public void testInvoke5(){
192 >    public void testInvoke5() {
193          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
194          
195 <        final Callable c = new Callable(){
196 <                public Object call(){
197 <                    try{
195 >        final Callable c = new Callable() {
196 >                public Object call() {
197 >                    try {
198                          Executors.invoke(p, new SmallCallable());
199 <                        fail("should throw");
200 <                    }catch(InterruptedException e){}
199 >                        shouldThrow();
200 >                    } catch(InterruptedException e){}
201                      catch(RejectedExecutionException e2){}
202                      catch(ExecutionException e3){}
203                      return Boolean.TRUE;
# Line 147 | Line 206 | public class ExecutorsTest extends JSR16
206  
207  
208          
209 <        Thread t = new Thread(new Runnable(){
210 <                public void run(){
211 <                    try{
209 >        Thread t = new Thread(new Runnable() {
210 >                public void run() {
211 >                    try {
212                          c.call();
213 <                    }catch(Exception e){}
213 >                    } catch(Exception e){}
214                  }
215            });
216 <        try{
216 >        try {
217              t.start();
218              Thread.sleep(SHORT_DELAY_MS);
219              t.interrupt();
220              t.join();
221 <        }catch(InterruptedException e){
222 <            fail("unexpected exception");
221 >        } catch(InterruptedException e){
222 >            unexpectedException();
223          }
224          
225          joinPool(p);
# Line 171 | Line 230 | public class ExecutorsTest extends JSR16
230       *  An ExecutionException occurs when the underlying Runnable throws
231       *  an exception, here the DivideByZeroException will cause an ExecutionException
232       */
233 <    public void testInvoke6(){
233 >    public void testInvoke6() {
234          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
235  
236 <        try{
237 <            Callable c = new Callable(){
238 <                    public Object call(){
236 >        try {
237 >            Callable c = new Callable() {
238 >                    public Object call() {
239                          int i = 5/0;
240                          return Boolean.TRUE;
241                      }
# Line 186 | Line 245 | public class ExecutorsTest extends JSR16
245                  Executors.invoke(p,c);
246              }
247              
248 <            fail("should throw");
249 <        }catch(RejectedExecutionException e){}
248 >            shouldThrow();
249 >        } catch(RejectedExecutionException e){}
250          catch(InterruptedException e2){}
251          catch(ExecutionException e3){}
252          joinPool(p);
253      }
254  
255 +    /**
256 +     *
257 +     */
258      public void testExecuteRunnable () {
259          try {
260              Executor e = new DirectExecutor();
261 <            Task task = new Task();
262 <
201 <            assertFalse("task should not be complete", task.isCompleted());
202 <
261 >            MyTask task = new MyTask();
262 >            assertFalse(task.isCompleted());
263              Future<String> future = Executors.execute(e, task, TEST_STRING);
264              String result = future.get();
265 <
266 <            assertTrue("task should be complete", task.isCompleted());
207 <            assertSame("should return test string", TEST_STRING, result);
265 >            assertTrue(task.isCompleted());
266 >            assertSame(TEST_STRING, result);
267          }
268          catch (ExecutionException ex) {
269 <            fail("Unexpected exception");
269 >            unexpectedException();
270          }
271          catch (InterruptedException ex) {
272 <            fail("Unexpected exception");
272 >            unexpectedException();
273          }
274      }
275  
276 +    /**
277 +     *
278 +     */
279      public void testInvokeRunnable () {
280          try {
281              Executor e = new DirectExecutor();
282 <            Task task = new Task();
283 <
222 <            assertFalse("task should not be complete", task.isCompleted());
223 <
282 >            MyTask task = new MyTask();
283 >            assertFalse(task.isCompleted());
284              Executors.invoke(e, task);
285 <
226 <            assertTrue("task should be complete", task.isCompleted());
285 >            assertTrue(task.isCompleted());
286          }
287          catch (ExecutionException ex) {
288 <            fail("Unexpected exception");
288 >            unexpectedException();
289          }
290          catch (InterruptedException ex) {
291 <            fail("Unexpected exception");
291 >            unexpectedException();
292          }
293      }
294  
295 +    /**
296 +     *
297 +     */
298      public void testExecuteCallable () {
299          try {
300              Executor e = new DirectExecutor();
301              Future<String> future = Executors.execute(e, new StringTask());
302              String result = future.get();
303 <
242 <            assertSame("should return test string", TEST_STRING, result);
303 >            assertSame(TEST_STRING, result);
304          }
305          catch (ExecutionException ex) {
306 <            fail("Unexpected exception");
306 >            unexpectedException();
307          }
308          catch (InterruptedException ex) {
309 <            fail("Unexpected exception");
309 >            unexpectedException();
310          }
311      }
312  
313 +    /**
314 +     *
315 +     */
316      public void testInvokeCallable () {
317          try {
318              Executor e = new DirectExecutor();
319              String result = Executors.invoke(e, new StringTask());
320  
321 <            assertSame("should return test string", TEST_STRING, result);
321 >            assertSame(TEST_STRING, result);
322          }
323          catch (ExecutionException ex) {
324 <            fail("Unexpected exception" );
324 >            unexpectedException();
325          }
326          catch (InterruptedException ex) {
327 <            fail("Unexpected exception");
264 <        }
265 <    }
266 <
267 <    private static final String TEST_STRING = "a test string";
268 <
269 <    private static class Task implements Runnable {
270 <        public void run() { completed = true; }
271 <        public boolean isCompleted() { return completed; }
272 <        public void reset() { completed = false; }
273 <        private boolean completed = false;
274 <    }
275 <
276 <    private static class StringTask implements Callable<String> {
277 <        public String call() { return TEST_STRING; }
278 <    }
279 <
280 <    static class DirectExecutor implements Executor {
281 <        public void execute(Runnable r) {
282 <            r.run();
327 >            unexpectedException();
328          }
329      }
330  
331      /**
332 <     * Check that timeouts from execute will time out if they compute
288 <     * too long.
332 >     *  timeouts from execute will time out if they compute too long.
333       */
290
334      public void testTimedCallable() {
335          int N = 10000;
336          ExecutorService executor = Executors.newSingleThreadExecutor();
# Line 313 | Line 356 | public class ExecutorsTest extends JSR16
356                      return;
357                  }
358                  catch (Exception e) {
359 <                    fail("unexpected exception: " + e);
359 >                    unexpectedException();
360                  }
361              }
362              // if by chance we didn't ever time out, total time must be small
# Line 326 | Line 369 | public class ExecutorsTest extends JSR16
369      }
370  
371      
329    static class TimedCallable<T> implements Callable<T> {
330        private final Executor exec;
331        private final Callable<T> func;
332        private final long msecs;
333        
334        TimedCallable(Executor exec, Callable<T> func, long msecs) {
335            this.exec = exec;
336            this.func = func;
337            this.msecs = msecs;
338        }
339        
340        public T call() throws Exception {
341            Future<T> ftask = Executors.execute(exec, func);
342            try {
343                return ftask.get(msecs, TimeUnit.MILLISECONDS);
344            } finally {
345                ftask.cancel(true);
346            }
347        }
348    }
349
350
351    private static class Fib implements Callable<BigInteger> {
352        private final BigInteger n;
353        Fib(long n) {
354            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
355            this.n = BigInteger.valueOf(n);
356        }
357        public BigInteger call() {
358            BigInteger f1 = BigInteger.ONE;
359            BigInteger f2 = f1;
360            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
361                BigInteger t = f1.add(f2);
362                f1 = f2;
363                f2 = t;
364            }
365            return f1;
366        }
367    };
368
372  
373  
374   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines