--- jsr166/src/test/tck/ForkJoinPoolTest.java 2011/05/15 17:32:29 1.40 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2017/05/29 19:15:02 1.74 @@ -4,41 +4,43 @@ * 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.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.AssertionFailedError; +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,7 +103,7 @@ 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; @@ -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,7 +158,7 @@ public class ForkJoinPoolTest extends JS */ public void testDefaultInitialState() { ForkJoinPool p = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, p.getFactory()); assertFalse(p.getAsyncMode()); @@ -173,8 +170,6 @@ public class ForkJoinPoolTest extends JS assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); assertFalse(p.isTerminated()); - } finally { - joinPool(p); } } @@ -198,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); } } @@ -215,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()); } } @@ -233,20 +266,23 @@ 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 + } } } @@ -256,9 +292,9 @@ public class ForkJoinPoolTest extends JS * the task has completed successfully, and construction * parameters continue to hold */ - public void testisQuiescent() throws Exception { + public void testIsQuiescent() throws Exception { ForkJoinPool p = new ForkJoinPool(2); - try { + try (PoolCleaner cleaner = cleaner(p)) { assertTrue(p.isQuiescent()); long startTime = System.nanoTime(); FibTask f = new FibTask(20); @@ -277,17 +313,18 @@ public class ForkJoinPoolTest extends JS 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()); assertTrue(f.isDone()); assertEquals(6765, (int) f.get()); - } finally { - joinPool(p); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -296,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); } } @@ -309,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)); shouldThrow(); } catch (RejectedExecutionException success) {} - } finally { - joinPool(p); } } @@ -343,16 +376,14 @@ public class ForkJoinPoolTest extends JS public void testPollSubmission() { final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); - try { + 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 { done.countDown(); - joinPool(p); } } @@ -362,7 +393,7 @@ public class ForkJoinPoolTest extends JS public void testDrainTasksTo() { final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); - try { + try (PoolCleaner cleaner = cleaner(p)) { ForkJoinTask a = p.submit(awaiter(done)); ForkJoinTask b = p.submit(awaiter(done)); ForkJoinTask c = p.submit(awaiter(done)); @@ -373,13 +404,10 @@ public class ForkJoinPoolTest extends JS assertTrue(r == a || r == b || r == c); assertFalse(r.isDone()); } - } finally { done.countDown(); - joinPool(p); } } - // FJ Versions of AbstractExecutorService tests /** @@ -387,33 +415,30 @@ public class ForkJoinPoolTest extends JS */ public void testExecuteRunnable() throws Throwable { ExecutorService e = new ForkJoinPool(1); - try { - TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); - assertFalse(task.isDone()); - Future future = e.submit(task); + 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(MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(task.isDone()); + assertNull(future.get(randomExpiredTimeout(), randomTimeUnit())); + assertTrue(done.get()); assertTrue(future.isDone()); assertFalse(future.isCancelled()); - } finally { - joinPool(e); } } - /** * 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()); assertSame(TEST_STRING, future.get()); assertTrue(future.isDone()); assertFalse(future.isCancelled()); - } finally { - joinPool(e); } } @@ -422,13 +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()); assertNull(future.get()); assertTrue(future.isDone()); assertFalse(future.isCancelled()); - } finally { - joinPool(e); } } @@ -437,13 +460,11 @@ 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); assertSame(TEST_STRING, future.get()); assertTrue(future.isDone()); assertFalse(future.isCancelled()); - } finally { - joinPool(e); } } @@ -451,35 +472,35 @@ public class ForkJoinPoolTest extends JS * A submitted privileged action runs to completion */ 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); - Future future = e.submit(Executors.callable(new PrivilegedAction() { - public Object run() { - return TEST_STRING; - }})); - + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(e)) { + Future future = e.submit(callable); assertSame(TEST_STRING, future.get()); - }}; + } + }}; - runWithPermissions(r, - new RuntimePermission("modifyThread")); + runWithPermissions(r, new RuntimePermission("modifyThread")); } /** * A submitted privileged exception action runs to completion */ 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); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() { - return TEST_STRING; - }})); - + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(e)) { + Future future = e.submit(callable); assertSame(TEST_STRING, future.get()); - }}; + } + }}; runWithPermissions(r, new RuntimePermission("modifyThread")); } @@ -488,20 +509,22 @@ public class ForkJoinPoolTest extends JS * A submitted failed privileged exception action reports exception */ 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); - Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { - public Object run() throws Exception { - throw new IndexOutOfBoundsException(); - }})); - + public void realRun() throws Exception { + ExecutorService e = new ForkJoinPool(1); + try (PoolCleaner cleaner = cleaner(e)) { + Future future = e.submit(callable); try { future.get(); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof IndexOutOfBoundsException); - }}}; + } + } + }}; runWithPermissions(r, new RuntimePermission("modifyThread")); } @@ -511,44 +534,40 @@ public class ForkJoinPoolTest extends JS */ public void testExecuteNullRunnable() { ExecutorService e = new ForkJoinPool(1); - try { - Future future = e.submit((Runnable) null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + Future future = e.submit((Runnable) null); + shouldThrow(); + } catch (NullPointerException success) {} } } - /** * submit(null callable) throws NullPointerException */ public void testSubmitNullCallable() { ExecutorService e = new ForkJoinPool(1); - try { - Future future = e.submit((Callable) null); - shouldThrow(); - } catch (NullPointerException success) { - } finally { - joinPool(e); + try (PoolCleaner cleaner = cleaner(e)) { + try { + Future future = 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() { 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); @@ -556,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); } } @@ -571,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); + } } } @@ -590,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) {} } } @@ -604,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) {} } } @@ -618,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) {} } } @@ -635,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); } } @@ -653,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); + } } } @@ -670,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); } } @@ -686,12 +694,11 @@ 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) {} } } @@ -700,12 +707,10 @@ public class ForkJoinPoolTest extends JS */ public void testInvokeAll2() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(e)) { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -714,15 +719,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) {} } } @@ -732,17 +736,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); + } } } @@ -751,31 +755,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) {} } } @@ -784,14 +784,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) {} } } @@ -800,13 +799,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) {} } } @@ -816,16 +814,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, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} latch.countDown(); - joinPool(e); } } @@ -834,15 +831,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); } } @@ -851,14 +850,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); } } @@ -867,12 +866,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, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -881,14 +879,13 @@ 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, MEDIUM_DELAY_MS, null); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -897,13 +894,11 @@ public class ForkJoinPoolTest extends JS */ public void testTimedInvokeAll2() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); - try { + try (PoolCleaner cleaner = cleaner(e)) { List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); assertTrue(r.isEmpty()); - } finally { - joinPool(e); } } @@ -912,15 +907,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, MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (NullPointerException success) {} } } @@ -929,18 +923,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); + } } } @@ -948,18 +942,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); } }