-
Notifications
You must be signed in to change notification settings - Fork 9
/
StepMeterRegistryTests.java
82 lines (62 loc) · 2.17 KB
/
StepMeterRegistryTests.java
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
package learningtest.io.micrometer.core.instrument.step;
import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.step.StepMeterRegistry;
import io.micrometer.core.instrument.step.StepRegistryConfig;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link StepMeterRegistry}.
*
* @author Johnny Lim
*/
class StepMeterRegistryTests {
private final StepRegistryConfig config = new StepRegistryConfig() {
@Override
public String prefix() {
return null;
}
@Override
public String get(String key) {
return null;
}
@Override
public Duration step() {
return Duration.ofSeconds(5);
}
};
private final StepMeterRegistry registry = new StepMeterRegistry(this.config, Clock.SYSTEM) {
@Override
protected void publish() {
}
@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.MILLISECONDS;
}
};
@Test
void find() throws InterruptedException {
Counter myCounter1 = this.registry.counter("myTestCounter", "tag1", "value1");
Counter myCounter2 = this.registry.counter("myTestCounter", "tag2", "value2");
Counter myCounter1n2 = this.registry.counter("myTestCounter", "tag1", "value1", "tag2", "value2");
myCounter1.increment();
myCounter2.increment();
myCounter1n2.increment();
TimeUnit.SECONDS.sleep(5);
assertThat(myCounter1.count()).isEqualTo(1);
assertThat(myCounter2.count()).isEqualTo(1);
assertThat(myCounter1n2.count()).isEqualTo(1);
Collection<Counter> counters = this.registry.find("myTestCounter").tag("tag1", "value1").counters();
assertThat(counters).containsExactlyInAnyOrder(myCounter1, myCounter1n2);
assertThat(aggregate(counters)).isEqualTo(2);
counters = this.registry.find("myTestCounter").tag("tag2", "value2").counters();
assertThat(counters).containsExactlyInAnyOrder(myCounter2, myCounter1n2);
assertThat(aggregate(counters)).isEqualTo(2);
}
private double aggregate(Collection<Counter> counters) {
return counters.stream().mapToDouble(Counter::count).sum();
}
}