--- jsr166/src/test/tck/ForkJoinPoolTest.java 2015/10/08 03:08:37 1.68 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2021/01/26 13:33:06 1.79 @@ -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,13 +58,6 @@ 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 @@ -74,9 +68,9 @@ public class ForkJoinPoolTest extends JS 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); } } @@ -115,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(); } } @@ -213,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()); } /** @@ -232,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)); @@ -295,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()); @@ -305,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); } } @@ -337,7 +348,7 @@ public class ForkJoinPoolTest extends JS p.shutdown(); assertTrue(p.isShutdown()); try { - ForkJoinTask f = p.submit(new FibTask(8)); + ForkJoinTask unused = p.submit(new FibTask(8)); shouldThrow(); } catch (RejectedExecutionException success) {} } @@ -366,10 +377,10 @@ public class ForkJoinPoolTest extends JS final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); 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(); + 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()); done.countDown(); @@ -383,13 +394,13 @@ public class ForkJoinPoolTest extends JS final CountDownLatch done = new CountDownLatch(1); SubFJP p = new SubFJP(); 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(); + 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()); } @@ -411,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()); @@ -461,13 +472,13 @@ public class ForkJoinPoolTest extends JS * A submitted privileged action runs to completion */ public void testSubmitPrivilegedAction() throws Exception { - final Callable callable = Executors.callable(new PrivilegedAction() { + 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 (PoolCleaner cleaner = cleaner(e)) { - Future future = e.submit(callable); + Future future = e.submit(callable); assertSame(TEST_STRING, future.get()); } }}; @@ -479,14 +490,14 @@ public class ForkJoinPoolTest extends JS * A submitted privileged exception action runs to completion */ public void testSubmitPrivilegedExceptionAction() throws Exception { - final Callable callable = - Executors.callable(new PrivilegedExceptionAction() { + 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 (PoolCleaner cleaner = cleaner(e)) { - Future future = e.submit(callable); + Future future = e.submit(callable); assertSame(TEST_STRING, future.get()); } }}; @@ -498,14 +509,14 @@ 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() { + 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 (PoolCleaner cleaner = cleaner(e)) { - Future future = e.submit(callable); + Future future = e.submit(callable); try { future.get(); shouldThrow(); @@ -525,7 +536,7 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { try { - Future future = e.submit((Runnable) null); + Future unused = e.submit((Runnable) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -538,7 +549,7 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { try { - Future future = e.submit((Callable) null); + Future unused = e.submit((Callable) null); shouldThrow(); } catch (NullPointerException success) {} } @@ -578,7 +589,7 @@ public class ForkJoinPoolTest extends JS ForkJoinPool p = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(p)) { try { - p.submit(new Callable() { + p.submit(new Callable() { public Object call() { throw new ArithmeticException(); }}) .get(); shouldThrow(); @@ -620,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); @@ -636,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 { @@ -653,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); @@ -670,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); @@ -692,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()); } } @@ -709,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 { @@ -726,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()); @@ -745,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); @@ -762,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) {} } @@ -774,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) {} } @@ -791,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) {} } @@ -804,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(); @@ -822,7 +834,7 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { long startTime = System.nanoTime(); - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new NPETask()); try { e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); @@ -841,7 +853,7 @@ public class ForkJoinPoolTest extends JS ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { long startTime = System.nanoTime(); - List> l = new ArrayList>(); + List> l = new ArrayList<>(); l.add(new StringTask()); l.add(new StringTask()); String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); @@ -857,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) {} } @@ -869,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()); } } @@ -897,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) {} } @@ -913,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); @@ -933,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