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