}
public void testIntegerCounterWithOffset() throws Exception
{
MBeanServer server = newMBeanServer();
CounterMonitor monitor = (CounterMonitor)createMonitor();
server.registerMBean(monitor, ObjectName.getInstance(":service=monitor"));
Counter counter = new Counter();
ObjectName counterName = ObjectName.getInstance(":mbean=counter");
server.registerMBean(counter, counterName);
long period = 1000;
monitor.addObservedObject(counterName);
monitor.setGranularityPeriod(period);
monitor.setObservedAttribute("IntegerCounter");
Integer initThreshold = new Integer(3);
monitor.setInitThreshold(initThreshold);
monitor.setNotify(true);
Integer offset = new Integer(5);
monitor.setOffset(offset);
// No modulus
counter.setIntegerCounter(initThreshold.intValue() - 1);
final MutableInteger times = new MutableInteger(0);
final MutableObject holder = new MutableObject(null);
monitor.addNotificationListener(new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
times.set(times.get() + 1);
holder.set(notification);
}
}, null, null);
monitor.start();
try
{
// Below threshold, no notifications should be sent
sleep(period * 3);
assertEquals(times.get(), 0);
assertNull(holder.get());
// Above threshold, just one notification should be sent
counter.setIntegerCounter(initThreshold.intValue() + 1);
sleep(period * 3);
assertEquals(times.get(), 1);
MonitorNotification notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
// The threshold should have offset
Number threshold = monitor.getThreshold(counterName);
assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue());
times.set(0);
holder.set(null);
sleep(period * 3);
assertEquals(times.get(), 0);
// Above threshold by more than 1 offset
counter.setIntegerCounter(initThreshold.intValue() + offset.intValue() * 2 + 1);
sleep(period * 3);
assertEquals(times.get(), 1);
notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
// The threshold should have offset correctly
threshold = monitor.getThreshold(counterName);
assertEquals(threshold.intValue(), monitor.getInitThreshold().intValue() + offset.intValue() * 3);
times.set(0);
holder.set(null);
sleep(period * 3);
assertEquals(times.get(), 0);
}
finally
{
monitor.stop();
}
}