Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/DerivedGaugeMonitorTest.java
38838 views
/*1* Copyright (c) 2008, 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* @run clean DerivedGaugeMonitorTest29* @run build DerivedGaugeMonitorTest30* @run main DerivedGaugeMonitorTest31*/3233import java.io.Serializable;34import java.util.concurrent.CountDownLatch;35import java.util.concurrent.TimeUnit;36import javax.management.MBeanServer;37import javax.management.MBeanServerFactory;38import javax.management.ObjectName;39import javax.management.monitor.CounterMonitor;40import javax.management.monitor.GaugeMonitor;4142public class DerivedGaugeMonitorTest {4344public static interface Things {45public long getALong();46public int getAnInt();47public double getADouble();48public short getAShort();49public byte getAByte();50public float getAFloat();51}52public static interface MyMBean extends Things {53public Things getAThing();54}5556public static class MyThings implements Things, Serializable {57private static final long serialVersionUID = -4333982919572564126L;5859private volatile long along = 0;60private volatile int anint = 0;61private volatile short ashort = 0;62private volatile byte abyte = 0;63private volatile float afloat = 0;64private volatile double adouble = 0;6566private volatile transient boolean mutable;6768public MyThings() {69this(false);70}7172protected MyThings(boolean mutable) {73this.mutable=mutable;74}7576public long getALong() {77return mutable?along++:along;78}7980public int getAnInt() {81return mutable?anint++:anint;82}8384public double getADouble() {85return mutable?adouble++:adouble;86}8788public short getAShort() {89return mutable?ashort++:ashort;90}9192public byte getAByte() {93return mutable?abyte++:abyte;94}9596public float getAFloat() {97return mutable?afloat++:afloat;98}99100@Override101public Object clone() throws CloneNotSupportedException {102final MyThings other = (MyThings)super.clone();103other.mutable=false;104return other;105}106}107108public static class My implements MyMBean {109110public final CountDownLatch cdl = new CountDownLatch(6);111112private final MyThings things = new MyThings(true);113private volatile int count = 0;114115public Things getAThing() {116count++;117cdl.countDown();118try {119return (Things) things.clone();120} catch (CloneNotSupportedException ex) {121return null;122}123}124125public long getALong() {126count++;127cdl.countDown();128return things.getALong();129}130131public int getAnInt() {132count++;133cdl.countDown();134return things.getAnInt();135}136137public double getADouble() {138count++;139cdl.countDown();140return things.getADouble();141}142143public short getAShort() {144count++;145cdl.countDown();146return things.getAShort();147}148149public byte getAByte() {150count++;151cdl.countDown();152return things.getAByte();153}154155public float getAFloat() {156count++;157cdl.countDown();158return things.getAFloat();159}160161}162163164public static String[] attributes = {165"AByte","AShort","AnInt","ALong","AFloat","ADouble"166};167public static String[] things = {168"AThing.AByte","AThing.AShort","AThing.AnInt","AThing.ALong",169"AThing.AFloat","AThing.ADouble"170};171172public static void check(String attr, MBeanServer server, ObjectName mon,173ObjectName mbean) throws Exception {174final Object obj = server.getAttribute(mon, "DerivedGauge");175check(attr,server,mon,mbean,obj);176}177178public static void check(String attr, MBeanServer server, ObjectName mon,179ObjectName mbean, Object obj) throws Exception {180if (obj == null)181throw new Exception("Derived gauge for: " + attr +182" ["+mon+", "+mbean+"] is null!");183if (!Integer.valueOf(0).equals(obj))184throw new Exception("Derived gauge for: " + attr +185" ["+mon+", "+mbean+"] is "+obj);186}187188public static void check(String attr, MBeanServer server, ObjectName mon,189ObjectName mbean, long start) throws Exception {190final Object obj = server.getAttribute(mon, "DerivedGauge");191final long now = System.currentTimeMillis();192final long gran = (Long)server.getAttribute(mon, "GranularityPeriod");193if (now > start +2*gran) {194throw new Exception(attr+": Can't verify test case: " +195"granularity period expired!");196}197check(attr,server,mon,mbean,obj);198}199200public static void test(String attr) throws Exception {201System.err.println("Testing "+ attr);202final MBeanServer server = MBeanServerFactory.createMBeanServer();203final ObjectName mbean = new ObjectName("ugly:type=cr.p");204final My my = new My();205final GaugeMonitor mon2 = new GaugeMonitor();206final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");207final CounterMonitor mon1 = new CounterMonitor();208final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");209210server.registerMBean(my, mbean);211server.registerMBean(mon1, mon1n);212server.registerMBean(mon2, mon2n);213214mon1.addObservedObject(mbean);215mon1.setGranularityPeriod(60000); // 60 sec...216mon1.setObservedAttribute(attr);217mon1.setDifferenceMode(true);218check(attr,server,mon1n,mbean);219220mon2.addObservedObject(mbean);221mon2.setGranularityPeriod(60000); // 60 sec...222mon2.setObservedAttribute(attr);223mon2.setDifferenceMode(true);224check(attr,server,mon2n,mbean);225226final long approxStart = System.currentTimeMillis();227mon1.start();228mon2.start();229230try {231check(attr,server,mon1n,mbean,approxStart);232check(attr,server,mon2n,mbean,approxStart);233check(attr,server,mon1n,mbean,approxStart);234check(attr,server,mon2n,mbean,approxStart);235236237mon1.setGranularityPeriod(5);238mon2.setGranularityPeriod(5);239240my.cdl.await(1000, TimeUnit.MILLISECONDS);241if (my.cdl.getCount() > 0)242throw new Exception(attr+": Count down not reached!");243244// just check that we don't get an exception now...245System.err.println(attr+": [" + mon1n+246"] DerivedGauge is now "+247server.getAttribute(mon1n, "DerivedGauge"));248System.err.println(attr+": [" + mon2n+249"] DerivedGauge is now "+250server.getAttribute(mon2n, "DerivedGauge"));251} finally {252try {mon1.stop();} catch (Exception x) {}253try {mon2.stop();} catch (Exception x) {}254}255}256257258259260public static void main(String[] args) throws Exception {261for (String attr:attributes) {262test(attr);263}264for (String attr:things) {265test(attr);266}267}268269}270271272