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