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.5 by dl, Thu Sep 25 11:02:41 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 <    
14 > public class ExecutorsTest extends JSR166TestCase{
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
17      }
18    
19
18      public static Test suite() {
19          return new TestSuite(ExecutorsTest.class);
20      }
21  
22 <    private static long SHORT_DELAY_MS = 100;
23 <    private static long MEDIUM_DELAY_MS = 1000;
24 <    private static long LONG_DELAY_MS = 10000;
25 <
26 <    class SleepRun implements Runnable {
27 <        public void run() {
28 <            try{
29 <                Thread.sleep(MEDIUM_DELAY_MS);
30 <            } catch(InterruptedException e){
31 <                fail("unexpected exception");
22 >    private static final String TEST_STRING = "a test string";
23 >
24 >    private static class MyTask implements Runnable {
25 >        public void run() { completed = true; }
26 >        public boolean isCompleted() { return completed; }
27 >        public void reset() { completed = false; }
28 >        private boolean completed = false;
29 >    }
30 >
31 >    private static class StringTask implements Callable<String> {
32 >        public String call() { return TEST_STRING; }
33 >    }
34 >
35 >    static class DirectExecutor implements Executor {
36 >        public void execute(Runnable r) {
37 >            r.run();
38 >        }
39 >    }
40 >
41 >    static class TimedCallable<T> implements Callable<T> {
42 >        private final Executor exec;
43 >        private final Callable<T> func;
44 >        private final long msecs;
45 >        
46 >        TimedCallable(Executor exec, Callable<T> func, long msecs) {
47 >            this.exec = exec;
48 >            this.func = func;
49 >            this.msecs = msecs;
50 >        }
51 >        
52 >        public T call() throws Exception {
53 >            Future<T> ftask = Executors.execute(exec, func);
54 >            try {
55 >                return ftask.get(msecs, TimeUnit.MILLISECONDS);
56 >            } finally {
57 >                ftask.cancel(true);
58              }
59          }
60      }
37    
61  
62 <    class SleepCall implements Callable {
63 <        public Object call(){
64 <            try{
65 <                Thread.sleep(MEDIUM_DELAY_MS);
66 <            }catch(InterruptedException e){
67 <                fail("unexpected exception");
62 >
63 >    private static class Fib implements Callable<BigInteger> {
64 >        private final BigInteger n;
65 >        Fib(long n) {
66 >            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
67 >            this.n = BigInteger.valueOf(n);
68 >        }
69 >        public BigInteger call() {
70 >            BigInteger f1 = BigInteger.ONE;
71 >            BigInteger f2 = f1;
72 >            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
73 >                BigInteger t = f1.add(f2);
74 >                f1 = f2;
75 >                f2 = t;
76              }
77 <            return Boolean.TRUE;
77 >            return f1;
78 >        }
79 >    };
80 >
81 >    /**
82 >     * For use as ThreadFactory in constructors
83 >     */
84 >    static class MyThreadFactory implements ThreadFactory{
85 >        public Thread newThread(Runnable r){
86 >            return new Thread(r);
87 >        }  
88 >    }
89 >
90 >    /**
91 >     * A newCachedThreadPool can execute runnables
92 >     */
93 >    public void testNewCachedThreadPool1() {
94 >        ExecutorService e = Executors.newCachedThreadPool();
95 >        e.execute(new NoOpRunnable());
96 >        e.execute(new NoOpRunnable());
97 >        e.execute(new NoOpRunnable());
98 >        e.shutdown();
99 >    }
100 >
101 >    /**
102 >     * A newCachedThreadPool with given ThreadFactory can execute runnables
103 >     */
104 >    public void testNewCachedThreadPool2() {
105 >        ExecutorService e = Executors.newCachedThreadPool(new MyThreadFactory());
106 >        e.execute(new NoOpRunnable());
107 >        e.execute(new NoOpRunnable());
108 >        e.execute(new NoOpRunnable());
109 >        e.shutdown();
110 >    }
111 >
112 >    /**
113 >     * A newCachedThreadPool with null ThreadFactory throws NPE
114 >     */
115 >    public void testNewCachedThreadPool3() {
116 >        try {
117 >            ExecutorService e = Executors.newCachedThreadPool(null);
118 >            shouldThrow();
119 >        }
120 >        catch(NullPointerException success) {
121 >        }
122 >    }
123 >
124 >
125 >    /**
126 >     * A new SingleThreadExecutor can execute runnables
127 >     */
128 >    public void testNewSingleThreadExecutor1() {
129 >        ExecutorService e = Executors.newSingleThreadExecutor();
130 >        e.execute(new NoOpRunnable());
131 >        e.execute(new NoOpRunnable());
132 >        e.execute(new NoOpRunnable());
133 >        e.shutdown();
134 >    }
135 >
136 >    /**
137 >     * A new SingleThreadExecutor with given ThreadFactory can execute runnables
138 >     */
139 >    public void testNewSingleThreadExecutor2() {
140 >        ExecutorService e = Executors.newSingleThreadExecutor(new MyThreadFactory());
141 >        e.execute(new NoOpRunnable());
142 >        e.execute(new NoOpRunnable());
143 >        e.execute(new NoOpRunnable());
144 >        e.shutdown();
145 >    }
146 >
147 >    /**
148 >     * A new SingleThreadExecutor with null ThreadFactory throws NPE
149 >     */
150 >    public void testNewSingleThreadExecutor3() {
151 >        try {
152 >            ExecutorService e = Executors.newSingleThreadExecutor(null);
153 >            shouldThrow();
154 >        }
155 >        catch(NullPointerException success) {
156 >        }
157 >    }
158 >
159 >    /**
160 >     * A new newFixedThreadPool can execute runnables
161 >     */
162 >    public void testNewFixedThreadPool1() {
163 >        ExecutorService e = Executors.newFixedThreadPool(2);
164 >        e.execute(new NoOpRunnable());
165 >        e.execute(new NoOpRunnable());
166 >        e.execute(new NoOpRunnable());
167 >        e.shutdown();
168 >    }
169 >
170 >    /**
171 >     * A new newFixedThreadPool with given ThreadFactory can execute runnables
172 >     */
173 >    public void testNewFixedThreadPool2() {
174 >        ExecutorService e = Executors.newFixedThreadPool(2, new MyThreadFactory());
175 >        e.execute(new NoOpRunnable());
176 >        e.execute(new NoOpRunnable());
177 >        e.execute(new NoOpRunnable());
178 >        e.shutdown();
179 >    }
180 >
181 >    /**
182 >     * A new newFixedThreadPool with null ThreadFactory throws NPE
183 >     */
184 >    public void testNewFixedThreadPool3() {
185 >        try {
186 >            ExecutorService e = Executors.newFixedThreadPool(2, null);
187 >            shouldThrow();
188 >        }
189 >        catch(NullPointerException success) {
190 >        }
191 >    }
192 >
193 >    /**
194 >     * A new newFixedThreadPool with 0 threads throws IAE
195 >     */
196 >    public void testNewFixedThreadPool4() {
197 >        try {
198 >            ExecutorService e = Executors.newFixedThreadPool(0);
199 >            shouldThrow();
200 >        }
201 >        catch(IllegalArgumentException success) {
202 >        }
203 >    }
204 >
205 >    /**
206 >     * execute of runnable runs it to completion
207 >     */
208 >    public void testExecuteRunnable () {
209 >        try {
210 >            Executor e = new DirectExecutor();
211 >            MyTask task = new MyTask();
212 >            assertFalse(task.isCompleted());
213 >            Future<String> future = Executors.execute(e, task, TEST_STRING);
214 >            String result = future.get();
215 >            assertTrue(task.isCompleted());
216 >            assertSame(TEST_STRING, result);
217 >        }
218 >        catch (ExecutionException ex) {
219 >            unexpectedException();
220 >        }
221 >        catch (InterruptedException ex) {
222 >            unexpectedException();
223 >        }
224 >    }
225 >
226 >    /**
227 >     * invoke of a runnable runs it to completion
228 >     */
229 >    public void testInvokeRunnable () {
230 >        try {
231 >            Executor e = new DirectExecutor();
232 >            MyTask task = new MyTask();
233 >            assertFalse(task.isCompleted());
234 >            Executors.invoke(e, task);
235 >            assertTrue(task.isCompleted());
236 >        }
237 >        catch (ExecutionException ex) {
238 >            unexpectedException();
239 >        }
240 >        catch (InterruptedException ex) {
241 >            unexpectedException();
242 >        }
243 >    }
244 >
245 >    /**
246 >     * execute of a callable runs it to completion
247 >     */
248 >    public void testExecuteCallable () {
249 >        try {
250 >            Executor e = new DirectExecutor();
251 >            Future<String> future = Executors.execute(e, new StringTask());
252 >            String result = future.get();
253 >            assertSame(TEST_STRING, result);
254 >        }
255 >        catch (ExecutionException ex) {
256 >            unexpectedException();
257 >        }
258 >        catch (InterruptedException ex) {
259 >            unexpectedException();
260 >        }
261 >    }
262 >
263 >    /**
264 >     * invoke of a collable runs it to completion
265 >     */
266 >    public void testInvokeCallable () {
267 >        try {
268 >            Executor e = new DirectExecutor();
269 >            String result = Executors.invoke(e, new StringTask());
270 >
271 >            assertSame(TEST_STRING, result);
272 >        }
273 >        catch (ExecutionException ex) {
274 >            unexpectedException();
275 >        }
276 >        catch (InterruptedException ex) {
277 >            unexpectedException();
278 >        }
279 >    }
280 >
281 >    /**
282 >     * execute with null executor throws NPE
283 >     */
284 >    public void testNullExecuteRunnable () {
285 >        try {
286 >            MyTask task = new MyTask();
287 >            assertFalse(task.isCompleted());
288 >            Future<String> future = Executors.execute(null, task, TEST_STRING);
289 >            shouldThrow();
290 >        }
291 >        catch (NullPointerException success) {
292 >        }
293 >        catch (Exception ex) {
294 >            unexpectedException();
295 >        }
296 >    }
297 >
298 >    /**
299 >     * execute with a null runnable throws NPE
300 >     */
301 >    public void testExecuteNullRunnable() {
302 >        try {
303 >            Executor e = new DirectExecutor();
304 >            MyTask task = null;
305 >            Future<String> future = Executors.execute(e, task, TEST_STRING);
306 >            shouldThrow();
307 >        }
308 >        catch (NullPointerException success) {
309 >        }
310 >        catch (Exception ex) {
311 >            unexpectedException();
312 >        }
313 >    }
314 >
315 >    /**
316 >     * invoke of a null runnable throws NPE
317 >     */
318 >    public void testInvokeNullRunnable () {
319 >        try {
320 >            Executor e = new DirectExecutor();
321 >            MyTask task = null;
322 >            Executors.invoke(e, task);
323 >            shouldThrow();
324 >        }
325 >        catch (NullPointerException success) {
326 >        }
327 >        catch (Exception ex) {
328 >            unexpectedException();
329          }
330      }
331  
332 +    /**
333 +     * execute of a null callable throws NPE
334 +     */
335 +    public void testExecuteNullCallable () {
336 +        try {
337 +            Executor e = new DirectExecutor();
338 +            StringTask t = null;
339 +            Future<String> future = Executors.execute(e, t);
340 +            shouldThrow();
341 +        }
342 +        catch (NullPointerException success) {
343 +        }
344 +        catch (Exception ex) {
345 +            unexpectedException();
346 +        }
347 +    }
348  
349 +    /**
350 +     * invoke of a null callable throws NPE
351 +     */
352 +    public void testInvokeNullCallable () {
353 +        try {
354 +            Executor e = new DirectExecutor();
355 +            StringTask t = null;
356 +            String result = Executors.invoke(e, t);
357 +            shouldThrow();
358 +        }
359 +        catch (NullPointerException success) {
360 +        }
361 +        catch (Exception ex) {
362 +            unexpectedException();
363 +        }
364 +    }
365  
366      /**
367 <     *  Test to verify execute(Executor, Runnable) will throw
368 <     *  RejectedExecutionException Attempting to execute a runnable on
55 <     *  a full ThreadPool will cause such an exception here, up to 5
56 <     *  runnables are attempted on a pool capable on handling one
57 <     *  until it throws an exception
367 >     *  execute(Executor, Runnable) throws RejectedExecutionException
368 >     *  if saturated.
369       */
370 <    public void testExecute1(){
371 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
372 <        try{
370 >    public void testExecute1() {
371 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
372 >        try {
373              
374              for(int i = 0; i < 5; ++i){
375 <                Executors.execute(p, new SleepRun(), Boolean.TRUE);
375 >                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
376              }
377 <            fail("should throw");
377 >            shouldThrow();
378          } catch(RejectedExecutionException success){}
379 <        p.shutdownNow();
379 >        joinPool(p);
380      }
381  
382      /**
383 <     *  Test to verify execute(Executor, Callable) will throw
384 <     *  RejectedExecutionException Attempting to execute a callable on
74 <     *  a full ThreadPool will cause such an exception here, up to 5
75 <     *  runnables are attempted on a pool capable on handling one
76 <     *  until it throws an exception
383 >     *  execute(Executor, Callable)throws RejectedExecutionException
384 >     *  if saturated.
385       */
386 <    public void testExecute2(){
387 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
388 <        try{
386 >    public void testExecute2() {
387 >         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
388 >        try {
389              for(int i = 0; i < 5; ++i) {
390 <                Executors.execute(p, new SleepCall());
390 >                Executors.execute(p, new SmallCallable());
391              }
392 <            fail("should throw");
393 <        }catch(RejectedExecutionException e){}
394 <        p.shutdownNow();
392 >            shouldThrow();
393 >        } catch(RejectedExecutionException e){}
394 >        joinPool(p);
395      }
396  
397  
398      /**
399 <     *  Test to verify invoke(Executor, Runnable) throws InterruptedException
400 <     *  A single use of invoke starts that will wait long enough
93 <     *  for the invoking thread to be interrupted
399 >     *  invoke(Executor, Runnable) throws InterruptedException if
400 >     *  caller interrupted.
401       */
402 <    public void testInvoke2(){
403 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
402 >    public void testInterruptedInvoke() {
403 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
404          Thread t = new Thread(new Runnable() {
405 <                public void run(){
406 <                    try{
407 <                        Executors.invoke(p,new Runnable(){
408 <                                public void run(){
409 <                                    try{
405 >                public void run() {
406 >                    try {
407 >                        Executors.invoke(p,new Runnable() {
408 >                                public void run() {
409 >                                    try {
410                                          Thread.sleep(MEDIUM_DELAY_MS);
411 <                                        fail("should throw");
412 <                                    }catch(InterruptedException e){
411 >                                        shouldThrow();
412 >                                    } catch(InterruptedException e){
413                                      }
414                                  }
415                              });
416                      } catch(InterruptedException success){
417                      } catch(Exception e) {
418 <                        fail("unexpected exception");
418 >                        unexpectedException();
419                      }
420                      
421                  }
422              });
423 <        try{
423 >        try {
424              t.start();
425              Thread.sleep(SHORT_DELAY_MS);
426              t.interrupt();
427 <        }catch(Exception e){
428 <            fail("unexpected exception");
427 >        } catch(Exception e){
428 >            unexpectedException();
429          }
430 <        p.shutdownNow();
430 >        joinPool(p);
431      }
432  
433      /**
434 <     *  Test to verify invoke(Executor, Runnable) will throw
435 <     *  ExecutionException An ExecutionException occurs when the
129 <     *  underlying Runnable throws an exception, here the
130 <     *  DivideByZeroException will cause an ExecutionException
434 >     *  invoke(Executor, Runnable) throws ExecutionException if
435 >     *  runnable throws exception.
436       */
437 <    public void testInvoke3(){
438 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
439 <        try{
440 <            Runnable r = new Runnable(){
441 <                    public void run(){
437 >    public void testInvoke3() {
438 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
439 >        try {
440 >            Runnable r = new Runnable() {
441 >                    public void run() {
442                          int i = 5/0;
443                      }
444                  };
# Line 142 | Line 447 | public class ExecutorsTest extends TestC
447                  Executors.invoke(p,r);
448              }
449              
450 <            fail("should throw");
450 >            shouldThrow();
451          } catch(ExecutionException success){
452          } catch(Exception e){
453 <            fail("should throw EE");
453 >            unexpectedException();
454          }
455 <        p.shutdownNow();
455 >        joinPool(p);
456      }
457  
458  
459  
460      /**
461 <     *  Test to verify invoke(Executor, Callable) throws
462 <     *  InterruptedException A single use of invoke starts that will
158 <     *  wait long enough for the invoking thread to be interrupted
461 >     *  invoke(Executor, Callable) throws InterruptedException if
462 >     *  callable throws exception
463       */
464 <    public void testInvoke5(){
465 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
464 >    public void testInvoke5() {
465 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
466          
467 <        final Callable c = new Callable(){
468 <                public Object call(){
469 <                    try{
470 <                        Executors.invoke(p, new SleepCall());
471 <                        fail("should throw");
472 <                    }catch(InterruptedException e){}
467 >        final Callable c = new Callable() {
468 >                public Object call() {
469 >                    try {
470 >                        Executors.invoke(p, new SmallCallable());
471 >                        shouldThrow();
472 >                    } catch(InterruptedException e){}
473                      catch(RejectedExecutionException e2){}
474                      catch(ExecutionException e3){}
475                      return Boolean.TRUE;
# Line 174 | Line 478 | public class ExecutorsTest extends TestC
478  
479  
480          
481 <        Thread t = new Thread(new Runnable(){
482 <                public void run(){
483 <                    try{
481 >        Thread t = new Thread(new Runnable() {
482 >                public void run() {
483 >                    try {
484                          c.call();
485 <                    }catch(Exception e){}
485 >                    } catch(Exception e){}
486                  }
487            });
488 <        try{
488 >        try {
489              t.start();
490              Thread.sleep(SHORT_DELAY_MS);
491              t.interrupt();
492              t.join();
493 <        }catch(InterruptedException e){
494 <            fail("unexpected exception");
493 >        } catch(InterruptedException e){
494 >            unexpectedException();
495          }
496          
497 <        p.shutdownNow();
497 >        joinPool(p);
498      }
499  
500      /**
501 <     *  Test to verify invoke(Executor, Callable) will throw ExecutionException
502 <     *  An ExecutionException occurs when the underlying Runnable throws
199 <     *  an exception, here the DivideByZeroException will cause an ExecutionException
501 >     *  invoke(Executor, Callable) will throw ExecutionException
502 >     *  if callable throws exception
503       */
504 <    public void testInvoke6(){
505 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
504 >    public void testInvoke6() {
505 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
506  
507 <        try{
508 <            Callable c = new Callable(){
509 <                    public Object call(){
507 >        try {
508 >            Callable c = new Callable() {
509 >                    public Object call() {
510                          int i = 5/0;
511                          return Boolean.TRUE;
512                      }
# Line 213 | Line 516 | public class ExecutorsTest extends TestC
516                  Executors.invoke(p,c);
517              }
518              
519 <            fail("should throw");
520 <        }catch(RejectedExecutionException e){}
521 <        catch(InterruptedException e2){}
522 <        catch(ExecutionException e3){}
523 <        p.shutdownNow();
221 <    }
222 <
223 <    public void testExecuteRunnable () {
224 <        try {
225 <            Executor e = new DirectExecutor();
226 <            Task task = new Task();
227 <
228 <            assertFalse("task should not be complete", task.isCompleted());
229 <
230 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
231 <            String result = future.get();
232 <
233 <            assertTrue("task should be complete", task.isCompleted());
234 <            assertSame("should return test string", TEST_STRING, result);
235 <        }
236 <        catch (ExecutionException ex) {
237 <            fail("Unexpected exception");
238 <        }
239 <        catch (InterruptedException ex) {
240 <            fail("Unexpected exception");
519 >            shouldThrow();
520 >        }
521 >        catch(ExecutionException success){
522 >        } catch(Exception e) {
523 >            unexpectedException();
524          }
525 +        joinPool(p);
526      }
527  
244    public void testInvokeRunnable () {
245        try {
246            Executor e = new DirectExecutor();
247            Task task = new Task();
528  
249            assertFalse("task should not be complete", task.isCompleted());
529  
530 <            Executors.invoke(e, task);
531 <
532 <            assertTrue("task should be complete", task.isCompleted());
533 <        }
534 <        catch (ExecutionException ex) {
535 <            fail("Unexpected exception");
536 <        }
258 <        catch (InterruptedException ex) {
259 <            fail("Unexpected exception");
260 <        }
261 <    }
262 <
263 <    public void testExecuteCallable () {
264 <        try {
265 <            Executor e = new DirectExecutor();
266 <            Future<String> future = Executors.execute(e, new StringTask());
267 <            String result = future.get();
268 <
269 <            assertSame("should return test string", TEST_STRING, result);
270 <        }
271 <        catch (ExecutionException ex) {
272 <            fail("Unexpected exception");
273 <        }
274 <        catch (InterruptedException ex) {
275 <            fail("Unexpected exception");
276 <        }
277 <    }
278 <
279 <    public void testInvokeCallable () {
530 >    /**
531 >     *  timeouts from execute will time out if they compute too long.
532 >     */
533 >    public void testTimedCallable() {
534 >        int N = 10000;
535 >        ExecutorService executor = Executors.newSingleThreadExecutor();
536 >        List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
537          try {
538 <            Executor e = new DirectExecutor();
539 <            String result = Executors.invoke(e, new StringTask());
540 <
541 <            assertSame("should return test string", TEST_STRING, result);
542 <        }
543 <        catch (ExecutionException ex) {
544 <            fail("Unexpected exception" );
538 >            long startTime = System.currentTimeMillis();
539 >            
540 >            long i = 0;
541 >            while (tasks.size() < N) {
542 >                tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1));
543 >                i += 10;
544 >            }
545 >            
546 >            int iters = 0;
547 >            BigInteger sum = BigInteger.ZERO;
548 >            for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) {
549 >                try {
550 >                    ++iters;
551 >                    sum = sum.add(it.next().call());
552 >                }
553 >                catch (TimeoutException success) {
554 >                    assertTrue(iters > 0);
555 >                    return;
556 >                }
557 >                catch (Exception e) {
558 >                    unexpectedException();
559 >                }
560 >            }
561 >            // if by chance we didn't ever time out, total time must be small
562 >            long elapsed = System.currentTimeMillis() - startTime;
563 >            assertTrue(elapsed < N);
564          }
565 <        catch (InterruptedException ex) {
566 <            fail("Unexpected exception");
565 >        finally {
566 >            joinPool(executor);
567          }
568      }
569  
570 <    private static final String TEST_STRING = "a test string";
295 <
296 <    private static class Task implements Runnable {
297 <        public void run() { completed = true; }
298 <        public boolean isCompleted() { return completed; }
299 <        public void reset() { completed = false; }
300 <        private boolean completed = false;
301 <    }
302 <
303 <    private static class StringTask implements Callable<String> {
304 <        public String call() { return TEST_STRING; }
305 <    }
306 <
307 <    static class DirectExecutor implements Executor {
308 <        public void execute(Runnable r) {
309 <            r.run();
310 <        }
311 <    }
570 >    
571  
572  
573   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines