Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/StartStopTest.java
38839 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 622282626* @summary Test that tasks are cancelled properly when27* monitors are started and stopped in a loop.28* @author Luis-Miguel Alventosa29* @library /lib/testlibrary30* @run clean StartStopTest31* @run build jdk.testlibrary.* StartStopTest32* @run main/othervm/timeout=300 StartStopTest 133* @run main/othervm/timeout=300 StartStopTest 234* @run main/othervm/timeout=300 StartStopTest 335* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 136* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 237* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 338* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 139* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 240* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 341*/4243import java.util.concurrent.atomic.AtomicInteger;44import javax.management.MBeanServer;45import javax.management.MBeanServerFactory;46import javax.management.Notification;47import javax.management.NotificationListener;48import javax.management.ObjectName;49import javax.management.monitor.CounterMonitor;50import javax.management.monitor.GaugeMonitor;51import javax.management.monitor.Monitor;52import javax.management.monitor.MonitorNotification;53import javax.management.monitor.StringMonitor;5455import jdk.testlibrary.Utils;5657public class StartStopTest {58static int maxPoolSize;59static final AtomicInteger counter = new AtomicInteger();6061// MBean class62public class ObservedObject implements ObservedObjectMBean {63volatile public boolean called = false;64public Integer getInteger() {65task("Integer");66return 0;67}68public Double getDouble() {69task("Double");70return 0.0;71}72public String getString() {73task("String");74return "";75}76private void task(String prop) {77called = true;78final int c = counter.incrementAndGet();79echo("\tTASK [" + c + "] in get" + prop);80}81}8283// MBean interface84public interface ObservedObjectMBean {85public Integer getInteger();86public Double getDouble();87public String getString();88}8990/**91* Run test92*/93public int runTest(int monitorType) throws Exception {9495int nTasks = maxPoolSize + 2;96ObjectName[] mbeanNames = new ObjectName[nTasks];97ObservedObject[] monitored = new ObservedObject[nTasks];98ObjectName[] monitorNames = new ObjectName[nTasks];99Monitor[] monitor = new Monitor[nTasks];100String[] attributes = { "Integer", "Double", "String" };101102try {103echo(">>> CREATE MBeanServer");104MBeanServer server = MBeanServerFactory.newMBeanServer();105106String domain = server.getDefaultDomain();107108for (int i = 0; i < nTasks; i++) {109mbeanNames[i] =110new ObjectName(":type=ObservedObject,instance=" + (i + 1));111monitored[i] = new ObservedObject();112echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());113server.registerMBean(monitored[i], mbeanNames[i]);114switch (monitorType) {115case 1:116monitorNames[i] = new ObjectName(":type=CounterMonitor," +117"instance=" + (i + 1));118monitor[i] = new CounterMonitor();119break;120case 2:121monitorNames[i] = new ObjectName(":type=GaugeMonitor," +122"instance=" + (i + 1));123monitor[i] = new GaugeMonitor();124break;125case 3:126monitorNames[i] = new ObjectName(":type=StringMonitor," +127"instance=" + (i + 1));128monitor[i] = new StringMonitor();129break;130default:131echo("Unsupported monitor type");132return 1;133}134echo(">>> CREATE Monitor = " + monitorNames[i].toString());135server.registerMBean(monitor[i], monitorNames[i]);136monitor[i].addObservedObject(mbeanNames[i]);137monitor[i].setObservedAttribute(attributes[monitorType-1]);138monitor[i].setGranularityPeriod(50);139}140141for (int j = 0; j < 2; j++) {142echo(">>> Start MONITORS");143for (int i = 0; i < nTasks; i++)144monitor[i].start();145echo(">>> MONITORS started");146doSleep(500);147echo(">>> Check FLAGS true");148for (int i = 0; i < nTasks; i++)149if (!monitored[i].called) {150echo("KO: At least one attribute was not called");151return 1;152}153echo(">>> FLAGS checked true");154echo(">>> Stop MONITORS");155for (int i = 0; i < nTasks; i++)156monitor[i].stop();157echo(">>> MONITORS stopped");158doSleep(500);159echo(">>> Set FLAGS to false");160for (int i = 0; i < nTasks; i++)161monitored[i].called = false;162echo(">>> FLAGS set to false");163echo(">>> Check FLAGS remain false");164for (int i = 0; i < nTasks; i++)165if (monitored[i].called) {166echo("KO: At least one attribute " +167"continued to get called");168return 1;169}170echo(">>> FLAGS checked false");171}172} finally {173for (int i = 0; i < nTasks; i++)174if (monitor[i] != null)175monitor[i].stop();176}177178return 0;179}180181/*182* Print message183*/184private static void echo(String message) {185System.out.println(message);186}187188/*189* Standalone entry point.190*191* Run the test and report to stdout.192*/193public static void main (String args[]) throws Exception {194Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");195if (size == null) {196maxPoolSize = 10;197echo(">>> MAXIMUM POOL SIZE = 10 [default value]");198} else {199maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();200echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);201}202StartStopTest test = new StartStopTest();203int error = test.runTest(Integer.parseInt(args[0]));204if (error > 0) {205echo(">>> Unhappy Bye, Bye!");206throw new IllegalStateException(207"Test FAILED: Unexpected Maximum Pool Size Overflow!");208} else {209echo(">>> Happy Bye, Bye!");210}211}212213private static void doSleep(long ms) throws Exception {214Thread.sleep(Math.round(ms * Utils.TIMEOUT_FACTOR));215}216}217218219