Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/NotifReconnectDeadlockTest.java
41155 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 619989926* @summary Tests reconnection done by a fetching notif thread.27* @author Shanliang JIANG28*29* @run clean NotifReconnectDeadlockTest30* @run build NotifReconnectDeadlockTest31* @run main NotifReconnectDeadlockTest32*/3334import java.util.HashMap;35import java.util.Map;36import javax.management.MBeanServer;37import javax.management.MBeanServerFactory;38import javax.management.Notification;39import javax.management.NotificationBroadcasterSupport;40import javax.management.NotificationListener;41import javax.management.ObjectName;42import javax.management.remote.JMXConnectionNotification;43import javax.management.remote.JMXConnector;44import javax.management.remote.JMXConnectorFactory;45import javax.management.remote.JMXConnectorServer;46import javax.management.remote.JMXConnectorServerFactory;47import javax.management.remote.JMXServiceURL;4849/**50* "This test checks for a bug whereby reconnection did not work if (a) it was51* initiated by the fetchNotifications thread and (b) it succeeded. These conditions52* are not usual, since connection failure is usually caused by either idle timeout53* (which doesn't usually happen if fetchNotifications is running) or communication54* problems (which are usually permanent so reconnection fails). But they can happen,55* so we test for them here.56* The test sets a very short idle timeout, and effectively suspends the57* fetchNotifications thread by having it invoke a listener with a delay in it.58* This means that the idle timeout happens. When the delayed listener returns,59* the fetchNotifications thread will attempt to reconnect, and this attempt should60* succeed, so we meet the two conditions above.61* The test succeeds if there is indeed a reconnection, detected by the connection62* listener seeing an OPENED notification. The connection listener should not see63* a CLOSED or FAILED notification."64*/65public class NotifReconnectDeadlockTest {6667public static void main(String[] args) throws Exception {68System.out.println(69">>> Tests reconnection done by a fetching notif thread.");7071ObjectName oname = new ObjectName ("Default:name=NotificationEmitter");72JMXServiceURL url = new JMXServiceURL("rmi", null, 0);73Map env = new HashMap(2);74env.put("jmx.remote.x.server.connection.timeout", new Long(serverTimeout));75env.put("jmx.remote.x.client.connection.check.period", new Long(Long.MAX_VALUE));7677final MBeanServer mbs = MBeanServerFactory.newMBeanServer();7879mbs.registerMBean(new NotificationEmitter(), oname);80JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(81url,82env,83mbs);84server.start();8586JMXServiceURL addr = server.getAddress();87JMXConnector client = JMXConnectorFactory.connect(addr, env);8889Thread.sleep(100); // let pass the first client open notif if there is90client.getMBeanServerConnection().addNotificationListener(oname,91listener,92null,93null);9495client.addConnectionNotificationListener(listener, null, null);9697// max test time: 2 minutes98final long end = System.currentTimeMillis()+120000;99100synchronized(lock) {101while(clientState == null && System.currentTimeMillis() < end) {102mbs.invoke(oname, "sendNotifications",103new Object[] {new Notification("MyType", "", 0)},104new String[] {"javax.management.Notification"});105106try {107lock.wait(10);108} catch (Exception e) {}109}110}111112if (clientState == null) {113throw new RuntimeException(114"No reconnection happened, need to reconfigure the test.");115} else if (JMXConnectionNotification.FAILED.equals(clientState) ||116JMXConnectionNotification.CLOSED.equals(clientState)) {117throw new RuntimeException("Failed to reconnect.");118}119120System.out.println(">>> Passed!");121122client.removeConnectionNotificationListener(listener);123client.close();124server.stop();125}126127//--------------------------128// private classes129//--------------------------130public static class NotificationEmitter extends NotificationBroadcasterSupport131implements NotificationEmitterMBean {132133public void sendNotifications(Notification n) {134sendNotification(n);135}136}137138public interface NotificationEmitterMBean {139public void sendNotifications(Notification n);140}141142private final static NotificationListener listener = new NotificationListener() {143public void handleNotification(Notification n, Object hb) {144145// treat the client notif to know the end146if (n instanceof JMXConnectionNotification) {147if (!JMXConnectionNotification.NOTIFS_LOST.equals(n.getType())) {148149clientState = n.getType();150System.out.println(151">>> The client state has been changed to: "+clientState);152153synchronized(lock) {154lock.notifyAll();155}156}157158return;159}160161System.out.println(">>> Do sleep to make reconnection.");162synchronized(lock) {163try {164lock.wait(listenerSleep);165} catch (Exception e) {166// OK167}168}169}170};171172private static final long serverTimeout = 1000;173private static final long listenerSleep = serverTimeout*6;174175private static String clientState = null;176private static final int[] lock = new int[0];177}178179180