Path: blob/master/test/jdk/javax/management/monitor/MultiMonitorTest.java
41149 views
/*1* Copyright (c) 2004, 2017, 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* @key randomness27* @summary Test that monitors can sample a large number of attributes28* @author Eamonn McManus29*30* @run clean MultiMonitorTest31* @run build MultiMonitorTest32* @run main MultiMonitorTest33*/3435import java.util.*;36import javax.management.*;37import javax.management.monitor.*;3839/* We create N MBeans and three monitors, one for each different40monitor type. Each monitor monitors a single attribute in each of41the N MBeans. We arrange for the trigger condition to be42satisfied, so the listener we register on each monitor should get N43notifications. */44public class MultiMonitorTest {45static final int N = 100;46static final ObjectName[] mbeanNames = new ObjectName[N];47static final Monitored[] monitored = new Monitored[N];48static final int COUNTER_THRESHOLD = 1000;49static final int OVER_COUNTER_THRESHOLD = 2000;50static final double GAUGE_THRESHOLD = 1000.0;51static final double OVER_GAUGE_THRESHOLD = 2000.0;52static final String STRING_TO_COMPARE = "chou";53static final String DIFFERENT_STRING = "chevre";5455public static void main(String[] args) throws Exception {56System.out.println("Test that monitors can sample a large " +57"number of attributes");5859final MBeanServer mbs = MBeanServerFactory.createMBeanServer();60for (int i = 0; i < N; i++) {61mbeanNames[i] = new ObjectName(":type=Monitored,instance=" + i);62monitored[i] = new Monitored();63mbs.registerMBean(monitored[i], mbeanNames[i]);64}65final ObjectName counterMonitor =66new ObjectName(":type=CounterMonitor");67final ObjectName gaugeMonitor =68new ObjectName(":type=GaugeMonitor");69final ObjectName stringMonitor =70new ObjectName(":type=StringMonitor");71final ObjectName[] monitorNames =72new ObjectName[] {counterMonitor, gaugeMonitor, stringMonitor};73final String[] attrNames =74new String[] {"CounterValue", "GaugeValue", "StringValue"};75mbs.createMBean(CounterMonitor.class.getName(), counterMonitor);76mbs.createMBean(GaugeMonitor.class.getName(), gaugeMonitor);77mbs.createMBean(StringMonitor.class.getName(), stringMonitor);78final CounterMonitorMBean counterProxy = (CounterMonitorMBean)79MBeanServerInvocationHandler80.newProxyInstance(mbs, counterMonitor, CounterMonitorMBean.class,81false);82final GaugeMonitorMBean gaugeProxy = (GaugeMonitorMBean)83MBeanServerInvocationHandler84.newProxyInstance(mbs, gaugeMonitor, GaugeMonitorMBean.class,85false);86final StringMonitorMBean stringProxy = (StringMonitorMBean)87MBeanServerInvocationHandler88.newProxyInstance(mbs, stringMonitor, StringMonitorMBean.class,89false);90final MonitorMBean[] proxies = new MonitorMBean[] {91counterProxy, gaugeProxy, stringProxy,92};93for (int i = 0; i < 3; i++) {94proxies[i].setGranularityPeriod(1);95proxies[i].setObservedAttribute(attrNames[i]);96for (int j = 0; j < N; j++)97proxies[i].addObservedObject(mbeanNames[j]);98}99100final CountListener[] listeners = new CountListener[] {101new CountListener(), new CountListener(), new CountListener()102};103for (int i = 0; i < 3; i++) {104mbs.addNotificationListener(monitorNames[i], listeners[i],105null, null);106}107108counterProxy.setInitThreshold(new Integer(COUNTER_THRESHOLD));109counterProxy.setNotify(true);110gaugeProxy.setThresholds(new Double(GAUGE_THRESHOLD), new Double(0.0));111gaugeProxy.setNotifyHigh(true);112stringProxy.setStringToCompare(STRING_TO_COMPARE);113stringProxy.setNotifyDiffer(true);114115// A couple of granularity periods to detect bad behaviour116Thread.sleep(2);117118System.out.println("Checking for all listeners to be 0");119if (!listenersAreAll(0, listeners)) {120System.out.println("TEST FAILED: listeners not all 0");121System.exit(1);122}123124for (int i = 0; i < 3; i++)125proxies[i].start();126127System.out.println("Waiting for listeners to all : " + N);128int iterations = 0;129while (!listenersAreAll(N, listeners)) {130Thread.sleep(500);131132if (++iterations == 10) {133for (int i = 0; i < listeners.length; i++) {134System.out.print(" " + listeners[i].getCount());135}136System.out.println();137iterations = 0;138}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