jdk/test/javax/management/MBeanServer/PreDeregisterDeadlockTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6318664
       
    27  * @summary Test deadlock in MBeanRegistration.preDeregister method
       
    28  * @author Eamonn McManus
       
    29  * @run clean PreDeregisterDeadlockTest
       
    30  * @run build PreDeregisterDeadlockTest
       
    31  * @run main PreDeregisterDeadlockTest
       
    32  */
       
    33 
       
    34 import java.util.concurrent.*;
       
    35 import javax.management.*;
       
    36 
       
    37 public class PreDeregisterDeadlockTest {
       
    38     public static interface BlibbyMBean {}
       
    39 
       
    40     public static class Blibby implements BlibbyMBean, MBeanRegistration {
       
    41         public Blibby(MBeanServer mbs, ObjectName otherName) {
       
    42             this.mbs = mbs;
       
    43             this.otherName = otherName;
       
    44         }
       
    45 
       
    46         public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
       
    47             return on;
       
    48         }
       
    49 
       
    50         public void postRegister(Boolean done) {}
       
    51 
       
    52         public void preDeregister() {
       
    53             if (otherName == null)
       
    54                 return;
       
    55             try {
       
    56                 Thread t = new Thread() {
       
    57                     public void run() {
       
    58                         try {
       
    59                             mbs.unregisterMBean(otherName);
       
    60                         } catch (Throwable e) {
       
    61                             e.printStackTrace(System.out);
       
    62                             fail(e.toString());
       
    63                         }
       
    64                     }
       
    65                 };
       
    66                 t.start();
       
    67                 t.join(5000L);
       
    68                 if (t.isAlive())
       
    69                     fail("Deadlock detected");
       
    70             } catch (Throwable e) {
       
    71                 e.printStackTrace(System.out);
       
    72                 fail(e.toString());
       
    73             }
       
    74         }
       
    75 
       
    76         public void postDeregister() {}
       
    77 
       
    78         private final MBeanServer mbs;
       
    79         private final ObjectName otherName;
       
    80     }
       
    81 
       
    82     public static interface BlobbyMBean {}
       
    83 
       
    84     public static class Blobby implements BlobbyMBean, MBeanRegistration {
       
    85         public Blobby(MBeanServer mbs, Semaphore semaphore) {
       
    86             this.mbs = mbs;
       
    87             this.semaphore = semaphore;
       
    88         }
       
    89 
       
    90         public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
       
    91             this.objectName = on;
       
    92             return on;
       
    93         }
       
    94 
       
    95         public void postRegister(Boolean done) {}
       
    96 
       
    97         public void preDeregister() throws Exception {
       
    98             Thread t = new Thread() {
       
    99                 public void run() {
       
   100                     try {
       
   101                         mbs.unregisterMBean(objectName);
       
   102                         fail("Nested unregister succeeded");
       
   103                     } catch (InstanceNotFoundException e) {
       
   104                         semaphore.release();
       
   105                     } catch (Throwable e) {
       
   106                         e.printStackTrace(System.out);
       
   107                         fail(e.toString());
       
   108                     }
       
   109                 }
       
   110             };
       
   111             t.start();
       
   112             // Give the thread a chance to block so we are really
       
   113             // testing parallelism.  (On slow machines we might not
       
   114             // really be testing it but we should be covered by our
       
   115             // faster machines.)
       
   116             Thread.sleep(500L);
       
   117         }
       
   118 
       
   119         public void postDeregister() {}
       
   120 
       
   121         private final MBeanServer mbs;
       
   122         private ObjectName objectName;
       
   123         private final Semaphore semaphore;
       
   124     }
       
   125 
       
   126     public static void main(String[] args) throws Exception {
       
   127         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
       
   128         ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");
       
   129         ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");
       
   130 
       
   131         // Test 1: preDeregister starts a thread which unregisters a
       
   132         // different MBean: this must not deadlock
       
   133         mbs.registerMBean(new Blibby(mbs, on2), on1);
       
   134         mbs.registerMBean(new Blibby(mbs, null), on2);
       
   135         mbs.unregisterMBean(on1);
       
   136 
       
   137         // Test 2: preDeregister starts a thread which tries to
       
   138         // unregister the same MBean: this thread should block until
       
   139         // the original thread succeeds in unregistering, then
       
   140         // get an InstanceNotFoundException.  We wait for it to
       
   141         // complete here, using the semaphore.
       
   142         Semaphore semaphore = new Semaphore(0);
       
   143         mbs.registerMBean(new Blobby(mbs, semaphore), on1);
       
   144         mbs.unregisterMBean(on1);
       
   145         boolean ok = semaphore.tryAcquire(1, 5, TimeUnit.SECONDS);
       
   146         if (!ok)
       
   147             fail("Second unregister thread did not complete");
       
   148 
       
   149         if (failure == null)
       
   150             System.out.println("OK: Test passed");
       
   151         else
       
   152             throw new Exception("TEST FAILED: " + failure);
       
   153     }
       
   154 
       
   155     private static void fail(String why) {
       
   156         System.out.println("FAILED: " + why);
       
   157         failure = why;
       
   158     }
       
   159 
       
   160     private static volatile String failure;
       
   161 }