--- jsr166/src/test/tck/ExecutorsTest.java 2009/11/21 02:07:26 1.25 +++ jsr166/src/test/tck/ExecutorsTest.java 2009/12/02 19:17:01 1.28 @@ -10,6 +10,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.math.BigInteger; import java.security.*; @@ -21,46 +22,6 @@ public class ExecutorsTest extends JSR16 return new TestSuite(ExecutorsTest.class); } - static class TimedCallable implements Callable { - private final ExecutorService exec; - private final Callable func; - private final long msecs; - - TimedCallable(ExecutorService exec, Callable func, long msecs) { - this.exec = exec; - this.func = func; - this.msecs = msecs; - } - - public T call() throws Exception { - Future ftask = exec.submit(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; - } - }; - /** * A newCachedThreadPool can execute runnables */ @@ -222,7 +183,7 @@ public class ExecutorsTest extends JSR16 public void testNewSingleThreadScheduledExecutor() throws Exception { TrackedCallable callable = new TrackedCallable(); ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); - Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); assertFalse(callable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(callable.done); @@ -236,7 +197,7 @@ public class ExecutorsTest extends JSR16 public void testnewScheduledThreadPool() throws Exception { TrackedCallable callable = new TrackedCallable(); ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); - Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); assertFalse(callable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(callable.done); @@ -250,7 +211,7 @@ public class ExecutorsTest extends JSR16 public void testunconfigurableScheduledExecutorService() throws Exception { TrackedCallable callable = new TrackedCallable(); ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); - Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); + Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); assertFalse(callable.done); Thread.sleep(MEDIUM_DELAY_MS); assertTrue(callable.done); @@ -259,39 +220,34 @@ public class ExecutorsTest extends JSR16 } /** - * timeouts from execute will time out if they compute too long. + * Future.get on submitted tasks will time out if they compute too long. */ public void testTimedCallable() throws Exception { - 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();) { + final Runnable sleeper = + new RunnableShouldThrow(InterruptedException.class) { + public void realRun() throws InterruptedException { + Thread.sleep(LONG_DELAY_MS); + }}; + for (ExecutorService executor : + new ExecutorService[] { + Executors.newSingleThreadExecutor(), + Executors.newCachedThreadPool(), + Executors.newFixedThreadPool(2), + Executors.newScheduledThreadPool(2), + }) { + try { + Future future = executor.submit(sleeper); try { - ++iters; - sum = sum.add(it.next().call()); - } - catch (TimeoutException success) { - assertTrue(iters > 0); - return; + future.get(SHORT_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (TimeoutException success) { + } finally { + future.cancel(true); } } - // if by chance we didn't ever time out, total time must be small - long elapsed = System.currentTimeMillis() - startTime; - assertTrue(elapsed < N); - } - finally { - joinPool(executor); + finally { + joinPool(executor); + } } } @@ -389,7 +345,6 @@ public class ExecutorsTest extends JSR16 } finally { joinPool(e); } - } void checkCCL() { @@ -538,7 +493,7 @@ public class ExecutorsTest extends JSR16 */ public void testCallable2() throws Exception { Callable c = Executors.callable(new NoOpRunnable(), one); - assertEquals(one, c.call()); + assertSame(one, c.call()); } /** @@ -547,7 +502,7 @@ public class ExecutorsTest extends JSR16 public void testCallable3() throws Exception { Callable c = Executors.callable(new PrivilegedAction() { public Object run() { return one; }}); - assertEquals(one, c.call()); + assertSame(one, c.call()); } /** @@ -556,7 +511,7 @@ public class ExecutorsTest extends JSR16 public void testCallable4() throws Exception { Callable c = Executors.callable(new PrivilegedExceptionAction() { public Object run() { return one; }}); - assertEquals(one, c.call()); + assertSame(one, c.call()); }