Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/IdleTimeoutTest.java
41159 views
/*1* Copyright (c) 2003, 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 4886838 4886830 802520426* @summary Tests that idle timeouts happen at appropriate times27* @author Eamonn McManus28* @modules java.management.rmi29* java.management/com.sun.jmx.remote.util30* @run clean IdleTimeoutTest31* @run build IdleTimeoutTest32* @run main IdleTimeoutTest33*/3435import java.util.Arrays;36import java.util.ArrayList;37import java.util.Iterator;38import java.util.List;39import java.util.HashMap;40import java.util.Map;41import javax.management.MBeanServer;42import javax.management.MBeanServerConnection;43import javax.management.MBeanServerFactory;44import javax.management.MBeanServerNotification;45import javax.management.Notification;46import javax.management.NotificationListener;47import javax.management.ObjectName;48import javax.management.remote.JMXConnector;49import javax.management.remote.JMXConnectorFactory;50import javax.management.remote.JMXConnectorServer;51import javax.management.remote.JMXConnectorServerFactory;52import javax.management.remote.JMXServiceURL;53import com.sun.jmx.remote.util.EnvHelp;5455public class IdleTimeoutTest {5657static boolean isPresent(String cn) {58try {59Class.forName(cn);60return true;61} catch (ClassNotFoundException x) {62return false;63}64}6566public static void main(String[] args) throws Exception {67boolean ok = true;68List protos;69if (args.length > 0)70protos = Arrays.asList(args);71else {72protos = new ArrayList(Arrays.asList(new String[] {"rmi"}));73if (isPresent("javax.management.remote.rmi._RMIConnectionImpl_Tie"))74protos.add("iiop");75if (isPresent("javax.management.remote.jmxmp.JMXMPConnectorServer"))76protos.add("jmxmp");77}78for (Iterator it = protos.iterator(); it.hasNext(); ) {79String proto = (String) it.next();80int liCount;81if (proto.equals("jmxmp")) liCount=1;82else liCount=2;83if (test(proto,4,liCount))84System.out.println("Test for protocol " + proto + " passed");85else {86System.out.println("Test for protocol " + proto + " FAILED");87ok = false;88}89}90if (!ok) {91throw new RuntimeException("Some tests failed - see log for details");92}93}9495private static long getIdleTimeout(MBeanServer mbs, JMXServiceURL url)96throws Exception97{98JMXConnectorServer server =99JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);100server.start();101try {102url = server.getAddress();103104// Force initialization (class loading, JIT, etc...)105//106JMXConnector client = JMXConnectorFactory.connect(url);107try {108String connId = client.getConnectionId();109MBeanServerConnection conn = client.getMBeanServerConnection();110} finally {111client.close();112}113114// Do the time measurement115//116final long firstTime = System.currentTimeMillis();117final long endtime;118client = JMXConnectorFactory.connect(url);119try {120String connId = client.getConnectionId();121MBeanServerConnection conn = client.getMBeanServerConnection();122endtime = System.currentTimeMillis();123} finally {124client.close();125}126127// multipled by 10 for a slow machine, plus 1500 for a fast one.128return 10*(endtime - firstTime) + 1500;129} finally {130server.stop();131}132}133134private static class NotificationCounter135implements NotificationListener {136private final int[] listenerCount;137private final String listenerName;138NotificationCounter(int[] counter, String name) {139listenerCount=counter;140listenerName=name;141}142143public void handleNotification(Notification n,144Object h) {145MBeanServerNotification mbsn =146(MBeanServerNotification) n;147System.out.println(listenerName + " got notification: "148+ mbsn.getMBeanName());149synchronized (listenerCount) {150listenerCount[0]++;151listenerCount.notify();152}153}154public String toString() {155return listenerName;156}157}158159private static boolean test(String proto,int opCount,int liCount)160throws Exception {161System.out.println("Idle timeout test for protocol " + proto);162ObjectName delegateName =163ObjectName.getInstance("JMImplementation:" +164"type=MBeanServerDelegate");165MBeanServer mbs = MBeanServerFactory.createMBeanServer();166JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");167168final long timeout = getIdleTimeout(mbs,url);169System.out.println("Timeout for " + proto + " is: " +170timeout + " ms");171172Map idleMap = new HashMap();173idleMap.put(EnvHelp.SERVER_CONNECTION_TIMEOUT, new Long(timeout));174JMXConnectorServer server =175JMXConnectorServerFactory.newJMXConnectorServer(url,idleMap,mbs);176177final int[] listenerCount = new int[1];178final NotificationListener countListeners[] =179new NotificationListener[liCount];180int i;181for (i=0; i<countListeners.length; i++) {182countListeners[i] =183new NotificationCounter(listenerCount,"Listener"+i);184}185186server.start();187try {188url = server.getAddress();189final long firstTime = System.currentTimeMillis();190JMXConnector client = JMXConnectorFactory.connect(url);191long elapsed, startIdle=0;192try {193String connId = client.getConnectionId();194MBeanServerConnection conn =195client.getMBeanServerConnection();196elapsed = System.currentTimeMillis() - firstTime;197System.out.println("Idle Time: " + elapsed + "ms");198199for (i=0; i<countListeners.length; i++) {200System.out.println("add " + countListeners[i] +201": starting at " + elapsed + "ms");202conn.addNotificationListener(delegateName,203countListeners[i],204null,null);205}206207System.out.println("connId=" + connId);208for (i = 0; i < opCount; i++) {209elapsed = System.currentTimeMillis() - firstTime;210System.out.println("Operation[" + (i+1)211+"]: starting at " +212elapsed + "ms");213final String name = "d:type=mlet,instance=" + i;214mbs.createMBean("javax.management.loading.MLet",215new ObjectName(name));216if (i == (opCount-1))217startIdle = System.currentTimeMillis();218Thread.sleep(2);219}220221// Wait for notifs to arrive before doing removeNListener222long startTime = System.currentTimeMillis();223long deadline = startTime + 10000;224225System.out.println("Waiting for notifs: starting at " +226(startTime - firstTime) + "ms");227228final int expectedCount = opCount*countListeners.length;229while (System.currentTimeMillis() < deadline) {230synchronized (listenerCount) {231if (listenerCount[0] >= expectedCount)232break;233listenerCount.wait();234}235}236237long elapsedWait = System.currentTimeMillis() - startTime;238System.out.println("Waited " + elapsedWait +239"ms for notifs to arrive");240241if (listenerCount[0] != expectedCount) {242System.out.println("Did not get expected " +243expectedCount + " notifications: "244+ listenerCount[0]);245return false;246}247248elapsed = System.currentTimeMillis() - firstTime;249250System.out.println("idle time since last operation: " +251(elapsed + firstTime - startIdle) + "ms");252System.out.println("Requesting conn id at: " +253elapsed + "ms");254final String cid = client.getConnectionId();255256elapsed = System.currentTimeMillis() - firstTime;257System.out.println("Got conn id <" + cid + "> at: " +258elapsed + "ms");259260if (!connId.equals(cid)) {261System.out.println("Client id changed: <" + connId +262"> -> <" + cid +263">");264return false;265}266267List ids = Arrays.asList(server.getConnectionIds());268if (!ids.contains(connId)) {269System.out.println("Server ids don't contain our id: " +270ids + " - " + connId);271return false;272}273274for (i=0;i<countListeners.length;i++) {275System.out.println("Removing notification listener: " +276countListeners[i]);277conn.removeNotificationListener(delegateName,278countListeners[i]);279}280281System.out.println("Waiting for id list to drop ours");282// pass or timed out by test harness - see 8025204283do {284Thread.sleep(100);285ids = Arrays.asList(server.getConnectionIds());286} while (ids.contains(connId));287288conn.getDefaultDomain();289if (connId.equals(client.getConnectionId())) {290System.out.println("Client id did not change: <" + connId +291">: idle timeout did not happen?");292return false;293} else {294System.out.println("Client id changed as expected: <" +295connId + "> -> <" +296client.getConnectionId() + ">");297}298} finally {299client.close();300System.out.println("Connection id list on server after " +301"client close: " +302Arrays.asList(server.getConnectionIds()));303}304} finally {305server.stop();306}307System.out.println("*** ------------------------------------------");308System.out.println("*** Test passed for " + proto);309System.out.println("*** ------------------------------------------");310return true;311}312}313314315