--- jsr166/src/test/tck/ExecutorsTest.java 2011/03/15 19:47:06 1.37 +++ jsr166/src/test/tck/ExecutorsTest.java 2020/02/01 18:52:17 1.54 @@ -6,17 +6,29 @@ * 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); @@ -26,22 +38,24 @@ public class ExecutorsTest extends JSR16 * A newCachedThreadPool can execute runnables */ public void testNewCachedThreadPool1() { - ExecutorService e = Executors.newCachedThreadPool(); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + final ExecutorService e = Executors.newCachedThreadPool(); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** * A newCachedThreadPool with given ThreadFactory can execute runnables */ public void testNewCachedThreadPool2() { - ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + final ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** @@ -49,32 +63,33 @@ public class ExecutorsTest extends JSR16 */ public void testNewCachedThreadPool3() { try { - ExecutorService e = Executors.newCachedThreadPool(null); + ExecutorService unused = Executors.newCachedThreadPool(null); shouldThrow(); } catch (NullPointerException success) {} } - /** * A new SingleThreadExecutor can execute runnables */ public void testNewSingleThreadExecutor1() { - ExecutorService e = Executors.newSingleThreadExecutor(); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + final ExecutorService e = Executors.newSingleThreadExecutor(); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** * A new SingleThreadExecutor with given ThreadFactory can execute runnables */ public void testNewSingleThreadExecutor2() { - ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + final ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** @@ -82,7 +97,7 @@ public class ExecutorsTest extends JSR16 */ public void testNewSingleThreadExecutor3() { try { - ExecutorService e = Executors.newSingleThreadExecutor(null); + ExecutorService unused = Executors.newSingleThreadExecutor(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -91,77 +106,79 @@ public class ExecutorsTest extends JSR16 * A new SingleThreadExecutor cannot be casted to concrete implementation */ public void testCastNewSingleThreadExecutor() { - ExecutorService e = Executors.newSingleThreadExecutor(); - try { - ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; - shouldThrow(); - } catch (ClassCastException success) { - } finally { - joinPool(e); + final ExecutorService e = Executors.newSingleThreadExecutor(); + try (PoolCleaner cleaner = cleaner(e)) { + try { + ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; + shouldThrow(); + } catch (ClassCastException success) {} } } - /** * A new newFixedThreadPool can execute runnables */ public void testNewFixedThreadPool1() { - ExecutorService e = Executors.newFixedThreadPool(2); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + final ExecutorService e = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** * A new newFixedThreadPool with given ThreadFactory can execute runnables */ public void testNewFixedThreadPool2() { - ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + final ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** - * A new newFixedThreadPool with null ThreadFactory throws NPE + * A new newFixedThreadPool with null ThreadFactory throws + * NullPointerException */ public void testNewFixedThreadPool3() { try { - ExecutorService e = Executors.newFixedThreadPool(2, null); + ExecutorService unused = Executors.newFixedThreadPool(2, null); shouldThrow(); } catch (NullPointerException success) {} } /** - * A new newFixedThreadPool with 0 threads throws IAE + * A new newFixedThreadPool with 0 threads throws IllegalArgumentException */ public void testNewFixedThreadPool4() { try { - ExecutorService e = Executors.newFixedThreadPool(0); + ExecutorService unused = Executors.newFixedThreadPool(0); shouldThrow(); } catch (IllegalArgumentException success) {} } - /** * An unconfigurable newFixedThreadPool can execute runnables */ - public void testunconfigurableExecutorService() { - ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - e.execute(new NoOpRunnable()); - joinPool(e); + public void testUnconfigurableExecutorService() { + final ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + e.execute(new NoOpRunnable()); + } } /** * unconfigurableExecutorService(null) throws NPE */ - public void testunconfigurableExecutorServiceNPE() { + public void testUnconfigurableExecutorServiceNPE() { try { - ExecutorService e = Executors.unconfigurableExecutorService(null); + ExecutorService unused = + Executors.unconfigurableExecutorService(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -169,82 +186,85 @@ public class ExecutorsTest extends JSR16 /** * unconfigurableScheduledExecutorService(null) throws NPE */ - public void testunconfigurableScheduledExecutorServiceNPE() { + public void testUnconfigurableScheduledExecutorServiceNPE() { try { - ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); + ExecutorService unused = + Executors.unconfigurableScheduledExecutorService(null); shouldThrow(); } catch (NullPointerException success) {} } - /** * a newSingleThreadScheduledExecutor successfully runs delayed task */ public void testNewSingleThreadScheduledExecutor() throws Exception { - ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); - try { - final CountDownLatch done = new CountDownLatch(1); + final ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch proceed = new CountDownLatch(1); final Runnable task = new CheckedRunnable() { public void realRun() { - done.countDown(); + await(proceed); }}; + long startTime = System.nanoTime(); Future f = p.schedule(Executors.callable(task, Boolean.TRUE), - SHORT_DELAY_MS, MILLISECONDS); + timeoutMillis(), MILLISECONDS); assertFalse(f.isDone()); - assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); - assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); + proceed.countDown(); + assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); assertSame(Boolean.TRUE, f.get()); assertTrue(f.isDone()); - } finally { - joinPool(p); + assertFalse(f.isCancelled()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); } } /** * a newScheduledThreadPool successfully runs delayed task */ - public void testnewScheduledThreadPool() throws Exception { - ScheduledExecutorService p = Executors.newScheduledThreadPool(2); - try { - final CountDownLatch done = new CountDownLatch(1); + public void testNewScheduledThreadPool() throws Exception { + final ScheduledExecutorService p = Executors.newScheduledThreadPool(2); + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch proceed = new CountDownLatch(1); final Runnable task = new CheckedRunnable() { public void realRun() { - done.countDown(); + await(proceed); }}; + long startTime = System.nanoTime(); Future f = p.schedule(Executors.callable(task, Boolean.TRUE), - SHORT_DELAY_MS, MILLISECONDS); + timeoutMillis(), MILLISECONDS); assertFalse(f.isDone()); - assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); - assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); + proceed.countDown(); + assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); assertSame(Boolean.TRUE, f.get()); assertTrue(f.isDone()); - } finally { - joinPool(p); + assertFalse(f.isCancelled()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); } } /** * an unconfigurable newScheduledThreadPool successfully runs delayed task */ - public void testunconfigurableScheduledExecutorService() throws Exception { - ScheduledExecutorService p = + public void testUnconfigurableScheduledExecutorService() throws Exception { + final ScheduledExecutorService p = Executors.unconfigurableScheduledExecutorService (Executors.newScheduledThreadPool(2)); - try { - final CountDownLatch done = new CountDownLatch(1); + try (PoolCleaner cleaner = cleaner(p)) { + final CountDownLatch proceed = new CountDownLatch(1); final Runnable task = new CheckedRunnable() { public void realRun() { - done.countDown(); + await(proceed); }}; + long startTime = System.nanoTime(); Future f = p.schedule(Executors.callable(task, Boolean.TRUE), - SHORT_DELAY_MS, MILLISECONDS); + timeoutMillis(), MILLISECONDS); assertFalse(f.isDone()); - assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); - assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); + proceed.countDown(); + assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); assertSame(Boolean.TRUE, f.get()); assertTrue(f.isDone()); - } finally { - joinPool(p); + assertFalse(f.isCancelled()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); } } @@ -252,70 +272,61 @@ public class ExecutorsTest extends JSR16 * Future.get on submitted tasks will time out if they compute too long. */ public void testTimedCallable() throws Exception { - final Runnable sleeper = new CheckedInterruptedRunnable() { + final ExecutorService[] executors = { + Executors.newSingleThreadExecutor(), + Executors.newCachedThreadPool(), + Executors.newFixedThreadPool(2), + Executors.newScheduledThreadPool(2), + }; + + final CountDownLatch done = new CountDownLatch(1); + + final Runnable sleeper = new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(LONG_DELAY_MS); + done.await(LONG_DELAY_MS, MILLISECONDS); }}; - 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() { + Future future = executor.submit(sleeper); + assertFutureTimesOut(future); + }})); } + for (Thread thread : threads) + awaitTermination(thread); + done.countDown(); + 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(); + final CountDownLatch done = new CountDownLatch(1); Runnable r = new CheckedRunnable() { public void realRun() { try { Thread current = Thread.currentThread(); - assertTrue(!current.isDaemon()); + assertFalse(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")); + assertSame(current.getThreadGroup(), + (s == null) ? egroup : s.getThreadGroup()); + assertTrue(current.getName().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); - try { - e.shutdown(); - } catch (SecurityException ok) { - } - - try { - Thread.sleep(SHORT_DELAY_MS); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(r); + await(done); } } @@ -325,6 +336,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(); @@ -333,24 +345,21 @@ public class ExecutorsTest extends JSR16 Runnable r = new CheckedRunnable() { public void realRun() { Thread current = Thread.currentThread(); - assertTrue(!current.isDaemon()); + assertFalse(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")); + assertSame(current.getThreadGroup(), + (s == null) ? egroup : s.getThreadGroup()); + assertTrue(current.getName().endsWith("thread-1")); assertSame(thisccl, current.getContextClassLoader()); assertEquals(thisacc, AccessController.getContext()); + done.countDown(); }}; ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); - e.execute(r); - e.shutdown(); - Thread.sleep(SHORT_DELAY_MS); - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + e.execute(r); + await(done); + } }}; runWithPermissions(r, @@ -387,7 +396,6 @@ public class ExecutorsTest extends JSR16 } } - /** * Without class loader permissions, creating * privilegedCallableUsingCurrentClassLoader throws ACE @@ -410,7 +418,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 @@ -426,7 +434,7 @@ 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()); @@ -498,13 +506,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")); } @@ -543,13 +551,12 @@ public class ExecutorsTest extends JSR16 assertSame(one, c.call()); } - /** * callable(null Runnable) throws NPE */ public void testCallableNPE1() { try { - Callable c = Executors.callable((Runnable) null); + Callable unused = Executors.callable((Runnable) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -559,7 +566,7 @@ public class ExecutorsTest extends JSR16 */ public void testCallableNPE2() { try { - Callable c = Executors.callable((Runnable) null, one); + Callable unused = Executors.callable((Runnable) null, one); shouldThrow(); } catch (NullPointerException success) {} } @@ -569,7 +576,7 @@ public class ExecutorsTest extends JSR16 */ public void testCallableNPE3() { try { - Callable c = Executors.callable((PrivilegedAction) null); + Callable unused = Executors.callable((PrivilegedAction) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -579,10 +586,61 @@ public class ExecutorsTest extends JSR16 */ public void testCallableNPE4() { try { - Callable c = Executors.callable((PrivilegedExceptionAction) null); + Callable unused = Executors.callable((PrivilegedExceptionAction) null); shouldThrow(); } catch (NullPointerException success) {} } + /** + * callable(runnable, x).toString() contains toString of wrapped task + */ + public void testCallable_withResult_toString() { + if (testImplementationDetails) { + Runnable r = () -> {}; + Callable c = Executors.callable(r, ""); + assertEquals( + identityString(c) + "[Wrapped task = " + r.toString() + "]", + c.toString()); + } + } + /** + * callable(runnable).toString() contains toString of wrapped task + */ + public void testCallable_toString() { + if (testImplementationDetails) { + Runnable r = () -> {}; + Callable c = Executors.callable(r); + assertEquals( + identityString(c) + "[Wrapped task = " + r.toString() + "]", + c.toString()); + } + } + + /** + * privilegedCallable(callable).toString() contains toString of wrapped task + */ + public void testPrivilegedCallable_toString() { + if (testImplementationDetails) { + Callable c = () -> ""; + Callable priv = Executors.privilegedCallable(c); + assertEquals( + identityString(priv) + "[Wrapped task = " + c.toString() + "]", + priv.toString()); + } + } + + /** + * privilegedCallableUsingCurrentClassLoader(callable).toString() + * contains toString of wrapped task + */ + public void testPrivilegedCallableUsingCurrentClassLoader_toString() { + if (testImplementationDetails) { + Callable c = () -> ""; + Callable priv = Executors.privilegedCallableUsingCurrentClassLoader(c); + assertEquals( + identityString(priv) + "[Wrapped task = " + c.toString() + "]", + priv.toString()); + } + } }