jdk/test/javax/management/remote/mandatory/connectorServer/ConnectorStopDeadlockTest.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 6475157
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests deadlock in simultaneous connection and connector-server close
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/* This test is somewhat dependent on implementation details.  If it suddenly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * starts failing after a rewrite of the RMIConnectorServer code, you should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * consider whether it is still relevant.
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.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.management.ThreadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.management.ThreadMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.concurrent.Exchanger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.remote.rmi.RMIConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.remote.rmi.RMIConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.rmi.RMIJRMPServerImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class ConnectorStopDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static String failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static RMIConnectorServer connectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.out.println("Creating connectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        connectorServer = new RMIConnectorServer(url, null, impl, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Starting connectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        connectorServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        System.out.println("Making client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        RMIConnection cc = impl.newClient(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        System.out.println("Closing client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        cc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (connectorServer.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println("Stopping connectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            connectorServer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (failure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.out.println("TEST PASSED, no deadlock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.out.println("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void fail(Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.println("FAILED WITH EXCEPTION: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        failure = e.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static void fail(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("FAILED: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        failure = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
//    static MonitorInfo[] threadLocks(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
//        ThreadMXBean tm = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
//        ThreadInfo[] tis = tm.getThreadInfo(new long[] {t.getId()}, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
//        if (tis[0] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
//            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
//        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
//            return tis[0].getLockedMonitors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
//    static void showLocks(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
//        System.out.println("Locks for " + t.getName() + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
//        MonitorInfo[] mis = threadLocks(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
//        if (mis == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
//            System.out.println("  (no longer exists)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
//        else if (mis.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
//            System.out.println("  (none)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
//        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
//            for (MonitorInfo mi : mis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
//                System.out.println("  " + mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
//        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Wait until thread t blocks waiting for a lock held by the calling thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // or until it exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static void waitForBlock(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Thread currentThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println("waiting for thread " + t.getName() + " to block " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                "on a lock held by thread " + currentThread.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ThreadMXBean tm = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            ThreadInfo ti = tm.getThreadInfo(t.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (ti == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.out.println("  thread has exited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (ti.getLockOwnerId() == currentThread.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("  thread now blocked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static class RMIJRMPServerImplSub extends RMIJRMPServerImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        RMIJRMPServerImplSub() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            super(0, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public RMIConnection makeClient() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return super.makeClient("connection id", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        protected void clientClosed(RMIConnection conn) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println("clientClosed, will call connectorServer.stop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            final Exchanger<Void> x = new Exchanger<Void>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Thread t = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        connectorServer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        fail(e);
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
            t.setName("connectorServer.stop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            waitForBlock(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            /* If this thread is synchronized on RMIServerImpl, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             * the thread that does connectorServer.stop will acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
             * the clientList lock and then block waiting for the RMIServerImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
             * lock.  Our call to super.clientClosed will then deadlock because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
             * it needs to acquire the clientList lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            System.out.println("calling super.clientClosed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            super.clientClosed(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}