1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import org.testng.annotations.Test;
  25 import org.testng.Assert;
  26 
  27 import jdk.test.lib.process.OutputAnalyzer;
  28 import jdk.test.lib.dcmd.CommandExecutor;
  29 import jdk.test.lib.dcmd.JMXExecutor;
  30 
  31 import java.text.NumberFormat;
  32 import java.text.ParseException;
  33 
  34 /*
  35  * @test
  36  * @summary Test of diagnostic command VM.uptime
  37  * @library /test/lib
  38  * @modules java.base/jdk.internal.misc
  39  *          java.compiler
  40  *          java.management
  41  *          jdk.jvmstat/sun.jvmstat.monitor
  42  * @run testng UptimeTest
  43  */
  44 public class UptimeTest {
  45     public void run(CommandExecutor executor) {
  46         double someUptime = 1.0;
  47         long startTime = System.currentTimeMillis();
  48         try {
  49             synchronized (this) {
  50                 /* Loop to guard against spurious wake ups */
  51                 while (System.currentTimeMillis() < (startTime + someUptime * 1000)) {
  52                     wait((int) someUptime * 1000);
  53                 }
  54             }
  55         } catch (InterruptedException e) {
  56             Assert.fail("Test error: Exception caught when sleeping:", e);
  57         }
  58 
  59         OutputAnalyzer output = executor.execute("VM.uptime");
  60 
  61         output.stderrShouldBeEmpty();
  62 
  63         /*
  64          * Output should be:
  65          * [pid]:
  66          * xx.yyy s
  67          *
  68          * If there is only one line in output there is no "[pid]:" printout;
  69          * skip first line, split on whitespace and grab first half
  70          */
  71         int index = output.asLines().size() == 1 ? 0 : 1;
  72         String uptimeString = output.asLines().get(index).split("\\s+")[0];
  73 
  74         try {
  75             double uptime = NumberFormat.getNumberInstance().parse(uptimeString).doubleValue();
  76             if (uptime < someUptime) {
  77                 Assert.fail(String.format(
  78                         "Test failure: Uptime was less than intended sleep time: %.3f s < %.3f s",
  79                         uptime, someUptime));
  80             }
  81         } catch (ParseException e) {
  82             Assert.fail("Test failure: Could not parse uptime string: " +
  83                     uptimeString, e);
  84         }
  85     }
  86 
  87     @Test
  88     public void jmx() {
  89         run(new JMXExecutor());
  90     }
  91 }