Path: blob/master/test/jdk/sun/tools/jstat/JStatInterval.java
41152 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 803566826* @library /test/lib27* @summary Test checks case when target application finishes execution and jstat didn't complete work.28jstat is started with interval = 100 (jstat -compiler 100) and monitored application finishes29after 500ms. This shouldn't cause crash or hang in target application or in jstat.30* @modules java.management31* @build JStatInterval32* @run main JStatInterval33*/3435import jdk.test.lib.JDKToolLauncher;36import jdk.test.lib.Utils;37import jdk.test.lib.process.ProcessTools;3839import java.util.concurrent.TimeUnit;40import java.util.concurrent.atomic.AtomicBoolean;4142public class JStatInterval {43private static final String READY = "READY";44private static final String ERROR = "!ERROR";4546public static class Application {47public static void main(String[] args) {48try {49System.out.println(READY);50System.out.flush();51int exitCode = System.in.read();52Thread.sleep(500);53System.exit(exitCode);54} catch (Exception e) {55System.out.println(ERROR);56System.out.flush();57throw new Error(e);58}59}60}61public static void main(String[] args) throws Exception {62ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(63"-cp",64System.getProperty("test.class.path"),65"-XX:+UsePerfData",66Application.class.getName()67);68AtomicBoolean error = new AtomicBoolean(false);69Process app = ProcessTools.startProcess(70"application",71pb,72line -> {73if (line.equals(READY)) {74return true;75} else if (line.equals(ERROR)) {76error.set(true);77return true;78}79return false;80},8110,82TimeUnit.SECONDS83);84if (error.get()) {85throw new Error("Unable to start the monitored application.");86}8788String pidStr = String.valueOf(app.pid());89JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jstat");90l.addVMArgs(Utils.getTestJavaOpts());91l.addToolArg("-compiler");92l.addToolArg(pidStr);93l.addToolArg("100");9495ProcessBuilder jstatDef = new ProcessBuilder(l.getCommand());96Process jstat = ProcessTools.startProcess(97"jstat",98jstatDef,99line -> {100if (line.trim().toLowerCase().startsWith("compiled")) {101return true;102}103return false;104},10510,106TimeUnit.SECONDS107);108109app.getOutputStream().write(0);110app.getOutputStream().flush();111112if (app.waitFor() != 0) {113throw new Error("Error detected upon exiting the monitored application with active jstat");114}115if (jstat.waitFor() != 0) {116throw new Error("Error detected in jstat when monitored application has exited prematurely");117}118}119}120121122