Path: blob/master/test/jdk/javax/management/remote/mandatory/threads/NoServerTimeoutTest.java
41159 views
/*1* Copyright (c) 2004, 2015, 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*29* @run clean NoServerTimeoutTest30* @run build NoServerTimeoutTest31* @run main NoServerTimeoutTest32*/3334import java.lang.management.*;35import java.net.MalformedURLException;36import java.util.*;37import javax.management.*;38import javax.management.remote.*;3940public class NoServerTimeoutTest {41public static void main(String[] args) throws Exception {42boolean ok = true;4344for (String proto : new String[] {"rmi", "iiop", "jmxmp"}) {45JMXServiceURL url = new JMXServiceURL(proto, null, 0);46try {47MBeanServer mbs = MBeanServerFactory.newMBeanServer();48// Create server just to see if the protocol is supported49JMXConnectorServerFactory.newJMXConnectorServer(url,50null,51mbs);52} catch (MalformedURLException e) {53System.out.println();54System.out.println("Ignoring protocol: " + proto);55continue;56}57try {58ok &= test(url);59} catch (Exception e) {60System.out.println("TEST FAILED WITH EXCEPTION:");61e.printStackTrace();62ok = false;63}64}6566System.out.println();67if (ok)68System.out.println("Test passed");69else70throw new Exception("Test failed");71}7273private static enum Test {74NO_ENV("No Map for connector server"),75EMPTY_ENV("Empty Map for connector server"),76PLAIN_TIMEOUT("Map with two-minute timeout as int"),77PLAIN_STRING_TIMEOUT("Map with two-minute timeout as string"),78INFINITE_TIMEOUT("Map with Long.MAX_VALUE timeout as long"),79INFINITE_STRING_TIMEOUT("Map with Long.MAX_VALUE timeout as string");8081Test(String description) {82this.description = description;83}8485public String toString() {86return description;87}8889private final String description;90}91// define which tests should see a timeout thread92private static final Set<Test> expectThread =93EnumSet.copyOf(Arrays.asList(Test.NO_ENV,94Test.EMPTY_ENV,95Test.PLAIN_TIMEOUT,96Test.PLAIN_STRING_TIMEOUT));9798private static boolean test(JMXServiceURL url) throws Exception {99System.out.println();100System.out.println("Test: " + url);101102boolean ok = true;103104for (Test test : Test.values())105ok &= test(url, test);106107return ok;108}109110private static final String TIMEOUT =111"jmx.remote.x.server.connection.timeout";112113private static boolean test(JMXServiceURL url, Test test)114throws Exception {115116System.out.println("* " + test);117118MBeanServer mbs = MBeanServerFactory.newMBeanServer();119Map<String, Object> env = new HashMap<String, Object>();120switch (test) {121case NO_ENV: env = null; break;122case EMPTY_ENV: break;123case PLAIN_TIMEOUT: env.put(TIMEOUT, 120 * 1000L); break;124case PLAIN_STRING_TIMEOUT: env.put(TIMEOUT, (120 * 1000L) + ""); break;125case INFINITE_TIMEOUT: env.put(TIMEOUT, Long.MAX_VALUE); break;126case INFINITE_STRING_TIMEOUT: env.put(TIMEOUT, "" + Long.MAX_VALUE);127break;128default: throw new AssertionError();129}130131// In case there's a timeout thread left over from a previous run132for (int i = 0; i < 10 && countTimeoutThreads() > 0; i++)133Thread.sleep(500);134if (countTimeoutThreads() > 0) {135System.out.println("TIMEOUT THREAD(S) WOULD NOT GO AWAY");136return false;137}138139JMXConnectorServer cs =140JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);141cs.start();142JMXServiceURL addr = cs.getAddress();143JMXConnector cc = JMXConnectorFactory.connect(addr);144MBeanServerConnection mbsc = cc.getMBeanServerConnection();145mbsc.getDefaultDomain();146int expectTimeoutThreads = expectThread.contains(test) ? 1 : 0;147int timeoutThreads = countTimeoutThreads();148boolean ok = (expectTimeoutThreads == timeoutThreads);149if (!ok) {150System.out.println("TEST FAILS: Expected timeout threads: " +151expectTimeoutThreads +152"; actual timeout threads: " + timeoutThreads);153ok = false;154}155cc.close();156cs.stop();157return ok;158}159160private static int countTimeoutThreads() {161ThreadMXBean mb = ManagementFactory.getThreadMXBean();162int count = 0;163long[] ids = mb.getAllThreadIds();164for (ThreadInfo ti : mb.getThreadInfo(ids)) {165if (ti != null &&166ti.getThreadName().startsWith("JMX server connection timeout"))167count++;168}169return count;170}171}172173174