Path: blob/master/test/jdk/sun/management/jmxremote/startstop/JMXStatusPerfCountersTest.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import java.io.IOException;24import java.net.BindException;25import java.util.Properties;26import java.util.function.Predicate;27import static org.testng.Assert.*;28import org.testng.annotations.AfterMethod;29import org.testng.annotations.BeforeClass;30import org.testng.annotations.BeforeMethod;31import org.testng.annotations.BeforeTest;32import org.testng.annotations.Test;3334import jdk.test.lib.process.ProcessTools;353637/**38* @test39* @bug 807592640* @key intermittent41* @summary Makes sure that the current management agent status is reflected42* in the related performance counters.43*44* @library /test/lib45*46* @build PortAllocator TestApp ManagementAgentJcmd47* @run testng/othervm -XX:+UsePerfData JMXStatusPerfCountersTest48*/49public class JMXStatusPerfCountersTest {50private final static String TEST_APP_NAME = "TestApp";5152private final static String REMOTE_STATUS_KEY = "sun.management.JMXConnectorServer.remote.enabled";5354private static ProcessBuilder testAppPb;55private Process testApp;5657private ManagementAgentJcmd jcmd;5859@BeforeClass60public static void setupClass() throws Exception {61testAppPb = ProcessTools.createJavaProcessBuilder(62"-XX:+UsePerfData",63"-cp", System.getProperty("test.class.path"),64TEST_APP_NAME65);66}6768@BeforeTest69public void setup() {70jcmd = new ManagementAgentJcmd(TEST_APP_NAME, false);71}7273@BeforeMethod74public void startTestApp() throws Exception {75testApp = ProcessTools.startProcess(76TEST_APP_NAME, testAppPb,77(Predicate<String>)l->l.trim().equals("main enter")78);79}8081@AfterMethod82public void stopTestApp() throws Exception {83testApp.getOutputStream().write(1);84testApp.getOutputStream().flush();85testApp.waitFor();86testApp = null;87}8889/**90* The 'sun.management.JMXConnectorServer.remote.enabled' counter must not be91* exported if the remote agent is not enabled.92* @throws Exception93*/94@Test95public void testNotInitializedRemote() throws Exception {96assertFalse(97getCounters().containsKey(REMOTE_STATUS_KEY),98"Unexpected occurrence of " + REMOTE_STATUS_KEY + " in perf counters"99);100}101102/**103* After enabling the remote agent the 'sun.management.JMXConnectorServer.remote.enabled'104* counter will be exported with value of '0' - corresponding to the actual105* version of the associated remote connector perf counters.106* @throws Exception107*/108@Test109public void testRemoteEnabled() throws Exception {110while (true) {111try {112int[] ports = PortAllocator.allocatePorts(1);113jcmd.start(114"jmxremote.port=" + ports[0],115"jmxremote.authenticate=false",116"jmxremote.ssl=false"117);118String v = getCounters().getProperty(REMOTE_STATUS_KEY);119assertNotNull(v);120assertEquals("0", v);121return;122} catch (BindException e) {123System.out.println("Failed to allocate ports. Retrying ...");124}125}126}127128/**129* After disabling the remote agent the value of 'sun.management.JMXConnectorServer.remote.enabled'130* counter will become '-1'.131* @throws Exception132*/133@Test134public void testRemoteDisabled() throws Exception {135while (true) {136try {137int[] ports = PortAllocator.allocatePorts(1);138jcmd.start(139"jmxremote.port=" + ports[0],140"jmxremote.authenticate=false",141"jmxremote.ssl=false"142);143jcmd.stop();144String v = getCounters().getProperty(REMOTE_STATUS_KEY);145assertNotNull(v);146assertEquals("-1", v);147return;148} catch (BindException e) {149System.out.println("Failed to allocate ports. Retrying ...");150}151}152}153154/**155* Each subsequent re-enablement of the remote agent must keep the value of156* 'sun.management.JMXConnectorServer.remote.enabled' counter in sync with157* the actual version of the associated remote connector perf counters.158* @throws Exception159*/160@Test161public void testRemoteReEnabled() throws Exception {162while (true) {163try {164int[] ports = PortAllocator.allocatePorts(1);165jcmd.start(166"jmxremote.port=" + ports[0],167"jmxremote.authenticate=false",168"jmxremote.ssl=false"169);170jcmd.stop();171jcmd.start(172"jmxremote.port=" + ports[0],173"jmxremote.authenticate=false",174"jmxremote.ssl=false"175);176177String v = getCounters().getProperty(REMOTE_STATUS_KEY);178assertNotNull(v);179assertEquals("1", v);180return;181} catch (BindException e) {182System.out.println("Failed to allocate ports. Retrying ...");183}184}185}186187private Properties getCounters() throws IOException, InterruptedException {188return jcmd.perfCounters("sun\\.management\\.JMXConnectorServer\\..*");189}190}191192193