Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/threads/NoServerTimeoutTest.java
38867 views
/*1* Copyright (c) 2004, 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 619212426* @summary Tests that you can turn off the server connection timeout thread27* @author Eamonn McManus28* @run clean NoServerTimeoutTest29* @run build NoServerTimeoutTest30* @run main NoServerTimeoutTest31*/3233import java.lang.management.*;34import java.net.MalformedURLException;35import java.util.*;36import javax.management.*;37import javax.management.remote.*;3839public class NoServerTimeoutTest {40public static void main(String[] args) throws Exception {41boolean ok = true;4243for (String proto : new String[] {"rmi", "iiop", "jmxmp"}) {44JMXServiceURL url = new JMXServiceURL(proto, null, 0);45try {46MBeanServer mbs = MBeanServerFactory.newMBeanServer();47// Create server just to see if the protocol is supported48JMXConnectorServerFactory.newJMXConnectorServer(url,49null,50mbs);51} catch (MalformedURLException e) {52System.out.println();53System.out.println("Ignoring protocol: " + proto);54continue;55}56try {57ok &= test(url);58} catch (Exception e) {59System.out.println("TEST FAILED WITH EXCEPTION:");60e.printStackTrace();61ok = false;62}63}6465System.out.println();66if (ok)67System.out.println("Test passed");68else69throw new Exception("Test failed");70}7172private static enum Test {73NO_ENV("No Map for connector server"),74EMPTY_ENV("Empty Map for connector server"),75PLAIN_TIMEOUT("Map with two-minute timeout as int"),76PLAIN_STRING_TIMEOUT("Map with two-minute timeout as string"),77INFINITE_TIMEOUT("Map with Long.MAX_VALUE timeout as long"),78INFINITE_STRING_TIMEOUT("Map with Long.MAX_VALUE timeout as string");7980Test(String description) {81this.description = description;82}8384public String toString() {85return description;86}8788private final String description;89}90// define which tests should see a timeout thread91private static final Set<Test> expectThread =92EnumSet.copyOf(Arrays.asList(Test.NO_ENV,93Test.EMPTY_ENV,94Test.PLAIN_TIMEOUT,95Test.PLAIN_STRING_TIMEOUT));9697private static boolean test(JMXServiceURL url) throws Exception {98System.out.println();99System.out.println("Test: " + url);100101boolean ok = true;102103for (Test test : Test.values())104ok &= test(url, test);105106return ok;107}108109private static final String TIMEOUT =110"jmx.remote.x.server.connection.timeout";111112private static boolean test(JMXServiceURL url, Test test)113throws Exception {114115System.out.println("* " + test);116117MBeanServer mbs = MBeanServerFactory.newMBeanServer();118Map<String, Object> env = new HashMap<String, Object>();119switch (test) {120case NO_ENV: env = null; break;121case EMPTY_ENV: break;122case PLAIN_TIMEOUT: env.put(TIMEOUT, 120 * 1000L); break;123case PLAIN_STRING_TIMEOUT: env.put(TIMEOUT, (120 * 1000L) + ""); break;124case INFINITE_TIMEOUT: env.put(TIMEOUT, Long.MAX_VALUE); break;125case INFINITE_STRING_TIMEOUT: env.put(TIMEOUT, "" + Long.MAX_VALUE);126break;127default: throw new AssertionError();128}129130// In case there's a timeout thread left over from a previous run131for (int i = 0; i < 10 && countTimeoutThreads() > 0; i++)132Thread.sleep(500);133if (countTimeoutThreads() > 0) {134System.out.println("TIMEOUT THREAD(S) WOULD NOT GO AWAY");135return false;136}137138JMXConnectorServer cs =139JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);140cs.start();141JMXServiceURL addr = cs.getAddress();142JMXConnector cc = JMXConnectorFactory.connect(addr);143MBeanServerConnection mbsc = cc.getMBeanServerConnection();144mbsc.getDefaultDomain();145int expectTimeoutThreads = expectThread.contains(test) ? 1 : 0;146int timeoutThreads = countTimeoutThreads();147boolean ok = (expectTimeoutThreads == timeoutThreads);148if (!ok) {149System.out.println("TEST FAILS: Expected timeout threads: " +150expectTimeoutThreads +151"; actual timeout threads: " + timeoutThreads);152ok = false;153}154cc.close();155cs.stop();156return ok;157}158159private static int countTimeoutThreads() {160ThreadMXBean mb = ManagementFactory.getThreadMXBean();161int count = 0;162long[] ids = mb.getAllThreadIds();163for (ThreadInfo ti : mb.getThreadInfo(ids)) {164if (ti != null &&165ti.getThreadName().startsWith("JMX server connection timeout"))166count++;167}168return count;169}170}171172173