Path: blob/master/test/jdk/sun/tools/jhsdb/JStackStressTest.java
41149 views
/*1* Copyright (c) 2021, 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 826227126* @requires vm.hasSA27* @library /test/lib28* @run main/timeout=240 JStackStressTest29*/3031import java.io.IOException;32import java.io.OutputStream;3334import jdk.test.lib.JDKToolLauncher;35import jdk.test.lib.JDKToolFinder;36import jdk.test.lib.process.OutputAnalyzer;37import jdk.test.lib.process.ProcessTools;38import jdk.test.lib.SA.SATestUtils;39import jdk.test.lib.Utils;4041public class JStackStressTest {4243static Process jShellProcess;4445public static void testjstack() throws IOException {46launchJshell();47long jShellPID = jShellProcess.pid();48OutputAnalyzer jshellOutput = new OutputAnalyzer(jShellProcess);4950try {51// Do 4 jstacks on the jshell process as it starts up52for (int i = 1; i <= 4; i++) {53JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");54launcher.addVMArgs(Utils.getTestJavaOpts());55launcher.addToolArg("jstack");56launcher.addToolArg("--pid=" + Long.toString(jShellPID));5758System.out.println("###### Starting jstack iteration " + i + " against " + jShellPID);59long startTime = System.currentTimeMillis();60ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);61OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);62System.out.println("jhsdb jstack stdout:");63System.out.println(output.getStdout());64System.out.println("jhsdb jstack stderr:");65System.out.println(output.getStderr());66long elapsedTime = System.currentTimeMillis() - startTime;67System.out.println("###### End of all output for iteration " + i +68" which took " + elapsedTime + "ms");69output.shouldHaveExitValue(0);70// This will detect most SA failures, including during the attach.71output.shouldNotMatch("^sun.jvm.hotspot.debugger.DebuggerException:.*$");72// This will detect unexpected exceptions, like NPEs and asserts, that are caught73// by sun.jvm.hotspot.tools.Tool.execute().74output.shouldNotMatch("^Error: .*$");75}76} catch (Exception ex) {77throw new RuntimeException("Test ERROR " + ex, ex);78} finally {79try (OutputStream out = jShellProcess.getOutputStream()) {80out.write("/exit\n".getBytes());81out.flush();82}83try {84jShellProcess.waitFor(); // jshell should exit quickly85} catch (InterruptedException e) {86}87System.out.println("jshell Output: " + jshellOutput.getOutput());88}89}9091public static void launchJshell() throws IOException {92System.out.println("Starting Jshell");93long startTime = System.currentTimeMillis();94try {95ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jshell"));96jShellProcess = ProcessTools.startProcess("JShell", pb);97} catch (Exception ex) {98throw new RuntimeException("Test ERROR " + ex, ex);99}100}101102public static void main(String[] args) throws Exception {103SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.104testjstack();105106// The test throws RuntimeException on error.107// IOException is thrown if Jshell can't start because of some bad108// environment condition109System.out.println("Test PASSED");110}111}112113114