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