aboutsummaryrefslogtreecommitdiffstats
path: root/container-disc/src/test/java/com/yahoo/container/jdisc/metric/MetricUpdaterTest.java
blob: e9e04eab3b4b926e1be7e9574761f4d412681349 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.metric;

import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.statistics.ContainerWatchdogMetrics;
import org.junit.Test;

import java.lang.management.ManagementFactory;
import java.time.Duration;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * @author bjorncs 
 */
public class MetricUpdaterTest {
    
    @Test
    public void metrics_are_updated_in_scheduler_cycle() throws InterruptedException {
        int gcCount = ManagementFactory.getGarbageCollectorMXBeans().size();

        Metric metric = mock(Metric.class);
        ContainerWatchdogMetrics containerWatchdogMetrics = mock(ContainerWatchdogMetrics.class);
        new MetricUpdater(new MockScheduler(), metric, containerWatchdogMetrics);
        verify(containerWatchdogMetrics, times(1)).emitMetrics(any());
        verify(metric, times(8 + 2 * gcCount)).set(anyString(), any(), any());
    }

    private static class MockScheduler implements MetricUpdater.Scheduler {
        @Override
        public void schedule(Runnable runnable, Duration frequency) {
            runnable.run();
        }
        @Override
        public void cancel() {}
    }

}