1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.jfr.jvm; 26 27 import java.nio.file.Paths; 28 29 import jdk.jfr.Configuration; 30 import jdk.jfr.Recording; 31 import jdk.jfr.internal.JVM; 32 33 /** 34 * @test 35 * @summary Checks that the JVM can rollback on native initialization failures. 36 * @key jfr 37 * @requires vm.hasJFR 38 * @library /test/lib 39 * @modules jdk.jfr/jdk.jfr.internal 40 * @run main/othervm jdk.jfr.jvm.TestCreateNative 41 */ 42 public class TestCreateNative { 43 44 // This is a white-box test where we fabricate a native initialization 45 // error by calling JMV#createNative(false), which will tear down 46 // all native structures after they are setup, as if something went wrong 47 // at the last step. 48 public static void main(String... args) throws Exception { 49 JVM jvm = JVM.getJVM(); 50 // Ensure that repeated failures can be handled 51 for (int i = 1; i < 4; i++) { 52 System.out.println("About to try failed initialization, attempt " + i + " out of 3"); 53 assertFailedInitialization(jvm); 54 System.out.println("As expected, initialization failed."); 55 } 56 // Ensure that Flight Recorder can be initialized properly after failures 57 Configuration defConfig = Configuration.getConfiguration("default"); 58 Recording r = new Recording(defConfig); 59 r.start(); 60 r.stop(); 61 r.dump(Paths.get("recording.jfr")); 62 r.close(); 63 } 64 65 private static void assertFailedInitialization(JVM jvm) throws Exception { 66 try { 67 jvm.createFailedNativeJFR(); 68 throw new Exception("Expected failure when creating native JFR"); 69 } catch (IllegalStateException ise) { 70 String message = ise.getMessage(); 71 if (!message.equals("Unable to start Jfr")) { 72 throw new Exception("Expected failure on initialization of native JFR"); 73 } 74 } 75 } 76 }