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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import java.math.BigInteger;
13  
14 < public class ExecutorsTest extends TestCase{
14 > public class ExecutorsTest extends JSR166TestCase{
15      
16      public static void main(String[] args) {
17          junit.textui.TestRunner.run (suite());  
# Line 21 | Line 22 | public class ExecutorsTest extends TestC
22          return new TestSuite(ExecutorsTest.class);
23      }
24  
24    private static long SHORT_DELAY_MS = 100;
25    private static long MEDIUM_DELAY_MS = 1000;
26    private static long LONG_DELAY_MS = 10000;
27
28    class SleepRun implements Runnable {
29        public void run() {
30            try{
31                Thread.sleep(MEDIUM_DELAY_MS);
32            } catch(InterruptedException e){
33                fail("unexpected exception");
34            }
35        }
36    }
37    
38
39    class SleepCall implements Callable {
40        public Object call(){
41            try{
42                Thread.sleep(MEDIUM_DELAY_MS);
43            }catch(InterruptedException e){
44                fail("unexpected exception");
45            }
46            return Boolean.TRUE;
47        }
48    }
49
50
51
25      /**
26 <     *  Test to verify execute(Executor, Runnable) will throw
26 >     *   execute(Executor, Runnable) will throw
27       *  RejectedExecutionException Attempting to execute a runnable on
28       *  a full ThreadPool will cause such an exception here, up to 5
29       *  runnables are attempted on a pool capable on handling one
30       *  until it throws an exception
31       */
32      public void testExecute1(){
33 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
33 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
34          try{
35              
36              for(int i = 0; i < 5; ++i){
37 <                Executors.execute(p, new SleepRun(), Boolean.TRUE);
37 >                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
38              }
39              fail("should throw");
40          } catch(RejectedExecutionException success){}
41 <        p.shutdownNow();
41 >        joinPool(p);
42      }
43  
44      /**
45 <     *  Test to verify execute(Executor, Callable) will throw
45 >     *   execute(Executor, Callable) will throw
46       *  RejectedExecutionException Attempting to execute a callable on
47       *  a full ThreadPool will cause such an exception here, up to 5
48       *  runnables are attempted on a pool capable on handling one
49       *  until it throws an exception
50       */
51      public void testExecute2(){
52 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
52 >         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
53          try{
54              for(int i = 0; i < 5; ++i) {
55 <                Executors.execute(p, new SleepCall());
55 >                Executors.execute(p, new SmallCallable());
56              }
57              fail("should throw");
58          }catch(RejectedExecutionException e){}
59 <        p.shutdownNow();
59 >        joinPool(p);
60      }
61  
62  
63      /**
64 <     *  Test to verify invoke(Executor, Runnable) throws InterruptedException
64 >     *   invoke(Executor, Runnable) throws InterruptedException
65       *  A single use of invoke starts that will wait long enough
66       *  for the invoking thread to be interrupted
67       */
68      public void testInvoke2(){
69 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
69 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
70          Thread t = new Thread(new Runnable() {
71                  public void run(){
72                      try{
# Line 120 | Line 93 | public class ExecutorsTest extends TestC
93          }catch(Exception e){
94              fail("unexpected exception");
95          }
96 <        p.shutdownNow();
96 >        joinPool(p);
97      }
98  
99      /**
100 <     *  Test to verify invoke(Executor, Runnable) will throw
100 >     *   invoke(Executor, Runnable) will throw
101       *  ExecutionException An ExecutionException occurs when the
102       *  underlying Runnable throws an exception, here the
103       *  DivideByZeroException will cause an ExecutionException
104       */
105      public void testInvoke3(){
106 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
106 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
107          try{
108              Runnable r = new Runnable(){
109                      public void run(){
# Line 147 | Line 120 | public class ExecutorsTest extends TestC
120          } catch(Exception e){
121              fail("should throw EE");
122          }
123 <        p.shutdownNow();
123 >        joinPool(p);
124      }
125  
126  
127  
128      /**
129 <     *  Test to verify invoke(Executor, Callable) throws
129 >     *   invoke(Executor, Callable) throws
130       *  InterruptedException A single use of invoke starts that will
131       *  wait long enough for the invoking thread to be interrupted
132       */
133      public void testInvoke5(){
134 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
134 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
135          
136          final Callable c = new Callable(){
137                  public Object call(){
138                      try{
139 <                        Executors.invoke(p, new SleepCall());
139 >                        Executors.invoke(p, new SmallCallable());
140                          fail("should throw");
141                      }catch(InterruptedException e){}
142                      catch(RejectedExecutionException e2){}
# Line 190 | Line 163 | public class ExecutorsTest extends TestC
163              fail("unexpected exception");
164          }
165          
166 <        p.shutdownNow();
166 >        joinPool(p);
167      }
168  
169      /**
170 <     *  Test to verify invoke(Executor, Callable) will throw ExecutionException
170 >     *   invoke(Executor, Callable) will throw ExecutionException
171       *  An ExecutionException occurs when the underlying Runnable throws
172       *  an exception, here the DivideByZeroException will cause an ExecutionException
173       */
174      public void testInvoke6(){
175 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
175 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
176  
177          try{
178              Callable c = new Callable(){
# Line 217 | Line 190 | public class ExecutorsTest extends TestC
190          }catch(RejectedExecutionException e){}
191          catch(InterruptedException e2){}
192          catch(ExecutionException e3){}
193 <        p.shutdownNow();
193 >        joinPool(p);
194      }
195  
196      public void testExecuteRunnable () {
# Line 310 | Line 283 | public class ExecutorsTest extends TestC
283          }
284      }
285  
286 +    /**
287 +     * Check that timeouts from execute will time out if they compute
288 +     * too long.
289 +     */
290 +
291 +    public void testTimedCallable() {
292 +        int N = 10000;
293 +        ExecutorService executor = Executors.newSingleThreadExecutor();
294 +        List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
295 +        try {
296 +            long startTime = System.currentTimeMillis();
297 +            
298 +            long i = 0;
299 +            while (tasks.size() < N) {
300 +                tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1));
301 +                i += 10;
302 +            }
303 +            
304 +            int iters = 0;
305 +            BigInteger sum = BigInteger.ZERO;
306 +            for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) {
307 +                try {
308 +                    ++iters;
309 +                    sum = sum.add(it.next().call());
310 +                }
311 +                catch (TimeoutException success) {
312 +                    assertTrue(iters > 0);
313 +                    return;
314 +                }
315 +                catch (Exception e) {
316 +                    fail("unexpected exception: " + e);
317 +                }
318 +            }
319 +            // if by chance we didn't ever time out, total time must be small
320 +            long elapsed = System.currentTimeMillis() - startTime;
321 +            assertTrue(elapsed < N);
322 +        }
323 +        finally {
324 +            joinPool(executor);
325 +        }
326 +    }
327 +
328 +    
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 +
369 +
370  
371   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines