1 /*
   2  * Copyright (c) 2013, 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.startupargs;
  27 
  28 import java.time.Duration;
  29 
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.RecordingState;
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.jfr.CommonHelper;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 import jdk.test.lib.process.ProcessTools;
  36 
  37 /**
  38  * @test
  39  * @summary Start a recording with duration. Verify recording stops.
  40  * @key jfr
  41  *
  42  * @library /lib /
  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 = ProcessTools.executeProcess(pb);
  62 
  63         out.shouldHaveExitValue(0);
  64     }
  65 
  66 
  67     private static void testDurationJavaVersion(String duration, boolean inRange) throws Exception {
  68         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
  69             "-XX:StartFlightRecording=name=TestStartDuration,duration=" + duration, "-version");
  70         OutputAnalyzer out = ProcessTools.executeProcess(pb);
  71 
  72         if (inRange) {
  73             out.shouldHaveExitValue(0);
  74         } else {
  75             out.shouldContain("Could not start recording, duration must be at least 1 second.");
  76             out.shouldHaveExitValue(1);
  77         }
  78     }
  79 
  80     private static void testDurationInRangeAccept(String duration) throws Exception {
  81         testDurationJavaVersion(duration, true);
  82     }
  83 
  84     private static void testDurationOutOfRange(String duration) throws Exception {
  85         testDurationJavaVersion(duration, false);
  86     }
  87 
  88     public static void main(String[] args) throws Exception {
  89         testDurationInRange("1s", Duration.ofSeconds(1), true);
  90         testDurationInRange("1234003005ns", Duration.ofNanos(1234003005L), true);
  91         testDurationInRange("1034ms", Duration.ofMillis(1034), false);
  92         testDurationInRange("32m", Duration.ofMinutes(32), false);
  93         testDurationInRange("65h", Duration.ofHours(65), false);
  94         testDurationInRange("354d", Duration.ofDays(354), false);
  95 
  96         // additional test for corner values, verify that JVM accepts following durations
  97         testDurationInRangeAccept("1000000000ns");
  98         testDurationInRangeAccept("1000ms");
  99         testDurationInRangeAccept("1m");
 100         testDurationInRangeAccept("1h");
 101         testDurationInRangeAccept("1d");
 102 
 103         // out-of-range durations
 104         testDurationOutOfRange("0s");
 105         testDurationOutOfRange("999ms");
 106         testDurationOutOfRange("999999999ns");
 107         testDurationOutOfRange("0m");
 108         testDurationOutOfRange("0h");
 109         testDurationOutOfRange("0d");
 110     }
 111 }