jdk/test/javax/management/MBeanServer/PostRegisterDeadlockTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 2015, 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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    29
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean PostRegisterDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build PostRegisterDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main PostRegisterDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.Thread.State;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class PostRegisterDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static interface BlibbyMBean {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static class Blibby implements BlibbyMBean, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        public Blibby(MBeanServer mbs, ObjectName otherName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            this.mbs = mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            this.otherName = otherName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        public ObjectName preRegister(MBeanServer mbs, ObjectName on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            return on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public void preDeregister() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public void postRegister(Boolean done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            // If no other MBean was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            // do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (otherName == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // Check that we can unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            // other MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                Thread t = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                mbs.unregisterMBean(otherName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                            } catch (InstanceNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                 // Race condition!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                 System.out.println(otherName+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                         " was unregistered by main thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                            fail(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                t.join(5000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                if (t.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    if (t.getState().equals(State.BLOCKED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        fail("Deadlock detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        fail("Test not conclusive: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                             "Thread is alive but not blocked.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                fail(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public void postDeregister() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        private final MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        private final ObjectName otherName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String previous = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ObjectName on1 = new ObjectName("a:type=Blibby,name=\"1\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ObjectName on2 = new ObjectName("a:type=Blibby,name=\"2\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Test 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // 1 MBean is registered with on1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Another MBean is registered with on1, postRegister(FALSE) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // called, and the second MBean attempts to unregister first MBean in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // postRegister:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // postRegister starts a thread which unregisters the first MBean:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // this must not deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println("\n****  TEST #1 ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println("Registering Blibby #1 with name: " + on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        mbs.registerMBean(new Blibby(mbs, null), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("Registering Blibby #2 with same name: " + on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            mbs.registerMBean(new Blibby(mbs, on1), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (InstanceAlreadyExistsException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("Received expected exception: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (mbs.isRegistered(on1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                mbs.unregisterMBean(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    fail(on1+" should have been unregistered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } catch (InstanceNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                // Race condition!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                System.out.println(on1+" was unregistered by mbean thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println(on1+" was correctly unregistered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (failure == previous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.out.println("\n****  TEST #1 PASSED ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        previous = failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Test 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // 1 MBean is registered with on1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Another MBean is registered with on2, postRegister(TRUE) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // called, and the second MBean attempts to unregister first MBean in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // postRegister:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // postRegister starts a thread which unregisters the first MBean:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // this must not deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        System.out.println("\n****  TEST #2 ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.out.println("Registering Blibby #1 with name: " + on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        mbs.registerMBean(new Blibby(mbs, null), on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        System.out.println("Registering Blibby #2 with other name: " + on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        mbs.registerMBean(new Blibby(mbs, on1), on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (mbs.isRegistered(on1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                mbs.unregisterMBean(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    fail(on1+" should have been unregistered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } catch (InstanceNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // Race condition!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                System.out.println(on1+" was unregistered by mbean thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println(on1+" was correctly unregistered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        System.out.println("unregistering "+on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        mbs.unregisterMBean(on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (failure == previous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            System.out.println("\n****  TEST #2 PASSED ****\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        previous = failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            System.out.println("OK: Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new Exception("TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static void fail(String why) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        System.out.println("FAILED: " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        failure = (failure == null)?why:(failure+",\n"+why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static volatile String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}