Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanServer/PostRegisterDeadlockTest.java
38838 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 641704426* @summary Test deadlock in MBeanRegistration.postRegister method27* @author Eamonn McManus, Daniel Fuchs28* @run clean PostRegisterDeadlockTest29* @run build PostRegisterDeadlockTest30* @run main PostRegisterDeadlockTest31*/3233import java.lang.Thread.State;34import java.util.concurrent.*;35import javax.management.*;3637public class PostRegisterDeadlockTest {38public static interface BlibbyMBean {}3940public static class Blibby implements BlibbyMBean, MBeanRegistration {41public Blibby(MBeanServer mbs, ObjectName otherName) {42this.mbs = mbs;43this.otherName = otherName;44}4546public ObjectName preRegister(MBeanServer mbs, ObjectName on) {47return on;48}4950public void preDeregister() {}5152public void postRegister(Boolean done) {53// If no other MBean was registered54// do nothing.55//56if (otherName == null) return;5758// Check that we can unregister59// other MBean60try {61Thread t = new Thread() {62public void run() {63try {64try {65mbs.unregisterMBean(otherName);66} catch (InstanceNotFoundException x) {67// Race condition!68System.out.println(otherName+69" was unregistered by main thread.");70}71} catch (Throwable e) {72e.printStackTrace(System.out);73fail(e.toString());74}75}76};77t.start();78t.join(5000L);79if (t.isAlive()) {80if (t.getState().equals(State.BLOCKED))81fail("Deadlock detected");82else83fail("Test not conclusive: "+84"Thread is alive but not blocked.");85}86} catch (Throwable e) {87e.printStackTrace(System.out);88fail(e.toString());89}90}9192public void postDeregister() {}9394private final MBeanServer mbs;95private final ObjectName otherName;96}9798public static void main(String[] args) throws Exception {99String previous = null;100MBeanServer mbs = MBeanServerFactory.newMBeanServer();101ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");102ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");103104105// Test 1:106// 1 MBean is registered with on1107// Another MBean is registered with on1, postRegister(FALSE) is108// called, and the second MBean attempts to unregister first MBean in109// postRegister:110// postRegister starts a thread which unregisters the first MBean:111// this must not deadlock112//113System.out.println("\n**** TEST #1 ****\n");114System.out.println("Registering Blibby #1 with name: " + on1);115mbs.registerMBean(new Blibby(mbs, null), on1);116try {117System.out.println("Registering Blibby #2 with same name: " + on1);118mbs.registerMBean(new Blibby(mbs, on1), on1);119} catch (InstanceAlreadyExistsException x) {120System.out.println("Received expected exception: " + x);121}122if (mbs.isRegistered(on1)) {123try {124mbs.unregisterMBean(on1);125if (failure == null)126fail(on1+" should have been unregistered");127} catch (InstanceNotFoundException x) {128// Race condition!129System.out.println(on1+" was unregistered by mbean thread.");130}131} else {132System.out.println(on1+" was correctly unregistered.");133}134135if (failure == previous)136System.out.println("\n**** TEST #1 PASSED ****\n");137138previous = failure;139140// Test 2:141// 1 MBean is registered with on1142// Another MBean is registered with on2, postRegister(TRUE) is143// called, and the second MBean attempts to unregister first MBean in144// postRegister:145// postRegister starts a thread which unregisters the first MBean:146// this must not deadlock147//148System.out.println("\n**** TEST #2 ****\n");149System.out.println("Registering Blibby #1 with name: " + on1);150mbs.registerMBean(new Blibby(mbs, null), on1);151System.out.println("Registering Blibby #2 with other name: " + on2);152mbs.registerMBean(new Blibby(mbs, on1), on2);153if (mbs.isRegistered(on1)) {154try {155mbs.unregisterMBean(on1);156if (failure == null)157fail(on1+" should have been unregistered");158} catch (InstanceNotFoundException x) {159// Race condition!160System.out.println(on1+" was unregistered by mbean thread.");161}162} else {163System.out.println(on1+" was correctly unregistered.");164}165166System.out.println("unregistering "+on2);167mbs.unregisterMBean(on2);168if (failure == previous)169System.out.println("\n**** TEST #2 PASSED ****\n");170previous = failure;171172if (failure == null)173System.out.println("OK: Test passed");174else175throw new Exception("TEST FAILED: " + failure);176}177178private static void fail(String why) {179System.out.println("FAILED: " + why);180failure = (failure == null)?why:(failure+",\n"+why);181}182183private static volatile String failure;184}185186187