Path: blob/master/test/jdk/javax/management/MBeanServer/PostRegisterDeadlockTest2.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 that a failing MBean registration does not lead to a deadlock27* @author Eamonn McManus28*29* @run main PostRegisterDeadlockTest230*/3132import javax.management.*;3334public class PostRegisterDeadlockTest2 {35private static String failed;3637public static interface EmptyMBean {}3839public static class Empty implements EmptyMBean, MBeanRegistration {40public ObjectName preRegister(MBeanServer mbs, ObjectName on) {41this.mbs = mbs;42this.on = on;43return on;44}45public void postRegister(Boolean done) {46Thread t = new Thread() {47public void run() {48if (!mbs.isRegistered(on))49failed = "Not registered!";50}51};52t.start();53try {54t.join(5000L);55} catch (InterruptedException e) {56failed = "Interrupted: " + e;57}58if (t.isAlive())59failed = "Deadlock detected";60}61public void preDeregister() {}62public void postDeregister() {}6364private MBeanServer mbs;65private ObjectName on;66}6768public static void main(String[] args) throws Exception {69MBeanServer mbs = MBeanServerFactory.newMBeanServer();70ObjectName on = new ObjectName("a:b=c");71mbs.registerMBean(new Empty(), on);72try {73mbs.registerMBean(new Empty(), on);74throw new Exception("FAILED: did not get expected exception");75} catch (InstanceAlreadyExistsException e) {76System.out.println("OK: got expected exception");77}78if (failed != null)79throw new Exception("FAILED: " + failed);80System.out.println("TEST PASSED");81}82}838485