use of org.apache.geode.internal.statistics.StatisticsNotification.Type in project geode by apache.
the class ValueMonitorIntegrationTest method testAddRemoveListener.
@Test
public void testAddRemoveListener() throws Exception {
long startTime = System.currentTimeMillis();
List<Statistics> statsList = new ArrayList<Statistics>();
StatisticsManager mockStatisticsManager = this.mockContext.mock(StatisticsManager.class, testName.getMethodName() + "$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).getStatListModCount();
will(returnValue(0));
allowing(mockStatisticsManager).getStatsList();
will(returnValue(statsList));
}
});
StatisticsSampler mockStatisticsSampler = this.mockContext.mock(StatisticsSampler.class, testName.getMethodName() + "$StatisticsSampler");
this.mockContext.checking(new Expectations() {
{
allowing(mockStatisticsSampler).getStatisticsModCount();
will(returnValue(0));
allowing(mockStatisticsSampler).getStatistics();
will(returnValue(new Statistics[] {}));
}
});
StatArchiveHandlerConfig mockStatArchiveHandlerConfig = this.mockContext.mock(StatArchiveHandlerConfig.class, testName.getMethodName() + "$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(), new MainWithChildrenRollingFileHandler());
List<StatisticsNotification> notifications = new ArrayList<>();
StatisticsListener listener = (final 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());
}
Aggregations