--- jsr166/src/test/tck/ExecutorsTest.java 2010/10/09 19:30:35 1.34 +++ jsr166/src/test/tck/ExecutorsTest.java 2011/05/29 06:54:23 1.41 @@ -1,17 +1,15 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ - import junit.framework.*; import java.util.*; import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.math.BigInteger; import java.security.*; public class ExecutorsTest extends JSR166TestCase { @@ -54,7 +52,6 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - /** * A new SingleThreadExecutor can execute runnables */ @@ -101,7 +98,6 @@ public class ExecutorsTest extends JSR16 } } - /** * A new newFixedThreadPool can execute runnables */ @@ -144,7 +140,6 @@ public class ExecutorsTest extends JSR16 } catch (IllegalArgumentException success) {} } - /** * An unconfigurable newFixedThreadPool can execute runnables */ @@ -176,81 +171,108 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - /** * a newSingleThreadScheduledExecutor successfully runs delayed task */ public void testNewSingleThreadScheduledExecutor() throws Exception { - TrackedCallable callable = new TrackedCallable(); - ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); - Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); - assertFalse(callable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(callable.done); - assertEquals(Boolean.TRUE, f.get()); - joinPool(p1); + ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); + try { + final CountDownLatch done = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { + done.countDown(); + }}; + Future f = p.schedule(Executors.callable(task, Boolean.TRUE), + SHORT_DELAY_MS, MILLISECONDS); + assertFalse(f.isDone()); + assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get()); + assertTrue(f.isDone()); + } finally { + joinPool(p); + } } /** * a newScheduledThreadPool successfully runs delayed task */ public void testnewScheduledThreadPool() throws Exception { - TrackedCallable callable = new TrackedCallable(); - ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); - Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); - assertFalse(callable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(callable.done); - assertEquals(Boolean.TRUE, f.get()); - joinPool(p1); + ScheduledExecutorService p = Executors.newScheduledThreadPool(2); + try { + final CountDownLatch done = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { + done.countDown(); + }}; + Future f = p.schedule(Executors.callable(task, Boolean.TRUE), + SHORT_DELAY_MS, MILLISECONDS); + assertFalse(f.isDone()); + assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get()); + assertTrue(f.isDone()); + } finally { + joinPool(p); + } } /** * an unconfigurable newScheduledThreadPool successfully runs delayed task */ public void testunconfigurableScheduledExecutorService() throws Exception { - TrackedCallable callable = new TrackedCallable(); - ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); - Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); - assertFalse(callable.done); - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(callable.done); - assertEquals(Boolean.TRUE, f.get()); - joinPool(p1); + ScheduledExecutorService p = + Executors.unconfigurableScheduledExecutorService + (Executors.newScheduledThreadPool(2)); + try { + final CountDownLatch done = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { + done.countDown(); + }}; + Future f = p.schedule(Executors.callable(task, Boolean.TRUE), + SHORT_DELAY_MS, MILLISECONDS); + assertFalse(f.isDone()); + assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get()); + assertTrue(f.isDone()); + } finally { + joinPool(p); + } } /** * Future.get on submitted tasks will time out if they compute too long. */ public void testTimedCallable() throws Exception { + final ExecutorService[] executors = { + Executors.newSingleThreadExecutor(), + Executors.newCachedThreadPool(), + Executors.newFixedThreadPool(2), + Executors.newScheduledThreadPool(2), + }; + final Runnable sleeper = new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(LONG_DELAY_MS); + delay(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 { - future.get(SHORT_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (TimeoutException success) { - } finally { - future.cancel(true); - } - } - finally { - joinPool(executor); - } + + List threads = new ArrayList(); + for (final ExecutorService executor : executors) { + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() { + long startTime = System.nanoTime(); + Future future = executor.submit(sleeper); + assertFutureTimesOut(future); + }})); } + for (Thread thread : threads) + awaitTermination(thread); + for (ExecutorService executor : executors) + joinPool(executor); } - /** * ThreadPoolExecutor using defaultThreadFactory has * specified group, priority, daemon status, and name @@ -284,7 +306,7 @@ public class ExecutorsTest extends JSR16 } try { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); } finally { joinPool(e); } @@ -320,7 +342,7 @@ public class ExecutorsTest extends JSR16 ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); e.execute(r); e.shutdown(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); joinPool(e); }}; @@ -358,7 +380,6 @@ public class ExecutorsTest extends JSR16 } } - /** * Without class loader permissions, creating * privilegedCallableUsingCurrentClassLoader throws ACE @@ -475,7 +496,7 @@ public class ExecutorsTest extends JSR16 Executors.privilegedCallable(new CheckCCL()).call(); }}; - runWithPermissions(r, + runWithPermissions(r, new RuntimePermission("getClassLoader"), new RuntimePermission("setContextClassLoader")); } @@ -514,7 +535,6 @@ public class ExecutorsTest extends JSR16 assertSame(one, c.call()); } - /** * callable(null Runnable) throws NPE */ @@ -555,5 +575,4 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - }