jdk/test/javax/management/remote/mandatory/notif/NotifReconnectDeadlockTest.java
author sjiang
Thu, 31 Jul 2008 15:31:13 +0200
changeset 1004 5ba8217eb504
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
5108776: Add reliable event handling to the JMX API 6218920: API bug - impossible to delete last MBeanServerForwarder on a connector Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    25
 * @test
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6199899
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests reconnection done by a fetching notif thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Shanliang JIANG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean NotifReconnectDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build NotifReconnectDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main NotifReconnectDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    34
import java.util.HashMap;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    35
import java.util.Map;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    36
import javax.management.MBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    37
import javax.management.MBeanServerFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    38
import javax.management.Notification;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    39
import javax.management.NotificationBroadcasterSupport;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    40
import javax.management.NotificationListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    41
import javax.management.ObjectName;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    42
import javax.management.remote.JMXConnectionNotification;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    43
import javax.management.remote.JMXConnector;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    44
import javax.management.remote.JMXConnectorFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    45
import javax.management.remote.JMXConnectorServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    46
import javax.management.remote.JMXConnectorServerFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    47
import javax.management.remote.JMXServiceURL;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    48
import javax.management.remote.rmi.RMIConnectorServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * "This test checks for a bug whereby reconnection did not work if (a) it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * initiated by the fetchNotifications thread and (b) it succeeded. These conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * are not usual, since connection failure is usually caused by either idle timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * (which doesn't usually happen if fetchNotifications is running) or communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * problems (which are usually permanent so reconnection fails).  But they can happen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * so we test for them here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The test sets a very short idle timeout, and effectively suspends the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * fetchNotifications thread by having it invoke a listener with a delay in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * This means that the idle timeout happens.  When the delayed listener returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the fetchNotifications thread will attempt to reconnect, and this attempt should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * succeed, so we meet the two conditions above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The test succeeds if there is indeed a reconnection, detected by the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * listener seeing an OPENED notification.  The connection listener should not see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * a CLOSED or FAILED notification."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class NotifReconnectDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
           ">>> Tests reconnection done by a fetching notif thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ObjectName oname = new ObjectName ("Default:name=NotificationEmitter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Map env = new HashMap(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        env.put("jmx.remote.x.server.connection.timeout", new Long(serverTimeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        env.put("jmx.remote.x.client.connection.check.period", new Long(Long.MAX_VALUE));
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    77
        env.put(RMIConnectorServer.DELEGATE_TO_EVENT_SERVICE, "false");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        mbs.registerMBean(new NotificationEmitter(), oname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                                                               url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                                                               env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                                                               mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        JMXServiceURL addr = server.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        JMXConnector client = JMXConnectorFactory.connect(addr, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Thread.sleep(100); // let pass the first client open notif if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        client.getMBeanServerConnection().addNotificationListener(oname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                                  listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                                  null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                                  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        client.addConnectionNotificationListener(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // max test time: 2 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        final long end = System.currentTimeMillis()+120000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            while(clientState == null && System.currentTimeMillis() < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                mbs.invoke(oname, "sendNotifications",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                           new Object[] {new Notification("MyType", "", 0)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                           new String[] {"javax.management.Notification"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    lock.wait(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (clientState == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                  "No reconnection happened, need to reconfigure the test.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else if (JMXConnectionNotification.FAILED.equals(clientState) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                   JMXConnectionNotification.CLOSED.equals(clientState)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new RuntimeException("Failed to reconnect.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.out.println(">>> Passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        client.removeConnectionNotificationListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
//--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
// private classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
//--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static class NotificationEmitter extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        implements NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        public void sendNotifications(Notification n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            sendNotification(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public interface NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public void sendNotifications(Notification n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private final static NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            public void handleNotification(Notification n, Object hb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // treat the client notif to know the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (n instanceof JMXConnectionNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    if (!JMXConnectionNotification.NOTIFS_LOST.equals(n.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        clientState = n.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                           ">>> The client state has been changed to: "+clientState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.out.println(">>> Do sleep to make reconnection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        lock.wait(listenerSleep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static final long serverTimeout = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final long listenerSleep = serverTimeout*6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static String clientState = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final int[] lock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}