0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6417044
0N/A * @summary Test deadlock in MBeanRegistration.postRegister method
0N/A * @author Eamonn McManus, Daniel Fuchs
0N/A * @run clean PostRegisterDeadlockTest
0N/A * @run build PostRegisterDeadlockTest
0N/A * @run main PostRegisterDeadlockTest
0N/A */
0N/A
0N/Aimport java.lang.Thread.State;
0N/Aimport java.util.concurrent.*;
0N/Aimport javax.management.*;
0N/A
0N/Apublic class PostRegisterDeadlockTest {
0N/A public static interface BlibbyMBean {}
0N/A
0N/A public static class Blibby implements BlibbyMBean, MBeanRegistration {
0N/A public Blibby(MBeanServer mbs, ObjectName otherName) {
0N/A this.mbs = mbs;
0N/A this.otherName = otherName;
0N/A }
0N/A
0N/A public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
0N/A return on;
0N/A }
0N/A
0N/A public void preDeregister() {}
0N/A
0N/A public void postRegister(Boolean done) {
0N/A // If no other MBean was registered
0N/A // do nothing.
0N/A //
0N/A if (otherName == null) return;
0N/A
0N/A // Check that we can unregister
0N/A // other MBean
0N/A try {
0N/A Thread t = new Thread() {
0N/A public void run() {
0N/A try {
0N/A try {
0N/A mbs.unregisterMBean(otherName);
0N/A } catch (InstanceNotFoundException x) {
0N/A // Race condition!
0N/A System.out.println(otherName+
0N/A " was unregistered by main thread.");
0N/A }
0N/A } catch (Throwable e) {
0N/A e.printStackTrace(System.out);
0N/A fail(e.toString());
0N/A }
0N/A }
0N/A };
0N/A t.start();
0N/A t.join(5000L);
0N/A if (t.isAlive()) {
0N/A if (t.getState().equals(State.BLOCKED))
0N/A fail("Deadlock detected");
0N/A else
0N/A fail("Test not conclusive: "+
0N/A "Thread is alive but not blocked.");
0N/A }
0N/A } catch (Throwable e) {
0N/A e.printStackTrace(System.out);
0N/A fail(e.toString());
0N/A }
0N/A }
0N/A
0N/A public void postDeregister() {}
0N/A
0N/A private final MBeanServer mbs;
0N/A private final ObjectName otherName;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A String previous = null;
0N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
0N/A ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");
0N/A ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");
0N/A
0N/A
0N/A // Test 1:
0N/A // 1 MBean is registered with on1
0N/A // Another MBean is registered with on1, postRegister(FALSE) is
0N/A // called, and the second MBean attempts to unregister first MBean in
0N/A // postRegister:
0N/A // postRegister starts a thread which unregisters the first MBean:
0N/A // this must not deadlock
0N/A //
0N/A System.out.println("\n**** TEST #1 ****\n");
0N/A System.out.println("Registering Blibby #1 with name: " + on1);
0N/A mbs.registerMBean(new Blibby(mbs, null), on1);
0N/A try {
0N/A System.out.println("Registering Blibby #2 with same name: " + on1);
0N/A mbs.registerMBean(new Blibby(mbs, on1), on1);
0N/A } catch (InstanceAlreadyExistsException x) {
0N/A System.out.println("Received expected exception: " + x);
0N/A }
0N/A if (mbs.isRegistered(on1)) {
0N/A try {
0N/A mbs.unregisterMBean(on1);
0N/A if (failure == null)
0N/A fail(on1+" should have been unregistered");
0N/A } catch (InstanceNotFoundException x) {
0N/A // Race condition!
0N/A System.out.println(on1+" was unregistered by mbean thread.");
0N/A }
0N/A } else {
0N/A System.out.println(on1+" was correctly unregistered.");
0N/A }
0N/A
0N/A if (failure == previous)
0N/A System.out.println("\n**** TEST #1 PASSED ****\n");
0N/A
0N/A previous = failure;
0N/A
0N/A // Test 2:
0N/A // 1 MBean is registered with on1
0N/A // Another MBean is registered with on2, postRegister(TRUE) is
0N/A // called, and the second MBean attempts to unregister first MBean in
0N/A // postRegister:
0N/A // postRegister starts a thread which unregisters the first MBean:
0N/A // this must not deadlock
0N/A //
0N/A System.out.println("\n**** TEST #2 ****\n");
0N/A System.out.println("Registering Blibby #1 with name: " + on1);
0N/A mbs.registerMBean(new Blibby(mbs, null), on1);
0N/A System.out.println("Registering Blibby #2 with other name: " + on2);
0N/A mbs.registerMBean(new Blibby(mbs, on1), on2);
0N/A if (mbs.isRegistered(on1)) {
0N/A try {
0N/A mbs.unregisterMBean(on1);
0N/A if (failure == null)
0N/A fail(on1+" should have been unregistered");
0N/A } catch (InstanceNotFoundException x) {
0N/A // Race condition!
0N/A System.out.println(on1+" was unregistered by mbean thread.");
0N/A }
0N/A } else {
0N/A System.out.println(on1+" was correctly unregistered.");
0N/A }
0N/A
0N/A System.out.println("unregistering "+on2);
0N/A mbs.unregisterMBean(on2);
0N/A if (failure == previous)
0N/A System.out.println("\n**** TEST #2 PASSED ****\n");
0N/A previous = failure;
0N/A
0N/A if (failure == null)
0N/A System.out.println("OK: Test passed");
0N/A else
0N/A throw new Exception("TEST FAILED: " + failure);
0N/A }
0N/A
0N/A private static void fail(String why) {
0N/A System.out.println("FAILED: " + why);
0N/A failure = (failure == null)?why:(failure+",\n"+why);
0N/A }
0N/A
0N/A private static volatile String failure;
0N/A}