Path: blob/master/test/jdk/javax/management/monitor/DerivedGaugeMonitorTest.java
41149 views
/*1* Copyright (c) 2008, 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 668321326* @summary Test that the initial derived gauge is (Integer)027* @author Daniel Fuchs28*29* @run clean DerivedGaugeMonitorTest30* @run build DerivedGaugeMonitorTest31* @run main DerivedGaugeMonitorTest32*/3334import java.io.Serializable;35import java.util.concurrent.CountDownLatch;36import java.util.concurrent.TimeUnit;37import javax.management.MBeanServer;38import javax.management.MBeanServerFactory;39import javax.management.ObjectName;40import javax.management.monitor.CounterMonitor;41import javax.management.monitor.GaugeMonitor;4243public class DerivedGaugeMonitorTest {4445public static interface Things {46public long getALong();47public int getAnInt();48public double getADouble();49public short getAShort();50public byte getAByte();51public float getAFloat();52}53public static interface MyMBean extends Things {54public Things getAThing();55}5657public static class MyThings implements Things, Serializable {58private static final long serialVersionUID = -4333982919572564126L;5960private volatile long along = 0;61private volatile int anint = 0;62private volatile short ashort = 0;63private volatile byte abyte = 0;64private volatile float afloat = 0;65private volatile double adouble = 0;6667private volatile transient boolean mutable;6869public MyThings() {70this(false);71}7273protected MyThings(boolean mutable) {74this.mutable=mutable;75}7677public long getALong() {78return mutable?along++:along;79}8081public int getAnInt() {82return mutable?anint++:anint;83}8485public double getADouble() {86return mutable?adouble++:adouble;87}8889public short getAShort() {90return mutable?ashort++:ashort;91}9293public byte getAByte() {94return mutable?abyte++:abyte;95}9697public float getAFloat() {98return mutable?afloat++:afloat;99}100101@Override102public Object clone() throws CloneNotSupportedException {103final MyThings other = (MyThings)super.clone();104other.mutable=false;105return other;106}107}108109public static class My implements MyMBean {110111public final CountDownLatch cdl = new CountDownLatch(6);112113private final MyThings things = new MyThings(true);114private volatile int count = 0;115116public Things getAThing() {117count++;118cdl.countDown();119try {120return (Things) things.clone();121} catch (CloneNotSupportedException ex) {122return null;123}124}125126public long getALong() {127count++;128cdl.countDown();129return things.getALong();130}131132public int getAnInt() {133count++;134cdl.countDown();135return things.getAnInt();136}137138public double getADouble() {139count++;140cdl.countDown();141return things.getADouble();142}143144public short getAShort() {145count++;146cdl.countDown();147return things.getAShort();148}149150public byte getAByte() {151count++;152cdl.countDown();153return things.getAByte();154}155156public float getAFloat() {157count++;158cdl.countDown();159return things.getAFloat();160}161162}163164165public static String[] attributes = {166"AByte","AShort","AnInt","ALong","AFloat","ADouble"167};168public static String[] things = {169"AThing.AByte","AThing.AShort","AThing.AnInt","AThing.ALong",170"AThing.AFloat","AThing.ADouble"171};172173public static void check(String attr, MBeanServer server, ObjectName mon,174ObjectName mbean) throws Exception {175final Object obj = server.getAttribute(mon, "DerivedGauge");176check(attr,server,mon,mbean,obj);177}178179public static void check(String attr, MBeanServer server, ObjectName mon,180ObjectName mbean, Object obj) throws Exception {181if (obj == null)182throw new Exception("Derived gauge for: " + attr +183" ["+mon+", "+mbean+"] is null!");184if (!Integer.valueOf(0).equals(obj))185throw new Exception("Derived gauge for: " + attr +186" ["+mon+", "+mbean+"] is "+obj);187}188189public static void check(String attr, MBeanServer server, ObjectName mon,190ObjectName mbean, long start) throws Exception {191final Object obj = server.getAttribute(mon, "DerivedGauge");192final long now = System.currentTimeMillis();193final long gran = (Long)server.getAttribute(mon, "GranularityPeriod");194if (now > start +2*gran) {195throw new Exception(attr+": Can't verify test case: " +196"granularity period expired!");197}198check(attr,server,mon,mbean,obj);199}200201public static void test(String attr) throws Exception {202System.err.println("Testing "+ attr);203final MBeanServer server = MBeanServerFactory.createMBeanServer();204final ObjectName mbean = new ObjectName("ugly:type=cr.p");205final My my = new My();206final GaugeMonitor mon2 = new GaugeMonitor();207final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");208final CounterMonitor mon1 = new CounterMonitor();209final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");210211server.registerMBean(my, mbean);212server.registerMBean(mon1, mon1n);213server.registerMBean(mon2, mon2n);214215mon1.addObservedObject(mbean);216mon1.setGranularityPeriod(60000); // 60 sec...217mon1.setObservedAttribute(attr);218mon1.setDifferenceMode(true);219check(attr,server,mon1n,mbean);220221mon2.addObservedObject(mbean);222mon2.setGranularityPeriod(60000); // 60 sec...223mon2.setObservedAttribute(attr);224mon2.setDifferenceMode(true);225check(attr,server,mon2n,mbean);226227final long approxStart = System.currentTimeMillis();228mon1.start();229mon2.start();230231try {232check(attr,server,mon1n,mbean,approxStart);233check(attr,server,mon2n,mbean,approxStart);234check(attr,server,mon1n,mbean,approxStart);235check(attr,server,mon2n,mbean,approxStart);236237238mon1.setGranularityPeriod(5);239mon2.setGranularityPeriod(5);240241my.cdl.await(1000, TimeUnit.MILLISECONDS);242if (my.cdl.getCount() > 0)243throw new Exception(attr+": Count down not reached!");244245// just check that we don't get an exception now...246System.err.println(attr+": [" + mon1n+247"] DerivedGauge is now "+248server.getAttribute(mon1n, "DerivedGauge"));249System.err.println(attr+": [" + mon2n+250"] DerivedGauge is now "+251server.getAttribute(mon2n, "DerivedGauge"));252} finally {253try {mon1.stop();} catch (Exception x) {}254try {mon2.stop();} catch (Exception x) {}255}256}257258259260261public static void main(String[] args) throws Exception {262for (String attr:attributes) {263test(attr);264}265for (String attr:things) {266test(attr);267}268}269270}271272273