diff -r e8999e87feae -r 22a5f09f4da9 jdk/test/sun/management/jmxremote/startstop/JMXStartStopTest.java --- a/jdk/test/sun/management/jmxremote/startstop/JMXStartStopTest.java Mon Mar 30 10:10:19 2015 +0200 +++ b/jdk/test/sun/management/jmxremote/startstop/JMXStartStopTest.java Wed Mar 25 18:33:17 2015 +0100 @@ -35,20 +35,16 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.Random; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Consumer; -import java.util.stream.Collectors; import javax.management.*; import javax.management.remote.*; import javax.net.ssl.SSLHandshakeException; import jdk.testlibrary.ProcessTools; -import jdk.testlibrary.JDKToolLauncher; import sun.management.Agent; import sun.management.AgentConfigurationError; @@ -56,45 +52,19 @@ * @test * @bug 7110104 * @library /lib/testlibrary - * @build jdk.testlibrary.* JMXStartStopTest JMXStartStopDoSomething + * @build jdk.testlibrary.* JMXStartStopTest PortAllocator TestApp ManagementAgentJcmd * @run main/othervm/timeout=600 -XX:+UsePerfData JMXStartStopTest * @summary Makes sure that enabling/disabling the management agent through JCMD * achieves the desired results */ public class JMXStartStopTest { + private static final String TEST_APP_NAME = "TestApp"; private static final String TEST_SRC = System.getProperty("test.src"); private static final boolean verbose = false; - /** - * Dynamically allocates distinct ports from the ephemeral range 49152-65535 - */ - private static class PortAllocator { - - private final static int LOWER_BOUND = 49152; - private final static int UPPER_BOUND = 65535; - - private final static Random RND = new Random(System.currentTimeMillis()); - - private static int[] allocatePorts(final int numPorts) { - int[] ports = new int[numPorts]; - for (int i = 0; i < numPorts; i++) { - int port = -1; - while (port == -1) { - port = RND.nextInt(UPPER_BOUND - LOWER_BOUND + 1) + LOWER_BOUND; - for (int j = 0; j < i; j++) { - if (ports[j] == port) { - port = -1; - break; - } - } - } - ports[i] = port; - } - return ports; - } - } + private static ManagementAgentJcmd jcmd = new ManagementAgentJcmd(TEST_APP_NAME, verbose); private static void dbg_print(String msg) { if (verbose) { @@ -317,14 +287,14 @@ } } - private static class Something { + private static class TestAppRun { private Process p; private final ProcessBuilder pb; private final String name; private final AtomicBoolean started = new AtomicBoolean(false); private volatile long pid = -1; - public Something(ProcessBuilder pb, String name) { + public TestAppRun(ProcessBuilder pb, String name) { this.pb = pb; this.name = name; } @@ -334,7 +304,7 @@ try { AtomicBoolean error = new AtomicBoolean(false); p = ProcessTools.startProcess( - "JMXStartStopDoSomething{" + name + "}", + TEST_APP_NAME + "{" + name + "}", pb, (line) -> { boolean ok = line.equals("main enter"); @@ -381,105 +351,31 @@ } /** - * Runs the test application "JMXStartStopDoSomething" + * Runs the test application "TestApp" * @param name Test run name * @param args Additional arguments - * @return Returns a {@linkplain Something} instance representing the run + * @return Returns a {@linkplain TestAppRun} instance representing the run * @throws IOException * @throws InterruptedException * @throws TimeoutException */ - private static Something doSomething(String name, String ... args) + private static TestAppRun doTest(String name, String ... args) throws Exception { List pbArgs = new ArrayList<>(Arrays.asList( "-cp", System.getProperty("test.class.path") )); pbArgs.addAll(Arrays.asList(args)); - pbArgs.add("JMXStartStopDoSomething"); + pbArgs.add(TEST_APP_NAME); ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( pbArgs.toArray(new String[pbArgs.size()]) ); - Something s = new Something(pb, name); + TestAppRun s = new TestAppRun(pb, name); s.start(); return s; } - /** - * Run the "jcmd" command - * - * @param command Command with parameters; space separated string - * @throws IOException - * @throws InterruptedException - */ - private static void jcmd(String ... command) throws IOException, InterruptedException { - if (command.length == 0) { - jcmd(null, c->{}); - } else { - jcmd(null, command); - } - } - - /** - * Run the "jcmd" command - * - * @param c {@linkplain Consumer} instance - * @param command Command with parameters; space separated string - * @throws IOException - * @throws InterruptedException - */ - private static void jcmd(Consumer c, String ... command) throws IOException, InterruptedException { - jcmd("JMXStartStopDoSomething", c, command); - } - - /** - * Run the "jcmd" command - * - * @param target The target application name (or PID) - * @param c {@linkplain Consumer} instance - * @param command Command with parameters; space separated string - * @throws IOException - * @throws InterruptedException - */ - private static void jcmd(String target, final Consumer c, String ... command) throws IOException, InterruptedException { - dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list")); - - JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd"); - l.addToolArg(target); - for (String cmd : command) { - l.addToolArg(cmd); - } - - AtomicBoolean portUnavailable = new AtomicBoolean(false); - Process p = ProcessTools.startProcess( - "jcmd", - new ProcessBuilder(l.getCommand()), - line -> { - if (line.contains("BindException") || - line.contains(Agent.getText(AgentConfigurationError.CONNECTOR_SERVER_IO_ERROR))) { - portUnavailable.set(true); - } else { - c.accept(line); - } - } - ); - - p.waitFor(); - dbg_print("[jcmd] --------"); - if (portUnavailable.get()) { - String cmd = Arrays.asList(l.getCommand()).stream() - .collect( - Collectors.joining(" ", "", ": Unable to bind address") - ); - throw new BindException(cmd); - } - } - - private static final String CMD_STOP = "ManagementAgent.stop"; - private static final String CMD_START = "ManagementAgent.start"; - private static final String CMD_START_LOCAL = "ManagementAgent.start_local"; - static void test_01() throws Exception { // Run an app with JMX enabled stop it and // restart on other port @@ -487,7 +383,7 @@ System.out.println("**** Test one ****"); int ports[] = PortAllocator.allocatePorts(2); - Something s = doSomething( + TestAppRun s = doTest( "test_01", "-Dcom.sun.management.jmxremote.port=" + ports[0], "-Dcom.sun.management.jmxremote.authenticate=false", @@ -496,10 +392,10 @@ try { testConnect(ports[0]); - jcmd(CMD_STOP); + jcmd.stop(); testNoConnect(ports[0]); - jcmd(CMD_START, "jmxremote.port=" + ports[1]); + jcmd.start("jmxremote.port=" + ports[1]); testConnect(ports[1]); } finally { s.stop(); @@ -513,12 +409,13 @@ System.out.println("**** Test two ****"); int[] ports = PortAllocator.allocatePorts(1); - Something s = doSomething("test_02"); + TestAppRun s = doTest("test_02"); try { - jcmd(CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[0], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); testConnect(ports[0]); } finally { @@ -534,18 +431,20 @@ System.out.println("**** Test three ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething("test_03"); + TestAppRun s = doTest("test_03"); try { - jcmd(CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[0], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); // Second agent shouldn't start - jcmd(CMD_START, - "jmxremote.port=" + ports[1], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[1], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); // First agent should connect testConnect(ports[0]); @@ -564,13 +463,14 @@ System.out.println("**** Test four ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething("test_04"); + TestAppRun s = doTest("test_04"); try { - jcmd(CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.rmi.port=" + ports[1], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[0], + "jmxremote.rmi.port=" + ports[1], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); testConnect(ports[0], ports[1]); } finally { @@ -584,9 +484,9 @@ System.out.println("**** Test five ****"); int[] ports = PortAllocator.allocatePorts(1); - Something s = doSomething("test_05"); + TestAppRun s = doTest("test_05"); try { - jcmd(CMD_START_LOCAL); + jcmd.startLocal(); testNoConnect(ports[0]); testConnectLocal(s.getPid()); @@ -604,26 +504,27 @@ System.out.println("**** Test six ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething("test_06"); + TestAppRun s = doTest("test_06"); try { - jcmd(CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[0], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); testConnect(ports[0], ports[1]); final AtomicBoolean checks = new AtomicBoolean(false); - jcmd( - line -> { - if (line.contains("java.lang.RuntimeException: Invalid agent state")) { - checks.set(true); - } - }, - CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + line -> { + if (line.contains("java.lang.RuntimeException: Invalid agent state")) { + checks.set(true); + } + }, + "jmxremote.port=" + ports[0], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); if (!checks.get()) { throw new Exception("Starting agent on port " + ports[0] + " should " @@ -643,27 +544,28 @@ System.out.println("**** Test seven ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething("test_07"); + TestAppRun s = doTest("test_07"); try { - jcmd(CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[0], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); testConnect(ports[0], ports[1]); final AtomicBoolean checks = new AtomicBoolean(false); - jcmd( - line -> { - if (line.contains("java.lang.RuntimeException: Invalid agent state")) { - checks.set(true); - } - }, - CMD_START, - "jmxremote.port=" + ports[1], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + line -> { + if (line.contains("java.lang.RuntimeException: Invalid agent state")) { + checks.set(true); + } + }, + "jmxremote.port=" + ports[1], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); if (!checks.get()) { throw new Exception("Starting agent on poprt " + ports[1] + " should " @@ -683,17 +585,18 @@ System.out.println("**** Test eight ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething("test_08"); + TestAppRun s = doTest("test_08"); try { - jcmd(CMD_START, - "jmxremote.port=" + ports[0], - "jmxremote.authenticate=false", - "jmxremote.ssl=false"); + jcmd.start( + "jmxremote.port=" + ports[0], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" + ); testConnect(ports[0], ports[1]); - jcmd(CMD_STOP); - jcmd(CMD_STOP); + jcmd.stop(); + jcmd.stop(); } finally { s.stop(); } @@ -706,7 +609,7 @@ System.out.println("**** Test nine ****"); - Something s = doSomething("test_09"); + TestAppRun s = doTest("test_09"); try (ServerSocket ss = new ServerSocket(0)) { int localPort = ss.getLocalPort(); @@ -722,13 +625,12 @@ final AtomicBoolean retry = new AtomicBoolean(false); try { - jcmd( + jcmd.start( line -> { if (line.contains(Agent.getText(AgentConfigurationError.AGENT_EXCEPTION))) { retry.set(true); } }, - CMD_START, "jmxremote.port=" + ports[0], "jmxremote.rmi.port=" + localPort, "jmxremote.authenticate=false", @@ -763,18 +665,17 @@ System.out.println("**** Test ten ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething( + TestAppRun s = doTest( "test_10", "-Dcom.sun.management.jmxremote.authenticate=false", "-Dcom.sun.management.jmxremote.ssl=true"); try { testNoConnect(ports[0]); - jcmd( - CMD_START, - "jmxremote.port=" + ports[1], - "jmxremote.authenticate=false", - "jmxremote.ssl=false" + jcmd.start( + "jmxremote.port=" + ports[1], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" ); testConnect(ports[1]); } finally { @@ -790,7 +691,7 @@ System.out.println("**** Test eleven ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething( + TestAppRun s = doTest( "test_11", "-Dcom.sun.management.jmxremote.port=" + ports[0], "-Dcom.sun.management.jmxremote.authenticate=false", @@ -799,15 +700,14 @@ try { testNoConnect(ports[0]); - jcmd(CMD_STOP); + jcmd.stop(); testNoConnect(ports[0]); - jcmd( - CMD_START, - "jmxremote.port=" + ports[1], - "jmxremote.authenticate=false", - "jmxremote.ssl=false" + jcmd.start( + "jmxremote.port=" + ports[1], + "jmxremote.authenticate=false", + "jmxremote.ssl=false" ); testConnect(ports[1]); @@ -827,7 +727,7 @@ System.out.println("**** Test twelve ****"); int[] ports = PortAllocator.allocatePorts(2); - Something s = doSomething("test_12", + TestAppRun s = doTest("test_12", "-Dcom.sun.management.config.file=" + TEST_SRC + File.separator + "management_cl.properties", "-Dcom.sun.management.jmxremote.authenticate=false" @@ -836,15 +736,15 @@ try { testNoConnect(ports[0]); - jcmd(CMD_STOP); + jcmd.stop(); testNoConnect(ports[0]); - jcmd(CMD_START, - "config.file=" + TEST_SRC + File.separator - + "management_jcmd.properties", - "jmxremote.authenticate=false", - "jmxremote.port=" + ports[1] + jcmd.start( + "config.file=" + TEST_SRC + File.separator + + "management_jcmd.properties", + "jmxremote.authenticate=false", + "jmxremote.port=" + ports[1] ); testConnect(ports[1]); @@ -862,7 +762,7 @@ System.out.println("**** Test thirteen ****"); int[] ports = PortAllocator.allocatePorts(1); - Something s = doSomething( + TestAppRun s = doTest( "test_13", "-Dcom.sun.management.jmxremote.port=" + ports[0], "-Dcom.sun.management.jmxremote.authenticate=false", @@ -871,16 +771,16 @@ try { testNoConnect(ports[0]); - jcmd(CMD_STOP); - jcmd(CMD_START, - "jmxremote.ssl=false", - "jmxremote.port=" + ports[0] + jcmd.stop(); + jcmd.start( + "jmxremote.ssl=false", + "jmxremote.port=" + ports[0] ); testConnect(ports[0]); - jcmd(CMD_STOP); - jcmd(CMD_START, - "jmxremote.port=" + ports[0] + jcmd.stop(); + jcmd.start( + "jmxremote.port=" + ports[0] ); testNoConnect(ports[0]); @@ -896,14 +796,14 @@ System.out.println("**** Test fourteen ****"); int[] ports = PortAllocator.allocatePorts(1); - Something s = doSomething( + TestAppRun s = doTest( "test_14", "-Dcom.sun.management.jmxremote.port=" + ports[0], "-Dcom.sun.management.jmxremote.authenticate=false", "-Dcom.sun.management.jmxremote.ssl=false"); try { testConnect(ports[0]); - jcmd(CMD_STOP); + jcmd.stop(); testConnectLocal(s.getPid()); } finally { s.stop(); @@ -917,11 +817,11 @@ System.out.println("**** Test fifteen ****"); int[] ports = PortAllocator.allocatePorts(1); - Something s = doSomething("test_15"); + TestAppRun s = doTest("test_15"); try { testNoConnect(ports[0]); - jcmd(CMD_START + "_local"); + jcmd.startLocal(); testConnectLocal(s.getPid());