forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounterTest.java
More file actions
85 lines (70 loc) · 2.59 KB
/
CounterTest.java
File metadata and controls
85 lines (70 loc) · 2.59 KB
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
package io.prometheus.client;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.Before;
public class CounterTest {
CollectorRegistry registry;
Counter noLabels, labels;
@Before
public void setUp() {
registry = new CollectorRegistry();
noLabels = Counter.build().name("nolabels").help("help").register(registry);
labels = Counter.build().name("labels").help("help").labelNames("l").register(registry);
}
private double getValue() {
return registry.getSampleValue("nolabels").doubleValue();
}
@Test
public void testIncrement() {
noLabels.inc();
assertEquals(1.0, getValue(), .001);
assertEquals(1.0, noLabels.get(), .001);
noLabels.inc(2);
assertEquals(3.0, getValue(), .001);
assertEquals(3.0, noLabels.get(), .001);
noLabels.labels().inc(4);
assertEquals(7.0, getValue(), .001);
assertEquals(7.0, noLabels.get(), .001);
noLabels.labels().inc();
assertEquals(8.0, getValue(), .001);
assertEquals(8.0, noLabels.get(), .001);
}
@Test(expected=IllegalArgumentException.class)
public void testNegativeIncrementFails() {
noLabels.inc(-1);
}
@Test
public void noLabelsDefaultZeroValue() {
assertEquals(0.0, getValue(), .001);
}
private Double getLabelsValue(String labelValue) {
return registry.getSampleValue("labels", new String[]{"l"}, new String[]{labelValue});
}
@Test
public void testLabels() {
assertEquals(null, getLabelsValue("a"));
assertEquals(null, getLabelsValue("b"));
labels.labels("a").inc();
assertEquals(1.0, getLabelsValue("a").doubleValue(), .001);
assertEquals(null, getLabelsValue("b"));
labels.labels("b").inc(3);
assertEquals(1.0, getLabelsValue("a").doubleValue(), .001);
assertEquals(3.0, getLabelsValue("b").doubleValue(), .001);
}
@Test
public void testCollect() {
labels.labels("a").inc();
List<Collector.MetricFamilySamples> mfs = labels.collect();
ArrayList<Collector.MetricFamilySamples.Sample> samples = new ArrayList<Collector.MetricFamilySamples.Sample>();
ArrayList<String> labelNames = new ArrayList<String>();
labelNames.add("l");
ArrayList<String> labelValues = new ArrayList<String>();
labelValues.add("a");
samples.add(new Collector.MetricFamilySamples.Sample("labels", labelNames, labelValues, 1.0));
Collector.MetricFamilySamples mfsFixture = new Collector.MetricFamilySamples("labels", Collector.Type.COUNTER, "help", samples);
assertEquals(1, mfs.size());
assertEquals(mfsFixture, mfs.get(0));
}
}