Path: blob/master/test/jdk/sun/management/jdp/PortAlreadyInUseTest.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.Utils;2627import java.io.IOException;28import java.net.ServerSocket;29import java.util.logging.Logger;3031/**32* This test used for test development and it is not meant to be run by JTReg.33* <p/>34* This test exercises a retry mechanism to avoid a test failure because of35* starting the VM on a busy jmxremote.port.36* <p/>37* To run this test you'll need to add this VM option: -Dtest.jdk=<path-to-jdk>38*/39public class PortAlreadyInUseTest extends DynamicLauncher {4041ServerSocket socket;42final Logger log = Logger.getLogger("PortAlreadyInUse");4344protected void go() throws Exception {45jmxPort = Utils.getFreePort();46occupyPort();4748log.info("Attempting to start a VM using the same port.");49OutputAnalyzer out = this.runVM();50out.shouldContain("Port already in use");51log.info("Failed as expected.");5253log.info("Trying again using retries.");54this.run();55}5657private void occupyPort() throws IOException {58socket = new ServerSocket(jmxPort);59log.info("Occupying port " + String.valueOf(jmxPort));60}6162protected String[] options() {63String[] options = {64"-Dcom.sun.management.jmxremote.authenticate=false",65"-Dcom.sun.management.jmxremote.ssl=false",66"-Dcom.sun.management.jmxremote=true",67"-Dcom.sun.management.jmxremote.port=" + String.valueOf(jmxPort),68"-version"69};70return options;71}7273public static void main(String[] args) throws Exception {74PortAlreadyInUseTest test = new PortAlreadyInUseTest();75test.go();76}7778}798081