test/jdk/jdk/jfr/jcmd/JcmdHelper.java
changeset 50113 caf115bb98ad
child 50226 408021edf22f
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2015, 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 
       
    26 package jdk.jfr.jcmd;
       
    27 
       
    28 import java.io.File;
       
    29 import java.util.Arrays;
       
    30 import java.util.stream.Collectors;
       
    31 
       
    32 import jdk.test.lib.Asserts;
       
    33 import jdk.test.lib.dcmd.CommandExecutor;
       
    34 import jdk.test.lib.dcmd.PidJcmdExecutor;
       
    35 import jdk.test.lib.process.OutputAnalyzer;
       
    36 
       
    37 public class JcmdHelper {
       
    38 
       
    39     // Wait until recording's state became running
       
    40     public static void waitUntilRunning(String name) throws Exception {
       
    41         long timeoutAt = System.currentTimeMillis() + 10000;
       
    42         while (true) {
       
    43             OutputAnalyzer output = jcmdCheck(name, false);
       
    44             try {
       
    45                 // The expected output can look like this:
       
    46                 // Recording: recording=1 name="Recording 1" (running)
       
    47                 output.shouldMatch("^Recording: recording=\\d+\\s+name=\"" + name
       
    48                         + "\".*\\W{1}running\\W{1}");
       
    49                 return;
       
    50             } catch (RuntimeException e) {
       
    51                 if (System.currentTimeMillis() > timeoutAt) {
       
    52                     Asserts.fail("Recording not started: " + name);
       
    53                 }
       
    54                 Thread.sleep(100);
       
    55             }
       
    56         }
       
    57     }
       
    58 
       
    59     // Wait until default recording's state became running
       
    60     public static void waitUntilDefaultRecordingRunning() throws Exception {
       
    61         while (true) {
       
    62             OutputAnalyzer output = jcmd("JFR.check", "recording=0");
       
    63             try {
       
    64                 output.shouldContain("Recording: recording=0 name=\"HotSpot default\" (running)");
       
    65                 return;
       
    66             } catch (RuntimeException e) {
       
    67                 Thread.sleep(100);
       
    68             }
       
    69         }
       
    70     }
       
    71 
       
    72     public static void stopAndCheck(String name) throws Exception {
       
    73         jcmd("JFR.stop", "name=\"" + name + "\"");
       
    74         assertRecordingNotRunning(name);
       
    75     }
       
    76 
       
    77     public static void stopWriteToFileAndCheck(String name, File file) throws Exception {
       
    78         OutputAnalyzer output = jcmd("JFR.stop",
       
    79                 "name=\"" + name + "\"",
       
    80                 "filename=\"" + file.getAbsolutePath() + "\"");
       
    81         JcmdAsserts.assertStoppedAndWrittenTo(output, name, file);
       
    82         assertRecordingNotRunning(name);
       
    83     }
       
    84 
       
    85     public static void stopCompressAndCheck(String name, File file) throws Exception {
       
    86         OutputAnalyzer output = jcmd("JFR.stop",
       
    87                 "name=\"" + name + "\"",
       
    88                 "compress=true",
       
    89                 "filename=\"" + file.getAbsolutePath() + "\"");
       
    90         JcmdAsserts.assertStoppedAndWrittenTo(output, name, file);
       
    91         checkAndAssertNoRecordingsAvailable();
       
    92     }
       
    93 
       
    94     public static void stopDefaultRecordingAndCheck() throws Exception {
       
    95         OutputAnalyzer output = jcmd("JFR.stop", "recording=0");
       
    96         JcmdAsserts.assertStoppedDefaultRecording(output);
       
    97         checkAndAssertNoRecordingsAvailable();
       
    98     }
       
    99 
       
   100     public static void checkAndAssertNoRecordingsAvailable() throws Exception {
       
   101         OutputAnalyzer output = jcmd("JFR.check");
       
   102         JcmdAsserts.assertNoRecordingsAvailable(output);
       
   103     }
       
   104 
       
   105     public static void assertRecordingNotExist(String name) throws Exception {
       
   106         OutputAnalyzer output = jcmdCheck(name, false);
       
   107         JcmdAsserts.assertRecordingNotExist(output, name);
       
   108     }
       
   109 
       
   110     public static void assertRecordingNotRunning(String name) throws Exception {
       
   111         OutputAnalyzer output = jcmdCheck(name, false);
       
   112         JcmdAsserts.assertRecordingNotRunning(output, name);
       
   113     }
       
   114 
       
   115     public static void assertRecordingIsRunning(String name) throws Exception {
       
   116         OutputAnalyzer output = jcmdCheck(name, false);
       
   117         JcmdAsserts.assertRecordingIsRunning(output, name);
       
   118     }
       
   119 
       
   120     public static OutputAnalyzer jcmd(int expectedExitValue, String... args) {
       
   121         String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
       
   122         CommandExecutor executor = new PidJcmdExecutor();
       
   123         OutputAnalyzer oa = executor.execute(argsString);
       
   124         oa.shouldHaveExitValue(expectedExitValue);
       
   125         return oa;
       
   126     }
       
   127 
       
   128     public static OutputAnalyzer jcmd(String... args) {
       
   129         return jcmd(0, args);
       
   130     }
       
   131 
       
   132 
       
   133     public static OutputAnalyzer jcmdCheck(String recordingName, boolean verbose) {
       
   134         return jcmd("JFR.check", "name=" + recordingName, "verbose=" + verbose);
       
   135     }
       
   136 }