Path: blob/master/test/jdk/javax/management/monitor/StartStopTest.java
41149 views
/*1* Copyright (c) 2005, 2018, 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*30* @library /test/lib31*32* @run clean StartStopTest33* @run build StartStopTest34* @run main/othervm/timeout=300 StartStopTest 135* @run main/othervm/timeout=300 StartStopTest 236* @run main/othervm/timeout=300 StartStopTest 337* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 138* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 239* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 340* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 141* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 242* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 343*/4445import java.util.concurrent.atomic.AtomicInteger;46import javax.management.MBeanServer;47import javax.management.MBeanServerFactory;48import javax.management.Notification;49import javax.management.NotificationListener;50import javax.management.ObjectName;51import javax.management.monitor.CounterMonitor;52import javax.management.monitor.GaugeMonitor;53import javax.management.monitor.Monitor;54import javax.management.monitor.MonitorNotification;55import javax.management.monitor.StringMonitor;5657import jdk.test.lib.Utils;5859public class StartStopTest {60static int maxPoolSize;61static final AtomicInteger counter = new AtomicInteger();6263// MBean class64public class ObservedObject implements ObservedObjectMBean {65volatile public boolean called = false;66public Integer getInteger() {67task("Integer");68return 0;69}70public Double getDouble() {71task("Double");72return 0.0;73}74public String getString() {75task("String");76return "";77}78private void task(String prop) {79called = true;80final int c = counter.incrementAndGet();81echo("\tTASK [" + c + "] in get" + prop);82}83}8485// MBean interface86public interface ObservedObjectMBean {87public Integer getInteger();88public Double getDouble();89public String getString();90}9192/**93* Run test94*/95public int runTest(int monitorType) throws Exception {9697int nTasks = maxPoolSize + 2;98ObjectName[] mbeanNames = new ObjectName[nTasks];99ObservedObject[] monitored = new ObservedObject[nTasks];100ObjectName[] monitorNames = new ObjectName[nTasks];101Monitor[] monitor = new Monitor[nTasks];102String[] attributes = { "Integer", "Double", "String" };103104try {105echo(">>> CREATE MBeanServer");106MBeanServer server = MBeanServerFactory.newMBeanServer();107108String domain = server.getDefaultDomain();109110for (int i = 0; i < nTasks; i++) {111mbeanNames[i] =112new ObjectName(":type=ObservedObject,instance=" + (i + 1));113monitored[i] = new ObservedObject();114echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());115server.registerMBean(monitored[i], mbeanNames[i]);116switch (monitorType) {117case 1:118monitorNames[i] = new ObjectName(":type=CounterMonitor," +119"instance=" + (i + 1));120monitor[i] = new CounterMonitor();121break;122case 2:123monitorNames[i] = new ObjectName(":type=GaugeMonitor," +124"instance=" + (i + 1));125monitor[i] = new GaugeMonitor();126break;127case 3:128monitorNames[i] = new ObjectName(":type=StringMonitor," +129"instance=" + (i + 1));130monitor[i] = new StringMonitor();131break;132default:133echo("Unsupported monitor type");134return 1;135}136echo(">>> CREATE Monitor = " + monitorNames[i].toString());137server.registerMBean(monitor[i], monitorNames[i]);138monitor[i].addObservedObject(mbeanNames[i]);139monitor[i].setObservedAttribute(attributes[monitorType-1]);140monitor[i].setGranularityPeriod(50);141}142143for (int j = 0; j < 2; j++) {144echo(">>> Start MONITORS");145for (int i = 0; i < nTasks; i++)146monitor[i].start();147echo(">>> MONITORS started");148doSleep(500);149echo(">>> Check FLAGS true");150for (int i = 0; i < nTasks; i++)151if (!monitored[i].called) {152echo("KO: At least one attribute was not called");153return 1;154}155echo(">>> FLAGS checked true");156echo(">>> Stop MONITORS");157for (int i = 0; i < nTasks; i++)158monitor[i].stop();159echo(">>> MONITORS stopped");160doSleep(500);161echo(">>> Set FLAGS to false");162for (int i = 0; i < nTasks; i++)163monitored[i].called = false;164echo(">>> FLAGS set to false");165echo(">>> Check FLAGS remain false");166for (int i = 0; i < nTasks; i++)167if (monitored[i].called) {168echo("KO: At least one attribute " +169"continued to get called");170return 1;171}172echo(">>> FLAGS checked false");173}174} finally {175for (int i = 0; i < nTasks; i++)176if (monitor[i] != null)177monitor[i].stop();178}179180return 0;181}182183/*184* Print message185*/186private static void echo(String message) {187System.out.println(message);188}189190/*191* Standalone entry point.192*193* Run the test and report to stdout.194*/195public static void main (String args[]) throws Exception {196Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");197if (size == null) {198maxPoolSize = 10;199echo(">>> MAXIMUM POOL SIZE = 10 [default value]");200} else {201maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();202echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);203}204StartStopTest test = new StartStopTest();205int error = test.runTest(Integer.parseInt(args[0]));206if (error > 0) {207echo(">>> Unhappy Bye, Bye!");208throw new IllegalStateException(209"Test FAILED: Unexpected Maximum Pool Size Overflow!");210} else {211echo(">>> Happy Bye, Bye!");212}213}214215private static void doSleep(long ms) throws Exception {216Thread.sleep(Utils.adjustTimeout(ms));217}218}219220221