Search in sources :

Example 1 with Type

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());
}
Also used : Expectations(org.jmock.Expectations) ArrayList(java.util.ArrayList) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) Statistics(org.apache.geode.Statistics) StatisticsType(org.apache.geode.StatisticsType) Type(org.apache.geode.internal.statistics.StatisticsNotification.Type) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 Statistics (org.apache.geode.Statistics)1 StatisticsType (org.apache.geode.StatisticsType)1 MainWithChildrenRollingFileHandler (org.apache.geode.internal.io.MainWithChildrenRollingFileHandler)1 Type (org.apache.geode.internal.statistics.StatisticsNotification.Type)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1 Expectations (org.jmock.Expectations)1 Test (org.junit.Test)1