Path: blob/master/test/jdk/sun/management/jdp/DynamicLauncher.java
41152 views
/*1* Copyright (c) 2013, 2018, 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*/222324import jdk.test.lib.process.OutputAnalyzer;25import jdk.test.lib.process.ProcessTools;26import jdk.test.lib.Utils;2728import java.util.UUID;293031/**32* This class will try to find an unused port and run a JdpTestCase using it.33* The unused port is needed for jmxremote.port.34* The problem with busy ports arises when running many automated tests on the same host.35* Note that jdp.port is a multicast port and thus it can be binded by different processes at the same time.36*/37public abstract class DynamicLauncher {3839final String jdpName = UUID.randomUUID().toString();40OutputAnalyzer output;41int jmxPort;4243protected void run() throws Exception {44int retries = 1;45boolean tryAgain;4647do {48tryAgain = false;49jmxPort = Utils.getFreePort();50output = runVM();51try {52output.shouldNotContain("Port already in use");53} catch (RuntimeException e) {54if (retries < 3) {55retries++;56tryAgain = true;57}58}59} while (tryAgain);60output.shouldHaveExitValue(0);61// java.lang.Exception is thrown by JdpTestCase if something goes wrong62// for instance - see JdpTestCase::shutdown()63output.shouldNotContain("java.lang.Exception:");64output.shouldNotContain("Error: Could not find or load main class");65}6667protected OutputAnalyzer runVM() throws Exception {68String[] options = this.options();69ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(options);70OutputAnalyzer out = ProcessTools.executeProcess(pb);71System.out.println(out.getStdout());72System.err.println(out.getStderr());73return out;74}7576protected abstract String[] options();7778protected OutputAnalyzer getProcessOutpoutAnalyzer() {79return output;80}81}828384