Path: blob/master/test/jdk/javax/management/monitor/CounterMonitorThresholdTest.java
41149 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 6229368 802520726* @summary Wrong threshold value in CounterMonitor with offset and modulus.27* @author Luis-Miguel Alventosa28*29* @run clean CounterMonitorThresholdTest30* @run build CounterMonitorThresholdTest31* @run main CounterMonitorThresholdTest32*/3334import java.lang.management.ManagementFactory;35import javax.management.MBeanServer;36import javax.management.MBeanServerInvocationHandler;37import javax.management.Notification;38import javax.management.NotificationEmitter;39import javax.management.NotificationListener;40import javax.management.ObjectName;41import javax.management.monitor.CounterMonitor;42import javax.management.monitor.CounterMonitorMBean;43import javax.management.monitor.MonitorNotification;4445public class CounterMonitorThresholdTest {4647// Offset = 148private static int counter1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };49private static int derivedGauge1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };50private static int threshold1[] = { 1, 2, 3, 4, 5, 5, 1, 1, 1, 2, 3, 4, 5, 1, 1, 2 };5152// Offset = 353private static int counter2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };54private static int derivedGauge2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };55private static int threshold2[] = { 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 4 };5657public interface TestMBean {58public int getCounter();59public void setCounter(int count);60}6162public static class Test implements TestMBean {63public int getCounter() {64return count;65}66public void setCounter(int count) {67this.count = count;68}69private int count = 0;70}7172public static class Listener implements NotificationListener {73public void handleNotification(Notification n, Object hb) {74System.out.println("\tReceived notification: " + n.getType());75if (n instanceof MonitorNotification) {76MonitorNotification mn = (MonitorNotification) n;77System.out.println("\tSource: " +78mn.getSource());79System.out.println("\tType: " +80mn.getType());81System.out.println("\tTimeStamp: " +82mn.getTimeStamp());83System.out.println("\tObservedObject: " +84mn.getObservedObject());85System.out.println("\tObservedAttribute: " +86mn.getObservedAttribute());87System.out.println("\tDerivedGauge: " +88mn.getDerivedGauge());89System.out.println("\tTrigger: " +90mn.getTrigger());91}92}93}9495public static void runTest(int offset,96int counter[],97int derivedGauge[],98int threshold[]) throws Exception {99// Retrieve the platform MBean server100//101System.out.println("\nRetrieve the platform MBean server");102MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();103String domain = mbs.getDefaultDomain();104105// Create and register TestMBean106//107ObjectName name =108new ObjectName(domain +109":type=" + Test.class.getName() +110",offset=" + offset);111mbs.createMBean(Test.class.getName(), name);112TestMBean mbean = (TestMBean)113MBeanServerInvocationHandler.newProxyInstance(114mbs, name, TestMBean.class, false);115116// Create and register CounterMonitorMBean117//118ObjectName cmn =119new ObjectName(domain +120":type=" + CounterMonitor.class.getName() +121",offset=" + offset);122CounterMonitor m = new CounterMonitor();123mbs.registerMBean(m, cmn);124CounterMonitorMBean cm = (CounterMonitorMBean)125MBeanServerInvocationHandler.newProxyInstance(126mbs, cmn, CounterMonitorMBean.class, true);127((NotificationEmitter) cm).addNotificationListener(128new Listener(), null, null);129cm.addObservedObject(name);130cm.setObservedAttribute("Counter");131cm.setGranularityPeriod(100);132cm.setInitThreshold(1);133cm.setOffset(offset);134cm.setModulus(5);135cm.setNotify(true);136137// Start the monitor138//139System.out.println("\nStart monitoring...");140cm.start();141142// Play with counter143//144for (int i = 0; i < counter.length; i++) {145mbean.setCounter(counter[i]);146System.out.println("\nCounter = " + mbean.getCounter());147Integer derivedGaugeValue;148// either pass or test timeout (killed by test harness)149// see 8025207150do {151Thread.sleep(150);152derivedGaugeValue = (Integer) cm.getDerivedGauge(name);153} while (derivedGaugeValue.intValue() != derivedGauge[i]);154155Number thresholdValue = cm.getThreshold(name);156System.out.println("Threshold = " + thresholdValue);157if (thresholdValue.intValue() != threshold[i]) {158System.out.println("Wrong threshold! Current value = " +159thresholdValue + " Expected value = " + threshold[i]);160System.out.println("\nStop monitoring...");161cm.stop();162throw new IllegalArgumentException("wrong threshold");163}164}165166// Stop the monitor167//168System.out.println("\nStop monitoring...");169cm.stop();170}171172public static void main(String[] args) throws Exception {173runTest(1, counter1, derivedGauge1, threshold1);174runTest(3, counter2, derivedGauge2, threshold2);175}176}177178179