--- jsr166/src/test/tck/ExecutorsTest.java 2010/01/05 02:08:37 1.29 +++ jsr166/src/test/tck/ExecutorsTest.java 2015/04/25 04:55:30 1.45 @@ -1,22 +1,34 @@ /* * 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.*; + +import java.security.AccessControlContext; +import java.security.AccessControlException; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ExecutorsTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ExecutorsTest.class); @@ -54,7 +66,6 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - /** * A new SingleThreadExecutor can execute runnables */ @@ -101,7 +112,6 @@ public class ExecutorsTest extends JSR16 } } - /** * A new newFixedThreadPool can execute runnables */ @@ -144,11 +154,10 @@ public class ExecutorsTest extends JSR16 } catch (IllegalArgumentException success) {} } - /** * An unconfigurable newFixedThreadPool can execute runnables */ - public void testunconfigurableExecutorService() { + public void testUnconfigurableExecutorService() { ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); @@ -159,7 +168,7 @@ public class ExecutorsTest extends JSR16 /** * unconfigurableExecutorService(null) throws NPE */ - public void testunconfigurableExecutorServiceNPE() { + public void testUnconfigurableExecutorServiceNPE() { try { ExecutorService e = Executors.unconfigurableExecutorService(null); shouldThrow(); @@ -169,127 +178,160 @@ public class ExecutorsTest extends JSR16 /** * unconfigurableScheduledExecutorService(null) throws NPE */ - public void testunconfigurableScheduledExecutorServiceNPE() { + public void testUnconfigurableScheduledExecutorServiceNPE() { try { ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); shouldThrow(); } 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 proceed = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { + await(proceed); + }}; + long startTime = System.nanoTime(); + Future f = p.schedule(Executors.callable(task, Boolean.TRUE), + timeoutMillis(), MILLISECONDS); + assertFalse(f.isDone()); + proceed.countDown(); + assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get()); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + } 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); + public void testNewScheduledThreadPool() throws Exception { + ScheduledExecutorService p = Executors.newScheduledThreadPool(2); + try { + final CountDownLatch proceed = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { + await(proceed); + }}; + long startTime = System.nanoTime(); + Future f = p.schedule(Executors.callable(task, Boolean.TRUE), + timeoutMillis(), MILLISECONDS); + assertFalse(f.isDone()); + proceed.countDown(); + assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get()); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + } 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); + public void testUnconfigurableScheduledExecutorService() throws Exception { + ScheduledExecutorService p = + Executors.unconfigurableScheduledExecutorService + (Executors.newScheduledThreadPool(2)); + try { + final CountDownLatch proceed = new CountDownLatch(1); + final Runnable task = new CheckedRunnable() { + public void realRun() { + await(proceed); + }}; + long startTime = System.nanoTime(); + Future f = p.schedule(Executors.callable(task, Boolean.TRUE), + timeoutMillis(), MILLISECONDS); + assertFalse(f.isDone()); + proceed.countDown(); + assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); + assertSame(Boolean.TRUE, f.get()); + assertTrue(f.isDone()); + assertFalse(f.isCancelled()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + } finally { + joinPool(p); + } } /** - * Future.get on submitted tasks 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 { - 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 { - future.get(SHORT_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (TimeoutException success) { - } finally { - future.cancel(true); - } - } - finally { - joinPool(executor); - } + final ExecutorService[] executors = { + Executors.newSingleThreadExecutor(), + Executors.newCachedThreadPool(), + Executors.newFixedThreadPool(2), + Executors.newScheduledThreadPool(2), + }; + + final Runnable sleeper = new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + delay(LONG_DELAY_MS); + }}; + + List threads = new ArrayList(); + for (final ExecutorService executor : executors) { + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() { + 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 */ public void testDefaultThreadFactory() throws Exception { final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); - Runnable r = new Runnable() { - public void run() { - try { - Thread current = Thread.currentThread(); - threadAssertTrue(!current.isDaemon()); - threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); - ThreadGroup g = current.getThreadGroup(); - SecurityManager s = System.getSecurityManager(); - if (s != null) - threadAssertTrue(g == s.getThreadGroup()); - else - threadAssertTrue(g == egroup); - String name = current.getName(); - threadAssertTrue(name.endsWith("thread-1")); - } catch (SecurityException ok) { - // Also pass if not allowed to change setting - } + final CountDownLatch done = new CountDownLatch(1); + Runnable r = new CheckedRunnable() { + public void realRun() { + try { + Thread current = Thread.currentThread(); + assertTrue(!current.isDaemon()); + assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + assertTrue(g == s.getThreadGroup()); + else + assertTrue(g == egroup); + String name = current.getName(); + assertTrue(name.endsWith("thread-1")); + } catch (SecurityException ok) { + // Also pass if not allowed to change setting } - }; + done.countDown(); + }}; ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); e.execute(r); + await(done); + try { e.shutdown(); } catch (SecurityException ok) { } - try { - Thread.sleep(SHORT_DELAY_MS); - } finally { - joinPool(e); - } + joinPool(e); } /** @@ -298,6 +340,7 @@ public class ExecutorsTest extends JSR16 * access control context and context class loader */ public void testPrivilegedThreadFactory() throws Exception { + final CountDownLatch done = new CountDownLatch(1); Runnable r = new CheckedRunnable() { public void realRun() throws Exception { final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); @@ -316,13 +359,14 @@ public class ExecutorsTest extends JSR16 assertTrue(g == egroup); String name = current.getName(); assertTrue(name.endsWith("thread-1")); - assertTrue(thisccl == current.getContextClassLoader()); - assertTrue(thisacc.equals(AccessController.getContext())); + assertSame(thisccl, current.getContextClassLoader()); + assertEquals(thisacc, AccessController.getContext()); + done.countDown(); }}; ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); e.execute(r); + await(done); e.shutdown(); - Thread.sleep(SHORT_DELAY_MS); joinPool(e); }}; @@ -360,7 +404,6 @@ public class ExecutorsTest extends JSR16 } } - /** * Without class loader permissions, creating * privilegedCallableUsingCurrentClassLoader throws ACE @@ -383,7 +426,7 @@ public class ExecutorsTest extends JSR16 * With class loader permissions, calling * privilegedCallableUsingCurrentClassLoader does not throw ACE */ - public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { + public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception { Runnable r = new CheckedRunnable() { public void realRun() throws Exception { Executors.privilegedCallableUsingCurrentClassLoader @@ -399,7 +442,10 @@ public class ExecutorsTest extends JSR16 /** * Without permissions, calling privilegedCallable throws ACE */ - public void testprivilegedCallableWithNoPrivs() throws Exception { + public void testPrivilegedCallableWithNoPrivs() throws Exception { + // Avoid classloader-related SecurityExceptions in swingui.TestRunner + Executors.privilegedCallable(new CheckCCL()); + Runnable r = new CheckedRunnable() { public void realRun() throws Exception { if (System.getSecurityManager() == null) @@ -468,13 +514,13 @@ public class ExecutorsTest extends JSR16 /** * With permissions, calling privilegedCallable succeeds */ - public void testprivilegedCallableWithPrivs() throws Exception { + public void testPrivilegedCallableWithPrivs() throws Exception { Runnable r = new CheckedRunnable() { public void realRun() throws Exception { Executors.privilegedCallable(new CheckCCL()).call(); }}; - runWithPermissions(r, + runWithPermissions(r, new RuntimePermission("getClassLoader"), new RuntimePermission("setContextClassLoader")); } @@ -513,7 +559,6 @@ public class ExecutorsTest extends JSR16 assertSame(one, c.call()); } - /** * callable(null Runnable) throws NPE */ @@ -554,5 +599,4 @@ public class ExecutorsTest extends JSR16 } catch (NullPointerException success) {} } - }