Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/MultiMonitorTest.java
38838 views
/*1* Copyright (c) 2004, 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 498405726* @summary Test that monitors can sample a large number of attributes27* @author Eamonn McManus28* @run clean MultiMonitorTest29* @run build MultiMonitorTest30* @run main MultiMonitorTest31* @key randomness32*/3334import java.util.*;35import javax.management.*;36import javax.management.monitor.*;3738/* We create N MBeans and three monitors, one for each different39monitor type. Each monitor monitors a single attribute in each of40the N MBeans. We arrange for the trigger condition to be41satisfied, so the listener we register on each monitor should get N42notifications. */43public class MultiMonitorTest {44static final int N = 100;45static final ObjectName[] mbeanNames = new ObjectName[N];46static final Monitored[] monitored = new Monitored[N];47static final int COUNTER_THRESHOLD = 1000;48static final int OVER_COUNTER_THRESHOLD = 2000;49static final double GAUGE_THRESHOLD = 1000.0;50static final double OVER_GAUGE_THRESHOLD = 2000.0;51static final String STRING_TO_COMPARE = "chou";52static final String DIFFERENT_STRING = "chevre";5354public static void main(String[] args) throws Exception {55System.out.println("Test that monitors can sample a large " +56"number of attributes");5758final MBeanServer mbs = MBeanServerFactory.createMBeanServer();59for (int i = 0; i < N; i++) {60mbeanNames[i] = new ObjectName(":type=Monitored,instance=" + i);61monitored[i] = new Monitored();62mbs.registerMBean(monitored[i], mbeanNames[i]);63}64final ObjectName counterMonitor =65new ObjectName(":type=CounterMonitor");66final ObjectName gaugeMonitor =67new ObjectName(":type=GaugeMonitor");68final ObjectName stringMonitor =69new ObjectName(":type=StringMonitor");70final ObjectName[] monitorNames =71new ObjectName[] {counterMonitor, gaugeMonitor, stringMonitor};72final String[] attrNames =73new String[] {"CounterValue", "GaugeValue", "StringValue"};74mbs.createMBean(CounterMonitor.class.getName(), counterMonitor);75mbs.createMBean(GaugeMonitor.class.getName(), gaugeMonitor);76mbs.createMBean(StringMonitor.class.getName(), stringMonitor);77final CounterMonitorMBean counterProxy = (CounterMonitorMBean)78MBeanServerInvocationHandler79.newProxyInstance(mbs, counterMonitor, CounterMonitorMBean.class,80false);81final GaugeMonitorMBean gaugeProxy = (GaugeMonitorMBean)82MBeanServerInvocationHandler83.newProxyInstance(mbs, gaugeMonitor, GaugeMonitorMBean.class,84false);85final StringMonitorMBean stringProxy = (StringMonitorMBean)86MBeanServerInvocationHandler87.newProxyInstance(mbs, stringMonitor, StringMonitorMBean.class,88false);89final MonitorMBean[] proxies = new MonitorMBean[] {90counterProxy, gaugeProxy, stringProxy,91};92for (int i = 0; i < 3; i++) {93proxies[i].setGranularityPeriod(1);94proxies[i].setObservedAttribute(attrNames[i]);95for (int j = 0; j < N; j++)96proxies[i].addObservedObject(mbeanNames[j]);97}9899final CountListener[] listeners = new CountListener[] {100new CountListener(), new CountListener(), new CountListener()101};102for (int i = 0; i < 3; i++) {103mbs.addNotificationListener(monitorNames[i], listeners[i],104null, null);105}106107counterProxy.setInitThreshold(new Integer(COUNTER_THRESHOLD));108counterProxy.setNotify(true);109gaugeProxy.setThresholds(new Double(GAUGE_THRESHOLD), new Double(0.0));110gaugeProxy.setNotifyHigh(true);111stringProxy.setStringToCompare(STRING_TO_COMPARE);112stringProxy.setNotifyDiffer(true);113114// A couple of granularity periods to detect bad behaviour115Thread.sleep(2);116117if (!listenersAreAll(0, listeners)) {118System.out.println("TEST FAILED: listeners not all 0");119System.exit(1);120}121122for (int i = 0; i < 3; i++)123proxies[i].start();124125long startTime = System.currentTimeMillis();126while (!listenersAreAll(N, listeners)127&& System.currentTimeMillis() < startTime + 5000)128Thread.sleep(1);129130// More time for bad behaviour131Thread.sleep(1000);132133if (!listenersAreAll(N, listeners)) {134System.out.print("TEST FAILED: listener counts wrong:");135for (int i = 0; i < listeners.length; i++)136System.out.print(" " + listeners[i].getCount());137System.out.println();138System.exit(1);139}140141for (int i = 0; i < 3; i++) {142proxies[i].stop();143for (int j = 0; j < N; j++)144proxies[i].removeObservedObject(mbeanNames[j]);145ObjectName[] observed = proxies[i].getObservedObjects();146if (observed.length != 0) {147System.out.println("TEST FAILED: not all observed objects " +148"removed: " + Arrays.asList(observed));149System.exit(1);150}151}152153System.out.println("Test passed");154}155156public static interface MonitoredMBean {157public int getCounterValue();158public double getGaugeValue();159public String getStringValue();160}161162public static class Monitored implements MonitoredMBean {163/* We give a small random number of normal readings (possibly164zero) before giving a reading that provokes a165notification. */166private int okCounter = randomInt(5);167private int okGauge = randomInt(5);168private int okString = randomInt(5);169170public synchronized int getCounterValue() {171if (--okCounter >= 0)172return 0;173else174return OVER_COUNTER_THRESHOLD;175}176177public synchronized double getGaugeValue() {178if (--okGauge >= 0)179return 0.0;180else181return OVER_GAUGE_THRESHOLD;182}183184public synchronized String getStringValue() {185if (--okString >= 0)186return STRING_TO_COMPARE;187else188return DIFFERENT_STRING;189}190}191192public static class CountListener implements NotificationListener {193private int count;194195public synchronized void handleNotification(Notification n, Object h) {196if (!(n instanceof MonitorNotification)) {197System.out.println("TEST FAILED: bad notif: " +198n.getClass().getName());199System.exit(1);200}201if (n.getType().indexOf("error") >= 0) {202System.out.println("TEST FAILED: error notif: " + n.getType());203System.exit(1);204}205count++;206}207208public synchronized int getCount() {209return count;210}211}212213private static boolean listenersAreAll(int n, CountListener[] listeners) {214for (int i = 0; i < listeners.length; i++) {215if (listeners[i].getCount() != n)216return false;217}218return true;219}220221private static final Random random = new Random();222static synchronized int randomInt(int n) {223return random.nextInt(n);224}225}226227228