--- jsr166/src/test/tck/ForkJoinPoolTest.java 2010/09/17 16:49:25 1.29 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2021/01/27 01:57:24 1.80 @@ -1,44 +1,46 @@ /* * 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/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.AbstractExecutorService; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.Callable; -import java.util.concurrent.Future; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import java.util.concurrent.CancellationException; -import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.Future; import java.util.concurrent.RecursiveTask; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.security.AccessControlException; -import java.security.Policy; -import java.security.PrivilegedAction; -import java.security.PrivilegedExceptionAction; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinPoolTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ForkJoinPoolTest.class); } - /** + /* * Testing coverage notes: * * 1. shutdown and related methods are tested via super.joinPool. @@ -56,24 +58,19 @@ public class ForkJoinPoolTest extends JS // Some classes to test extension and factory methods - static class MyHandler implements Thread.UncaughtExceptionHandler { - volatile int catches = 0; - public void uncaughtException(Thread t, Throwable e) { - ++catches; - } - } + static class MyError extends Error {} // to test handlers static class FailingFJWSubclass extends ForkJoinWorkerThread { public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } - protected void onStart() { super.onStart(); throw new Error(); } + protected void onStart() { super.onStart(); throw new MyError(); } } static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { - volatile int calls = 0; + final AtomicInteger calls = new AtomicInteger(0); public ForkJoinWorkerThread newThread(ForkJoinPool p) { - if (++calls > 1) return null; + if (calls.incrementAndGet() > 1) return null; return new FailingFJWSubclass(p); } } @@ -106,13 +103,13 @@ public class ForkJoinPoolTest extends JS static final class FibTask extends RecursiveTask { final int number; FibTask(int n) { number = n; } - public Integer compute() { + protected Integer compute() { int n = number; if (n <= 1) return n; FibTask f1 = new FibTask(n - 1); f1.fork(); - return (new FibTask(n - 2)).compute() + f1.join(); + return new FibTask(n - 2).compute() + f1.join(); } } @@ -134,7 +131,7 @@ public class ForkJoinPoolTest extends JS this.locker = locker; this.lock = lock; } - public Integer compute() { + protected Integer compute() { int n; LockingFibTask f1 = null; LockingFibTask f2 = null; @@ -161,10 +158,9 @@ public class ForkJoinPoolTest extends JS */ public void testDefaultInitialState() { ForkJoinPool p = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); - assertTrue(p.isQuiescent()); assertFalse(p.getAsyncMode()); assertEquals(0, p.getActiveThreadCount()); assertEquals(0, p.getStealCount()); @@ -174,8 +170,6 @@ public class ForkJoinPoolTest extends JS assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); - } finally { - joinPool(p); } } @@ -199,16 +193,13 @@ public class ForkJoinPoolTest extends JS } catch (NullPointerException success) {} } - /** * getParallelism returns size set in constructor */ public void testGetParallelism() { ForkJoinPool p = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertEquals(1, p.getParallelism()); - } finally { - joinPool(p); } } @@ -216,13 +207,54 @@ public class ForkJoinPoolTest extends JS * getPoolSize returns number of started workers. */ public void testGetPoolSize() { - ForkJoinPool p = new ForkJoinPool(1); - try { + final CountDownLatch taskStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); + final ForkJoinPool p = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(p)) { assertEquals(0, p.getActiveThreadCount()); - Future future = p.submit(new StringTask()); + final Runnable task = new CheckedRunnable() { + public void realRun() throws InterruptedException { + taskStarted.countDown(); + assertEquals(1, p.getPoolSize()); + assertEquals(1, p.getActiveThreadCount()); + await(done); + }}; + Future future = p.submit(task); + await(taskStarted); assertEquals(1, p.getPoolSize()); - } finally { - joinPool(p); + assertEquals(1, p.getActiveThreadCount()); + done.countDown(); + } + assertEquals(0, p.getPoolSize()); + assertEquals(0, p.getActiveThreadCount()); + } + + /** + * awaitTermination on a non-shutdown pool times out + */ + public void testAwaitTermination_timesOut() throws InterruptedException { + ForkJoinPool p = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(p)) { + assertFalse(p.isTerminated()); + assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); + assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); + assertFalse(p.awaitTermination(-1L, NANOSECONDS)); + assertFalse(p.awaitTermination(-1L, MILLISECONDS)); + assertFalse(p.awaitTermination(randomExpiredTimeout(), + randomTimeUnit())); + long timeoutNanos = 999999L; + long startTime = System.nanoTime(); + assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); + assertTrue(System.nanoTime() - startTime >= timeoutNanos); + assertFalse(p.isTerminated()); + startTime = System.nanoTime(); + long timeoutMillis = timeoutMillis(); + assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + assertFalse(p.isTerminated()); + p.shutdown(); + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); } } @@ -234,46 +266,65 @@ public class ForkJoinPoolTest extends JS */ public void testSetUncaughtExceptionHandler() throws InterruptedException { final CountDownLatch uehInvoked = new CountDownLatch(1); - final Thread.UncaughtExceptionHandler eh = + final Thread.UncaughtExceptionHandler ueh = new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { + threadAssertTrue(e instanceof MyError); + threadAssertTrue(t instanceof FailingFJWSubclass); uehInvoked.countDown(); }}; ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), - eh, false); - try { - assertSame(eh, p.getUncaughtExceptionHandler()); - p.execute(new FibTask(8)); - assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); - } finally { - p.shutdownNow(); // failure might have prevented processing task - joinPool(p); + ueh, false); + try (PoolCleaner cleaner = cleaner(p)) { + assertSame(ueh, p.getUncaughtExceptionHandler()); + try { + p.execute(new FibTask(8)); + await(uehInvoked); + } finally { + p.shutdownNow(); // failure might have prevented processing task + } } } /** - * After invoking a single task, isQuiescent is true, - * queues are empty, threads are not active, and - * construction parameters continue to hold + * After invoking a single task, isQuiescent eventually becomes + * true, at which time queues are empty, threads are not active, + * the task has completed successfully, and construction + * parameters continue to hold */ - public void testisQuiescent() throws InterruptedException { + public void testIsQuiescent() throws Exception { ForkJoinPool p = new ForkJoinPool(2); - try { - p.invoke(new FibTask(20)); + try (PoolCleaner cleaner = cleaner(p)) { + assertTrue(p.isQuiescent()); + long startTime = System.nanoTime(); + FibTask f = new FibTask(20); + p.invoke(f); assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); - Thread.sleep(MEDIUM_DELAY_MS); + while (! p.isQuiescent()) { + if (millisElapsedSince(startTime) > LONG_DELAY_MS) + throw new AssertionError("timed out"); + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertTrue(p.isQuiescent()); assertFalse(p.getAsyncMode()); - assertEquals(0, p.getActiveThreadCount()); assertEquals(0, p.getQueuedTaskCount()); assertEquals(0, p.getQueuedSubmissionCount()); assertFalse(p.hasQueuedSubmissions()); + while (p.getActiveThreadCount() != 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); - } finally { - joinPool(p); + assertTrue(f.isDone()); + assertEquals(6765, (int) f.get()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -282,11 +333,9 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitForkJoinTask() throws Throwable { ForkJoinPool p = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { ForkJoinTask f = p.submit(new FibTask(8)); assertEquals(21, (int) f.get()); - } finally { - joinPool(p); } } @@ -295,15 +344,13 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitAfterShutdown() { ForkJoinPool p = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { p.shutdown(); assertTrue(p.isShutdown()); try { - ForkJoinTask f = p.submit(new FibTask(8)); + ForkJoinTask unused = p.submit(new FibTask(8)); shouldThrow(); } catch (RejectedExecutionException success) {} - } finally { - joinPool(p); } } @@ -315,9 +362,9 @@ public class ForkJoinPoolTest extends JS try { ReentrantLock lock = new ReentrantLock(); ManagedLocker locker = new ManagedLocker(lock); - ForkJoinTask f = new LockingFibTask(30, locker, lock); + ForkJoinTask f = new LockingFibTask(20, locker, lock); p.execute(f); - assertEquals(832040, (int) f.get()); + assertEquals(6765, (int) f.get()); } finally { p.shutdownNow(); // don't wait out shutdown } @@ -327,16 +374,16 @@ public class ForkJoinPoolTest extends JS * pollSubmission returns unexecuted submitted task, if present */ public void testPollSubmission() { + final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); - try { - ForkJoinTask a = p.submit(new MediumRunnable()); - ForkJoinTask b = p.submit(new MediumRunnable()); - ForkJoinTask c = p.submit(new MediumRunnable()); - ForkJoinTask r = p.pollSubmission(); + try (PoolCleaner cleaner = cleaner(p)) { + ForkJoinTask a = p.submit(awaiter(done)); + ForkJoinTask b = p.submit(awaiter(done)); + ForkJoinTask c = p.submit(awaiter(done)); + ForkJoinTask r = p.pollSubmission(); assertTrue(r == a || r == b || r == c); assertFalse(r.isDone()); - } finally { - joinPool(p); + done.countDown(); } } @@ -344,24 +391,23 @@ public class ForkJoinPoolTest extends JS * drainTasksTo transfers unexecuted submitted tasks, if present */ public void testDrainTasksTo() { + final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); - try { - ForkJoinTask a = p.submit(new MediumRunnable()); - ForkJoinTask b = p.submit(new MediumRunnable()); - ForkJoinTask c = p.submit(new MediumRunnable()); - ArrayList al = new ArrayList(); + try (PoolCleaner cleaner = cleaner(p)) { + ForkJoinTask a = p.submit(awaiter(done)); + ForkJoinTask b = p.submit(awaiter(done)); + ForkJoinTask c = p.submit(awaiter(done)); + ArrayList> al = new ArrayList<>(); p.drainTasksTo(al); assertTrue(al.size() > 0); - for (ForkJoinTask r : al) { + for (ForkJoinTask r : al) { assertTrue(r == a || r == b || r == c); assertFalse(r.isDone()); } - } finally { - joinPool(p); + done.countDown(); } } - // FJ Versions of AbstractExecutorService tests /** @@ -369,29 +415,30 @@ public class ForkJoinPoolTest extends JS */ public void testExecuteRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - TrackedShortRunnable task = new TrackedShortRunnable(); - assertFalse(task.done); - Future future = e.submit(task); - future.get(); - assertTrue(task.done); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + final AtomicBoolean done = new AtomicBoolean(false); + Future future = e.submit(new CheckedRunnable() { + public void realRun() { + done.set(true); + }}); + assertNull(future.get()); + assertNull(future.get(randomExpiredTimeout(), randomTimeUnit())); + assertTrue(done.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } } - /** * Completed submit(callable) returns result */ public void testSubmitCallable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(e)) { Future future = e.submit(new StringTask()); - String result = future.get(); - assertSame(TEST_STRING, result); - } finally { - joinPool(e); + assertSame(TEST_STRING, future.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } } @@ -400,12 +447,11 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(e)) { Future future = e.submit(new NoOpRunnable()); - future.get(); + assertNull(future.get()); assertTrue(future.isDone()); - } finally { - joinPool(e); + assertFalse(future.isCancelled()); } } @@ -414,115 +460,73 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitRunnable2() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(e)) { Future future = e.submit(new NoOpRunnable(), TEST_STRING); - String result = future.get(); - assertSame(TEST_STRING, result); - } finally { - joinPool(e); + assertSame(TEST_STRING, future.get()); + assertTrue(future.isDone()); + assertFalse(future.isCancelled()); } } - /** - * A submitted privileged action to completion + * A submitted privileged action runs to completion */ - public void testSubmitPrivilegedAction() throws Throwable { - Policy savedPolicy = null; - try { - savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - } catch (AccessControlException ok) { - return; - } - - try { + public void testSubmitPrivilegedAction() throws Exception { + final Callable callable = Executors.callable(new PrivilegedAction() { + public Object run() { return TEST_STRING; }}); + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { ExecutorService e = new ForkJoinPool(1); - try { - Future future = e.submit(Executors.callable(new PrivilegedAction() { - public Object run() { - return TEST_STRING; - }})); - - Object result = future.get(); - assertSame(TEST_STRING, result); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + Future future = e.submit(callable); + assertSame(TEST_STRING, future.get()); } - } finally { - Policy.setPolicy(savedPolicy); - } + }}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** - * A submitted a privileged exception action runs to completion + * A submitted privileged exception action runs to completion */ - public void testSubmitPrivilegedExceptionAction() throws Throwable { - Policy savedPolicy = null; - try { - savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - } catch (AccessControlException ok) { - return; - } - - try { + public void testSubmitPrivilegedExceptionAction() throws Exception { + final Callable callable = + Executors.callable(new PrivilegedExceptionAction() { + public Object run() { return TEST_STRING; }}); + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { ExecutorService e = new ForkJoinPool(1); - try { - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() { - return TEST_STRING; - }})); - - Object result = future.get(); - assertSame(TEST_STRING, result); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + Future future = e.submit(callable); + assertSame(TEST_STRING, future.get()); } - } finally { - Policy.setPolicy(savedPolicy); - } + }}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** * A submitted failed privileged exception action reports exception */ - public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { - Policy savedPolicy = null; - try { - savedPolicy = Policy.getPolicy(); - AdjustablePolicy policy = new AdjustablePolicy(); - policy.addPermission(new RuntimePermission("getContextClassLoader")); - policy.addPermission(new RuntimePermission("setContextClassLoader")); - Policy.setPolicy(policy); - } catch (AccessControlException ok) { - return; - } - - try { + public void testSubmitFailedPrivilegedExceptionAction() throws Exception { + final Callable callable = + Executors.callable(new PrivilegedExceptionAction() { + public Object run() { throw new IndexOutOfBoundsException(); }}); + Runnable r = new CheckedRunnable() { + public void realRun() throws Exception { ExecutorService e = new ForkJoinPool(1); - try { - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() throws Exception { - throw new IndexOutOfBoundsException(); - }})); - - Object result = future.get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof IndexOutOfBoundsException); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + Future future = e.submit(callable); + try { + future.get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof IndexOutOfBoundsException); + } } - } finally { - Policy.setPolicy(savedPolicy); - } + }}; + + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** @@ -530,46 +534,40 @@ public class ForkJoinPoolTest extends JS */ public void testExecuteNullRunnable() { ExecutorService e = new ForkJoinPool(1); - TrackedShortRunnable task = null; - try { - Future future = e.submit(task); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + Future unused = e.submit((Runnable) null); + shouldThrow(); + } catch (NullPointerException success) {} } } - /** * submit(null callable) throws NullPointerException */ public void testSubmitNullCallable() { ExecutorService e = new ForkJoinPool(1); - StringTask t = null; - try { - Future future = e.submit(t); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + Future unused = e.submit((Callable) null); + shouldThrow(); + } catch (NullPointerException success) {} } } - /** * submit(callable).get() throws InterruptedException if interrupted */ public void testInterruptedSubmit() throws InterruptedException { final CountDownLatch submitted = new CountDownLatch(1); final CountDownLatch quittingTime = new CountDownLatch(1); - final ExecutorService p = new ForkJoinPool(1); - final Callable awaiter = new CheckedCallable() { + final Callable awaiter = new CheckedCallable<>() { public Void realCall() throws InterruptedException { - assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); return null; }}; - try { + final ExecutorService p = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(p, quittingTime)) { Thread t = new Thread(new CheckedInterruptedRunnable() { public void realRun() throws Exception { Future future = p.submit(awaiter); @@ -577,12 +575,9 @@ public class ForkJoinPoolTest extends JS future.get(); }}); t.start(); - assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); + await(submitted); t.interrupt(); - t.join(); - } finally { - quittingTime.countDown(); - joinPool(p); + awaitTermination(t); } } @@ -592,17 +587,15 @@ public class ForkJoinPoolTest extends JS */ public void testSubmitEE() throws Throwable { ForkJoinPool p = new ForkJoinPool(1); - try { - p.submit(new Callable() { - public Object call() { - int i = 5/0; - return Boolean.TRUE; - }}).get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof ArithmeticException); - } finally { - joinPool(p); + try (PoolCleaner cleaner = cleaner(p)) { + try { + p.submit(new Callable() { + public Object call() { throw new ArithmeticException(); }}) + .get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof ArithmeticException); + } } } @@ -611,12 +604,11 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny1() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - e.invokeAny(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -625,12 +617,11 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny2() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - e.invokeAny(new ArrayList>()); - shouldThrow(); - } catch (IllegalArgumentException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(new ArrayList>()); + shouldThrow(); + } catch (IllegalArgumentException success) {} } } @@ -639,14 +630,13 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny3() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(null); - try { - e.invokeAny(l); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(null); + try { + e.invokeAny(l); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -656,16 +646,15 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny4() throws Throwable { CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(latchAwaitingStringTask(latch)); - l.add(null); - try { - e.invokeAny(l); - shouldThrow(); - } catch (NullPointerException success) { - } finally { + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); + try { + e.invokeAny(l); + shouldThrow(); + } catch (NullPointerException success) {} latch.countDown(); - joinPool(e); } } @@ -674,15 +663,15 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new NPETask()); - try { - e.invokeAny(l); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new NPETask()); + try { + e.invokeAny(l); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } } } @@ -691,14 +680,12 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAny6() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - List> l = new ArrayList>(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); assertSame(TEST_STRING, result); - } finally { - joinPool(e); } } @@ -707,26 +694,24 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll1() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - e.invokeAll(null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAll(null); + shouldThrow(); + } catch (NullPointerException success) {} } } /** - * invokeAll(empty collection) returns empty collection + * invokeAll(empty collection) returns empty list */ public void testInvokeAll2() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); - try { - List> r - = e.invokeAll(new ArrayList>()); + final Collection> emptyCollection + = Collections.emptyList(); + try (PoolCleaner cleaner = cleaner(e)) { + List> r = e.invokeAll(emptyCollection); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -735,15 +720,14 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); - try { - e.invokeAll(l); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new StringTask()); + l.add(null); + try { + e.invokeAll(l); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -753,17 +737,17 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new NPETask()); - List> futures = e.invokeAll(l); - assertEquals(1, futures.size()); - try { - futures.get(0).get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new NPETask()); + List> futures = e.invokeAll(l); + assertEquals(1, futures.size()); + try { + futures.get(0).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } } } @@ -772,31 +756,27 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll5() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - List> l = new ArrayList>(); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = e.invokeAll(l); assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get()); - } finally { - joinPool(e); } } - /** * timed invokeAny(null) throws NullPointerException */ public void testTimedInvokeAny1() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(null, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -805,14 +785,13 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAnyNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new StringTask()); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new StringTask()); + try { + e.invokeAny(l, randomTimeout(), null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -821,13 +800,12 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAny2() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - e.invokeAny(new ArrayList>(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (IllegalArgumentException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAny(new ArrayList>(), + randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (IllegalArgumentException success) {} } } @@ -837,16 +815,15 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAny3() throws Throwable { CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(latchAwaitingStringTask(latch)); - l.add(null); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(latchAwaitingStringTask(latch)); + l.add(null); + try { + e.invokeAny(l, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (NullPointerException success) {} latch.countDown(); - joinPool(e); } } @@ -855,15 +832,17 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAny4() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new NPETask()); - try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); + l.add(new NPETask()); + try { + e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -872,14 +851,14 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - List> l = new ArrayList>(); + try (PoolCleaner cleaner = cleaner(e)) { + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); - String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); assertSame(TEST_STRING, result); - } finally { - joinPool(e); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -888,12 +867,11 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAll1() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + e.invokeAll(null, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -902,29 +880,28 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAllNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new StringTask()); - try { - e.invokeAll(l, MEDIUM_DELAY_MS, null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new StringTask()); + try { + e.invokeAll(l, randomTimeout(), null); + shouldThrow(); + } catch (NullPointerException success) {} } } /** - * timed invokeAll(empty collection) returns empty collection + * timed invokeAll(empty collection) returns empty list */ public void testTimedInvokeAll2() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); - try { + final Collection> emptyCollection + = Collections.emptyList(); + try (PoolCleaner cleaner = cleaner(e)) { List> r - = e.invokeAll(new ArrayList>(), - MEDIUM_DELAY_MS, MILLISECONDS); + = e.invokeAll(emptyCollection, + randomTimeout(), randomTimeUnit()); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -933,15 +910,14 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(null); - try { - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new StringTask()); + l.add(null); + try { + e.invokeAll(l, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -950,18 +926,18 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); - List> l = new ArrayList>(); - l.add(new NPETask()); - List> futures - = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); - assertEquals(1, futures.size()); - try { - futures.get(0).get(); - shouldThrow(); - } catch (ExecutionException success) { - assertTrue(success.getCause() instanceof NullPointerException); - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); + l.add(new NPETask()); + List> futures + = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); + assertEquals(1, futures.size()); + try { + futures.get(0).get(); + shouldThrow(); + } catch (ExecutionException success) { + assertTrue(success.getCause() instanceof NullPointerException); + } } } @@ -969,18 +945,16 @@ public class ForkJoinPoolTest extends JS * timed invokeAll(c) returns results of all completed tasks in c */ public void testTimedInvokeAll5() throws Throwable { - ExecutorService e = new ForkJoinPool(1); - try { - List> l = new ArrayList>(); + ForkJoinPool e = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(e)) { + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures - = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(2, futures.size()); for (Future future : futures) assertSame(TEST_STRING, future.get()); - } finally { - joinPool(e); } }