jdk/test/javax/management/monitor/ThreadPoolAccTest.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 21650 390b5df3fcaf
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2013 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 6222826
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that each thread in the thread pool runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          in the context of the monitor.start() caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean ThreadPoolAccTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build ThreadPoolAccTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ThreadPoolAccTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
    37
import java.util.Date;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.monitor.GaugeMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.monitor.Monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.monitor.StringMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ThreadPoolAccTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // MBean class
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
    52
    public static class ObservedObject implements ObservedObjectMBean {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
    53
        public volatile String principal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public Integer getInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            setPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Double getDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            setPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            setPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        private void setPrincipal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            Subject subject = Subject.getSubject(AccessController.getContext());
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
    68
            Set<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class);
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
    69
            principal = principals.iterator().next().getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public Integer getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public Double getDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public String getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
    80
    public static void main (String args[]) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ObjectName[] mbeanNames = new ObjectName[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ObservedObject[] monitored = new ObservedObject[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ObjectName[] monitorNames = new ObjectName[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Monitor[] monitor = new Monitor[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String[] principals = { "role1", "role2" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String[] attributes = { "Integer", "Double", "String" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            echo(">>> CREATE MBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            for (int i = 0; i < 6; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                mbeanNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    new ObjectName(":type=ObservedObject,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                monitored[i] = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                server.registerMBean(monitored[i], mbeanNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        monitorNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                            new ObjectName(":type=CounterMonitor,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        monitor[i] = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        monitorNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        monitor[i] = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        monitorNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                            new ObjectName(":type=StringMonitor,instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        monitor[i] = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                server.registerMBean(monitor[i], monitorNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                monitor[i].addObservedObject(mbeanNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                monitor[i].setObservedAttribute(attributes[i % 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                monitor[i].setGranularityPeriod(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                final Monitor m = monitor[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                Subject subject = new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                echo(">>> RUN Principal = " + principals[i / 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   130
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   131
                    public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        m.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                Subject.doAs(subject, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   139
            while(!testPrincipals(monitored, monitorNames, monitor, principals));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            for (int i = 0; i < 6; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (monitor[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    monitor[i].stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
21650
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   148
    private static boolean testPrincipals(ObservedObject[] monitored, ObjectName[] monitorNames,
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   149
            Monitor[] monitor, String[] principals) throws Exception {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   150
        for (int i = 0; i < 6; i++) {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   151
            String principal =  monitored[i].principal;
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   152
            String expected = principals[i / 3];
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   153
            if (principal == null) {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   154
                echo("Task not submitted " + new Date() + ". RETRY");
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   155
                return false;
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   156
            }
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   157
            echo(">>> Monitor = " + monitorNames[i]);
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   158
            echo(">>> ObservedObject = " + monitor[i].getObservedObject());
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   159
            echo(">>> ObservedAttribute = " + monitor[i].getObservedAttribute());
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   160
            echo(">>> Principal = " + principal);
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   161
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   162
            if (expected.equals(principal)) {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   163
                echo("\tOK: Got Expected principal");
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   164
            } else {
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   165
                throw new Exception("Unexpected principal. Got: " + principal + " Expected: " + expected);
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   166
            }
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   167
        }
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   168
        return true;
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   169
    }
390b5df3fcaf 8027209: javax/management/monitor/ThreadPoolAccTest.java fails intermittently
egahlin
parents: 5506
diff changeset
   170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}