--- jsr166/src/test/tck/ForkJoinPoolTest.java 2015/10/05 22:54:45 1.64 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2018/11/21 23:42:12 1.77 @@ -11,6 +11,7 @@ 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.Callable; import java.util.concurrent.CountDownLatch; @@ -24,9 +25,9 @@ import java.util.concurrent.Future; import java.util.concurrent.RecursiveTask; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; -import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; @@ -57,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); } } @@ -113,7 +109,7 @@ public class ForkJoinPoolTest extends JS 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(); } } @@ -211,12 +207,26 @@ public class ForkJoinPoolTest extends JS * getPoolSize returns number of started workers. */ public void testGetPoolSize() { - ForkJoinPool p = new ForkJoinPool(1); + 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()); + assertEquals(1, p.getActiveThreadCount()); + done.countDown(); } + assertEquals(0, p.getPoolSize()); + assertEquals(0, p.getActiveThreadCount()); } /** @@ -230,8 +240,8 @@ public class ForkJoinPoolTest extends JS assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); assertFalse(p.awaitTermination(-1L, NANOSECONDS)); assertFalse(p.awaitTermination(-1L, MILLISECONDS)); - assertFalse(p.awaitTermination(0L, NANOSECONDS)); - assertFalse(p.awaitTermination(0L, MILLISECONDS)); + assertFalse(p.awaitTermination(randomExpiredTimeout(), + randomTimeUnit())); long timeoutNanos = 999999L; long startTime = System.nanoTime(); assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); @@ -256,23 +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()); + ueh, false); + try (PoolCleaner cleaner = cleaner(p)) { + assertSame(ueh, p.getUncaughtExceptionHandler()); try { p.execute(new FibTask(8)); - assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); - } catch (RejectedExecutionException ok) { + await(uehInvoked); + } finally { + p.shutdownNow(); // failure might have prevented processing task } - } finally { - p.shutdownNow(); // failure might have prevented processing task - joinPool(p); } } @@ -293,7 +303,7 @@ public class ForkJoinPoolTest extends JS p.getFactory()); while (! p.isQuiescent()) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) - throw new AssertionFailedError("timed out"); + throw new AssertionError("timed out"); assertFalse(p.getAsyncMode()); assertFalse(p.isShutdown()); assertFalse(p.isTerminating()); @@ -303,15 +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()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -409,7 +422,7 @@ public class ForkJoinPoolTest extends JS done.set(true); }}); assertNull(future.get()); - assertNull(future.get(0, MILLISECONDS)); + assertNull(future.get(randomExpiredTimeout(), randomTimeUnit())); assertTrue(done.get()); assertTrue(future.isDone()); assertFalse(future.isCancelled()); @@ -618,7 +631,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny3() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(null); try { e.invokeAny(l); @@ -634,7 +647,7 @@ public class ForkJoinPoolTest extends JS CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { @@ -651,7 +664,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l); @@ -668,7 +681,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAny6() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l); @@ -690,13 +703,14 @@ public class ForkJoinPoolTest extends JS } /** - * invokeAll(empty collection) returns empty collection + * invokeAll(empty collection) returns empty list */ public void testInvokeAll2() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); + final Collection> emptyCollection + = Collections.emptyList(); try (PoolCleaner cleaner = cleaner(e)) { - List> r - = e.invokeAll(new ArrayList>()); + List> r = e.invokeAll(emptyCollection); assertTrue(r.isEmpty()); } } @@ -707,7 +721,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { @@ -724,7 +738,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l); assertEquals(1, futures.size()); @@ -743,7 +757,7 @@ public class ForkJoinPoolTest extends JS public void testInvokeAll5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures = e.invokeAll(l); @@ -760,7 +774,7 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(null, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -772,10 +786,10 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAnyNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, null); + e.invokeAny(l, randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} } @@ -789,7 +803,7 @@ public class ForkJoinPoolTest extends JS try (PoolCleaner cleaner = cleaner(e)) { try { e.invokeAny(new ArrayList>(), - MEDIUM_DELAY_MS, MILLISECONDS); + randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -802,11 +816,11 @@ public class ForkJoinPoolTest extends JS CountDownLatch latch = new CountDownLatch(1); ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(latchAwaitingStringTask(latch)); l.add(null); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} latch.countDown(); @@ -819,14 +833,16 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAny4() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + long startTime = System.nanoTime(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -836,11 +852,13 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAny5() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + 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); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } } @@ -851,7 +869,7 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(null, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -863,24 +881,26 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAllNullTimeUnit() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); try { - e.invokeAll(l, MEDIUM_DELAY_MS, null); + 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); + 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()); } } @@ -891,11 +911,11 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll3() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(null); try { - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(l, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -907,7 +927,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll4() throws Throwable { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); List> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); @@ -927,7 +947,7 @@ public class ForkJoinPoolTest extends JS public void testTimedInvokeAll5() throws Throwable { ForkJoinPool e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); List> futures