1 /*
   2  * Copyright (c) 2016, 2019, 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  * @library /lib/testlibrary
  38  * @modules jdk.jfr/jdk.jfr.internal
  39  * @run main/othervm jdk.jfr.jvm.TestCreateNative
  40  */
  41 public class TestCreateNative {
  42 
  43     // This is a white-box test where we fabricate a native initialization
  44     // error by calling JMV#createNative(false), which will tear down
  45     // all native structures after they are setup, as if something went wrong
  46     // at the last step.
  47     public static void main(String... args) throws Exception {
  48         JVM jvm = JVM.getJVM();
  49         // Ensure that repeated failures can be handled
  50         for (int i = 1; i < 4; i++) {
  51             System.out.println("About to try failed initialization, attempt " + i + " out of 3");
  52             assertFailedInitialization(jvm);
  53             System.out.println("As expected, initialization failed.");
  54         }
  55         // Ensure that Flight Recorder can be initialized properly after failures
  56         Configuration defConfig = Configuration.getConfiguration("default");
  57         Recording r = new Recording(defConfig);
  58         r.start();
  59         r.stop();
  60         r.dump(Paths.get("recording.jfr"));
  61         r.close();
  62     }
  63 
  64     private static void assertFailedInitialization(JVM jvm) throws Exception {
  65         try {
  66             jvm.createFailedNativeJFR();
  67             throw new Exception("Expected failure when creating native JFR");
  68         } catch (IllegalStateException ise) {
  69             String message = ise.getMessage();
  70             if (!message.equals("Unable to start Jfr")) {
  71                 throw new Exception("Expected failure on initialization of native JFR");
  72             }
  73         }
  74     }
  75 }