author | dbuck |
Tue, 18 Aug 2015 04:29:28 -0700 | |
changeset 32417 | 6859107fc6c3 |
parent 30376 | 2ccf2cf7ea48 |
child 43503 | bc7f8619ab70 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
20768
d7bfaa88e290
8025204: Intermittent test failure: javax/management/remote/mandatory/connection/IdleTimeoutTest.java
sjiang
parents:
14917
diff
changeset
|
26 |
* @bug 4886838 4886830 8025204 |
2 | 27 |
* @summary Tests that idle timeouts happen at appropriate times |
28 |
* @author Eamonn McManus |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
23010
diff
changeset
|
29 |
* @modules java.management/com.sun.jmx.remote.util |
2 | 30 |
* @run clean IdleTimeoutTest |
31 |
* @run build IdleTimeoutTest |
|
32 |
* @run main IdleTimeoutTest |
|
33 |
*/ |
|
34 |
||
35 |
import java.util.Arrays; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.Iterator; |
|
38 |
import java.util.List; |
|
39 |
import java.util.HashMap; |
|
40 |
import java.util.Map; |
|
41 |
import javax.management.MBeanServer; |
|
42 |
import javax.management.MBeanServerConnection; |
|
43 |
import javax.management.MBeanServerFactory; |
|
44 |
import javax.management.MBeanServerNotification; |
|
45 |
import javax.management.Notification; |
|
46 |
import javax.management.NotificationListener; |
|
47 |
import javax.management.ObjectName; |
|
48 |
import javax.management.remote.JMXConnector; |
|
49 |
import javax.management.remote.JMXConnectorFactory; |
|
50 |
import javax.management.remote.JMXConnectorServer; |
|
51 |
import javax.management.remote.JMXConnectorServerFactory; |
|
52 |
import javax.management.remote.JMXServiceURL; |
|
53 |
import com.sun.jmx.remote.util.EnvHelp; |
|
54 |
||
55 |
public class IdleTimeoutTest { |
|
14917
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
56 |
|
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
57 |
static boolean isPresent(String cn) { |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
58 |
try { |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
59 |
Class.forName(cn); |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
60 |
return true; |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
61 |
} catch (ClassNotFoundException x) { |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
62 |
return false; |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
63 |
} |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
64 |
} |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
65 |
|
2 | 66 |
public static void main(String[] args) throws Exception { |
67 |
boolean ok = true; |
|
68 |
List protos; |
|
69 |
if (args.length > 0) |
|
70 |
protos = Arrays.asList(args); |
|
71 |
else { |
|
14917
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
72 |
protos = new ArrayList(Arrays.asList(new String[] {"rmi"})); |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
73 |
if (isPresent("javax.management.remote.rmi._RMIConnectionImpl_Tie")) |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
74 |
protos.add("iiop"); |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
75 |
if (isPresent("javax.management.remote.jmxmp.JMXMPConnectorServer")) |
2 | 76 |
protos.add("jmxmp"); |
77 |
} |
|
78 |
for (Iterator it = protos.iterator(); it.hasNext(); ) { |
|
79 |
String proto = (String) it.next(); |
|
80 |
int liCount; |
|
81 |
if (proto.equals("jmxmp")) liCount=1; |
|
82 |
else liCount=2; |
|
83 |
if (test(proto,4,liCount)) |
|
84 |
System.out.println("Test for protocol " + proto + " passed"); |
|
85 |
else { |
|
86 |
System.out.println("Test for protocol " + proto + " FAILED"); |
|
87 |
ok = false; |
|
88 |
} |
|
89 |
} |
|
90 |
if (!ok) { |
|
14917
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
91 |
throw new RuntimeException("Some tests failed - see log for details"); |
2 | 92 |
} |
93 |
} |
|
94 |
||
95 |
private static long getIdleTimeout(MBeanServer mbs, JMXServiceURL url) |
|
14917
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
96 |
throws Exception |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
97 |
{ |
2 | 98 |
JMXConnectorServer server = |
4156 | 99 |
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); |
2 | 100 |
server.start(); |
101 |
try { |
|
102 |
url = server.getAddress(); |
|
103 |
||
104 |
// Force initialization (class loading, JIT, etc...) |
|
105 |
// |
|
106 |
JMXConnector client = JMXConnectorFactory.connect(url); |
|
107 |
try { |
|
108 |
String connId = client.getConnectionId(); |
|
109 |
MBeanServerConnection conn = client.getMBeanServerConnection(); |
|
110 |
} finally { |
|
111 |
client.close(); |
|
112 |
} |
|
113 |
||
114 |
// Do the time measurement |
|
115 |
// |
|
116 |
final long firstTime = System.currentTimeMillis(); |
|
117 |
final long endtime; |
|
118 |
client = JMXConnectorFactory.connect(url); |
|
119 |
try { |
|
120 |
String connId = client.getConnectionId(); |
|
121 |
MBeanServerConnection conn = client.getMBeanServerConnection(); |
|
122 |
endtime = System.currentTimeMillis(); |
|
123 |
} finally { |
|
124 |
client.close(); |
|
125 |
} |
|
126 |
||
127 |
// multipled by 10 for a slow machine, plus 1500 for a fast one. |
|
128 |
return 10*(endtime - firstTime) + 1500; |
|
129 |
} finally { |
|
130 |
server.stop(); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
private static class NotificationCounter |
|
135 |
implements NotificationListener { |
|
136 |
private final int[] listenerCount; |
|
137 |
private final String listenerName; |
|
138 |
NotificationCounter(int[] counter, String name) { |
|
139 |
listenerCount=counter; |
|
140 |
listenerName=name; |
|
141 |
} |
|
142 |
||
143 |
public void handleNotification(Notification n, |
|
144 |
Object h) { |
|
145 |
MBeanServerNotification mbsn = |
|
146 |
(MBeanServerNotification) n; |
|
147 |
System.out.println(listenerName + " got notification: " |
|
148 |
+ mbsn.getMBeanName()); |
|
149 |
synchronized (listenerCount) { |
|
150 |
listenerCount[0]++; |
|
151 |
listenerCount.notify(); |
|
152 |
} |
|
153 |
} |
|
154 |
public String toString() { |
|
155 |
return listenerName; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
private static boolean test(String proto,int opCount,int liCount) |
|
160 |
throws Exception { |
|
161 |
System.out.println("Idle timeout test for protocol " + proto); |
|
162 |
ObjectName delegateName = |
|
163 |
ObjectName.getInstance("JMImplementation:" + |
|
164 |
"type=MBeanServerDelegate"); |
|
165 |
MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
|
166 |
JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://"); |
|
167 |
||
168 |
final long timeout = getIdleTimeout(mbs,url); |
|
169 |
System.out.println("Timeout for " + proto + " is: " + |
|
170 |
timeout + " ms"); |
|
171 |
||
172 |
Map idleMap = new HashMap(); |
|
173 |
idleMap.put(EnvHelp.SERVER_CONNECTION_TIMEOUT, new Long(timeout)); |
|
174 |
JMXConnectorServer server = |
|
175 |
JMXConnectorServerFactory.newJMXConnectorServer(url,idleMap,mbs); |
|
176 |
||
177 |
final int[] listenerCount = new int[1]; |
|
178 |
final NotificationListener countListeners[] = |
|
179 |
new NotificationListener[liCount]; |
|
180 |
int i; |
|
181 |
for (i=0; i<countListeners.length; i++) { |
|
182 |
countListeners[i] = |
|
183 |
new NotificationCounter(listenerCount,"Listener"+i); |
|
184 |
} |
|
185 |
||
186 |
server.start(); |
|
187 |
try { |
|
188 |
url = server.getAddress(); |
|
189 |
final long firstTime = System.currentTimeMillis(); |
|
190 |
JMXConnector client = JMXConnectorFactory.connect(url); |
|
191 |
long elapsed, startIdle=0; |
|
192 |
try { |
|
193 |
String connId = client.getConnectionId(); |
|
194 |
MBeanServerConnection conn = |
|
195 |
client.getMBeanServerConnection(); |
|
196 |
elapsed = System.currentTimeMillis() - firstTime; |
|
197 |
System.out.println("Idle Time: " + elapsed + "ms"); |
|
198 |
||
199 |
for (i=0; i<countListeners.length; i++) { |
|
200 |
System.out.println("add " + countListeners[i] + |
|
201 |
": starting at " + elapsed + "ms"); |
|
202 |
conn.addNotificationListener(delegateName, |
|
203 |
countListeners[i], |
|
204 |
null,null); |
|
205 |
} |
|
206 |
||
207 |
System.out.println("connId=" + connId); |
|
208 |
for (i = 0; i < opCount; i++) { |
|
209 |
elapsed = System.currentTimeMillis() - firstTime; |
|
210 |
System.out.println("Operation[" + (i+1) |
|
211 |
+"]: starting at " + |
|
212 |
elapsed + "ms"); |
|
213 |
final String name = "d:type=mlet,instance=" + i; |
|
214 |
mbs.createMBean("javax.management.loading.MLet", |
|
215 |
new ObjectName(name)); |
|
216 |
if (i == (opCount-1)) |
|
217 |
startIdle = System.currentTimeMillis(); |
|
218 |
Thread.sleep(2); |
|
219 |
} |
|
220 |
||
221 |
// Wait for notifs to arrive before doing removeNListener |
|
222 |
long startTime = System.currentTimeMillis(); |
|
223 |
long deadline = startTime + 10000; |
|
224 |
||
225 |
System.out.println("Waiting for notifs: starting at " + |
|
226 |
(startTime - firstTime) + "ms"); |
|
227 |
||
228 |
final int expectedCount = opCount*countListeners.length; |
|
229 |
while (System.currentTimeMillis() < deadline) { |
|
230 |
synchronized (listenerCount) { |
|
231 |
if (listenerCount[0] >= expectedCount) |
|
232 |
break; |
|
233 |
listenerCount.wait(); |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
long elapsedWait = System.currentTimeMillis() - startTime; |
|
238 |
System.out.println("Waited " + elapsedWait + |
|
239 |
"ms for notifs to arrive"); |
|
240 |
||
241 |
if (listenerCount[0] != expectedCount) { |
|
242 |
System.out.println("Did not get expected " + |
|
243 |
expectedCount + " notifications: " |
|
244 |
+ listenerCount[0]); |
|
245 |
return false; |
|
246 |
} |
|
247 |
||
248 |
elapsed = System.currentTimeMillis() - firstTime; |
|
249 |
||
250 |
System.out.println("idle time since last operation: " + |
|
251 |
(elapsed + firstTime - startIdle) + "ms"); |
|
252 |
System.out.println("Requesting conn id at: " + |
|
253 |
elapsed + "ms"); |
|
254 |
final String cid = client.getConnectionId(); |
|
255 |
||
256 |
elapsed = System.currentTimeMillis() - firstTime; |
|
257 |
System.out.println("Got conn id <" + cid + "> at: " + |
|
258 |
elapsed + "ms"); |
|
259 |
||
260 |
if (!connId.equals(cid)) { |
|
261 |
System.out.println("Client id changed: <" + connId + |
|
262 |
"> -> <" + cid + |
|
263 |
">"); |
|
264 |
return false; |
|
265 |
} |
|
266 |
||
267 |
List ids = Arrays.asList(server.getConnectionIds()); |
|
268 |
if (!ids.contains(connId)) { |
|
269 |
System.out.println("Server ids don't contain our id: " + |
|
270 |
ids + " - " + connId); |
|
271 |
return false; |
|
272 |
} |
|
273 |
||
274 |
for (i=0;i<countListeners.length;i++) { |
|
275 |
System.out.println("Removing notification listener: " + |
|
276 |
countListeners[i]); |
|
277 |
conn.removeNotificationListener(delegateName, |
|
278 |
countListeners[i]); |
|
279 |
} |
|
280 |
||
281 |
System.out.println("Waiting for id list to drop ours"); |
|
20768
d7bfaa88e290
8025204: Intermittent test failure: javax/management/remote/mandatory/connection/IdleTimeoutTest.java
sjiang
parents:
14917
diff
changeset
|
282 |
// pass or timed out by test harness - see 8025204 |
d7bfaa88e290
8025204: Intermittent test failure: javax/management/remote/mandatory/connection/IdleTimeoutTest.java
sjiang
parents:
14917
diff
changeset
|
283 |
do { |
d7bfaa88e290
8025204: Intermittent test failure: javax/management/remote/mandatory/connection/IdleTimeoutTest.java
sjiang
parents:
14917
diff
changeset
|
284 |
Thread.sleep(100); |
d7bfaa88e290
8025204: Intermittent test failure: javax/management/remote/mandatory/connection/IdleTimeoutTest.java
sjiang
parents:
14917
diff
changeset
|
285 |
ids = Arrays.asList(server.getConnectionIds()); |
d7bfaa88e290
8025204: Intermittent test failure: javax/management/remote/mandatory/connection/IdleTimeoutTest.java
sjiang
parents:
14917
diff
changeset
|
286 |
} while (ids.contains(connId)); |
2 | 287 |
|
288 |
conn.getDefaultDomain(); |
|
289 |
if (connId.equals(client.getConnectionId())) { |
|
290 |
System.out.println("Client id did not change: <" + connId + |
|
291 |
">: idle timeout did not happen?"); |
|
292 |
return false; |
|
293 |
} else { |
|
294 |
System.out.println("Client id changed as expected: <" + |
|
295 |
connId + "> -> <" + |
|
296 |
client.getConnectionId() + ">"); |
|
297 |
} |
|
298 |
} finally { |
|
299 |
client.close(); |
|
300 |
System.out.println("Connection id list on server after " + |
|
301 |
"client close: " + |
|
302 |
Arrays.asList(server.getConnectionIds())); |
|
303 |
} |
|
304 |
} finally { |
|
305 |
server.stop(); |
|
306 |
} |
|
307 |
System.out.println("*** ------------------------------------------"); |
|
308 |
System.out.println("*** Test passed for " + proto); |
|
309 |
System.out.println("*** ------------------------------------------"); |
|
310 |
return true; |
|
311 |
} |
|
312 |
} |