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 2017 Yahoo Holdings. 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.Assert.*;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.yahoo.metrics.simple.UntypedMetric.AssumedType;
/**
* Functional test for point persistence layer.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class DimensionsCacheTest {
private static final int POINTS_TO_KEEP = 3;
DimensionCache cache;
@Before
public void setUp() throws Exception {
cache = new DimensionCache(POINTS_TO_KEEP);
}
@After
public void tearDown() throws Exception {
cache = null;
}
@Test
public 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("Kept newest measurement when padding points.", newestFound);
}
@Test
public 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
public 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
public 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
public 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);
}
}
|