2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2006, 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
|
|
26 |
* @bug 6417044
|
|
27 |
* @summary Test deadlock in MBeanRegistration.postRegister method
|
|
28 |
* @author Eamonn McManus, Daniel Fuchs
|
|
29 |
* @run clean PostRegisterDeadlockTest
|
|
30 |
* @run build PostRegisterDeadlockTest
|
|
31 |
* @run main PostRegisterDeadlockTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.lang.Thread.State;
|
|
35 |
import java.util.concurrent.*;
|
|
36 |
import javax.management.*;
|
|
37 |
|
|
38 |
public class PostRegisterDeadlockTest {
|
|
39 |
public static interface BlibbyMBean {}
|
|
40 |
|
|
41 |
public static class Blibby implements BlibbyMBean, MBeanRegistration {
|
|
42 |
public Blibby(MBeanServer mbs, ObjectName otherName) {
|
|
43 |
this.mbs = mbs;
|
|
44 |
this.otherName = otherName;
|
|
45 |
}
|
|
46 |
|
|
47 |
public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
|
|
48 |
return on;
|
|
49 |
}
|
|
50 |
|
|
51 |
public void preDeregister() {}
|
|
52 |
|
|
53 |
public void postRegister(Boolean done) {
|
|
54 |
// If no other MBean was registered
|
|
55 |
// do nothing.
|
|
56 |
//
|
|
57 |
if (otherName == null) return;
|
|
58 |
|
|
59 |
// Check that we can unregister
|
|
60 |
// other MBean
|
|
61 |
try {
|
|
62 |
Thread t = new Thread() {
|
|
63 |
public void run() {
|
|
64 |
try {
|
|
65 |
try {
|
|
66 |
mbs.unregisterMBean(otherName);
|
|
67 |
} catch (InstanceNotFoundException x) {
|
|
68 |
// Race condition!
|
|
69 |
System.out.println(otherName+
|
|
70 |
" was unregistered by main thread.");
|
|
71 |
}
|
|
72 |
} catch (Throwable e) {
|
|
73 |
e.printStackTrace(System.out);
|
|
74 |
fail(e.toString());
|
|
75 |
}
|
|
76 |
}
|
|
77 |
};
|
|
78 |
t.start();
|
|
79 |
t.join(5000L);
|
|
80 |
if (t.isAlive()) {
|
|
81 |
if (t.getState().equals(State.BLOCKED))
|
|
82 |
fail("Deadlock detected");
|
|
83 |
else
|
|
84 |
fail("Test not conclusive: "+
|
|
85 |
"Thread is alive but not blocked.");
|
|
86 |
}
|
|
87 |
} catch (Throwable e) {
|
|
88 |
e.printStackTrace(System.out);
|
|
89 |
fail(e.toString());
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
public void postDeregister() {}
|
|
94 |
|
|
95 |
private final MBeanServer mbs;
|
|
96 |
private final ObjectName otherName;
|
|
97 |
}
|
|
98 |
|
|
99 |
public static void main(String[] args) throws Exception {
|
|
100 |
String previous = null;
|
|
101 |
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
|
|
102 |
ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");
|
|
103 |
ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");
|
|
104 |
|
|
105 |
|
|
106 |
// Test 1:
|
|
107 |
// 1 MBean is registered with on1
|
|
108 |
// Another MBean is registered with on1, postRegister(FALSE) is
|
|
109 |
// called, and the second MBean attempts to unregister first MBean in
|
|
110 |
// postRegister:
|
|
111 |
// postRegister starts a thread which unregisters the first MBean:
|
|
112 |
// this must not deadlock
|
|
113 |
//
|
|
114 |
System.out.println("\n**** TEST #1 ****\n");
|
|
115 |
System.out.println("Registering Blibby #1 with name: " + on1);
|
|
116 |
mbs.registerMBean(new Blibby(mbs, null), on1);
|
|
117 |
try {
|
|
118 |
System.out.println("Registering Blibby #2 with same name: " + on1);
|
|
119 |
mbs.registerMBean(new Blibby(mbs, on1), on1);
|
|
120 |
} catch (InstanceAlreadyExistsException x) {
|
|
121 |
System.out.println("Received expected exception: " + x);
|
|
122 |
}
|
|
123 |
if (mbs.isRegistered(on1)) {
|
|
124 |
try {
|
|
125 |
mbs.unregisterMBean(on1);
|
|
126 |
if (failure == null)
|
|
127 |
fail(on1+" should have been unregistered");
|
|
128 |
} catch (InstanceNotFoundException x) {
|
|
129 |
// Race condition!
|
|
130 |
System.out.println(on1+" was unregistered by mbean thread.");
|
|
131 |
}
|
|
132 |
} else {
|
|
133 |
System.out.println(on1+" was correctly unregistered.");
|
|
134 |
}
|
|
135 |
|
|
136 |
if (failure == previous)
|
|
137 |
System.out.println("\n**** TEST #1 PASSED ****\n");
|
|
138 |
|
|
139 |
previous = failure;
|
|
140 |
|
|
141 |
// Test 2:
|
|
142 |
// 1 MBean is registered with on1
|
|
143 |
// Another MBean is registered with on2, postRegister(TRUE) is
|
|
144 |
// called, and the second MBean attempts to unregister first MBean in
|
|
145 |
// postRegister:
|
|
146 |
// postRegister starts a thread which unregisters the first MBean:
|
|
147 |
// this must not deadlock
|
|
148 |
//
|
|
149 |
System.out.println("\n**** TEST #2 ****\n");
|
|
150 |
System.out.println("Registering Blibby #1 with name: " + on1);
|
|
151 |
mbs.registerMBean(new Blibby(mbs, null), on1);
|
|
152 |
System.out.println("Registering Blibby #2 with other name: " + on2);
|
|
153 |
mbs.registerMBean(new Blibby(mbs, on1), on2);
|
|
154 |
if (mbs.isRegistered(on1)) {
|
|
155 |
try {
|
|
156 |
mbs.unregisterMBean(on1);
|
|
157 |
if (failure == null)
|
|
158 |
fail(on1+" should have been unregistered");
|
|
159 |
} catch (InstanceNotFoundException x) {
|
|
160 |
// Race condition!
|
|
161 |
System.out.println(on1+" was unregistered by mbean thread.");
|
|
162 |
}
|
|
163 |
} else {
|
|
164 |
System.out.println(on1+" was correctly unregistered.");
|
|
165 |
}
|
|
166 |
|
|
167 |
System.out.println("unregistering "+on2);
|
|
168 |
mbs.unregisterMBean(on2);
|
|
169 |
if (failure == previous)
|
|
170 |
System.out.println("\n**** TEST #2 PASSED ****\n");
|
|
171 |
previous = failure;
|
|
172 |
|
|
173 |
if (failure == null)
|
|
174 |
System.out.println("OK: Test passed");
|
|
175 |
else
|
|
176 |
throw new Exception("TEST FAILED: " + failure);
|
|
177 |
}
|
|
178 |
|
|
179 |
private static void fail(String why) {
|
|
180 |
System.out.println("FAILED: " + why);
|
|
181 |
failure = (failure == null)?why:(failure+",\n"+why);
|
|
182 |
}
|
|
183 |
|
|
184 |
private static volatile String failure;
|
|
185 |
}
|