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
|
// Copyright 2016 Yahoo Inc. 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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.yahoo.metrics.ManagerConfig;
/**
* Functional tests for gauges.
*
* @author steinar
*/
public class GaugeTest {
MetricReceiver receiver;
@Before
public void setUp() throws Exception {
receiver = new MetricReceiver.MockReceiver();
}
@After
public void tearDown() throws Exception {
receiver = null;
}
@Test
public final void testSampleDouble() throws InterruptedException {
final String metricName = "unitTestGauge";
Gauge g = receiver.declareGauge(metricName);
g.sample(1.0d);
Bucket b = receiver.getSnapshot();
final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName();
assertEquals(1, valuesByMetricName.size());
List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName);
assertEquals(1, x.size());
assertNull(x.get(0).getKey());
assertEquals(1L, x.get(0).getValue().getCount());
assertEquals(1.0d, x.get(0).getValue().getLast(), 0.0d);
}
@Test
public final void testSampleDoublePoint() throws InterruptedException {
final String metricName = "unitTestGauge";
Point p = receiver.pointBuilder().set("x", 2L).set("y", 3.0d).set("z", "5").build();
Gauge g = receiver.declareGauge(metricName, p);
g.sample(Math.E, g.builder().set("x", 7).set("_y", 11.0d).set("Z", "13").build());
Bucket b = receiver.getSnapshot();
final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName();
assertEquals(1, valuesByMetricName.size());
List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName);
assertEquals(1, x.size());
Point actual = x.get(0).getKey();
assertEquals(5, actual.dimensionality());
List<String> dimensions = actual.dimensions();
List<Value> location = actual.location();
assertEquals(dimensions.size(), location.size());
Iterator<String> i0 = dimensions.iterator();
Iterator<Value> i1 = location.iterator();
Map<String, Value> asMap = new HashMap<>();
while (i0.hasNext() && i1.hasNext()) {
asMap.put(i0.next(), i1.next());
}
assertEquals(Value.of(7), asMap.get("x"));
assertEquals(Value.of(3.0d), asMap.get("y"));
assertEquals(Value.of("5"), asMap.get("z"));
assertEquals(Value.of(11.0d), asMap.get("_y"));
assertEquals(Value.of("13"), asMap.get("Z"));
assertEquals(Math.E, x.get(0).getValue().getLast(), 1e-15);
}
}
|