@Test
  public void testValueMonitorListener() throws Exception {
    long startTime = System.currentTimeMillis();
    TestStatisticsManager manager =
        new TestStatisticsManager(1, "ValueMonitorIntegrationTest", startTime);
    StatisticsSampler sampler = new TestStatisticsSampler(manager);
    SampleCollector sampleCollector = new SampleCollector(sampler);
    sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(),
        new MainWithChildrenRollingFileHandler());

    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);
    st1_1.incDouble("double_counter_1", 1000.0001);
    st1_1.incInt("int_counter_2", 2);
    st1_1.incLong("long_counter_3", 3333333333L);

    Statistics st1_2 = manager.createAtomicStatistics(ST1, "st1_2_text", 2);
    st1_2.incDouble("double_counter_1", 2000.0002);
    st1_2.incInt("int_counter_2", 3);
    st1_2.incLong("long_counter_3", 4444444444L);

    List<StatisticsNotification> notifications = new ArrayList<>();
    StatisticsListener listener = notifications::add;

    ValueMonitor monitor = new ValueMonitor().addStatistics(st1_1);
    monitor.addListener(listener);
    assertThat(notifications.isEmpty()).isTrue();

    long timeStamp = NanoTimer.getTime();
    sampleCollector.sample(timeStamp);
    await()
        .until(() -> notifications.size() > 0);
    assertThat(notifications.size()).isEqualTo(1);

    StatisticsNotification notification = notifications.remove(0);
    assertThat(notification.getType()).isEqualTo(StatisticsNotification.Type.VALUE_CHANGED);

    // 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);
    await()
        .until(() -> notifications.size() > 0);
    assertThat(notifications.size()).isEqualTo(1);
    notification = notifications.remove(0);
    assertThat(notification.getType()).isEqualTo(StatisticsNotification.Type.VALUE_CHANGED);

    Map<String, Number> expectedValues = new HashMap<>();
    expectedValues.put("double_counter_1", 1001.1001);
    expectedValues.put("int_counter_2", 4L);
    expectedValues.put("long_counter_3", 3333333336L);
    int statCount = assertStatisticsNotification(notification, expectedValues);
    assertThat(statCount).isEqualTo(3);

    // validate no notification occurs when no stats are updated
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    await()
        .until(() -> notifications.size() == 0);
    assertThat(notifications.isEmpty()).isTrue();

    // 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);
    await()
        .until(() -> notifications.size() == 0);
    assertThat(notifications.isEmpty()).isTrue();

    // validate notification only contains stats added to monitor
    st1_1.incInt("int_counter_2", 100);
    st1_2.incInt("int_counter_2", 200);
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    await()
        .until(() -> notifications.size() > 0);
    assertThat(notifications.size()).isEqualTo(1);
    notification = notifications.remove(0);
    assertThat(notification.getType()).isEqualTo(StatisticsNotification.Type.VALUE_CHANGED);
    expectedValues = new HashMap<>();
    expectedValues.put("int_counter_2", 104L);
    statCount = assertStatisticsNotification(notification, expectedValues);
    assertThat(statCount).isEqualTo(1);
  }
Integration test for the SampleCollector class.

@since GemFire 7.0
geode-core/src/integrationTest/java/org/apache/geode/internal/statistics/ValueMonitorIntegrationTest.java