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