Path: blob/master/test/jdk/javax/management/monitor/ThreadPoolTest.java
41152 views
/*1* Copyright (c) 2005, 2015, 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*30* @run clean ThreadPoolTest31* @run build ThreadPoolTest32* @run main/othervm/timeout=300 ThreadPoolTest 133* @run main/othervm/timeout=300 ThreadPoolTest 234* @run main/othervm/timeout=300 ThreadPoolTest 335* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 ThreadPoolTest 136* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 ThreadPoolTest 237* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 ThreadPoolTest 338* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 ThreadPoolTest 139* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 ThreadPoolTest 240* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 ThreadPoolTest 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;5455public class ThreadPoolTest {5657static int maxPoolSize;58static int nTasks;59private static Waiter waiter;6061static final long MAX_WAITING_TIME = 10000;6263// MBean class64public class ObservedObject implements ObservedObjectMBean {65private boolean called = false;66public Integer getInteger() {67inform("getInteger()");68return 0;69}70public Double getDouble() {71inform("getDouble()");72return 0.0;73}74public String getString() {75inform("getString()");76return "";77}78private void inform(String prop) {79synchronized(waiter) {80if (!called) {81called = true;82waiter.count();83}84}8586echo(">>> TASK "+prop+" is called.");87}88}8990// MBean interface91public interface ObservedObjectMBean {92public Integer getInteger();93public Double getDouble();94public String getString();95}9697/**98* Run test99*/100public int runTest(int monitorType) throws Exception {101102103ObjectName[] mbeanNames = new ObjectName[nTasks];104ObservedObject[] monitored = new ObservedObject[nTasks];105ObjectName[] monitorNames = new ObjectName[nTasks];106Monitor[] monitor = new Monitor[nTasks];107String[] attributes = { "Integer", "Double", "String" };108109try {110echo(">>> CREATE MBeanServer");111MBeanServer server = MBeanServerFactory.newMBeanServer();112113String domain = server.getDefaultDomain();114115for (int i = 0; i < nTasks; i++) {116mbeanNames[i] =117new ObjectName(":type=ObservedObject,instance=" + (i + 1));118monitored[i] = new ObservedObject();119echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());120server.registerMBean(monitored[i], mbeanNames[i]);121switch (monitorType) {122case 1:123monitorNames[i] = new ObjectName(":type=CounterMonitor," +124"instance=" + (i + 1));125monitor[i] = new CounterMonitor();126break;127case 2:128monitorNames[i] = new ObjectName(":type=GaugeMonitor," +129"instance=" + (i + 1));130monitor[i] = new GaugeMonitor();131break;132case 3:133monitorNames[i] = new ObjectName(":type=StringMonitor," +134"instance=" + (i + 1));135monitor[i] = new StringMonitor();136break;137default:138echo("Unsupported monitor type");139return 1;140}141echo(">>> CREATE Monitor = " + monitorNames[i].toString());142server.registerMBean(monitor[i], monitorNames[i]);143monitor[i].addObservedObject(mbeanNames[i]);144monitor[i].setObservedAttribute(attributes[monitorType-1]);145monitor[i].setGranularityPeriod(50);146monitor[i].start();147}148149if (!waiter.waiting(MAX_WAITING_TIME)) {150echo("Error, not all "+nTasks+" monitor tasks are called after "151+MAX_WAITING_TIME);152return 1;153}154} finally {155for (int i = 0; i < nTasks; i++)156if (monitor[i] != null)157monitor[i].stop();158}159160echo("All "+nTasks+" monitors are called.");161return 0;162}163164/*165* Print message166*/167private static void echo(String message) {168System.out.println(message);169}170171/*172* Standalone entry point.173*174* Run the test and report to stdout.175*/176public static void main (String args[]) throws Exception {177Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");178if (size == null) {179maxPoolSize = 10;180echo(">>> MAXIMUM POOL SIZE = 10 [default value]");181} else {182maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();183echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);184}185186nTasks = maxPoolSize + 2;187waiter = new Waiter(nTasks);188ThreadPoolTest test = new ThreadPoolTest();189190int error = test.runTest(Integer.parseInt(args[0]));191if (error > 0) {192echo(">>> Unhappy Bye, Bye!");193throw new IllegalStateException(194"Test FAILED: Unexpected Maximum Pool Size Overflow!");195} else {196echo(">>> Happy Bye, Bye!");197}198}199200private static class Waiter {201public Waiter(int waitedNB) {202this.waitedNB = waitedNB;203}204205public void count() {206synchronized(this) {207counted++;208209if (counted == waitedNB) {210this.notifyAll();211}212}213}214215public boolean waiting(long timeout) {216final long startTime = System.currentTimeMillis();217long toWait = timeout;218219synchronized(this) {220while(counted < waitedNB && toWait > 0) {221try {222this.wait(toWait);223} catch (InterruptedException ire) {224break;225}226227toWait = timeout -228(System.currentTimeMillis() - startTime);229}230}231232return counted == waitedNB;233}234235private int waitedNB;236private int counted = 0;237}238}239240241