jdk/test/javax/management/remote/mandatory/notif/NotificationBufferDeadlockTest.java
author dfuchs
Thu, 02 Feb 2017 16:50:46 +0000
changeset 43503 bc7f8619ab70
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8173607: JMX RMI connector should be in its own module Summary: The JMX RMI connector is moved to a new java.management.rmi module. Reviewed-by: mchung, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 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: 4156
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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 6239400
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
    27
 * @summary Tests NotificationBuffer doesn't hold locks when adding listeners,
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
    28
 *  if test times out then deadlock is suspected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28300
diff changeset
    30
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean NotificationBufferDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build NotificationBufferDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main NotificationBufferDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Vector;
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
    43
import java.util.concurrent.CountDownLatch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Regression test for a rare but not unheard-of deadlock condition in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the notification buffer support for connector servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * See bug 6239400 for the description of the bug and the example that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * showed it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Here we test that, when the connector server adds its listener to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * MBean, it is not holding a lock that would prevent another thread from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * emitting a notification (from that MBean or another one).  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * important, because we don't know how user MBeans might implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * NotificationBroadcaster.addNotificationListener, and in particular we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * can't be sure that the method is well-behaved and can never do a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * blocking operation, such as attempting to acquire a lock that is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * acquired when notifications are emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The test creates a special MBean whose addNotificationListener method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * does the standard addNotificationListener logic inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * from NotificationBroadcasterSupport, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * creates another thread that emits a notification from the same MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * The addNotificationListener method waits for this thread to complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * If the notification buffer logic is incorrect, then emitting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * notification will attempt to acquire the lock on the buffer, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * lock is being held by the thread that called addNotificationListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * so there will be deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * We use this DeadlockMBean several times.  First, we create one and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * add a remote listener to it.  The first time you add a remote listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * through a connector server, the connector server adds its own listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * to all NotificationBroadcaster MBeans.  If it holds a lock while doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * this, we will see deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Then we create a second DeadlockMBean.  When a new MBean is created that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * is a NotificationBroadcaster, the connector server adds its listener to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * that MBean too.  Again if it holds a lock while doing this, we will see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Finally, we do some magic with MBeanServerForwarders so that while
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * queryNames is running (to find MBeans to which listeners must be added)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * we will create new MBeans.  This tests that this tricky situation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * handled correctly.  It also tests the queryNames that is run when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * notification buffer is being destroyed (to remove the listeners).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * We cause all of our test MBeans to emit exactly one notification and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * check that we have received exactly one notification from each MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * If the logic for adding the notification buffer's listener is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * we could remove zero or two notifications from an MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
public class NotificationBufferDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.out.println("Check no deadlock if notif sent while initial " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                           "remote listeners being added");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        final String[] protos = {"rmi", "iiop", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (String p : protos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                test(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                System.out.println("TEST FAILED: GOT EXCEPTION:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                failure = e.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new Exception("TEST FAILED: " + failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static void test(String proto) throws Exception {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   115
        System.out.println("Testing protocol " + proto);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        ObjectName testName = newName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        DeadlockTest test = new DeadlockTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        mbs.registerMBean(test, testName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + ":///");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        JMXConnectorServer cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            cs =
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   124
                JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println("...protocol not supported, ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        MBeanServerForwarder createDuringQueryForwarder = (MBeanServerForwarder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Proxy.newProxyInstance(new Object() {}.getClass().getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                   new Class[] {MBeanServerForwarder.class},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                   new CreateDuringQueryInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        cs.setMBeanServerForwarder(createDuringQueryForwarder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        JMXConnector cc = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            String fail = test(mbsc, testName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (fail != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.out.println("FAILED: " + fail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            failure = fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            cc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static String test(MBeanServerConnection mbsc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                               ObjectName testName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        NotificationListener dummyListener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        thisFailure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        mbsc.addNotificationListener(testName, dummyListener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (thisFailure != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return thisFailure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        ObjectName newName = newName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        mbsc.createMBean(DeadlockTest.class.getName(), newName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (thisFailure != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return thisFailure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Set<ObjectName> names =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            mbsc.queryNames(new ObjectName("d:type=DeadlockTest,*"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        System.out.printf("...found %d test MBeans\n", names.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sources.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        countListener = new MyListener(names.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (ObjectName name : names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            mbsc.addNotificationListener(name, countListener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (thisFailure != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return thisFailure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (ObjectName name : names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            mbsc.invoke(name, "send", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   179
        countListener.waiting();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (!sources.containsAll(names))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return "missing names: " + sources;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return thisFailure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public static interface DeadlockTestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public void send();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static class DeadlockTest extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            implements DeadlockTestMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                            NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                            Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            super.addNotificationListener(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            Thread t = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    Notification n =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        new Notification("type", DeadlockTest.this, 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    DeadlockTest.this.sendNotification(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            t.start();
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   206
            System.out.println("DeadlockTest-addNotificationListener waiting for the sending thread to die...");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            try {
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   208
                t.join(); //if times out here then deadlock is suspected
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   209
                System.out.println("DeadlockTest-addNotificationListener OK.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                thisFailure = "Join exception: " + e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public void send() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            sendNotification(new Notification(TESTING_TYPE, DeadlockTest.this, 1L));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static class CreateDuringQueryInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public Object invoke(Object proxy, Method m, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (m.getName().equals("setMBeanServer")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                mbs = (MBeanServer) args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            createMBeanIfQuery(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Object ret = m.invoke(mbs, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            createMBeanIfQuery(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        private void createMBeanIfQuery(Method m) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (m.getName().equals("queryNames")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                Thread t = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            mbs.createMBean(DeadlockTest.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                            newName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            thisFailure = e.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                t.start();
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   248
                System.out.println("CreateDuringQueryInvocationHandler-createMBeanIfQuery waiting for the creating thread to die...");
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   249
                t.join();  // if times out here then deadlock is suspected
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   250
                System.out.println("CreateDuringQueryInvocationHandler-createMBeanIfQuery OK");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        private MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private static synchronized ObjectName newName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return new ObjectName("d:type=DeadlockTest,instance=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                  ++nextNameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (MalformedObjectNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new IllegalArgumentException("bad ObjectName", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private static class MyListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        public MyListener(int waitNB) {
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   268
            count = new CountDownLatch(waitNB);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public void handleNotification(Notification n, Object h) {
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   272
            System.out.println("MyListener got: " + n.getSource() + " " + n.getType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   274
            if (TESTING_TYPE.equals(n.getType())) {
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   275
                sources.add((ObjectName) n.getSource());
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   276
                count.countDown();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   280
        public void waiting() throws InterruptedException {
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   281
            System.out.println("MyListener-waiting ...");
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   282
            count.await(); // if times out here then deadlock is suspected
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   283
            System.out.println("MyListener-waiting done!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
28300
bd5ad15efa76 8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
sjiang
parents: 5506
diff changeset
   286
        private final CountDownLatch count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    static String thisFailure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    static String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static int nextNameIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private static MyListener countListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private static final List<ObjectName> sources = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private static final String TESTING_TYPE = "testing_type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}