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.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 /testlibrary 38 * @modules java.base/jdk.internal.misc 39 * java.compiler 40 * java.management 41 * jdk.jvmstat/sun.jvmstat.monitor 42 * @build jdk.test.lib.* 43 * @build jdk.test.lib.dcmd.* 44 * @run testng UptimeTest 45 */ 46 public class UptimeTest { 47 public void run(CommandExecutor executor) { 48 double someUptime = 1.0; 49 long startTime = System.currentTimeMillis(); 50 try { 51 synchronized (this) { 52 /* Loop to guard against spurious wake ups */ 53 while (System.currentTimeMillis() < (startTime + someUptime * 1000)) { 54 wait((int) someUptime * 1000); 55 } 56 } 57 } catch (InterruptedException e) { 58 Assert.fail("Test error: Exception caught when sleeping:", e); 59 } 60 61 OutputAnalyzer output = executor.execute("VM.uptime"); 62 63 output.stderrShouldBeEmpty(); 64 65 /* 66 * Output should be: 67 * [pid]: 68 * xx.yyy s 69 * 70 * If there is only one line in output there is no "[pid]:" printout; 71 * skip first line, split on whitespace and grab first half 72 */ 73 int index = output.asLines().size() == 1 ? 0 : 1; 74 String uptimeString = output.asLines().get(index).split("\\s+")[0]; 75 76 try { 77 double uptime = NumberFormat.getNumberInstance().parse(uptimeString).doubleValue(); 78 if (uptime < someUptime) { 79 Assert.fail(String.format( 80 "Test failure: Uptime was less than intended sleep time: %.3f s < %.3f s", 81 uptime, someUptime)); 82 } 83 } catch (ParseException e) { 84 Assert.fail("Test failure: Could not parse uptime string: " + 85 uptimeString, e); 86 } 87 } 88 89 @Test 90 public void jmx() { 91 run(new JMXExecutor()); 92 } 93 }