1 /*
2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
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  *
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.
22  */
23 
24 /*
25  * @test
26  * @bug 6229368 8025207
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 {
getCounter()58         public int getCounter();
setCounter(int count)59         public void setCounter(int count);
60     }
61 
62     public static class Test implements TestMBean {
getCounter()63         public int getCounter() {
64             return count;
65         }
setCounter(int count)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 {
handleNotification(Notification n, Object hb)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 
runTest(int offset, int counter[], int derivedGauge[], int threshold[])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             Integer derivedGaugeValue;
148             // either pass or test timeout (killed by test harness)
149             // see 8025207
150             do {
151                 Thread.sleep(150);
152                 derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
153             } while (derivedGaugeValue.intValue() != derivedGauge[i]);
154 
155             Number thresholdValue = cm.getThreshold(name);
156             System.out.println("Threshold = " + thresholdValue);
157             if (thresholdValue.intValue() != threshold[i]) {
158                 System.out.println("Wrong threshold! Current value = " +
159                     thresholdValue + " Expected value = " + threshold[i]);
160                 System.out.println("\nStop monitoring...");
161                 cm.stop();
162                 throw new IllegalArgumentException("wrong threshold");
163             }
164         }
165 
166         // Stop the monitor
167         //
168         System.out.println("\nStop monitoring...");
169         cm.stop();
170     }
171 
main(String[] args)172     public static void main(String[] args) throws Exception {
173         runTest(1, counter1, derivedGauge1, threshold1);
174         runTest(3, counter2, derivedGauge2, threshold2);
175     }
176 }
177