File: DerivedGaugeMonitorTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (271 lines) | stat: -rw-r--r-- 8,599 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6683213
 * @summary Test that the initial derived gauge is (Integer)0
 * @author Daniel Fuchs
 *
 * @run clean DerivedGaugeMonitorTest
 * @run build DerivedGaugeMonitorTest
 * @run main DerivedGaugeMonitorTest
 */

import java.io.Serializable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.monitor.CounterMonitor;
import javax.management.monitor.GaugeMonitor;

public class DerivedGaugeMonitorTest {

    public static interface Things {
        public long getALong();
        public int getAnInt();
        public double getADouble();
        public short getAShort();
        public byte getAByte();
        public float getAFloat();
    }
    public static interface MyMBean extends Things {
        public Things getAThing();
    }

    public static class MyThings implements Things, Serializable {
        private static final long serialVersionUID = -4333982919572564126L;

        private volatile long along = 0;
        private volatile int anint = 0;
        private volatile short ashort = 0;
        private volatile byte abyte = 0;
        private volatile float afloat = 0;
        private volatile double adouble = 0;

        private volatile transient boolean mutable;

        public MyThings() {
            this(false);
        }

        protected MyThings(boolean mutable) {
            this.mutable=mutable;
        }

        public long getALong() {
            return mutable?along++:along;
        }

        public int getAnInt() {
            return mutable?anint++:anint;
        }

        public double getADouble() {
            return mutable?adouble++:adouble;
        }

        public short getAShort() {
            return mutable?ashort++:ashort;
        }

        public byte getAByte() {
            return mutable?abyte++:abyte;
        }

        public float getAFloat() {
            return mutable?afloat++:afloat;
        }

        @Override
        public Object clone() throws CloneNotSupportedException {
            final MyThings other = (MyThings)super.clone();
            other.mutable=false;
            return other;
        }
    }

    public static class My implements MyMBean {

        public final CountDownLatch cdl = new CountDownLatch(6);

        private final MyThings things = new MyThings(true);
        private volatile int count = 0;

        public Things getAThing() {
            count++;
            cdl.countDown();
            try {
                return (Things) things.clone();
            } catch (CloneNotSupportedException ex) {
                return null;
            }
        }

        public long getALong() {
            count++;
            cdl.countDown();
            return things.getALong();
        }

        public int getAnInt() {
            count++;
            cdl.countDown();
            return things.getAnInt();
        }

        public double getADouble() {
            count++;
            cdl.countDown();
            return things.getADouble();
        }

        public short getAShort() {
            count++;
            cdl.countDown();
            return things.getAShort();
        }

        public byte getAByte() {
            count++;
            cdl.countDown();
            return things.getAByte();
        }

        public float getAFloat() {
            count++;
            cdl.countDown();
            return things.getAFloat();
        }

    }


    public static String[] attributes = {
        "AByte","AShort","AnInt","ALong","AFloat","ADouble"
    };
    public static String[] things = {
        "AThing.AByte","AThing.AShort","AThing.AnInt","AThing.ALong",
        "AThing.AFloat","AThing.ADouble"
    };

    public static void check(String attr, MBeanServer server, ObjectName mon,
            ObjectName mbean) throws Exception {
        final Object obj = server.getAttribute(mon, "DerivedGauge");
        check(attr,server,mon,mbean,obj);
    }

    public static void check(String attr, MBeanServer server, ObjectName mon,
            ObjectName mbean, Object obj) throws Exception  {
        if (obj == null)
            throw new Exception("Derived gauge for: " + attr +
                    " ["+mon+", "+mbean+"] is null!");
        if (!Integer.valueOf(0).equals(obj))
            throw new Exception("Derived gauge for: " + attr +
                    " ["+mon+", "+mbean+"] is "+obj);
    }

    public static void check(String attr, MBeanServer server, ObjectName mon,
            ObjectName mbean, long start) throws Exception {
        final Object obj = server.getAttribute(mon, "DerivedGauge");
        final long now = System.currentTimeMillis();
        final long gran = (Long)server.getAttribute(mon, "GranularityPeriod");
        if (now > start +2*gran) {
            throw new Exception(attr+": Can't verify test case: " +
                    "granularity period expired!");
        }
        check(attr,server,mon,mbean,obj);
    }

    public static void test(String attr) throws Exception {
        System.err.println("Testing "+ attr);
        final MBeanServer server = MBeanServerFactory.createMBeanServer();
        final ObjectName mbean = new ObjectName("ugly:type=cr.p");
        final My my = new My();
        final GaugeMonitor mon2 = new GaugeMonitor();
        final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");
        final CounterMonitor mon1 = new CounterMonitor();
        final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");

        server.registerMBean(my, mbean);
        server.registerMBean(mon1, mon1n);
        server.registerMBean(mon2, mon2n);

        mon1.addObservedObject(mbean);
        mon1.setGranularityPeriod(60000); // 60 sec...
        mon1.setObservedAttribute(attr);
        mon1.setDifferenceMode(true);
        check(attr,server,mon1n,mbean);

        mon2.addObservedObject(mbean);
        mon2.setGranularityPeriod(60000); // 60 sec...
        mon2.setObservedAttribute(attr);
        mon2.setDifferenceMode(true);
        check(attr,server,mon2n,mbean);

        final long approxStart = System.currentTimeMillis();
        mon1.start();
        mon2.start();

        try {
            check(attr,server,mon1n,mbean,approxStart);
            check(attr,server,mon2n,mbean,approxStart);
            check(attr,server,mon1n,mbean,approxStart);
            check(attr,server,mon2n,mbean,approxStart);


            mon1.setGranularityPeriod(5);
            mon2.setGranularityPeriod(5);

            my.cdl.await(1000, TimeUnit.MILLISECONDS);
            if (my.cdl.getCount() > 0)
                throw new Exception(attr+": Count down not reached!");

            // just check that we don't get an exception now...
            System.err.println(attr+": [" + mon1n+
                    "] DerivedGauge is now "+
                    server.getAttribute(mon1n, "DerivedGauge"));
            System.err.println(attr+": [" + mon2n+
                    "] DerivedGauge is now "+
                    server.getAttribute(mon2n, "DerivedGauge"));
        } finally {
           try {mon1.stop();} catch (Exception x) {}
           try {mon2.stop();} catch (Exception x) {}
        }
    }




    public static void main(String[] args) throws Exception {
        for (String attr:attributes) {
            test(attr);
        }
        for (String attr:things) {
            test(attr);
        }
    }

}