Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanServer/PreDeregisterDeadlockTest.java
38838 views
/*1* Copyright (c) 2005, 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 631866426* @summary Test deadlock in MBeanRegistration.preDeregister method27* @author Eamonn McManus28* @run clean PreDeregisterDeadlockTest29* @run build PreDeregisterDeadlockTest30* @run main PreDeregisterDeadlockTest31*/3233import java.util.concurrent.*;34import javax.management.*;3536public class PreDeregisterDeadlockTest {37public static interface BlibbyMBean {}3839public static class Blibby implements BlibbyMBean, MBeanRegistration {40public Blibby(MBeanServer mbs, ObjectName otherName) {41this.mbs = mbs;42this.otherName = otherName;43}4445public ObjectName preRegister(MBeanServer mbs, ObjectName on) {46return on;47}4849public void postRegister(Boolean done) {}5051public void preDeregister() {52if (otherName == null)53return;54try {55Thread t = new Thread() {56public void run() {57try {58mbs.unregisterMBean(otherName);59} catch (Throwable e) {60e.printStackTrace(System.out);61fail(e.toString());62}63}64};65t.start();66t.join(5000L);67if (t.isAlive())68fail("Deadlock detected");69} catch (Throwable e) {70e.printStackTrace(System.out);71fail(e.toString());72}73}7475public void postDeregister() {}7677private final MBeanServer mbs;78private final ObjectName otherName;79}8081public static interface BlobbyMBean {}8283public static class Blobby implements BlobbyMBean, MBeanRegistration {84public Blobby(MBeanServer mbs, Semaphore semaphore) {85this.mbs = mbs;86this.semaphore = semaphore;87}8889public ObjectName preRegister(MBeanServer mbs, ObjectName on) {90this.objectName = on;91return on;92}9394public void postRegister(Boolean done) {}9596public void preDeregister() throws Exception {97Thread t = new Thread() {98public void run() {99try {100mbs.unregisterMBean(objectName);101fail("Nested unregister succeeded");102} catch (InstanceNotFoundException e) {103semaphore.release();104} catch (Throwable e) {105e.printStackTrace(System.out);106fail(e.toString());107}108}109};110t.start();111// Give the thread a chance to block so we are really112// testing parallelism. (On slow machines we might not113// really be testing it but we should be covered by our114// faster machines.)115Thread.sleep(500L);116}117118public void postDeregister() {}119120private final MBeanServer mbs;121private ObjectName objectName;122private final Semaphore semaphore;123}124125public static void main(String[] args) throws Exception {126MBeanServer mbs = MBeanServerFactory.newMBeanServer();127ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");128ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");129130// Test 1: preDeregister starts a thread which unregisters a131// different MBean: this must not deadlock132mbs.registerMBean(new Blibby(mbs, on2), on1);133mbs.registerMBean(new Blibby(mbs, null), on2);134mbs.unregisterMBean(on1);135136// Test 2: preDeregister starts a thread which tries to137// unregister the same MBean: this thread should block until138// the original thread succeeds in unregistering, then139// get an InstanceNotFoundException. We wait for it to140// complete here, using the semaphore.141Semaphore semaphore = new Semaphore(0);142mbs.registerMBean(new Blobby(mbs, semaphore), on1);143mbs.unregisterMBean(on1);144boolean ok = semaphore.tryAcquire(1, 5, TimeUnit.SECONDS);145if (!ok)146fail("Second unregister thread did not complete");147148if (failure == null)149System.out.println("OK: Test passed");150else151throw new Exception("TEST FAILED: " + failure);152}153154private static void fail(String why) {155System.out.println("FAILED: " + why);156failure = why;157}158159private static volatile String failure;160}161162163