com.gemstone.gemfire.internal.statistics.ValueMonitorJUnitTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.internal.statistics;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import com.gemstone.gemfire.StatisticDescriptor;
import com.gemstone.gemfire.Statistics;
import com.gemstone.gemfire.StatisticsType;
import com.gemstone.gemfire.i18n.LogWriterI18n;
import com.gemstone.gemfire.internal.LogWriterImpl;
import com.gemstone.gemfire.internal.NanoTimer;
import com.gemstone.gemfire.internal.PureLogWriter;
import com.gemstone.gemfire.internal.StatisticsManager;
import com.gemstone.gemfire.internal.statistics.StatisticsNotification.Type;
import io.snappydata.test.dunit.DistributedTestBase;
import io.snappydata.test.dunit.DistributedTestBase.WaitCriterion;
import junit.framework.TestCase;
/**
* Integration test for the SampleCollector class.
*
* @author Kirk Lund
* @since 7.0
*/
public class ValueMonitorJUnitTest extends TestCase {
private Mockery mockContext;
public ValueMonitorJUnitTest(String name) {
super(name);
}
public void setUp() throws Exception {
super.setUp();
this.mockContext = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
}
public void tearDown() throws Exception {
super.tearDown();
this.mockContext.assertIsSatisfied();
this.mockContext = null;
}
public void testAddRemoveListener() throws Exception {
final long startTime = System.currentTimeMillis();
final LogWriterI18n log = new PureLogWriter(LogWriterImpl.levelNameToCode("config"));
final List statsList = new ArrayList();
final StatisticsManager mockStatisticsManager = this.mockContext.mock(StatisticsManager.class, "testAddRemoveListener$StatisticsManager");
this.mockContext.checking(new Expectations() {{
allowing(mockStatisticsManager).getName();
will(returnValue("mockStatisticsManager"));
allowing(mockStatisticsManager).getId();
will(returnValue(1));
allowing(mockStatisticsManager).getStartTime();
will(returnValue(startTime));
allowing(mockStatisticsManager).getLogWriterI18n();
will(returnValue(log));
allowing(mockStatisticsManager).getStatListModCount();
will(returnValue(0));
allowing(mockStatisticsManager).getStatsList();
will(returnValue(statsList));
}});
final StatisticsSampler mockStatisticsSampler = this.mockContext.mock(StatisticsSampler.class, "testAddRemoveListener$StatisticsSampler");
this.mockContext.checking(new Expectations() {{
allowing(mockStatisticsSampler).getStatisticsModCount();
will(returnValue(0));
allowing(mockStatisticsSampler).getStatistics();
will(returnValue(new Statistics[]{}));
allowing(mockStatisticsSampler).getLogger();
will(returnValue(log));
}});
final StatArchiveHandlerConfig mockStatArchiveHandlerConfig = this.mockContext.mock(StatArchiveHandlerConfig.class, "testAddRemoveListener$StatArchiveHandlerConfig");
this.mockContext.checking(new Expectations() {{
allowing(mockStatArchiveHandlerConfig).getArchiveFileName();
will(returnValue(new File("")));
allowing(mockStatArchiveHandlerConfig).getArchiveFileSizeLimit();
will(returnValue(0));
allowing(mockStatArchiveHandlerConfig).getArchiveDiskSpaceLimit();
will(returnValue(0));
allowing(mockStatArchiveHandlerConfig).getSystemId();
will(returnValue(1));
allowing(mockStatArchiveHandlerConfig).getSystemStartTime();
will(returnValue(startTime));
allowing(mockStatArchiveHandlerConfig).getSystemDirectoryPath();
will(returnValue(""));
allowing(mockStatArchiveHandlerConfig).getProductDescription();
will(returnValue("testAddRemoveListener"));
}});
// need a real SampleCollector for this test or the monitor can't get the handler
SampleCollector sampleCollector = new SampleCollector(mockStatisticsSampler);
sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime());
final List notifications = new ArrayList();
StatisticsListener listener = new StatisticsListener() {
@Override
public void handleNotification(StatisticsNotification notification) {
notifications.add(notification);
}
};
ValueMonitor monitor = new ValueMonitor();
long timeStamp = System.currentTimeMillis();
Type type = Type.VALUE_CHANGED;
Number value = 43;
StatisticsNotification notification = createStatisticsNotification(timeStamp, type, value);
monitor.notifyListeners(notification);
assertTrue(notifications.isEmpty());
monitor.addListener(listener);
monitor.notifyListeners(notification);
assertEquals(1, notifications.size());
notification = notifications.remove(0);
assertNotNull(notification);
assertEquals(timeStamp, notification.getTimeStamp());
assertEquals(type, notification.getType());
StatisticId statId = createStatisticId(null, null);
assertEquals(value, notification.getValue(statId));
monitor.removeListener(listener);
monitor.notifyListeners(notification);
assertTrue(notifications.isEmpty());
}
public void testValueMonitorListener() throws Exception {
final long startTime = System.currentTimeMillis();
final LogWriterI18n log = new PureLogWriter(LogWriterImpl.levelNameToCode("config"));
TestStatisticsManager manager = new TestStatisticsManager(
1,
"ValueMonitorJUnitTest",
startTime,
log);
StatisticsSampler sampler = new TestStatisticsSampler(manager);
assertEquals(log, sampler.getLogger());
final StatArchiveHandlerConfig mockStatArchiveHandlerConfig =
this.mockContext.mock(StatArchiveHandlerConfig.class, "testFoo$StatArchiveHandlerConfig");
this.mockContext.checking(new Expectations() {{
allowing(mockStatArchiveHandlerConfig).getArchiveFileName();
will(returnValue(new File("")));
allowing(mockStatArchiveHandlerConfig).getArchiveFileSizeLimit();
will(returnValue(0));
allowing(mockStatArchiveHandlerConfig).getArchiveDiskSpaceLimit();
will(returnValue(0));
allowing(mockStatArchiveHandlerConfig).getSystemId();
will(returnValue(1));
allowing(mockStatArchiveHandlerConfig).getSystemStartTime();
will(returnValue(startTime));
allowing(mockStatArchiveHandlerConfig).getSystemDirectoryPath();
will(returnValue(""));
allowing(mockStatArchiveHandlerConfig).getProductDescription();
will(returnValue("testFoo"));
}});
SampleCollector sampleCollector = new SampleCollector(sampler);
sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime());
assertEquals(log, sampleCollector.getLogWriterI18n());
StatisticDescriptor[] statsST1 = new StatisticDescriptor[] {
manager.createDoubleCounter("double_counter_1", "double_counter_1_desc", "double_counter_1_units"),
manager.createIntCounter( "int_counter_2", "int_counter_2_desc", "int_counter_2_units"),
manager.createLongCounter( "long_counter_3", "long_counter_3_desc", "long_counter_3_units")
};
StatisticsType ST1 = manager.createType("ST1_name", "ST1_desc", statsST1);
Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1_text", 1);
Statistics st1_2 = manager.createAtomicStatistics(ST1, "st1_2_text", 2);
st1_1.incDouble("double_counter_1", 1000.0001);
st1_1.incInt("int_counter_2", 2);
st1_1.incLong("long_counter_3", 3333333333L);
st1_2.incDouble("double_counter_1", 2000.0002);
st1_2.incInt("int_counter_2", 3);
st1_2.incLong("long_counter_3", 4444444444L);
final List notifications = new ArrayList();
StatisticsListener listener = new StatisticsListener() {
@Override
public void handleNotification(StatisticsNotification notification) {
notifications.add(notification);
}
};
ValueMonitor monitor = new ValueMonitor().addStatistics(st1_1);
monitor.addListener(listener);
assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
long timeStamp = NanoTimer.getTime();
sampleCollector.sample(timeStamp);
waitForNotification(notifications, 2*1000, 10, false);
assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
StatisticsNotification notification = notifications.remove(0);
assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
// validate 1 notification occurs with all 3 stats of st1_1
st1_1.incDouble("double_counter_1", 1.1);
st1_1.incInt("int_counter_2", 2);
st1_1.incLong("long_counter_3", 3);
timeStamp += NanoTimer.millisToNanos(1000);
sampleCollector.sample(timeStamp);
waitForNotification(notifications, 2*1000, 10, false);
assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
notification = notifications.remove(0);
assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
int statCount = 0;
Map expectedValues = new HashMap();
expectedValues.put("double_counter_1", 1001.1001);
expectedValues.put("int_counter_2", 4);
expectedValues.put("long_counter_3", 3333333336L);
for (StatisticId statId : notification) {
Number value = expectedValues.remove(statId.getStatisticDescriptor().getName());
assertNotNull(value);
assertEquals(value, notification.getValue(statId));
statCount++;
}
assertEquals(3, statCount);
// validate no notification occurs when no stats are updated
timeStamp += NanoTimer.millisToNanos(1000);
sampleCollector.sample(timeStamp);
waitForNotification(notifications, 2*1000, 10, false);
assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
// validate no notification occurs when only other stats are updated
st1_2.incDouble("double_counter_1", 3.3);
st1_2.incInt("int_counter_2", 1);
st1_2.incLong("long_counter_3", 2);
timeStamp += NanoTimer.millisToNanos(1000);
sampleCollector.sample(timeStamp);
waitForNotification(notifications, 2*1000, 10, false);
assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
// validate notification only contains stats added to monitor
st1_1.incInt("int_counter_2", 100);
st1_2.incInt("int_counter_2", 200);
assertEquals(2, sampleCollector.currentHandlersForTesting().size());
timeStamp += NanoTimer.millisToNanos(1000);
sampleCollector.sample(timeStamp);
waitForNotification(notifications, 2*1000, 10, false);
assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
notification = notifications.remove(0);
assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
statCount = 0;
expectedValues = new HashMap();
expectedValues.put("int_counter_2", 104);
for (StatisticId statId : notification) {
Number value = expectedValues.remove(statId.getStatisticDescriptor().getName());
assertNotNull(value);
assertEquals(value, notification.getValue(statId));
statCount++;
}
assertEquals(1, statCount);
}
protected StatisticId createStatisticId(
final StatisticDescriptor descriptor, final Statistics stats) {
return new StatisticId() {
@Override
public StatisticDescriptor getStatisticDescriptor() {
return descriptor;
}
@Override
public Statistics getStatistics() {
return stats;
}
};
}
protected StatisticsNotification createStatisticsNotification(
final long timeStamp, final Type type, final Number value) {
return new StatisticsNotification() {
@Override
public long getTimeStamp() {
return timeStamp;
}
@Override
public Type getType() {
return type;
}
@Override
public Iterator iterator() {
return null; // TODO
}
@Override
public Iterator iterator(StatisticDescriptor statDesc) {
return null; // TODO
}
@Override
public Iterator iterator(Statistics statistics) {
return null; // TODO
}
@Override
public Iterator iterator(StatisticsType statisticsType) {
return null; // TODO
}
@Override
public Number getValue(StatisticId statId) throws StatisticNotFoundException {
return value;
}
};
}
private static void waitForNotification(final List notifications, long ms, long interval, boolean throwOnTimeout) {
WaitCriterion wc = new WaitCriterion() {
public boolean done() {
return notifications.size() > 0;
}
public String description() {
return "waiting for notification";
}
};
DistributedTestBase.waitForCriterion(wc, ms, interval, throwOnTimeout);
}
}