jdk/test/javax/management/remote/mandatory/notif/NotifReconnectDeadlockTest.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1247 b4c26443dee5
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 1004
diff changeset
     2
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * "This test checks for a bug whereby reconnection did not work if (a) it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * initiated by the fetchNotifications thread and (b) it succeeded. These conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * are not usual, since connection failure is usually caused by either idle timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * (which doesn't usually happen if fetchNotifications is running) or communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * problems (which are usually permanent so reconnection fails).  But they can happen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * so we test for them here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The test sets a very short idle timeout, and effectively suspends the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * fetchNotifications thread by having it invoke a listener with a delay in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * This means that the idle timeout happens.  When the delayed listener returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the fetchNotifications thread will attempt to reconnect, and this attempt should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * succeed, so we meet the two conditions above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The test succeeds if there is indeed a reconnection, detected by the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * listener seeing an OPENED notification.  The connection listener should not see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * a CLOSED or FAILED notification."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class NotifReconnectDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
           ">>> Tests reconnection done by a fetching notif thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ObjectName oname = new ObjectName ("Default:name=NotificationEmitter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Map env = new HashMap(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        env.put("jmx.remote.x.server.connection.timeout", new Long(serverTimeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        env.put("jmx.remote.x.client.connection.check.period", new Long(Long.MAX_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        mbs.registerMBean(new NotificationEmitter(), oname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                                                               url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                                                               env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                                                               mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JMXServiceURL addr = server.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        JMXConnector client = JMXConnectorFactory.connect(addr, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Thread.sleep(100); // let pass the first client open notif if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        client.getMBeanServerConnection().addNotificationListener(oname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                                                  listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                                  null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                                  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        client.addConnectionNotificationListener(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // max test time: 2 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        final long end = System.currentTimeMillis()+120000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            while(clientState == null && System.currentTimeMillis() < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                mbs.invoke(oname, "sendNotifications",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                           new Object[] {new Notification("MyType", "", 0)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                           new String[] {"javax.management.Notification"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    lock.wait(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (clientState == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                  "No reconnection happened, need to reconfigure the test.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else if (JMXConnectionNotification.FAILED.equals(clientState) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                   JMXConnectionNotification.CLOSED.equals(clientState)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new RuntimeException("Failed to reconnect.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println(">>> Passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        client.removeConnectionNotificationListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
//--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
// private classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
//--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static class NotificationEmitter extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        implements NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public void sendNotifications(Notification n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            sendNotification(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public interface NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public void sendNotifications(Notification n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private final static NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            public void handleNotification(Notification n, Object hb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // treat the client notif to know the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (n instanceof JMXConnectionNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    if (!JMXConnectionNotification.NOTIFS_LOST.equals(n.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        clientState = n.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                           ">>> The client state has been changed to: "+clientState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                System.out.println(">>> Do sleep to make reconnection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        lock.wait(listenerSleep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
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
    private static final long serverTimeout = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static final long listenerSleep = serverTimeout*6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static String clientState = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static final int[] lock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}