--- jsr166/src/test/tck/ExecutorsTest.java 2003/08/31 19:24:55 1.1 +++ jsr166/src/test/tck/ExecutorsTest.java 2003/09/07 20:39:11 1.2 @@ -9,6 +9,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import java.math.BigInteger; public class ExecutorsTest extends TestCase{ @@ -310,5 +311,89 @@ public class ExecutorsTest extends TestC } } + /** + * Check that timeouts from execute will time out if they compute + * too long. + */ + + public void testTimedCallable() { + int N = 10000; + ExecutorService executor = Executors.newSingleThreadExecutor(); + List> tasks = new ArrayList>(N); + try { + long startTime = System.currentTimeMillis(); + + long i = 0; + while (tasks.size() < N) { + tasks.add(new TimedCallable(executor, new Fib(i), 1)); + i += 10; + } + + int iters = 0; + BigInteger sum = BigInteger.ZERO; + for (Iterator> it = tasks.iterator(); it.hasNext();) { + try { + ++iters; + sum = sum.add(it.next().call()); + } + catch (TimeoutException success) { + assertTrue(iters > 0); + return; + } + catch (Exception e) { + fail("unexpected exception: " + e); + } + } + // if by chance we didn't ever time out, total time must be small + long elapsed = System.currentTimeMillis() - startTime; + assertTrue(elapsed < N); + } + finally { + executor.shutdownNow(); + } + } + + + static class TimedCallable implements Callable { + private final Executor exec; + private final Callable func; + private final long msecs; + + TimedCallable(Executor exec, Callable func, long msecs) { + this.exec = exec; + this.func = func; + this.msecs = msecs; + } + + public T call() throws Exception { + Future ftask = Executors.execute(exec, func); + try { + return ftask.get(msecs, TimeUnit.MILLISECONDS); + } finally { + ftask.cancel(true); + } + } + } + + + private static class Fib implements Callable { + private final BigInteger n; + Fib(long n) { + if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); + this.n = BigInteger.valueOf(n); + } + public BigInteger call() { + BigInteger f1 = BigInteger.ONE; + BigInteger f2 = f1; + for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { + BigInteger t = f1.add(f2); + f1 = f2; + f2 = t; + } + return f1; + } + }; + + }