1/*
2 * Copyright (c) 2005, 2015, 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 6318664
27 * @summary Test deadlock in MBeanRegistration.preDeregister method
28 * @author Eamonn McManus
29 *
30 * @run clean PreDeregisterDeadlockTest
31 * @run build PreDeregisterDeadlockTest
32 * @run main PreDeregisterDeadlockTest
33 */
34
35import java.util.concurrent.*;
36import javax.management.*;
37
38public class PreDeregisterDeadlockTest {
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 postRegister(Boolean done) {}
52
53        public void preDeregister() {
54            if (otherName == null)
55                return;
56            try {
57                Thread t = new Thread() {
58                    public void run() {
59                        try {
60                            mbs.unregisterMBean(otherName);
61                        } catch (Throwable e) {
62                            e.printStackTrace(System.out);
63                            fail(e.toString());
64                        }
65                    }
66                };
67                t.start();
68                t.join(5000L);
69                if (t.isAlive())
70                    fail("Deadlock detected");
71            } catch (Throwable e) {
72                e.printStackTrace(System.out);
73                fail(e.toString());
74            }
75        }
76
77        public void postDeregister() {}
78
79        private final MBeanServer mbs;
80        private final ObjectName otherName;
81    }
82
83    public static interface BlobbyMBean {}
84
85    public static class Blobby implements BlobbyMBean, MBeanRegistration {
86        public Blobby(MBeanServer mbs, Semaphore semaphore) {
87            this.mbs = mbs;
88            this.semaphore = semaphore;
89        }
90
91        public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
92            this.objectName = on;
93            return on;
94        }
95
96        public void postRegister(Boolean done) {}
97
98        public void preDeregister() throws Exception {
99            Thread t = new Thread() {
100                public void run() {
101                    try {
102                        mbs.unregisterMBean(objectName);
103                        fail("Nested unregister succeeded");
104                    } catch (InstanceNotFoundException e) {
105                        semaphore.release();
106                    } catch (Throwable e) {
107                        e.printStackTrace(System.out);
108                        fail(e.toString());
109                    }
110                }
111            };
112            t.start();
113            // Give the thread a chance to block so we are really
114            // testing parallelism.  (On slow machines we might not
115            // really be testing it but we should be covered by our
116            // faster machines.)
117            Thread.sleep(500L);
118        }
119
120        public void postDeregister() {}
121
122        private final MBeanServer mbs;
123        private ObjectName objectName;
124        private final Semaphore semaphore;
125    }
126
127    public static void main(String[] args) throws Exception {
128        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
129        ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");
130        ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");
131
132        // Test 1: preDeregister starts a thread which unregisters a
133        // different MBean: this must not deadlock
134        mbs.registerMBean(new Blibby(mbs, on2), on1);
135        mbs.registerMBean(new Blibby(mbs, null), on2);
136        mbs.unregisterMBean(on1);
137
138        // Test 2: preDeregister starts a thread which tries to
139        // unregister the same MBean: this thread should block until
140        // the original thread succeeds in unregistering, then
141        // get an InstanceNotFoundException.  We wait for it to
142        // complete here, using the semaphore.
143        Semaphore semaphore = new Semaphore(0);
144        mbs.registerMBean(new Blobby(mbs, semaphore), on1);
145        mbs.unregisterMBean(on1);
146        boolean ok = semaphore.tryAcquire(1, 5, TimeUnit.SECONDS);
147        if (!ok)
148            fail("Second unregister thread did not complete");
149
150        if (failure == null)
151            System.out.println("OK: Test passed");
152        else
153            throw new Exception("TEST FAILED: " + failure);
154    }
155
156    private static void fail(String why) {
157        System.out.println("FAILED: " + why);
158        failure = why;
159    }
160
161    private static volatile String failure;
162}
163