Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/IdleTimeoutTest.java
38867 views
/*1* Copyright (c) 2003, 2013, 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* @run clean IdleTimeoutTest29* @run build IdleTimeoutTest30* @run main IdleTimeoutTest31*/3233import java.util.Arrays;34import java.util.ArrayList;35import java.util.Iterator;36import java.util.List;37import java.util.HashMap;38import java.util.Map;39import javax.management.MBeanServer;40import javax.management.MBeanServerConnection;41import javax.management.MBeanServerFactory;42import javax.management.MBeanServerNotification;43import javax.management.Notification;44import javax.management.NotificationListener;45import javax.management.ObjectName;46import javax.management.remote.JMXConnector;47import javax.management.remote.JMXConnectorFactory;48import javax.management.remote.JMXConnectorServer;49import javax.management.remote.JMXConnectorServerFactory;50import javax.management.remote.JMXServiceURL;51import com.sun.jmx.remote.util.EnvHelp;5253public class IdleTimeoutTest {5455static boolean isPresent(String cn) {56try {57Class.forName(cn);58return true;59} catch (ClassNotFoundException x) {60return false;61}62}6364public static void main(String[] args) throws Exception {65boolean ok = true;66List protos;67if (args.length > 0)68protos = Arrays.asList(args);69else {70protos = new ArrayList(Arrays.asList(new String[] {"rmi"}));71if (isPresent("javax.management.remote.rmi._RMIConnectionImpl_Tie"))72protos.add("iiop");73if (isPresent("javax.management.remote.jmxmp.JMXMPConnectorServer"))74protos.add("jmxmp");75}76for (Iterator it = protos.iterator(); it.hasNext(); ) {77String proto = (String) it.next();78int liCount;79if (proto.equals("jmxmp")) liCount=1;80else liCount=2;81if (test(proto,4,liCount))82System.out.println("Test for protocol " + proto + " passed");83else {84System.out.println("Test for protocol " + proto + " FAILED");85ok = false;86}87}88if (!ok) {89throw new RuntimeException("Some tests failed - see log for details");90}91}9293private static long getIdleTimeout(MBeanServer mbs, JMXServiceURL url)94throws Exception95{96JMXConnectorServer server =97JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);98server.start();99try {100url = server.getAddress();101102// Force initialization (class loading, JIT, etc...)103//104JMXConnector client = JMXConnectorFactory.connect(url);105try {106String connId = client.getConnectionId();107MBeanServerConnection conn = client.getMBeanServerConnection();108} finally {109client.close();110}111112// Do the time measurement113//114final long firstTime = System.currentTimeMillis();115final long endtime;116client = JMXConnectorFactory.connect(url);117try {118String connId = client.getConnectionId();119MBeanServerConnection conn = client.getMBeanServerConnection();120endtime = System.currentTimeMillis();121} finally {122client.close();123}124125// multipled by 10 for a slow machine, plus 1500 for a fast one.126return 10*(endtime - firstTime) + 1500;127} finally {128server.stop();129}130}131132private static class NotificationCounter133implements NotificationListener {134private final int[] listenerCount;135private final String listenerName;136NotificationCounter(int[] counter, String name) {137listenerCount=counter;138listenerName=name;139}140141public void handleNotification(Notification n,142Object h) {143MBeanServerNotification mbsn =144(MBeanServerNotification) n;145System.out.println(listenerName + " got notification: "146+ mbsn.getMBeanName());147synchronized (listenerCount) {148listenerCount[0]++;149listenerCount.notify();150}151}152public String toString() {153return listenerName;154}155}156157private static boolean test(String proto,int opCount,int liCount)158throws Exception {159System.out.println("Idle timeout test for protocol " + proto);160ObjectName delegateName =161ObjectName.getInstance("JMImplementation:" +162"type=MBeanServerDelegate");163MBeanServer mbs = MBeanServerFactory.createMBeanServer();164JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");165166final long timeout = getIdleTimeout(mbs,url);167System.out.println("Timeout for " + proto + " is: " +168timeout + " ms");169170Map idleMap = new HashMap();171idleMap.put(EnvHelp.SERVER_CONNECTION_TIMEOUT, new Long(timeout));172JMXConnectorServer server =173JMXConnectorServerFactory.newJMXConnectorServer(url,idleMap,mbs);174175final int[] listenerCount = new int[1];176final NotificationListener countListeners[] =177new NotificationListener[liCount];178int i;179for (i=0; i<countListeners.length; i++) {180countListeners[i] =181new NotificationCounter(listenerCount,"Listener"+i);182}183184server.start();185try {186url = server.getAddress();187final long firstTime = System.currentTimeMillis();188JMXConnector client = JMXConnectorFactory.connect(url);189long elapsed, startIdle=0;190try {191String connId = client.getConnectionId();192MBeanServerConnection conn =193client.getMBeanServerConnection();194elapsed = System.currentTimeMillis() - firstTime;195System.out.println("Idle Time: " + elapsed + "ms");196197for (i=0; i<countListeners.length; i++) {198System.out.println("add " + countListeners[i] +199": starting at " + elapsed + "ms");200conn.addNotificationListener(delegateName,201countListeners[i],202null,null);203}204205System.out.println("connId=" + connId);206for (i = 0; i < opCount; i++) {207elapsed = System.currentTimeMillis() - firstTime;208System.out.println("Operation[" + (i+1)209+"]: starting at " +210elapsed + "ms");211final String name = "d:type=mlet,instance=" + i;212mbs.createMBean("javax.management.loading.MLet",213new ObjectName(name));214if (i == (opCount-1))215startIdle = System.currentTimeMillis();216Thread.sleep(2);217}218219// Wait for notifs to arrive before doing removeNListener220long startTime = System.currentTimeMillis();221long deadline = startTime + 10000;222223System.out.println("Waiting for notifs: starting at " +224(startTime - firstTime) + "ms");225226final int expectedCount = opCount*countListeners.length;227while (System.currentTimeMillis() < deadline) {228synchronized (listenerCount) {229if (listenerCount[0] >= expectedCount)230break;231listenerCount.wait();232}233}234235long elapsedWait = System.currentTimeMillis() - startTime;236System.out.println("Waited " + elapsedWait +237"ms for notifs to arrive");238239if (listenerCount[0] != expectedCount) {240System.out.println("Did not get expected " +241expectedCount + " notifications: "242+ listenerCount[0]);243return false;244}245246elapsed = System.currentTimeMillis() - firstTime;247248System.out.println("idle time since last operation: " +249(elapsed + firstTime - startIdle) + "ms");250System.out.println("Requesting conn id at: " +251elapsed + "ms");252final String cid = client.getConnectionId();253254elapsed = System.currentTimeMillis() - firstTime;255System.out.println("Got conn id <" + cid + "> at: " +256elapsed + "ms");257258if (!connId.equals(cid)) {259System.out.println("Client id changed: <" + connId +260"> -> <" + cid +261">");262return false;263}264265List ids = Arrays.asList(server.getConnectionIds());266if (!ids.contains(connId)) {267System.out.println("Server ids don't contain our id: " +268ids + " - " + connId);269return false;270}271272for (i=0;i<countListeners.length;i++) {273System.out.println("Removing notification listener: " +274countListeners[i]);275conn.removeNotificationListener(delegateName,276countListeners[i]);277}278279System.out.println("Waiting for id list to drop ours");280// pass or timed out by test harness - see 8025204281do {282Thread.sleep(100);283ids = Arrays.asList(server.getConnectionIds());284} while (ids.contains(connId));285286conn.getDefaultDomain();287if (connId.equals(client.getConnectionId())) {288System.out.println("Client id did not change: <" + connId +289">: idle timeout did not happen?");290return false;291} else {292System.out.println("Client id changed as expected: <" +293connId + "> -> <" +294client.getConnectionId() + ">");295}296} finally {297client.close();298System.out.println("Connection id list on server after " +299"client close: " +300Arrays.asList(server.getConnectionIds()));301}302} finally {303server.stop();304}305System.out.println("*** ------------------------------------------");306System.out.println("*** Test passed for " + proto);307System.out.println("*** ------------------------------------------");308return true;309}310}311312313