0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6222826
0N/A * @summary Test that tasks are cancelled properly when
0N/A * monitors are started and stopped in a loop.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean StartStopTest
0N/A * @run build StartStopTest
0N/A * @run main/othervm/timeout=300 StartStopTest 1
0N/A * @run main/othervm/timeout=300 StartStopTest 2
0N/A * @run main/othervm/timeout=300 StartStopTest 3
0N/A * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 1
0N/A * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 2
0N/A * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 3
0N/A * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 1
0N/A * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 2
0N/A * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 3
0N/A */
0N/A
0N/Aimport java.util.concurrent.atomic.AtomicInteger;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.monitor.CounterMonitor;
0N/Aimport javax.management.monitor.GaugeMonitor;
0N/Aimport javax.management.monitor.Monitor;
0N/Aimport javax.management.monitor.MonitorNotification;
0N/Aimport javax.management.monitor.StringMonitor;
0N/A
0N/Apublic class StartStopTest {
0N/A
0N/A static int maxPoolSize;
0N/A static final AtomicInteger counter = new AtomicInteger();
0N/A
0N/A // MBean class
0N/A public class ObservedObject implements ObservedObjectMBean {
0N/A public boolean called = false;
0N/A public Integer getInteger() {
0N/A task("Integer");
0N/A return 0;
0N/A }
0N/A public Double getDouble() {
0N/A task("Double");
0N/A return 0.0;
0N/A }
0N/A public String getString() {
0N/A task("String");
0N/A return "";
0N/A }
0N/A private void task(String prop) {
0N/A called = true;
0N/A final int c = counter.incrementAndGet();
0N/A echo("\tTASK [" + c + "] in get" + prop);
0N/A }
0N/A }
0N/A
0N/A // MBean interface
0N/A public interface ObservedObjectMBean {
0N/A public Integer getInteger();
0N/A public Double getDouble();
0N/A public String getString();
0N/A }
0N/A
0N/A /**
0N/A * Run test
0N/A */
0N/A public int runTest(int monitorType) throws Exception {
0N/A
0N/A int nTasks = maxPoolSize + 2;
0N/A ObjectName[] mbeanNames = new ObjectName[nTasks];
0N/A ObservedObject[] monitored = new ObservedObject[nTasks];
0N/A ObjectName[] monitorNames = new ObjectName[nTasks];
0N/A Monitor[] monitor = new Monitor[nTasks];
0N/A String[] attributes = { "Integer", "Double", "String" };
0N/A
0N/A try {
0N/A echo(">>> CREATE MBeanServer");
0N/A MBeanServer server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A String domain = server.getDefaultDomain();
0N/A
0N/A for (int i = 0; i < nTasks; i++) {
0N/A mbeanNames[i] =
0N/A new ObjectName(":type=ObservedObject,instance=" + (i + 1));
0N/A monitored[i] = new ObservedObject();
0N/A echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
0N/A server.registerMBean(monitored[i], mbeanNames[i]);
0N/A switch (monitorType) {
0N/A case 1:
0N/A monitorNames[i] = new ObjectName(":type=CounterMonitor," +
0N/A "instance=" + (i + 1));
0N/A monitor[i] = new CounterMonitor();
0N/A break;
0N/A case 2:
0N/A monitorNames[i] = new ObjectName(":type=GaugeMonitor," +
0N/A "instance=" + (i + 1));
0N/A monitor[i] = new GaugeMonitor();
0N/A break;
0N/A case 3:
0N/A monitorNames[i] = new ObjectName(":type=StringMonitor," +
0N/A "instance=" + (i + 1));
0N/A monitor[i] = new StringMonitor();
0N/A break;
0N/A default:
0N/A echo("Unsupported monitor type");
0N/A return 1;
0N/A }
0N/A echo(">>> CREATE Monitor = " + monitorNames[i].toString());
0N/A server.registerMBean(monitor[i], monitorNames[i]);
0N/A monitor[i].addObservedObject(mbeanNames[i]);
0N/A monitor[i].setObservedAttribute(attributes[monitorType-1]);
0N/A monitor[i].setGranularityPeriod(50);
0N/A }
0N/A
0N/A for (int j = 0; j < 2; j++) {
0N/A echo(">>> Start MONITORS");
0N/A for (int i = 0; i < nTasks; i++)
0N/A monitor[i].start();
0N/A echo(">>> MONITORS started");
0N/A Thread.sleep(500);
0N/A echo(">>> Check FLAGS true");
0N/A for (int i = 0; i < nTasks; i++)
0N/A if (!monitored[i].called) {
0N/A echo("KO: At least one attribute was not called");
0N/A return 1;
0N/A }
0N/A echo(">>> FLAGS checked true");
0N/A echo(">>> Stop MONITORS");
0N/A for (int i = 0; i < nTasks; i++)
0N/A monitor[i].stop();
0N/A echo(">>> MONITORS stopped");
0N/A Thread.sleep(500);
0N/A echo(">>> Set FLAGS to false");
0N/A for (int i = 0; i < nTasks; i++)
0N/A monitored[i].called = false;
0N/A echo(">>> FLAGS set to false");
0N/A echo(">>> Check FLAGS remain false");
0N/A for (int i = 0; i < nTasks; i++)
0N/A if (monitored[i].called) {
0N/A echo("KO: At least one attribute " +
0N/A "continued to get called");
0N/A return 1;
0N/A }
0N/A echo(">>> FLAGS checked false");
0N/A }
0N/A } finally {
0N/A for (int i = 0; i < nTasks; i++)
0N/A if (monitor[i] != null)
0N/A monitor[i].stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /*
0N/A * Print message
0N/A */
0N/A private static void echo(String message) {
0N/A System.out.println(message);
0N/A }
0N/A
0N/A /*
0N/A * Standalone entry point.
0N/A *
0N/A * Run the test and report to stdout.
0N/A */
0N/A public static void main (String args[]) throws Exception {
0N/A Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");
0N/A if (size == null) {
0N/A maxPoolSize = 10;
0N/A echo(">>> MAXIMUM POOL SIZE = 10 [default value]");
0N/A } else {
0N/A maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();
0N/A echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);
0N/A }
0N/A StartStopTest test = new StartStopTest();
0N/A int error = test.runTest(Integer.parseInt(args[0]));
0N/A if (error > 0) {
0N/A echo(">>> Unhappy Bye, Bye!");
0N/A throw new IllegalStateException(
0N/A "Test FAILED: Unexpected Maximum Pool Size Overflow!");
0N/A } else {
0N/A echo(">>> Happy Bye, Bye!");
0N/A }
0N/A }
0N/A}