jdk/test/javax/management/monitor/StartStopTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 tasks are cancelled properly when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          monitors are started and stopped in a loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean StartStopTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build StartStopTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main/othervm/timeout=300 StartStopTest 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main/othervm/timeout=300 StartStopTest 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main/othervm/timeout=300 StartStopTest 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.monitor.GaugeMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.monitor.Monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.monitor.MonitorNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.monitor.StringMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class StartStopTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static int maxPoolSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final AtomicInteger counter = new AtomicInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public class ObservedObject implements ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public boolean called = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public Integer getInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            task("Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public Double getDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            task("Double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            task("String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        private void task(String prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            called = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            final int c = counter.incrementAndGet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            echo("\tTASK [" + c + "] in get" + prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public Integer getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public Double getDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public String getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Run test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public int runTest(int monitorType) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int nTasks = maxPoolSize + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ObjectName[] mbeanNames = new ObjectName[nTasks];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        ObservedObject[] monitored = new ObservedObject[nTasks];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ObjectName[] monitorNames = new ObjectName[nTasks];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Monitor[] monitor = new Monitor[nTasks];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        String[] attributes = { "Integer", "Double", "String" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            echo(">>> CREATE MBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            MBeanServer server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            String domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            for (int i = 0; i < nTasks; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                mbeanNames[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    new ObjectName(":type=ObservedObject,instance=" + (i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                monitored[i] = new ObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                server.registerMBean(monitored[i], mbeanNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                switch (monitorType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    monitorNames[i] = new ObjectName(":type=CounterMonitor," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                     "instance=" + (i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    monitor[i] = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    monitorNames[i] = new ObjectName(":type=GaugeMonitor," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                                     "instance=" + (i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    monitor[i] = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    monitorNames[i] = new ObjectName(":type=StringMonitor," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                     "instance=" + (i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    monitor[i] = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    echo("Unsupported monitor type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                server.registerMBean(monitor[i], monitorNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                monitor[i].addObservedObject(mbeanNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                monitor[i].setObservedAttribute(attributes[monitorType-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                monitor[i].setGranularityPeriod(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            for (int j = 0; j < 2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                echo(">>> Start MONITORS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                for (int i = 0; i < nTasks; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    monitor[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                echo(">>> MONITORS started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                echo(">>> Check FLAGS true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                for (int i = 0; i < nTasks; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (!monitored[i].called) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        echo("KO: At least one attribute was not called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                echo(">>> FLAGS checked true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                echo(">>> Stop MONITORS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                for (int i = 0; i < nTasks; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    monitor[i].stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                echo(">>> MONITORS stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                echo(">>> Set FLAGS to false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                for (int i = 0; i < nTasks; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    monitored[i].called = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                echo(">>> FLAGS set to false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                echo(">>> Check FLAGS remain false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                for (int i = 0; i < nTasks; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    if (monitored[i].called) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        echo("KO: At least one attribute " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                             "continued to get called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                echo(">>> FLAGS checked false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            for (int i = 0; i < nTasks; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (monitor[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    monitor[i].stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (size == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            maxPoolSize = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            echo(">>> MAXIMUM POOL SIZE = 10 [default value]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        StartStopTest test = new StartStopTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int error = test.runTest(Integer.parseInt(args[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                "Test FAILED: Unexpected Maximum Pool Size Overflow!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}