Skip to content

Commit d192fda

Browse files
authored
Add assertion for no re-registering Kafka metrics (#6119)
* Add assertion for no re-registering Kafka metrics This commit also copies OutputCaptureExtension to assert it. See gh-6068 Signed-off-by: Johnny Lim <[email protected]> * Add sources for OutputCaptureExtension support to Javadoc Signed-off-by: Johnny Lim <[email protected]> --------- Signed-off-by: Johnny Lim <[email protected]>
1 parent fa523b1 commit d192fda

File tree

5 files changed

+543
-1
lines changed

5 files changed

+543
-1
lines changed

Diff for: micrometer-core/src/test/java/io/micrometer/core/instrument/binder/kafka/KafkaMetricsTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import io.micrometer.core.instrument.MeterRegistry;
2222
import io.micrometer.core.instrument.Tag;
2323
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
24+
import io.micrometer.core.testsupport.system.CapturedOutput;
25+
import io.micrometer.core.testsupport.system.OutputCaptureExtension;
2426
import org.apache.kafka.common.Metric;
2527
import org.apache.kafka.common.MetricName;
2628
import org.apache.kafka.common.metrics.KafkaMetric;
@@ -29,6 +31,7 @@
2931
import org.apache.kafka.common.utils.Time;
3032
import org.junit.jupiter.api.AfterEach;
3133
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.extension.ExtendWith;
3235

3336
import java.util.Collections;
3437
import java.util.HashMap;
@@ -39,6 +42,7 @@
3942

4043
import static org.assertj.core.api.Assertions.assertThat;
4144

45+
@ExtendWith(OutputCaptureExtension.class)
4246
class KafkaMetricsTest {
4347

4448
private KafkaMetrics kafkaMetrics;
@@ -257,7 +261,7 @@ void shouldRemoveOlderMeterWithLessTagsWhenCommonTagsConfigured() {
257261

258262
@Issue("#2212")
259263
@Test
260-
void shouldRemoveMeterWithLessTagsWithMultipleClients() {
264+
void shouldRemoveMeterWithLessTagsWithMultipleClients(CapturedOutput output) {
261265
// Given
262266
AtomicReference<Map<MetricName, KafkaMetric>> metrics = new AtomicReference<>(new LinkedHashMap<>());
263267
Supplier<Map<MetricName, ? extends Metric>> supplier = () -> metrics.updateAndGet(map -> {
@@ -320,6 +324,8 @@ void shouldRemoveMeterWithLessTagsWithMultipleClients() {
320324
registry.getMeters()
321325
.forEach(meter -> assertThat(meter.getId().getTags()).extracting(Tag::getKey)
322326
.containsOnly("key0", "key1", "client.id", "kafka.version"));
327+
328+
assertThat(output).doesNotContain("This Gauge has been already registered");
323329
}
324330

325331
@Issue("#2726")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2019 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 io.micrometer.core.testsupport.system;
18+
19+
/**
20+
* Provides access to {@link System#out System.out} and {@link System#err System.err}
21+
* output that has been captured by the {@link OutputCaptureExtension}. Can be used to
22+
* apply assertions either using AssertJ or standard JUnit assertions. For example:
23+
* <pre class="code">
24+
* assertThat(output).contains("started"); // Checks all output
25+
* assertThat(output.getErr()).contains("failed"); // Only checks System.err
26+
* assertThat(output.getOut()).contains("ok"); // Only checks System.out
27+
* </pre>
28+
* <p>
29+
* Copied from <a href=
30+
* "https://github.com/spring-projects/spring-boot/blob/88c9ae97b5638491964537bb5b1f7dfbea1ae047/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/CapturedOutput.java">org.springframework.boot.test.system.CapturedOutput</a>
31+
* in Spring Boot.
32+
*
33+
* @author Madhura Bhave
34+
* @author Phillip Webb
35+
* @author Andy Wilkinson
36+
* @see OutputCaptureExtension
37+
*/
38+
public interface CapturedOutput extends CharSequence {
39+
40+
@Override
41+
default int length() {
42+
return toString().length();
43+
}
44+
45+
@Override
46+
default char charAt(int index) {
47+
return toString().charAt(index);
48+
}
49+
50+
@Override
51+
default CharSequence subSequence(int start, int end) {
52+
return toString().subSequence(start, end);
53+
}
54+
55+
/**
56+
* Return all content (both {@link System#out System.out} and {@link System#err
57+
* System.err}) in the order that it was captured.
58+
* @return all captured output
59+
*/
60+
String getAll();
61+
62+
/**
63+
* Return {@link System#out System.out} content in the order that it was captured.
64+
* @return {@link System#out System.out} captured output
65+
*/
66+
String getOut();
67+
68+
/**
69+
* Return {@link System#err System.err} content in the order that it was captured.
70+
* @return {@link System#err System.err} captured output
71+
*/
72+
String getErr();
73+
74+
}

0 commit comments

Comments
 (0)