Skip to content

Commit 4566070

Browse files
committed
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]>
1 parent fa523b1 commit 4566070

File tree

5 files changed

+531
-1
lines changed

5 files changed

+531
-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,70 @@
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+
*
29+
* @author Madhura Bhave
30+
* @author Phillip Webb
31+
* @author Andy Wilkinson
32+
* @see OutputCaptureExtension
33+
*/
34+
public interface CapturedOutput extends CharSequence {
35+
36+
@Override
37+
default int length() {
38+
return toString().length();
39+
}
40+
41+
@Override
42+
default char charAt(int index) {
43+
return toString().charAt(index);
44+
}
45+
46+
@Override
47+
default CharSequence subSequence(int start, int end) {
48+
return toString().subSequence(start, end);
49+
}
50+
51+
/**
52+
* Return all content (both {@link System#out System.out} and {@link System#err
53+
* System.err}) in the order that it was captured.
54+
* @return all captured output
55+
*/
56+
String getAll();
57+
58+
/**
59+
* Return {@link System#out System.out} content in the order that it was captured.
60+
* @return {@link System#out System.out} captured output
61+
*/
62+
String getOut();
63+
64+
/**
65+
* Return {@link System#err System.err} content in the order that it was captured.
66+
* @return {@link System#err System.err} captured output
67+
*/
68+
String getErr();
69+
70+
}

0 commit comments

Comments
 (0)