--- jsr166/src/test/tck/ExecutorsTest.java 2015/10/04 16:14:48 1.46 +++ jsr166/src/test/tck/ExecutorsTest.java 2020/02/01 18:52:17 1.54 @@ -63,7 +63,7 @@ public class ExecutorsTest extends JSR16 */ public void testNewCachedThreadPool3() { try { - ExecutorService e = Executors.newCachedThreadPool(null); + ExecutorService unused = Executors.newCachedThreadPool(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -97,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) {} } @@ -140,21 +140,22 @@ public class ExecutorsTest extends JSR16 } /** - * 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) {} } @@ -176,7 +177,8 @@ public class ExecutorsTest extends JSR16 */ public void testUnconfigurableExecutorServiceNPE() { try { - ExecutorService e = Executors.unconfigurableExecutorService(null); + ExecutorService unused = + Executors.unconfigurableExecutorService(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -186,7 +188,8 @@ public class ExecutorsTest extends JSR16 */ public void testUnconfigurableScheduledExecutorServiceNPE() { try { - ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); + ExecutorService unused = + Executors.unconfigurableScheduledExecutorService(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -276,12 +279,14 @@ public class ExecutorsTest extends JSR16 Executors.newScheduledThreadPool(2), }; - final Runnable sleeper = new CheckedInterruptedRunnable() { + final CountDownLatch done = new CountDownLatch(1); + + final Runnable sleeper = new CheckedRunnable() { public void realRun() throws InterruptedException { - delay(LONG_DELAY_MS); + done.await(LONG_DELAY_MS, MILLISECONDS); }}; - List threads = new ArrayList(); + List threads = new ArrayList<>(); for (final ExecutorService executor : executors) { threads.add(newStartedThread(new CheckedRunnable() { public void realRun() { @@ -291,6 +296,7 @@ public class ExecutorsTest extends JSR16 } for (Thread thread : threads) awaitTermination(thread); + done.countDown(); for (ExecutorService executor : executors) joinPool(executor); } @@ -306,16 +312,12 @@ public class ExecutorsTest extends JSR16 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 } @@ -343,16 +345,12 @@ 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(); @@ -558,7 +556,7 @@ public class ExecutorsTest extends JSR16 */ public void testCallableNPE1() { try { - Callable c = Executors.callable((Runnable) null); + Callable unused = Executors.callable((Runnable) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -568,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) {} } @@ -578,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) {} } @@ -588,9 +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()); + } + } }