Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/ThreadPoolTest.java
38838 views
/*1* Copyright (c) 2005, 2006, 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 6222826 637971226* @summary Test that all monitors will be well started when sharing27* a single thread pool.28* @author Luis-Miguel Alventosa29* @run clean ThreadPoolTest30* @run build ThreadPoolTest31* @run main/othervm/timeout=300 ThreadPoolTest 132* @run main/othervm/timeout=300 ThreadPoolTest 233* @run main/othervm/timeout=300 ThreadPoolTest 334* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 ThreadPoolTest 135* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 ThreadPoolTest 236* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 ThreadPoolTest 337* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 ThreadPoolTest 138* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 ThreadPoolTest 239* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 ThreadPoolTest 340*/4142import java.util.concurrent.atomic.AtomicInteger;43import javax.management.MBeanServer;44import javax.management.MBeanServerFactory;45import javax.management.Notification;46import javax.management.NotificationListener;47import javax.management.ObjectName;48import javax.management.monitor.CounterMonitor;49import javax.management.monitor.GaugeMonitor;50import javax.management.monitor.Monitor;51import javax.management.monitor.MonitorNotification;52import javax.management.monitor.StringMonitor;5354public class ThreadPoolTest {5556static int maxPoolSize;57static int nTasks;58private static Waiter waiter;5960static final long MAX_WAITING_TIME = 10000;6162// MBean class63public class ObservedObject implements ObservedObjectMBean {64private boolean called = false;65public Integer getInteger() {66inform("getInteger()");67return 0;68}69public Double getDouble() {70inform("getDouble()");71return 0.0;72}73public String getString() {74inform("getString()");75return "";76}77private void inform(String prop) {78synchronized(waiter) {79if (!called) {80called = true;81waiter.count();82}83}8485echo(">>> TASK "+prop+" is called.");86}87}8889// MBean interface90public interface ObservedObjectMBean {91public Integer getInteger();92public Double getDouble();93public String getString();94}9596/**97* Run test98*/99public int runTest(int monitorType) throws Exception {100101102ObjectName[] mbeanNames = new ObjectName[nTasks];103ObservedObject[] monitored = new ObservedObject[nTasks];104ObjectName[] monitorNames = new ObjectName[nTasks];105Monitor[] monitor = new Monitor[nTasks];106String[] attributes = { "Integer", "Double", "String" };107108try {109echo(">>> CREATE MBeanServer");110MBeanServer server = MBeanServerFactory.newMBeanServer();111112String domain = server.getDefaultDomain();113114for (int i = 0; i < nTasks; i++) {115mbeanNames[i] =116new ObjectName(":type=ObservedObject,instance=" + (i + 1));117monitored[i] = new ObservedObject();118echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());119server.registerMBean(monitored[i], mbeanNames[i]);120switch (monitorType) {121case 1:122monitorNames[i] = new ObjectName(":type=CounterMonitor," +123"instance=" + (i + 1));124monitor[i] = new CounterMonitor();125break;126case 2:127monitorNames[i] = new ObjectName(":type=GaugeMonitor," +128"instance=" + (i + 1));129monitor[i] = new GaugeMonitor();130break;131case 3:132monitorNames[i] = new ObjectName(":type=StringMonitor," +133"instance=" + (i + 1));134monitor[i] = new StringMonitor();135break;136default:137echo("Unsupported monitor type");138return 1;139}140echo(">>> CREATE Monitor = " + monitorNames[i].toString());141server.registerMBean(monitor[i], monitorNames[i]);142monitor[i].addObservedObject(mbeanNames[i]);143monitor[i].setObservedAttribute(attributes[monitorType-1]);144monitor[i].setGranularityPeriod(50);145monitor[i].start();146}147148if (!waiter.waiting(MAX_WAITING_TIME)) {149echo("Error, not all "+nTasks+" monitor tasks are called after "150+MAX_WAITING_TIME);151return 1;152}153} finally {154for (int i = 0; i < nTasks; i++)155if (monitor[i] != null)156monitor[i].stop();157}158159echo("All "+nTasks+" monitors are called.");160return 0;161}162163/*164* Print message165*/166private static void echo(String message) {167System.out.println(message);168}169170/*171* Standalone entry point.172*173* Run the test and report to stdout.174*/175public static void main (String args[]) throws Exception {176Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");177if (size == null) {178maxPoolSize = 10;179echo(">>> MAXIMUM POOL SIZE = 10 [default value]");180} else {181maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();182echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);183}184185nTasks = maxPoolSize + 2;186waiter = new Waiter(nTasks);187ThreadPoolTest test = new ThreadPoolTest();188189int error = test.runTest(Integer.parseInt(args[0]));190if (error > 0) {191echo(">>> Unhappy Bye, Bye!");192throw new IllegalStateException(193"Test FAILED: Unexpected Maximum Pool Size Overflow!");194} else {195echo(">>> Happy Bye, Bye!");196}197}198199private static class Waiter {200public Waiter(int waitedNB) {201this.waitedNB = waitedNB;202}203204public void count() {205synchronized(this) {206counted++;207208if (counted == waitedNB) {209this.notifyAll();210}211}212}213214public boolean waiting(long timeout) {215final long startTime = System.currentTimeMillis();216long toWait = timeout;217218synchronized(this) {219while(counted < waitedNB && toWait > 0) {220try {221this.wait(toWait);222} catch (InterruptedException ire) {223break;224}225226toWait = timeout -227(System.currentTimeMillis() - startTime);228}229}230231return counted == waitedNB;232}233234private int waitedNB;235private int counted = 0;236}237}238239240