--- jsr166/src/test/tck/ExecutorsTest.java 2010/01/05 02:08:37 1.29 +++ jsr166/src/test/tck/ExecutorsTest.java 2011/04/14 22:55:08 1.38 @@ -1,7 +1,7 @@ /* * 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. */ @@ -16,7 +16,7 @@ import java.security.*; public class ExecutorsTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(ExecutorsTest.class); @@ -181,53 +181,81 @@ public class ExecutorsTest extends JSR16 * 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. + * 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); - }}; + final Runnable sleeper = new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(LONG_DELAY_MS); + }}; for (ExecutorService executor : new ExecutorService[] { Executors.newSingleThreadExecutor(), @@ -258,25 +286,24 @@ public class ExecutorsTest extends JSR16 */ 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 - } + 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 } - }; + }}; ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); e.execute(r); @@ -316,8 +343,8 @@ 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()); }}; ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); e.execute(r); @@ -400,6 +427,9 @@ public class ExecutorsTest extends JSR16 * Without permissions, calling privilegedCallable throws ACE */ 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) @@ -474,7 +504,7 @@ public class ExecutorsTest extends JSR16 Executors.privilegedCallable(new CheckCCL()).call(); }}; - runWithPermissions(r, + runWithPermissions(r, new RuntimePermission("getClassLoader"), new RuntimePermission("setContextClassLoader")); }