Skip to content

Commit fed81d3

Browse files
author
Vineeth
committed
Add tests for ContainerLifecycleMicrometerHolder
Signed-off-by: Vineeth <vineeth>
1 parent 900ddfc commit fed81d3

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2024-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.support.micrometer;
18+
19+
import io.micrometer.core.instrument.Counter;
20+
import io.micrometer.core.instrument.MeterRegistry;
21+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link ContainerLifecycleMicrometerHolder}.
28+
*
29+
* @author Vineeth Yelagandula
30+
* @since 4.1.0
31+
*/
32+
public class ContainerLifecycleMicrometerHolderTests {
33+
34+
@Test
35+
void startCounterIncrementsOnRecordStart() {
36+
MeterRegistry registry = new SimpleMeterRegistry();
37+
ContainerLifecycleMicrometerHolder holder =
38+
new ContainerLifecycleMicrometerHolder(registry, "testContainer");
39+
40+
holder.recordStart();
41+
holder.recordStart();
42+
43+
Counter startCounter = registry.find("spring.kafka.container.start.count")
44+
.tag("name", "testContainer")
45+
.counter();
46+
assertThat(startCounter).isNotNull();
47+
assertThat(startCounter.count()).isEqualTo(2.0);
48+
}
49+
50+
@Test
51+
void stopCounterIncrementsOnRecordStop() {
52+
MeterRegistry registry = new SimpleMeterRegistry();
53+
ContainerLifecycleMicrometerHolder holder =
54+
new ContainerLifecycleMicrometerHolder(registry, "testContainer");
55+
56+
holder.recordStop();
57+
holder.recordStop();
58+
holder.recordStop();
59+
60+
Counter stopCounter = registry.find("spring.kafka.container.stop.count")
61+
.tag("name", "testContainer")
62+
.counter();
63+
assertThat(stopCounter).isNotNull();
64+
assertThat(stopCounter.count()).isEqualTo(3.0);
65+
}
66+
67+
@Test
68+
void countersAreTaggedWithContainerName() {
69+
MeterRegistry registry = new SimpleMeterRegistry();
70+
ContainerLifecycleMicrometerHolder holder =
71+
new ContainerLifecycleMicrometerHolder(registry, "myContainer");
72+
73+
holder.recordStart();
74+
holder.recordStop();
75+
76+
assertThat(registry.find("spring.kafka.container.start.count")
77+
.tag("name", "myContainer").counter()).isNotNull();
78+
assertThat(registry.find("spring.kafka.container.stop.count")
79+
.tag("name", "myContainer").counter()).isNotNull();
80+
}
81+
82+
@Test
83+
void startAndStopCountersAreIndependent() {
84+
MeterRegistry registry = new SimpleMeterRegistry();
85+
ContainerLifecycleMicrometerHolder holder =
86+
new ContainerLifecycleMicrometerHolder(registry, "testContainer");
87+
88+
holder.recordStart();
89+
holder.recordStop();
90+
holder.recordStop();
91+
92+
Counter startCounter = registry.find("spring.kafka.container.start.count")
93+
.tag("name", "testContainer").counter();
94+
Counter stopCounter = registry.find("spring.kafka.container.stop.count")
95+
.tag("name", "testContainer").counter();
96+
97+
assertThat(startCounter).isNotNull();
98+
assertThat(stopCounter).isNotNull();
99+
assertThat(startCounter.count()).isEqualTo(1.0);
100+
assertThat(stopCounter.count()).isEqualTo(2.0);
101+
}
102+
103+
}

0 commit comments

Comments
 (0)