aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java
blob: dd949627f30aa166d82c1db8c1e1f7d28baea145 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics.simple;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collection;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.yahoo.metrics.simple.jdisc.JdiscMetricsFactory;
import com.yahoo.metrics.simple.jdisc.SimpleMetricConsumer;

/**
 * Functional test for simple metric implementation.
 *
 * @author Steinar Knutsen
 */
public class MetricsTest extends UnitTestSetup {

    SimpleMetricConsumer metricApi;

    @BeforeEach
    public void setUp() throws Exception {
        super.init();
        metricApi  = (SimpleMetricConsumer) new JdiscMetricsFactory(metricManager.get()).newInstance();
    }

    @AfterEach
    public void tearDown() throws Exception {
        super.fini();
    }

    @Test
    final void smokeTest() throws InterruptedException {
        final String metricName = "testMetric";
        metricApi.set(metricName, 1.0d, null);
        updater.gotData.await(10, TimeUnit.SECONDS);
        Bucket s = getUpdatedSnapshot();
        Collection<Entry<Point, UntypedMetric>> values = s.getValuesForMetric(metricName);
        assertEquals(1, values.size());
        Entry<Point, UntypedMetric> value = values.iterator().next();
        assertEquals(Point.emptyPoint(), value.getKey());
        assertEquals(1.0d, value.getValue().getLast(), 0.0d); // using number exactly expressible as doubles
        assertEquals(1L, value.getValue().getCount());
    }

    @Test
    final void testRedefinition() {
        MetricReceiver r = metricManager.get();
        final String metricName = "gah";
        r.addMetricDefinition(metricName, new MetricSettings.Builder().build());
        r.addMetricDefinition(metricName, new MetricSettings.Builder().histogram(true).build());
        assertTrue(r.getMetricDefinition(metricName).isHistogram());
    }

}