jdk/test/javax/management/remote/mandatory/connection/DaemonRMIExporterTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 30376 2ccf2cf7ea48
child 43503 bc7f8619ab70
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     4
 *
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     8
 *
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    13
 * accompanied this code).
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    14
 *
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1706
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1706
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1706
diff changeset
    21
 * questions.
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    22
 */
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    23
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    24
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    25
/*
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    26
 * @test
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    27
 * @bug 6760712
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    28
 * @summary test the connector server option that causes it not to prevent the
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    29
 * VM from exiting
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    30
 * @author Shanliang JIANG, Eamonn McManus
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    31
 * @modules java.management
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    32
 * @run main/othervm DaemonRMIExporterTest
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    33
 */
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    34
import java.util.Arrays;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    35
import java.util.Collections;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    36
import java.util.HashSet;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    37
import java.util.Map;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    38
import java.util.Set;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    39
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    41
import javax.management.remote.JMXConnector;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    42
import javax.management.remote.JMXConnectorFactory;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    43
import javax.management.remote.JMXServiceURL;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    44
import javax.management.remote.JMXConnectorServer;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    45
import javax.management.remote.JMXConnectorServerFactory;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    46
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    47
// Test the connector server option that causes it not to prevent the VM
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    48
// from exiting.  It's tricky to test exactly that, though possible.  If
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    49
// we're being run from within jtreg, then jtreg has threads of its own
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    50
// that will prevent the VM from exiting.  What's more it will kill all
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    51
// threads that the test created as soon as the main method returns,
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    52
// including the ones that would prevent the VM from exiting without the
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    53
// special option.
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    54
// Here we check that the test code does not create
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    55
// any permanent non-daemon threads, by recording the initial set of
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    56
// non-daemon threads (including at least one from jtreg), doing our stuff,
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    57
// then waiting for there to be no non-daemon threads that were not in
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    58
// the initial set.
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    59
public class DaemonRMIExporterTest {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    60
    public static void main(String[] args) throws Exception {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    61
        Set<Thread> initialNonDaemonThreads = getNonDaemonThreads();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    62
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    63
        JMXServiceURL addr = new JMXServiceURL("rmi", null, 0);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    64
        System.out.println("DaemonRMIExporterTest: Creating a RMIConnectorServer on " + addr);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    65
        Map<String, ?> env =
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    66
            Collections.singletonMap("jmx.remote.x.daemon", "true");
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    67
        JMXConnectorServer server =
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    68
                JMXConnectorServerFactory.newJMXConnectorServer(addr,
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    69
                env,
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    70
                MBeanServerFactory.createMBeanServer());
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    71
        server.start();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    72
        System.out.println("DaemonRMIExporterTest: Started the server on " + server.getAddress());
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    73
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    74
        System.out.println("DaemonRMIExporterTest: Connecting a client to the server ...");
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    75
        final JMXConnector conn = JMXConnectorFactory.connect(server.getAddress());
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    76
        conn.getMBeanServerConnection().getDefaultDomain();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    77
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    78
        System.out.println("DaemonRMIExporterTest: Closing the client ...");
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    79
        conn.close();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    80
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    81
        System.out.println("DaemonRMIExporterTest No more user code to execute, the VM should " +
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    82
                "exit normally, otherwise will be blocked forever if the bug is not fixed.");
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    83
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    84
        long deadline = System.currentTimeMillis() + 10000;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    85
        ok: {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    86
            while (System.currentTimeMillis() < deadline) {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    87
                Set<Thread> nonDaemonThreads = getNonDaemonThreads();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    88
                nonDaemonThreads.removeAll(initialNonDaemonThreads);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    89
                if (nonDaemonThreads.isEmpty())
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    90
                    break ok;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    91
                System.out.println("Non-daemon threads: " + nonDaemonThreads);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    92
                try {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    93
                    Thread.sleep(500);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    94
                } catch (InterruptedException e) {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    95
                    throw new AssertionError(e);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    96
                }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    97
            }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    98
            throw new Exception("TEST FAILED: non-daemon threads remain");
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
    99
        }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   100
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   101
        System.out.println("TEST PASSED");
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   102
    }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   103
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   104
    private static Set<Thread> getNonDaemonThreads() {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   105
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   106
        while (tg.getParent() != null)
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   107
            tg = tg.getParent();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   108
        Thread[] threads = null;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   109
        for (int size = 10; size < 10000; size *= 2) {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   110
            threads = new Thread[size];
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   111
            int n = tg.enumerate(threads, true);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   112
            if (n < size) {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   113
                threads = Arrays.copyOf(threads, n);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   114
                break;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   115
            }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   116
        }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   117
        Set<Thread> ndThreads = new HashSet<Thread>();
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   118
        for (Thread t : threads) {
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   119
            if (!t.isDaemon())
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   120
                ndThreads.add(t);
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   121
        }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   122
        return ndThreads;
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   123
    }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents:
diff changeset
   124
}