Path: blob/master/test/jdk/javax/management/MBeanServer/PostRegisterDeadlockTest.java
51504 views
/*1* Copyright (c) 2006, 2015, 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*29* @run clean PostRegisterDeadlockTest30* @run build PostRegisterDeadlockTest31* @run main PostRegisterDeadlockTest32*/3334import java.lang.Thread.State;35import java.util.concurrent.*;36import javax.management.*;3738public class PostRegisterDeadlockTest {39public static interface BlibbyMBean {}4041public static class Blibby implements BlibbyMBean, MBeanRegistration {42public Blibby(MBeanServer mbs, ObjectName otherName) {43this.mbs = mbs;44this.otherName = otherName;45}4647public ObjectName preRegister(MBeanServer mbs, ObjectName on) {48return on;49}5051public void preDeregister() {}5253public void postRegister(Boolean done) {54// If no other MBean was registered55// do nothing.56//57if (otherName == null) return;5859// Check that we can unregister60// other MBean61try {62Thread t = new Thread() {63public void run() {64try {65try {66mbs.unregisterMBean(otherName);67} catch (InstanceNotFoundException x) {68// Race condition!69System.out.println(otherName+70" was unregistered by main thread.");71}72} catch (Throwable e) {73e.printStackTrace(System.out);74fail(e.toString());75}76}77};78t.start();79t.join(5000L);80if (t.isAlive()) {81if (t.getState().equals(State.BLOCKED))82fail("Deadlock detected");83else84fail("Test not conclusive: "+85"Thread is alive but not blocked.");86}87} catch (Throwable e) {88e.printStackTrace(System.out);89fail(e.toString());90}91}9293public void postDeregister() {}9495private final MBeanServer mbs;96private final ObjectName otherName;97}9899public static void main(String[] args) throws Exception {100String previous = null;101MBeanServer mbs = MBeanServerFactory.newMBeanServer();102ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");103ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");104105106// Test 1:107// 1 MBean is registered with on1108// Another MBean is registered with on1, postRegister(FALSE) is109// called, and the second MBean attempts to unregister first MBean in110// postRegister:111// postRegister starts a thread which unregisters the first MBean:112// this must not deadlock113//114System.out.println("\n**** TEST #1 ****\n");115System.out.println("Registering Blibby #1 with name: " + on1);116mbs.registerMBean(new Blibby(mbs, null), on1);117try {118System.out.println("Registering Blibby #2 with same name: " + on1);119mbs.registerMBean(new Blibby(mbs, on1), on1);120} catch (InstanceAlreadyExistsException x) {121System.out.println("Received expected exception: " + x);122}123if (mbs.isRegistered(on1)) {124try {125mbs.unregisterMBean(on1);126if (failure == null)127fail(on1+" should have been unregistered");128} catch (InstanceNotFoundException x) {129// Race condition!130System.out.println(on1+" was unregistered by mbean thread.");131}132} else {133System.out.println(on1+" was correctly unregistered.");134}135136if (failure == previous)137System.out.println("\n**** TEST #1 PASSED ****\n");138139previous = failure;140141// Test 2:142// 1 MBean is registered with on1143// Another MBean is registered with on2, postRegister(TRUE) is144// called, and the second MBean attempts to unregister first MBean in145// postRegister:146// postRegister starts a thread which unregisters the first MBean:147// this must not deadlock148//149System.out.println("\n**** TEST #2 ****\n");150System.out.println("Registering Blibby #1 with name: " + on1);151mbs.registerMBean(new Blibby(mbs, null), on1);152System.out.println("Registering Blibby #2 with other name: " + on2);153mbs.registerMBean(new Blibby(mbs, on1), on2);154if (mbs.isRegistered(on1)) {155try {156mbs.unregisterMBean(on1);157if (failure == null)158fail(on1+" should have been unregistered");159} catch (InstanceNotFoundException x) {160// Race condition!161System.out.println(on1+" was unregistered by mbean thread.");162}163} else {164System.out.println(on1+" was correctly unregistered.");165}166167System.out.println("unregistering "+on2);168mbs.unregisterMBean(on2);169if (failure == previous)170System.out.println("\n**** TEST #2 PASSED ****\n");171previous = failure;172173if (failure == null)174System.out.println("OK: Test passed");175else176throw new Exception("TEST FAILED: " + failure);177}178179private static void fail(String why) {180System.out.println("FAILED: " + why);181failure = (failure == null)?why:(failure+",\n"+why);182}183184private static volatile String failure;185}186187188