aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java
blob: d8743d063fb37556a27aca9d281f144db9a0959e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics.simple.jdisc;

import com.yahoo.container.jdisc.state.CountMetric;
import com.yahoo.container.jdisc.state.GaugeMetric;
import com.yahoo.container.jdisc.state.MetricDimensions;
import com.yahoo.container.jdisc.state.MetricSet;
import com.yahoo.container.jdisc.state.MetricSnapshot;
import com.yahoo.container.jdisc.state.MetricValue;
import com.yahoo.metrics.simple.Bucket;
import com.yahoo.metrics.simple.Identifier;
import com.yahoo.metrics.simple.MetricReceiver;
import com.yahoo.metrics.simple.Point;
import com.yahoo.metrics.simple.UntypedMetric;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

/**
 * @author bratseth
 */
public class SnapshotConverterTest {

    @Test
    void testPointConversion() {
        MetricDimensions a = SnapshotConverter.convert(new Point(Collections.emptyMap()));
        MetricDimensions b = SnapshotConverter.convert(new Point(new HashMap<>(0)));
        MetricDimensions c = SnapshotConverter.convert((Point) null);
        assertEquals(a.hashCode(), b.hashCode());
        assertEquals(a, b);
        assertEquals(a.hashCode(), c.hashCode());
        assertEquals(a, c);
        assertEquals(b.hashCode(), c.hashCode());
        assertEquals(b, c);
    }

    @Test
    void testConversion() {
        MetricReceiver mock = new MetricReceiver.MockReceiver();
        mock.declareCounter("foo").add(1);
        mock.declareGauge("quuux").sample(42.25);
        mock.declareCounter("bar", new Point(new HashMap<String, String>())).add(4);

        MetricSnapshot snapshot = new SnapshotConverter(mock.getSnapshot()).convert();

        for (Map.Entry<MetricDimensions, MetricSet> entry : snapshot) {
            for (Map.Entry<String, String> dv : entry.getKey()) {
                fail();
            }

            int cnt = 0;
            for (Map.Entry<String, MetricValue> mv : entry.getValue()) {
                ++cnt;
                if ("foo".equals(mv.getKey())) {
                    assertTrue(mv.getValue() instanceof CountMetric);
                    assertEquals(1, ((CountMetric) mv.getValue()).getCount());
                } else if ("bar".equals(mv.getKey())) {
                    assertTrue(mv.getValue() instanceof CountMetric);
                    assertEquals(4, ((CountMetric) mv.getValue()).getCount());
                } else if ("quuux".equals(mv.getKey())) {
                    assertTrue(mv.getValue() instanceof GaugeMetric);
                    assertEquals(42.25, ((GaugeMetric) mv.getValue()).getLast(), 0.001);
                    assertEquals(1, ((GaugeMetric) mv.getValue()).getCount());
                } else {
                    fail();
                }
            }
            assertEquals(3, cnt);
        }
    }

}