2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6229368
|
|
27 |
* @summary Wrong threshold value in CounterMonitor with offset and modulus.
|
|
28 |
* @author Luis-Miguel Alventosa
|
|
29 |
* @run clean CounterMonitorThresholdTest
|
|
30 |
* @run build CounterMonitorThresholdTest
|
|
31 |
* @run main CounterMonitorThresholdTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.lang.management.ManagementFactory;
|
|
35 |
import javax.management.MBeanServer;
|
|
36 |
import javax.management.MBeanServerInvocationHandler;
|
|
37 |
import javax.management.Notification;
|
|
38 |
import javax.management.NotificationEmitter;
|
|
39 |
import javax.management.NotificationListener;
|
|
40 |
import javax.management.ObjectName;
|
|
41 |
import javax.management.monitor.CounterMonitor;
|
|
42 |
import javax.management.monitor.CounterMonitorMBean;
|
|
43 |
import javax.management.monitor.MonitorNotification;
|
|
44 |
|
|
45 |
public class CounterMonitorThresholdTest {
|
|
46 |
|
|
47 |
// Offset = 1
|
|
48 |
private static int counter1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
|
|
49 |
private static int derivedGauge1[] = { 0, 1, 2, 3, 4, 4, 5, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
|
|
50 |
private static int threshold1[] = { 1, 2, 3, 4, 5, 5, 1, 1, 1, 2, 3, 4, 5, 1, 1, 2 };
|
|
51 |
|
|
52 |
// Offset = 3
|
|
53 |
private static int counter2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
|
|
54 |
private static int derivedGauge2[] = { 0, 1, 2, 3, 3, 4, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1 };
|
|
55 |
private static int threshold2[] = { 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 4 };
|
|
56 |
|
|
57 |
public interface TestMBean {
|
|
58 |
public int getCounter();
|
|
59 |
public void setCounter(int count);
|
|
60 |
}
|
|
61 |
|
|
62 |
public static class Test implements TestMBean {
|
|
63 |
public int getCounter() {
|
|
64 |
return count;
|
|
65 |
}
|
|
66 |
public void setCounter(int count) {
|
|
67 |
this.count = count;
|
|
68 |
}
|
|
69 |
private int count = 0;
|
|
70 |
}
|
|
71 |
|
|
72 |
public static class Listener implements NotificationListener {
|
|
73 |
public void handleNotification(Notification n, Object hb) {
|
|
74 |
System.out.println("\tReceived notification: " + n.getType());
|
|
75 |
if (n instanceof MonitorNotification) {
|
|
76 |
MonitorNotification mn = (MonitorNotification) n;
|
|
77 |
System.out.println("\tSource: " +
|
|
78 |
mn.getSource());
|
|
79 |
System.out.println("\tType: " +
|
|
80 |
mn.getType());
|
|
81 |
System.out.println("\tTimeStamp: " +
|
|
82 |
mn.getTimeStamp());
|
|
83 |
System.out.println("\tObservedObject: " +
|
|
84 |
mn.getObservedObject());
|
|
85 |
System.out.println("\tObservedAttribute: " +
|
|
86 |
mn.getObservedAttribute());
|
|
87 |
System.out.println("\tDerivedGauge: " +
|
|
88 |
mn.getDerivedGauge());
|
|
89 |
System.out.println("\tTrigger: " +
|
|
90 |
mn.getTrigger());
|
|
91 |
}
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
public static void runTest(int offset,
|
|
96 |
int counter[],
|
|
97 |
int derivedGauge[],
|
|
98 |
int threshold[]) throws Exception {
|
|
99 |
// Retrieve the platform MBean server
|
|
100 |
//
|
|
101 |
System.out.println("\nRetrieve the platform MBean server");
|
|
102 |
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
|
|
103 |
String domain = mbs.getDefaultDomain();
|
|
104 |
|
|
105 |
// Create and register TestMBean
|
|
106 |
//
|
|
107 |
ObjectName name =
|
|
108 |
new ObjectName(domain +
|
|
109 |
":type=" + Test.class.getName() +
|
|
110 |
",offset=" + offset);
|
|
111 |
mbs.createMBean(Test.class.getName(), name);
|
|
112 |
TestMBean mbean = (TestMBean)
|
|
113 |
MBeanServerInvocationHandler.newProxyInstance(
|
|
114 |
mbs, name, TestMBean.class, false);
|
|
115 |
|
|
116 |
// Create and register CounterMonitorMBean
|
|
117 |
//
|
|
118 |
ObjectName cmn =
|
|
119 |
new ObjectName(domain +
|
|
120 |
":type=" + CounterMonitor.class.getName() +
|
|
121 |
",offset=" + offset);
|
|
122 |
CounterMonitor m = new CounterMonitor();
|
|
123 |
mbs.registerMBean(m, cmn);
|
|
124 |
CounterMonitorMBean cm = (CounterMonitorMBean)
|
|
125 |
MBeanServerInvocationHandler.newProxyInstance(
|
|
126 |
mbs, cmn, CounterMonitorMBean.class, true);
|
|
127 |
((NotificationEmitter) cm).addNotificationListener(
|
|
128 |
new Listener(), null, null);
|
|
129 |
cm.addObservedObject(name);
|
|
130 |
cm.setObservedAttribute("Counter");
|
|
131 |
cm.setGranularityPeriod(100);
|
|
132 |
cm.setInitThreshold(1);
|
|
133 |
cm.setOffset(offset);
|
|
134 |
cm.setModulus(5);
|
|
135 |
cm.setNotify(true);
|
|
136 |
|
|
137 |
// Start the monitor
|
|
138 |
//
|
|
139 |
System.out.println("\nStart monitoring...");
|
|
140 |
cm.start();
|
|
141 |
|
|
142 |
// Play with counter
|
|
143 |
//
|
|
144 |
for (int i = 0; i < counter.length; i++) {
|
|
145 |
mbean.setCounter(counter[i]);
|
|
146 |
System.out.println("\nCounter = " + mbean.getCounter());
|
|
147 |
Thread.sleep(300);
|
|
148 |
Integer derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
|
|
149 |
System.out.println("Derived Gauge = " + derivedGaugeValue);
|
|
150 |
if (derivedGaugeValue.intValue() != derivedGauge[i]) {
|
|
151 |
System.out.println("Wrong derived gauge! Current value = " +
|
|
152 |
derivedGaugeValue + " Expected value = " + derivedGauge[i]);
|
|
153 |
System.out.println("\nStop monitoring...");
|
|
154 |
cm.stop();
|
|
155 |
throw new IllegalArgumentException("wrong derived gauge");
|
|
156 |
}
|
|
157 |
Number thresholdValue = cm.getThreshold(name);
|
|
158 |
System.out.println("Threshold = " + thresholdValue);
|
|
159 |
if (thresholdValue.intValue() != threshold[i]) {
|
|
160 |
System.out.println("Wrong threshold! Current value = " +
|
|
161 |
thresholdValue + " Expected value = " + threshold[i]);
|
|
162 |
System.out.println("\nStop monitoring...");
|
|
163 |
cm.stop();
|
|
164 |
throw new IllegalArgumentException("wrong threshold");
|
|
165 |
}
|
|
166 |
Thread.sleep(300);
|
|
167 |
}
|
|
168 |
|
|
169 |
// Stop the monitor
|
|
170 |
//
|
|
171 |
System.out.println("\nStop monitoring...");
|
|
172 |
cm.stop();
|
|
173 |
}
|
|
174 |
|
|
175 |
public static void main(String[] args) throws Exception {
|
|
176 |
runTest(1, counter1, derivedGauge1, threshold1);
|
|
177 |
runTest(3, counter2, derivedGauge2, threshold2);
|
|
178 |
}
|
|
179 |
}
|