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