jdk/test/javax/management/remote/mandatory/connection/RMIConnectorInternalMapTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 30376 2ccf2cf7ea48
child 42338 a60f280f803c
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:
19791
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 19791
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
19791
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     4
 *
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     8
 *
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    13
 * accompanied this code).
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    14
 *
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    18
 *
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    21
 * questions.
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    22
 */
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    23
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    24
import java.lang.management.ManagementFactory;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    25
import java.lang.ref.WeakReference;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    26
import java.lang.reflect.Field;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    27
import java.util.Collections;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    28
import java.util.Map;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    29
import javax.management.MBeanServer;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    30
import javax.management.MBeanServerConnection;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    31
import javax.management.remote.JMXConnector;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    32
import javax.management.remote.JMXConnectorFactory;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    33
import javax.management.remote.JMXConnectorServer;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    34
import javax.management.remote.JMXConnectorServerFactory;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    35
import javax.management.remote.JMXPrincipal;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    36
import javax.management.remote.JMXServiceURL;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    37
import javax.management.remote.rmi.RMIConnector;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    38
import javax.security.auth.Subject;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    39
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    40
/*
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    41
 * @test
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    42
 * @bug 6566891
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    43
 * @summary Check no memory leak on RMIConnector's rmbscMap
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    44
 * @author Shanliang JIANG
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 19791
diff changeset
    45
 * @modules java.management
19791
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    46
 * @run clean RMIConnectorInternalMapTest
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    47
 * @run build RMIConnectorInternalMapTest
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    48
 * @run main RMIConnectorInternalMapTest
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    49
 */
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    50
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    51
public class RMIConnectorInternalMapTest {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    53
        System.out.println("---RMIConnectorInternalMapTest starting...");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    54
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    55
        JMXConnectorServer connectorServer = null;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    56
        JMXConnector connectorClient = null;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    57
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    58
        try {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    59
            MBeanServer mserver = ManagementFactory.getPlatformMBeanServer();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    60
            JMXServiceURL serverURL = new JMXServiceURL("rmi", "localhost", 0);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    61
            connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverURL, null, mserver);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    62
            connectorServer.start();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    63
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    64
            JMXServiceURL serverAddr = connectorServer.getAddress();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    65
            connectorClient = JMXConnectorFactory.connect(serverAddr, null);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    66
            connectorClient.connect();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    67
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    68
            Field rmbscMapField = RMIConnector.class.getDeclaredField("rmbscMap");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    69
            rmbscMapField.setAccessible(true);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    70
            Map<Subject, WeakReference<MBeanServerConnection>> map =
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    71
                    (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    72
            if (map != null && !map.isEmpty()) { // failed
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    73
                throw new RuntimeException("RMIConnector's rmbscMap must be empty at the initial time.");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    74
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    75
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    76
            Subject delegationSubject =
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    77
                    new Subject(true,
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    78
                    Collections.singleton(new JMXPrincipal("delegate")),
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    79
                    Collections.EMPTY_SET,
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    80
                    Collections.EMPTY_SET);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    81
            MBeanServerConnection mbsc1 =
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    82
                    connectorClient.getMBeanServerConnection(delegationSubject);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    83
            MBeanServerConnection mbsc2 =
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    84
                    connectorClient.getMBeanServerConnection(delegationSubject);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    85
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    86
            if (mbsc1 == null) {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    87
                throw new RuntimeException("Got null connection.");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    88
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    89
            if (mbsc1 != mbsc2) {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    90
                throw new RuntimeException("Not got same connection with a same subject.");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    91
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    92
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    93
            map = (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    94
            if (map == null || map.isEmpty()) { // failed
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    95
                throw new RuntimeException("RMIConnector's rmbscMap has wrong size "
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    96
                        + "after creating a delegated connection.");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    97
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    98
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
    99
            delegationSubject = null;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   100
            mbsc1 = null;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   101
            mbsc2 = null;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   102
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   103
            int i = 0;
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   104
            while (!map.isEmpty() && i++ < 60) {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   105
                System.gc();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   106
                Thread.sleep(100);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   107
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   108
            System.out.println("---GC times: " + i);
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   109
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   110
            if (!map.isEmpty()) {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   111
                throw new RuntimeException("Failed to clean RMIConnector's rmbscMap");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   112
            } else {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   113
                System.out.println("---RMIConnectorInternalMapTest: PASSED!");
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   114
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   115
        } finally {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   116
            try {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   117
                connectorClient.close();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   118
                connectorServer.stop();
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   119
            } catch (Exception e) {
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   120
            }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   121
        }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   122
    }
d0bcc2086175 6566891: RMIConnector: map value referencing map key in WeakHashMap prevents map entry to be removed
sjiang
parents:
diff changeset
   123
}