Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/LFCaching/LambdaFormTestCase.java
48803 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.oracle.testlibrary.jsr292.Helper;24import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;25import java.lang.invoke.MethodHandle;26import java.lang.management.GarbageCollectorMXBean;27import java.lang.management.ManagementFactory;28import java.lang.ref.Reference;29import java.lang.reflect.Field;30import java.lang.reflect.Method;31import java.util.Collection;32import java.util.List;33import java.util.function.Function;34import jdk.testlibrary.Utils;35import jdk.testlibrary.TimeLimitedRunner;3637/**38* Lambda forms caching test case class. Contains all necessary test routines to39* test lambda forms caching in method handles returned by methods of40* MethodHandles class.41*42* @author kshefov43*/44public abstract class LambdaFormTestCase {4546private static final long TIMEOUT = Helper.IS_THOROUGH ? 0L : (long) (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) * 0.9);4748/**49* Reflection link to {@code j.l.i.MethodHandle.internalForm} method. It is50* used to get a lambda form from a method handle.51*/52protected final static Method INTERNAL_FORM;53protected final static Field DEBUG_NAME;54protected final static Field REF_FIELD;55private static final List<GarbageCollectorMXBean> gcInfo;5657private static long gcCount() {58return gcInfo.stream().mapToLong(GarbageCollectorMXBean::getCollectionCount).sum();59}6061static {62try {63INTERNAL_FORM = MethodHandle.class.getDeclaredMethod("internalForm");64INTERNAL_FORM.setAccessible(true);6566DEBUG_NAME = Class.forName("java.lang.invoke.LambdaForm").getDeclaredField("debugName");67DEBUG_NAME.setAccessible(true);6869REF_FIELD = Reference.class.getDeclaredField("referent");70REF_FIELD.setAccessible(true);71} catch (Exception ex) {72throw new Error("Unexpected exception", ex);73}7475gcInfo = ManagementFactory.getGarbageCollectorMXBeans();76if (gcInfo.size() == 0) {77throw new Error("No GarbageCollectorMXBeans found.");78}79}8081private final TestMethods testMethod;82private long gcCountAtStart;8384private static class TestRun {8586final Function<TestMethods, LambdaFormTestCase> ctor;87final Collection<TestMethods> testMethods;88final long totalIterations;89long doneIterations;90long testCounter;91long failCounter;92boolean passed;9394TestRun(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {95this.ctor = ctor;96this.testMethods = testMethods;97long testCaseNum = testMethods.size();98long iterations = Math.max(1, Helper.TEST_LIMIT / testCaseNum);99System.out.printf("Number of iterations according to -DtestLimit is %d (%d cases)%n",100iterations, iterations * testCaseNum);101System.out.printf("Number of iterations is set to %d (%d cases)%n",102iterations, iterations * testCaseNum);103System.out.flush();104totalIterations = iterations;105doneIterations = 0L;106testCounter = 0L;107failCounter = 0L;108passed = true;109}110111Boolean doIteration() {112if (doneIterations >= totalIterations) {113return false;114}115System.err.println(String.format("Iteration %d:", doneIterations));116for (TestMethods testMethod : testMethods) {117LambdaFormTestCase testCase = ctor.apply(testMethod);118try {119System.err.printf("Tested LF caching feature"120+ " with MethodHandles.%s method.%n",121testCase.getTestMethod().name);122Throwable t = CodeCacheOverflowProcessor123.runMHTest(testCase::doTest);124if (t != null) {125return false;126}127System.err.println("PASSED");128} catch (OutOfMemoryError oome) {129// Don't swallow OOME so a heap dump can be created.130System.err.println("FAILED");131throw oome;132} catch (Throwable t) {133t.printStackTrace();134System.err.printf("FAILED. Caused by %s%n", t.getMessage());135passed = false;136failCounter++;137}138testCounter++;139}140doneIterations++;141return true;142}143144void checkPassed() {145if (!passed) {146throw new Error(String.format("%d of %d test cases FAILED! %n"147+ "Rerun the test with the same \"-Dseed=\" option as in the log file!",148failCounter, testCounter));149} else {150System.err.printf("All %d test cases PASSED!%n", testCounter);151}152}153}154155/**156* Test case constructor. Generates test cases with random method types for157* given methods form {@code j.l.i.MethodHandles} class.158*159* @param testMethod A method from {@code j.l.i.MethodHandles} class which160* returns a {@code j.l.i.MethodHandle}.161*/162protected LambdaFormTestCase(TestMethods testMethod) {163this.testMethod = testMethod;164this.gcCountAtStart = gcCount();165}166167public TestMethods getTestMethod() {168return testMethod;169}170171protected boolean noGCHappened() {172return gcCount() == gcCountAtStart;173}174175/**176* Routine that executes a test case.177*/178public abstract void doTest();179180/**181* Runs a number of test cases defined by the size of testCases list.182*183* @param ctor constructor of LambdaFormCachingTest or its child classes184* object.185* @param testMethods list of test methods186*/187public static void runTests(Function<TestMethods, LambdaFormTestCase> ctor, Collection<TestMethods> testMethods) {188LambdaFormTestCase.TestRun run189= new LambdaFormTestCase.TestRun(ctor, testMethods);190TimeLimitedRunner runner = new TimeLimitedRunner(TIMEOUT, 4.0d, run::doIteration);191try {192runner.call();193} catch (Exception ex) {194System.err.println("FAILED");195throw new Error("Unexpected error!", ex);196}197run.checkPassed();198}199}200201202