author | vtewari |
Thu, 01 Sep 2016 15:02:32 +0530 | |
changeset 40699 | df9fab06ba9d |
parent 31455 | 412ba247f8df |
child 42338 | a60f280f803c |
permissions | -rw-r--r-- |
7170 | 1 |
/* |
40699
df9fab06ba9d
8164609: javax/management/remote/mandatory/notif/DeadListenerTest.java fails with Assertion Error
vtewari
parents:
31455
diff
changeset
|
2 |
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. |
7170 | 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 6957378 |
|
27 |
* @summary Test that a listener can be removed remotely from an MBean that no longer exists. |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
23010
diff
changeset
|
28 |
* @modules java.management/com.sun.jmx.remote.internal |
7170 | 29 |
* @author Eamonn McManus |
31455
412ba247f8df
8085973: The targeted processes in javax/management tests should be launched with -XX:+UsePerfData flag in order to work on embedded platforms
ykantser
parents:
30376
diff
changeset
|
30 |
* @run main/othervm -XX:+UsePerfData DeadListenerTest |
7170 | 31 |
*/ |
32 |
||
33 |
import com.sun.jmx.remote.internal.ServerNotifForwarder; |
|
34 |
import java.io.IOException; |
|
35 |
import java.lang.management.ManagementFactory; |
|
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
36 |
import java.lang.ref.WeakReference; |
7170 | 37 |
import java.lang.reflect.Field; |
38 |
import java.lang.reflect.Method; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.HashMap; |
|
41 |
import java.util.List; |
|
42 |
import java.util.Map; |
|
43 |
import java.util.Set; |
|
44 |
import java.util.concurrent.atomic.AtomicInteger; |
|
45 |
import javax.management.ListenerNotFoundException; |
|
46 |
import javax.management.MBeanServer; |
|
47 |
import javax.management.MBeanServerConnection; |
|
48 |
import javax.management.MBeanServerDelegate; |
|
49 |
import javax.management.Notification; |
|
50 |
import javax.management.NotificationBroadcasterSupport; |
|
51 |
import javax.management.NotificationFilterSupport; |
|
52 |
import javax.management.NotificationListener; |
|
53 |
import javax.management.ObjectName; |
|
54 |
import javax.management.remote.JMXConnector; |
|
55 |
import javax.management.remote.JMXConnectorFactory; |
|
56 |
import javax.management.remote.JMXServiceURL; |
|
57 |
import javax.management.remote.rmi.RMIConnection; |
|
58 |
import javax.management.remote.rmi.RMIConnectionImpl; |
|
59 |
import javax.management.remote.rmi.RMIConnectorServer; |
|
60 |
import javax.management.remote.rmi.RMIJRMPServerImpl; |
|
61 |
import javax.security.auth.Subject; |
|
62 |
||
63 |
public class DeadListenerTest { |
|
64 |
public static void main(String[] args) throws Exception { |
|
65 |
final ObjectName delegateName = MBeanServerDelegate.DELEGATE_NAME; |
|
66 |
||
67 |
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
|
68 |
Noddy mbean = new Noddy(); |
|
69 |
ObjectName name = new ObjectName("d:k=v"); |
|
70 |
mbs.registerMBean(mbean, name); |
|
71 |
||
72 |
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///"); |
|
73 |
SnoopRMIServerImpl rmiServer = new SnoopRMIServerImpl(); |
|
74 |
RMIConnectorServer cs = new RMIConnectorServer(url, null, rmiServer, mbs); |
|
75 |
cs.start(); |
|
76 |
JMXServiceURL addr = cs.getAddress(); |
|
77 |
assertTrue("No connections in new connector server", rmiServer.connections.isEmpty()); |
|
78 |
||
79 |
JMXConnector cc = JMXConnectorFactory.connect(addr); |
|
80 |
MBeanServerConnection mbsc = cc.getMBeanServerConnection(); |
|
81 |
assertTrue("One connection on server after client connect", rmiServer.connections.size() == 1); |
|
82 |
RMIConnectionImpl connection = rmiServer.connections.get(0); |
|
83 |
Method getServerNotifFwdM = RMIConnectionImpl.class.getDeclaredMethod("getServerNotifFwd"); |
|
84 |
getServerNotifFwdM.setAccessible(true); |
|
85 |
ServerNotifForwarder serverNotifForwarder = (ServerNotifForwarder) getServerNotifFwdM.invoke(connection); |
|
86 |
Field listenerMapF = ServerNotifForwarder.class.getDeclaredField("listenerMap"); |
|
87 |
listenerMapF.setAccessible(true); |
|
88 |
@SuppressWarnings("unchecked") |
|
89 |
Map<ObjectName, Set<?>> listenerMap = (Map<ObjectName, Set<?>>) listenerMapF.get(serverNotifForwarder); |
|
90 |
assertTrue("Server listenerMap initially empty", mapWithoutKey(listenerMap, delegateName).isEmpty()); |
|
91 |
||
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
92 |
final AtomicInteger count1Val = new AtomicInteger(); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
93 |
CountListener count1 = new CountListener(count1Val); |
7170 | 94 |
mbsc.addNotificationListener(name, count1, null, null); |
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
95 |
WeakReference<CountListener> count1Ref = new WeakReference<>(count1); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
96 |
count1 = null; |
7170 | 97 |
|
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
98 |
final AtomicInteger count2Val = new AtomicInteger(); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
99 |
CountListener count2 = new CountListener(count2Val); |
7170 | 100 |
NotificationFilterSupport dummyFilter = new NotificationFilterSupport(); |
101 |
dummyFilter.enableType(""); |
|
102 |
mbsc.addNotificationListener(name, count2, dummyFilter, "noddy"); |
|
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
103 |
WeakReference<CountListener> count2Ref = new WeakReference<>(count2); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
104 |
count2 = null; |
7170 | 105 |
|
106 |
assertTrue("One entry in listenerMap for two listeners on same MBean", mapWithoutKey(listenerMap, delegateName).size() == 1); |
|
107 |
Set<?> set = listenerMap.get(name); |
|
108 |
assertTrue("Set in listenerMap for MBean has two elements", set != null && set.size() == 2); |
|
109 |
||
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
110 |
assertTrue("Initial value of count1 == 0", count1Val.get() == 0); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
111 |
assertTrue("Initial value of count2 == 0", count2Val.get() == 0); |
7170 | 112 |
|
113 |
Notification notif = new Notification("type", name, 0); |
|
114 |
||
115 |
mbean.sendNotification(notif); |
|
116 |
||
117 |
// Make sure notifs are working normally. |
|
40699
df9fab06ba9d
8164609: javax/management/remote/mandatory/notif/DeadListenerTest.java fails with Assertion Error
vtewari
parents:
31455
diff
changeset
|
118 |
while ((count1Val.get() != 1 || count2Val.get() != 1) ) { |
df9fab06ba9d
8164609: javax/management/remote/mandatory/notif/DeadListenerTest.java fails with Assertion Error
vtewari
parents:
31455
diff
changeset
|
119 |
Thread.sleep(20); |
7170 | 120 |
} |
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
121 |
assertTrue("New value of count1 == 1", count1Val.get() == 1); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
122 |
assertTrue("Initial value of count2 == 1", count2Val.get() == 1); |
7170 | 123 |
|
124 |
// Make sure that removing a nonexistent listener from an existent MBean produces ListenerNotFoundException |
|
125 |
CountListener count3 = new CountListener(); |
|
126 |
try { |
|
127 |
mbsc.removeNotificationListener(name, count3); |
|
128 |
assertTrue("Remove of nonexistent listener succeeded but should not have", false); |
|
129 |
} catch (ListenerNotFoundException e) { |
|
130 |
// OK: expected |
|
131 |
} |
|
132 |
||
133 |
// Make sure that removing a nonexistent listener from a nonexistent MBean produces ListenerNotFoundException |
|
134 |
ObjectName nonexistent = new ObjectName("foo:bar=baz"); |
|
135 |
assertTrue("Nonexistent is nonexistent", !mbs.isRegistered(nonexistent)); |
|
136 |
try { |
|
137 |
mbsc.removeNotificationListener(nonexistent, count3); |
|
138 |
assertTrue("Remove of listener from nonexistent MBean succeeded but should not have", false); |
|
139 |
} catch (ListenerNotFoundException e) { |
|
140 |
// OK: expected |
|
141 |
} |
|
142 |
||
143 |
// Now unregister our MBean, and check that notifs it sends no longer go anywhere. |
|
144 |
mbs.unregisterMBean(name); |
|
145 |
mbean.sendNotification(notif); |
|
146 |
Thread.sleep(200); |
|
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
147 |
|
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
148 |
assertTrue("New value of count1 == 1", count1Val.get() == 1); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
149 |
assertTrue("Initial value of count2 == 1", count2Val.get() == 1); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
150 |
|
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
151 |
// wait for the listener cleanup to take place upon processing notifications |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
152 |
int countdown = 50; // waiting max. 5 secs |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
153 |
while (countdown-- > 0 && |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
154 |
(count1Ref.get() != null || |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
155 |
count2Ref.get() != null)) { |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
156 |
System.gc(); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
157 |
Thread.sleep(100); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
158 |
System.gc(); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
159 |
} |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
160 |
// listener has been removed or the wait has timed out |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
161 |
|
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
162 |
assertTrue("count1 notification listener has not been cleaned up", count1Ref.get() == null); |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
163 |
assertTrue("count2 notification listener has not been cleaned up", count2Ref.get() == null); |
7170 | 164 |
|
165 |
// Check that there is no trace of the listeners any more in ServerNotifForwarder.listenerMap. |
|
166 |
// THIS DEPENDS ON JMX IMPLEMENTATION DETAILS. |
|
167 |
// If the JMX implementation changes, the code here may have to change too. |
|
168 |
Set<?> setForUnreg = listenerMap.get(name); |
|
169 |
assertTrue("No trace of unregistered MBean: " + setForUnreg, setForUnreg == null); |
|
170 |
} |
|
171 |
||
172 |
private static <K, V> Map<K, V> mapWithoutKey(Map<K, V> map, K key) { |
|
173 |
Map<K, V> copy = new HashMap<K, V>(map); |
|
174 |
copy.remove(key); |
|
175 |
return copy; |
|
176 |
} |
|
177 |
||
178 |
public static interface NoddyMBean {} |
|
179 |
||
180 |
public static class Noddy extends NotificationBroadcasterSupport implements NoddyMBean {} |
|
181 |
||
182 |
public static class CountListener implements NotificationListener { |
|
183 |
final AtomicInteger count; |
|
184 |
||
15536
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
185 |
public CountListener(AtomicInteger i) { |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
186 |
count = i; |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
187 |
} |
ac6c0f4a8898
7170447: Intermittent DeadListenerTest.java failure
jbachorik
parents:
7170
diff
changeset
|
188 |
|
7170 | 189 |
public CountListener() { |
190 |
this.count = new AtomicInteger(); |
|
191 |
} |
|
192 |
||
193 |
int count() { |
|
194 |
return count.get(); |
|
195 |
} |
|
196 |
||
197 |
public void handleNotification(Notification notification, Object handback) { |
|
198 |
count.incrementAndGet(); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
private static void assertTrue(String what, boolean cond) { |
|
203 |
if (!cond) { |
|
204 |
throw new AssertionError("Assertion failed: " + what); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
private static class SnoopRMIServerImpl extends RMIJRMPServerImpl { |
|
209 |
final List<RMIConnectionImpl> connections = new ArrayList<RMIConnectionImpl>(); |
|
210 |
SnoopRMIServerImpl() throws IOException { |
|
211 |
super(0, null, null, null); |
|
212 |
} |
|
213 |
||
214 |
@Override |
|
215 |
protected RMIConnection makeClient(String id, Subject subject) throws IOException { |
|
216 |
RMIConnectionImpl conn = (RMIConnectionImpl) super.makeClient(id, subject); |
|
217 |
connections.add(conn); |
|
218 |
return conn; |
|
219 |
} |
|
220 |
} |
|
221 |
} |