--- jsr166/src/test/tck/ForkJoinPoolTest.java 2016/09/15 01:18:01 1.71 +++ jsr166/src/test/tck/ForkJoinPoolTest.java 2021/01/27 01:57:24 1.80 @@ -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; @@ -27,7 +28,6 @@ import java.util.concurrent.atomic.Atomi import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; -import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; @@ -109,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(); } } @@ -217,7 +217,7 @@ public class ForkJoinPoolTest extends JS taskStarted.countDown(); assertEquals(1, p.getPoolSize()); assertEquals(1, p.getActiveThreadCount()); - done.await(); + await(done); }}; Future future = p.submit(task); await(taskStarted); @@ -240,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)); @@ -303,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()); @@ -348,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) {} } @@ -377,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(); @@ -394,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()); } @@ -422,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()); @@ -472,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()); } }}; @@ -490,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()); } }}; @@ -509,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(); @@ -536,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) {} } @@ -549,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) {} } @@ -561,7 +561,7 @@ public class ForkJoinPoolTest extends JS public void testInterruptedSubmit() throws InterruptedException { final CountDownLatch submitted = new CountDownLatch(1); final CountDownLatch quittingTime = new CountDownLatch(1); - final Callable awaiter = new CheckedCallable() { + final Callable awaiter = new CheckedCallable<>() { public Void realCall() throws InterruptedException { assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); return null; @@ -589,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(); @@ -631,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); @@ -647,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 { @@ -664,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); @@ -681,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); @@ -703,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()); } } @@ -720,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 { @@ -737,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()); @@ -756,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); @@ -773,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) {} } @@ -785,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) {} } @@ -802,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) {} } @@ -815,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(); @@ -833,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); @@ -852,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); @@ -868,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) {} } @@ -880,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()); } } @@ -908,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) {} } @@ -924,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); @@ -944,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