Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/CounterMonitorThresholdTest.java
38838 views
/*1* Copyright (c) 2005, 2013, 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* @run clean CounterMonitorThresholdTest29* @run build CounterMonitorThresholdTest30* @run main CounterMonitorThresholdTest31*/3233import java.lang.management.ManagementFactory;34import javax.management.MBeanServer;35import javax.management.MBeanServerInvocationHandler;36import javax.management.Notification;37import javax.management.NotificationEmitter;38import javax.management.NotificationListener;39import javax.management.ObjectName;40import javax.management.monitor.CounterMonitor;41import javax.management.monitor.CounterMonitorMBean;42import javax.management.monitor.MonitorNotification;4344public class CounterMonitorThresholdTest {4546// Offset = 147private static int counter1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };48private static int derivedGauge1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };49private static int threshold1[] = { 1, 2, 3, 4, 5, 5, 1, 1, 1, 2, 3, 4, 5, 1, 1, 2 };5051// Offset = 352private static int counter2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };53private static int derivedGauge2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };54private static int threshold2[] = { 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 4 };5556public interface TestMBean {57public int getCounter();58public void setCounter(int count);59}6061public static class Test implements TestMBean {62public int getCounter() {63return count;64}65public void setCounter(int count) {66this.count = count;67}68private int count = 0;69}7071public static class Listener implements NotificationListener {72public void handleNotification(Notification n, Object hb) {73System.out.println("\tReceived notification: " + n.getType());74if (n instanceof MonitorNotification) {75MonitorNotification mn = (MonitorNotification) n;76System.out.println("\tSource: " +77mn.getSource());78System.out.println("\tType: " +79mn.getType());80System.out.println("\tTimeStamp: " +81mn.getTimeStamp());82System.out.println("\tObservedObject: " +83mn.getObservedObject());84System.out.println("\tObservedAttribute: " +85mn.getObservedAttribute());86System.out.println("\tDerivedGauge: " +87mn.getDerivedGauge());88System.out.println("\tTrigger: " +89mn.getTrigger());90}91}92}9394public static void runTest(int offset,95int counter[],96int derivedGauge[],97int threshold[]) throws Exception {98// Retrieve the platform MBean server99//100System.out.println("\nRetrieve the platform MBean server");101MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();102String domain = mbs.getDefaultDomain();103104// Create and register TestMBean105//106ObjectName name =107new ObjectName(domain +108":type=" + Test.class.getName() +109",offset=" + offset);110mbs.createMBean(Test.class.getName(), name);111TestMBean mbean = (TestMBean)112MBeanServerInvocationHandler.newProxyInstance(113mbs, name, TestMBean.class, false);114115// Create and register CounterMonitorMBean116//117ObjectName cmn =118new ObjectName(domain +119":type=" + CounterMonitor.class.getName() +120",offset=" + offset);121CounterMonitor m = new CounterMonitor();122mbs.registerMBean(m, cmn);123CounterMonitorMBean cm = (CounterMonitorMBean)124MBeanServerInvocationHandler.newProxyInstance(125mbs, cmn, CounterMonitorMBean.class, true);126((NotificationEmitter) cm).addNotificationListener(127new Listener(), null, null);128cm.addObservedObject(name);129cm.setObservedAttribute("Counter");130cm.setGranularityPeriod(100);131cm.setInitThreshold(1);132cm.setOffset(offset);133cm.setModulus(5);134cm.setNotify(true);135136// Start the monitor137//138System.out.println("\nStart monitoring...");139cm.start();140141// Play with counter142//143for (int i = 0; i < counter.length; i++) {144mbean.setCounter(counter[i]);145System.out.println("\nCounter = " + mbean.getCounter());146Integer derivedGaugeValue;147// either pass or test timeout (killed by test harness)148// see 8025207149do {150Thread.sleep(150);151derivedGaugeValue = (Integer) cm.getDerivedGauge(name);152} while (derivedGaugeValue.intValue() != derivedGauge[i]);153154Number thresholdValue = cm.getThreshold(name);155System.out.println("Threshold = " + thresholdValue);156if (thresholdValue.intValue() != threshold[i]) {157System.out.println("Wrong threshold! Current value = " +158thresholdValue + " Expected value = " + threshold[i]);159System.out.println("\nStop monitoring...");160cm.stop();161throw new IllegalArgumentException("wrong threshold");162}163}164165// Stop the monitor166//167System.out.println("\nStop monitoring...");168cm.stop();169}170171public static void main(String[] args) throws Exception {172runTest(1, counter1, derivedGauge1, threshold1);173runTest(3, counter2, derivedGauge2, threshold2);174}175}176177178