1 /*
   2  * Copyright (c) 2013, 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 
  26 package jdk.jfr.startupargs;
  27 
  28 import java.time.Duration;
  29 
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.RecordingState;
  32 import jdk.testlibrary.Asserts;
  33 import jdk.testlibrary.jfr.CommonHelper;
  34 import jdk.testlibrary.OutputAnalyzer;
  35 import jdk.testlibrary.ProcessTools;
  36 import jdk.testlibrary.jfr.StartupHelper;
  37 
  38 /*
  39  * @test
  40  * @summary Start a recording with duration. Verify recording stops.
  41  * @key jfr
  42  * @library /lib/testlibrary
  43  * @run main jdk.jfr.startupargs.TestStartDuration
  44  */
  45 public class TestStartDuration {
  46 
  47     public static class TestValues {
  48         public static void main(String[] args) throws Exception {
  49             Recording r = StartupHelper.getRecording("TestStartDuration");
  50             Asserts.assertEquals(r.getDuration(), Duration.parse(args[0]));
  51             if (args.length > 1 && args[1].equals("wait")) {
  52                 CommonHelper.waitForRecordingState(r, RecordingState.STOPPED);
  53             }
  54         }
  55     }
  56 
  57     private static void testDurationInRange(String duration, Duration durationString, boolean wait) throws Exception {
  58         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
  59             "-XX:StartFlightRecording=name=TestStartDuration,duration=" + duration, TestValues.class.getName(),
  60             durationString.toString(), wait ? "wait" : "");
  61         OutputAnalyzer out = null;
  62         try {
  63             out = ProcessTools.executeProcess(pb);
  64         } catch (Throwable e) {
  65             e.printStackTrace();
  66         }
  67         out.shouldHaveExitValue(0);
  68     }
  69 
  70 
  71     private static void testDurationJavaVersion(String duration, boolean inRange) throws Exception {
  72         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
  73             "-XX:StartFlightRecording=name=TestStartDuration,duration=" + duration, "-version");
  74         OutputAnalyzer out = null;
  75         try {
  76             out = ProcessTools.executeProcess(pb);
  77         } catch (Throwable e) {
  78             e.printStackTrace();
  79         }
  80         if (inRange) {
  81             out.shouldHaveExitValue(0);
  82         } else {
  83             out.shouldContain("Could not start recording, duration must be at least 1 second.");
  84             out.shouldHaveExitValue(1);
  85         }
  86     }
  87 
  88     private static void testDurationInRangeAccept(String duration) throws Exception {
  89         testDurationJavaVersion(duration, true);
  90     }
  91 
  92     private static void testDurationOutOfRange(String duration) throws Exception {
  93         testDurationJavaVersion(duration, false);
  94     }
  95 
  96     public static void main(String[] args) throws Exception {
  97         testDurationInRange("1s", Duration.ofSeconds(1), true);
  98         testDurationInRange("1234003005ns", Duration.ofNanos(1234003005L), true);
  99         testDurationInRange("1034ms", Duration.ofMillis(1034), false);
 100         testDurationInRange("32m", Duration.ofMinutes(32), false);
 101         testDurationInRange("65h", Duration.ofHours(65), false);
 102         testDurationInRange("354d", Duration.ofDays(354), false);
 103 
 104         // additional test for corner values, verify that JVM accepts following durations
 105         testDurationInRangeAccept("1000000000ns");
 106         testDurationInRangeAccept("1000ms");
 107         testDurationInRangeAccept("1m");
 108         testDurationInRangeAccept("1h");
 109         testDurationInRangeAccept("1d");
 110 
 111         // out-of-range durations
 112         testDurationOutOfRange("0s");
 113         testDurationOutOfRange("999ms");
 114         testDurationOutOfRange("999999999ns");
 115         testDurationOutOfRange("0m");
 116         testDurationOutOfRange("0h");
 117         testDurationOutOfRange("0d");
 118     }
 119 }