changeset 10311:481390d5d8ce

8059070: [TESTBUG] java/lang/invoke/LFCaching/LFMultiThreadCachingTest.java failed - timeout Reviewed-by: psandoz, vlivanov, dfuchs
author kshefov
date Fri, 05 Dec 2014 18:55:29 +0300
parents ba1b84e5a697
children 4d847d8dfedb
files test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java test/java/lang/invoke/LFCaching/LFMultiThreadCachingTest.java test/java/lang/invoke/LFCaching/LFSingleThreadCachingTest.java test/java/lang/invoke/LFCaching/LambdaFormTestCase.java test/lib/testlibrary/jdk/testlibrary/TimeLimitedRunner.java test/lib/testlibrary/jdk/testlibrary/Utils.java
diffstat 6 files changed, 150 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java	Wed Dec 03 18:04:50 2014 +0300
+++ b/test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java	Fri Dec 05 18:55:29 2014 +0300
@@ -26,12 +26,12 @@
  * @bug 8046703
  * @summary Test verifies that lambda forms are garbage collected
  * @author kshefov
+ * @library /lib/testlibrary/jsr292 /lib/testlibrary
  * @ignore 8057020
- * @library /lib/testlibrary/jsr292 /lib/testlibrary
  * @build TestMethods
  * @build LambdaFormTestCase
  * @build LFGarbageCollectedTest
- * @run main/othervm/timeout=600 -DtestLimit=150 LFGarbageCollectedTest
+ * @run main/othervm LFGarbageCollectedTest
  */
 
 import java.lang.invoke.MethodHandle;
--- a/test/java/lang/invoke/LFCaching/LFMultiThreadCachingTest.java	Wed Dec 03 18:04:50 2014 +0300
+++ b/test/java/lang/invoke/LFCaching/LFMultiThreadCachingTest.java	Fri Dec 05 18:55:29 2014 +0300
@@ -31,7 +31,7 @@
  * @build LambdaFormTestCase
  * @build LFCachingTestCase
  * @build LFMultiThreadCachingTest
- * @run main/othervm/timeout=300 LFMultiThreadCachingTest
+ * @run main/othervm LFMultiThreadCachingTest
  */
 
 import java.lang.invoke.MethodHandle;
--- a/test/java/lang/invoke/LFCaching/LFSingleThreadCachingTest.java	Wed Dec 03 18:04:50 2014 +0300
+++ b/test/java/lang/invoke/LFCaching/LFSingleThreadCachingTest.java	Fri Dec 05 18:55:29 2014 +0300
@@ -31,7 +31,7 @@
  * @build LambdaFormTestCase
  * @build LFCachingTestCase
  * @build LFSingleThreadCachingTest
- * @run main/othervm/timeout=300 LFSingleThreadCachingTest
+ * @run main/othervm LFSingleThreadCachingTest
  */
 
 import java.lang.invoke.MethodHandle;
--- a/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java	Wed Dec 03 18:04:50 2014 +0300
+++ b/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java	Fri Dec 05 18:55:29 2014 +0300
@@ -27,6 +27,8 @@
 import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.function.Function;
+import jdk.testlibrary.Utils;
+import jdk.testlibrary.TimeLimitedRunner;
 
 /**
  * Lambda forms caching test case class. Contains all necessary test routines to
@@ -41,6 +43,7 @@
     private final static String INTERNAL_FORM_METHOD_NAME = "internalForm";
     private static final double ITERATIONS_TO_CODE_CACHE_SIZE_RATIO
             = 45 / (128.0 * 1024 * 1024);
+    private static final long TIMEOUT = Helper.IS_THOROUGH ? 0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9);
 
     /**
      * Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is
@@ -59,6 +62,11 @@
     }
 
     private final TestMethods testMethod;
+    private static long totalIterations = 0L;
+    private static long doneIterations = 0L;
+    private static boolean passed = true;
+    private static int testCounter = 0;
+    private static int failCounter = 0;
 
     /**
      * Test case constructor. Generates test cases with random method types for
@@ -88,13 +96,10 @@
      * @param testMethods list of test methods
      */
     public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {
-        boolean passed = true;
-        int testCounter = 0;
-        int failCounter = 0;
         long testCaseNum = testMethods.size();
-        long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
+        totalIterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);
         System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",
-                iterations, iterations * testCaseNum);
+                totalIterations, totalIterations * testCaseNum);
         HotSpotDiagnosticMXBean hsDiagBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
         long codeCacheSize = Long.parseLong(
                 hsDiagBean.getVMOption("ReservedCodeCacheSize").getValue());
@@ -103,29 +108,42 @@
                 * ITERATIONS_TO_CODE_CACHE_SIZE_RATIO);
         System.out.printf("Number of iterations limited by code cache size is %d (%d cases)%n",
                 iterationsByCodeCacheSize, iterationsByCodeCacheSize * testCaseNum);
-        if (iterations > iterationsByCodeCacheSize) {
-            iterations = iterationsByCodeCacheSize;
+        if (totalIterations > iterationsByCodeCacheSize) {
+            totalIterations = iterationsByCodeCacheSize;
         }
         System.out.printf("Number of iterations is set to %d (%d cases)%n",
-                iterations, iterations * testCaseNum);
+                totalIterations, totalIterations * testCaseNum);
         System.out.flush();
-        for (long i = 0; i < iterations; i++) {
-            System.err.println(String.format("Iteration %d:", i));
-            for (TestMethods testMethod : testMethods) {
-                LambdaFormTestCase testCase = ctor.apply(testMethod);
-                try {
-                    System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
-                            testCase.getTestMethod().name);
-                    testCase.doTest();
-                    System.err.println("PASSED");
-                } catch (Throwable t) {
-                    t.printStackTrace();
-                    System.err.println("FAILED");
-                    passed = false;
-                    failCounter++;
-                }
-                testCounter++;
-            }
+        TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d,
+                () -> {
+                    if (doneIterations >= totalIterations) {
+                        return false;
+                    }
+                    System.err.println(String.format("Iteration %d:", doneIterations));
+                    for (TestMethods testMethod : testMethods) {
+                        LambdaFormTestCase testCase = ctor.apply(testMethod);
+                        try {
+                            System.err.printf("Tested LF caching feature with MethodHandles.%s method.%n",
+                                    testCase.getTestMethod().name);
+                            testCase.doTest();
+                            System.err.println("PASSED");
+                        } catch (Throwable t) {
+                            t.printStackTrace();
+                            System.err.println("FAILED");
+                            passed = false;
+                            failCounter++;
+                        }
+                        testCounter++;
+                    }
+                    doneIterations++;
+                    return true;
+                });
+        try {
+            runner.call();
+        } catch (Throwable t) {
+            t.printStackTrace();
+            System.err.println("FAILED");
+            throw new Error("Unexpected error!");
         }
         if (!passed) {
             throw new Error(String.format("%d of %d test cases FAILED! %n"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/lib/testlibrary/jdk/testlibrary/TimeLimitedRunner.java	Fri Dec 05 18:55:29 2014 +0300
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.testlibrary;
+
+import java.util.Objects;
+import java.util.concurrent.Callable;
+
+/**
+ * Auxiliary class to run target w/ given timeout.
+ */
+public class TimeLimitedRunner implements Callable<Void> {
+    private final long              stoptime;
+    private final long              timeout;
+    private final double            factor;
+    private final Callable<Boolean> target;
+
+    /**
+     * @param timeout   a timeout. zero means no time limitation
+     * @param factor    a multiplier used to estimate next iteration time
+     * @param target    a target to run
+     * @throws NullPointerException     if target is null
+     * @throws IllegalArgumentException if timeout is negative or
+                                        factor isn't positive
+     */
+    public TimeLimitedRunner(long timeout, double factor,
+            Callable<Boolean> target) {
+        Objects.requireNonNull(target, "target must not be null");
+        if (timeout < 0) {
+            throw new IllegalArgumentException("timeout[" + timeout + "] < 0");
+        }
+        if (factor <= 0d) {
+            throw new IllegalArgumentException("factor[" + factor + "] <= 0");
+        }
+        this.stoptime = System.currentTimeMillis() + timeout;
+        this.timeout = timeout;
+        this.factor = factor;
+        this.target = target;
+    }
+
+    /**
+     * Runs @{linkplan target} while it returns true and timeout isn't exceeded
+     */
+    @Override
+    public Void call() throws Exception {
+        long maxDuration = 0L;
+        long iterStart = System.currentTimeMillis();
+        if (timeout != 0 && iterStart > stoptime) {
+            return null;
+        }
+        while (target.call()) {
+            if (timeout != 0) {
+                long iterDuration = System.currentTimeMillis() - iterStart;
+                maxDuration = Math.max(maxDuration, iterDuration);
+                iterStart = System.currentTimeMillis();
+                if (iterStart + (maxDuration * factor) > stoptime) {
+                    System.out.println("Not enough time to continue execution. "
+                            + "Interrupted.");
+                    break;
+                }
+            }
+        }
+        return null;
+    }
+
+}
--- a/test/lib/testlibrary/jdk/testlibrary/Utils.java	Wed Dec 03 18:04:50 2014 +0300
+++ b/test/lib/testlibrary/jdk/testlibrary/Utils.java	Fri Dec 05 18:55:29 2014 +0300
@@ -35,6 +35,7 @@
 import java.util.Collections;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Common library for various test helper functions.
@@ -67,6 +68,12 @@
        TIMEOUT_FACTOR = Double.parseDouble(toFactor);
     }
 
+    /**
+    * Returns the value of JTREG default test timeout in milliseconds
+    * converted to {@code long}.
+    */
+    public static final long DEFAULT_TEST_TIMEOUT = TimeUnit.SECONDS.toMillis(120);
+
     private Utils() {
         // Private constructor to prevent class instantiation
     }
@@ -259,4 +266,14 @@
             throw t;
         }
     }
+
+    /**
+     * Adjusts the provided timeout value for the TIMEOUT_FACTOR
+     * @param tOut the timeout value to be adjusted
+     * @return The timeout value adjusted for the value of "test.timeout.factor"
+     *         system property
+     */
+    public static long adjustTimeout(long tOut) {
+        return Math.round(tOut * Utils.TIMEOUT_FACTOR);
+    }
 }