jdk/test/javax/management/MBeanServer/PostRegisterDeadlockTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6417044
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test deadlock in MBeanRegistration.postRegister method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus, Daniel Fuchs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean PostRegisterDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build PostRegisterDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main PostRegisterDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.Thread.State;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class PostRegisterDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static interface BlibbyMBean {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static class Blibby implements BlibbyMBean, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        public Blibby(MBeanServer mbs, ObjectName otherName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            this.mbs = mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            this.otherName = otherName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public void preDeregister() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public void postRegister(Boolean done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            // If no other MBean was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            // do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (otherName == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            // Check that we can unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // other MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                Thread t = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                mbs.unregisterMBean(otherName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                            } catch (InstanceNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                 // Race condition!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                 System.out.println(otherName+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                         " was unregistered by main thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            fail(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                t.join(5000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (t.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    if (t.getState().equals(State.BLOCKED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        fail("Deadlock detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        fail("Test not conclusive: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                             "Thread is alive but not blocked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                fail(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public void postDeregister() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        private final MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        private final ObjectName otherName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String previous = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Test 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // 1 MBean is registered with on1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Another MBean is registered with on1, postRegister(FALSE) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // called, and the second MBean attempts to unregister first MBean in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // postRegister:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // postRegister starts a thread which unregisters the first MBean:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // this must not deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.out.println("\n****  TEST #1 ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println("Registering Blibby #1 with name: " + on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        mbs.registerMBean(new Blibby(mbs, null), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println("Registering Blibby #2 with same name: " + on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            mbs.registerMBean(new Blibby(mbs, on1), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (InstanceAlreadyExistsException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("Received expected exception: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (mbs.isRegistered(on1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                mbs.unregisterMBean(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    fail(on1+" should have been unregistered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } catch (InstanceNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // Race condition!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println(on1+" was unregistered by mbean thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println(on1+" was correctly unregistered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (failure == previous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println("\n****  TEST #1 PASSED ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        previous = failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // Test 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // 1 MBean is registered with on1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Another MBean is registered with on2, postRegister(TRUE) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // called, and the second MBean attempts to unregister first MBean in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // postRegister:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // postRegister starts a thread which unregisters the first MBean:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // this must not deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        System.out.println("\n****  TEST #2 ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        System.out.println("Registering Blibby #1 with name: " + on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        mbs.registerMBean(new Blibby(mbs, null), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        System.out.println("Registering Blibby #2 with other name: " + on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        mbs.registerMBean(new Blibby(mbs, on1), on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (mbs.isRegistered(on1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                mbs.unregisterMBean(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    fail(on1+" should have been unregistered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (InstanceNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                // Race condition!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                System.out.println(on1+" was unregistered by mbean thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println(on1+" was correctly unregistered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.println("unregistering "+on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        mbs.unregisterMBean(on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (failure == previous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.out.println("\n****  TEST #2 PASSED ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        previous = failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            System.out.println("OK: Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new Exception("TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        System.out.println("FAILED: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        failure = (failure == null)?why:(failure+",\n"+why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static volatile String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}