aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/metrics/simple/DimensionsCacheTest.java
blob: 33695e846c5fbf7c686afe2d2d8a6693e8b62a1f (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright Vespa.ai. 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;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.yahoo.metrics.simple.UntypedMetric.AssumedType;

/**
 * Functional test for point persistence layer.
 *
 * @author Steinar Knutsen
 */
public class DimensionsCacheTest {

    private static final int POINTS_TO_KEEP = 3;
    DimensionCache cache;

    @BeforeEach
    public void setUp() throws Exception {
        cache = new DimensionCache(POINTS_TO_KEEP);
    }

    @AfterEach
    public void tearDown() throws Exception {
        cache = null;
    }

    @Test
    final void smokeTest() {
        String metricName = "testMetric";
        Bucket first = new Bucket();
        for (int i = 0; i < 4; ++i) {
            populateSingleValue(metricName, first, i);
        }
        cache.updateDimensionPersistence(null, first);
        Bucket second = new Bucket();
        final int newest = 42;
        populateSingleValue(metricName, second, newest);
        cache.updateDimensionPersistence(first, second);
        assertEquals(POINTS_TO_KEEP, second.getValuesForMetric(metricName).size());
        boolean newestFound = false;
        for (Entry<Point, UntypedMetric> x : second.getValuesForMetric(metricName)) {
            if (x.getValue().getLast() == newest) {
                newestFound = true;
            }
        }
        assertTrue(newestFound, "Kept newest measurement when padding points.");
    }

    @Test
    final void testNoBoomWithEmptyBuckets() {
        Bucket check = new Bucket();
        cache.updateDimensionPersistence(null, new Bucket());
        cache.updateDimensionPersistence(null, new Bucket());
        cache.updateDimensionPersistence(new Bucket(), check);
        assertEquals(0, check.entrySet().size());
    }

    @Test
    final void testUpdateWithNullThenDataThenData() {
        Bucket first = new Bucket();
        populateDimensionLessValue("one", first, 2);
        cache.updateDimensionPersistence(null, first);
        Bucket second = new Bucket();
        populateDimensionLessValue("other", second, 3);
        cache.updateDimensionPersistence(first, second);
        Collection<String> names = second.getAllMetricNames();
        assertEquals(2, names.size());
        assertTrue(names.contains("one"));
        assertTrue(names.contains("other"));
    }

    @Test
    final void requireThatOldDataIsForgotten() {
        Bucket first = new Bucket(); // "now" as timestamp
        populateDimensionLessValue("one", first, 2);
        cache.updateDimensionPersistence(first, new Bucket());
        Bucket second = new Bucket(17, 42); // really old timestamp
        populateDimensionLessValue("other", second, 3);
        Bucket third = new Bucket();
        populateDimensionLessValue("two", third, 4);
        cache.updateDimensionPersistence(second, third);
        Collection<String> names = third.getAllMetricNames();
        assertEquals(2, names.size());
        assertTrue(names.contains("one"));
        assertTrue(names.contains("two"));
    }

    @Test
    final void testUpdateWithNullThenDataThenNoDataThenData() {
        Bucket first = new Bucket();
        Bucket second = new Bucket();
        populateDimensionLessValue("first", first, 1.0d);
        populateDimensionLessValue("second", second, 2.0d);
        cache.updateDimensionPersistence(null, first);
        cache.updateDimensionPersistence(first, new Bucket());
        cache.updateDimensionPersistence(new Bucket(), second);
        assertEquals(2, second.entrySet().size());
        assertTrue(second.getAllMetricNames().contains("first"));
        assertTrue(second.getAllMetricNames().contains("second"));
    }

    private void populateDimensionLessValue(String metricName, Bucket bucket, double x) {
        Identifier id = new Identifier(metricName, null);
        Sample wrappedX = new Sample(new Measurement(Double.valueOf(x)), id, AssumedType.GAUGE);
        bucket.put(wrappedX);
    }

    private void populateSingleValue(String metricName, Bucket bucket, int i) {
        Map<String, Integer> m = new TreeMap<>();
        m.put(String.valueOf(i), Integer.valueOf(i));
        Point p = new Point(m);
        Identifier id = new Identifier(metricName, p);
        Sample x = new Sample(new Measurement(Double.valueOf(i)), id, AssumedType.GAUGE);
        bucket.put(x);
    }

}