Skip to content

Commit 153a36f

Browse files
garyrussellartembilan
authored andcommitted
Fix Race in Micrometer Test
When timers have dynamic tags, creation of the timers is deferred until the first completion of a timer with a set of tag values. Use awaitility to wait for the timer to be created before checking the count. **Cherry-pick to `2.9.x`**
1 parent 9271438 commit 153a36f

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

spring-kafka/src/test/java/org/springframework/kafka/annotation/EnableKafkaIntegrationTests.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
21+
import static org.awaitility.Awaitility.await;
2122
import static org.mockito.ArgumentMatchers.any;
2223
import static org.mockito.ArgumentMatchers.anyMap;
2324
import static org.mockito.ArgumentMatchers.anyString;
@@ -166,6 +167,8 @@
166167

167168
import io.micrometer.core.instrument.ImmutableTag;
168169
import io.micrometer.core.instrument.MeterRegistry;
170+
import io.micrometer.core.instrument.Timer;
171+
import io.micrometer.core.instrument.search.MeterNotFoundException;
169172
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
170173

171174
/**
@@ -563,23 +566,41 @@ public void testNulls() throws Exception {
563566
this.kafkaJsonTemplate.send("annotated12", null, null);
564567
assertThat(this.listener.latch8.await(60, TimeUnit.SECONDS)).isTrue();
565568

566-
assertThat(this.meterRegistry.get("spring.kafka.template")
567-
.tag("name", "kafkaJsonTemplate")
568-
.tag("extraTag", "bar")
569-
.tag("topic", "annotated12")
570-
.tag("result", "success")
571-
.timer()
572-
.count())
573-
.isGreaterThan(0L);
574-
575-
assertThat(this.meterRegistry.get("spring.kafka.listener")
576-
.tag("name", "quux-0")
577-
.tag("extraTag", "foo")
578-
.tag("topic", "annotated12")
579-
.tag("result", "success")
580-
.timer()
581-
.count())
582-
.isGreaterThan(0L);
569+
await().untilAsserted(() -> {
570+
Timer timer = null;
571+
try {
572+
timer = this.meterRegistry.get("spring.kafka.template")
573+
.tag("name", "kafkaJsonTemplate")
574+
.tag("extraTag", "bar")
575+
.tag("topic", "annotated12")
576+
.tag("result", "success")
577+
.timer();
578+
}
579+
catch (MeterNotFoundException ex) {
580+
}
581+
assertThat(timer)
582+
.describedAs("Timer not found in " + ((SimpleMeterRegistry) this.meterRegistry).getMetersAsString())
583+
.isNotNull();
584+
assertThat(timer.count()).isGreaterThan(0L);
585+
});
586+
587+
await().untilAsserted(() -> {
588+
Timer timer = null;
589+
try {
590+
timer = this.meterRegistry.get("spring.kafka.listener")
591+
.tag("name", "quux-0")
592+
.tag("extraTag", "foo")
593+
.tag("topic", "annotated12")
594+
.tag("result", "success")
595+
.timer();
596+
}
597+
catch (MeterNotFoundException ex) {
598+
}
599+
assertThat(timer)
600+
.describedAs("Timer not found in " + ((SimpleMeterRegistry) this.meterRegistry).getMetersAsString())
601+
.isNotNull();
602+
assertThat(timer.count()).isGreaterThan(0L);
603+
});
583604
}
584605

585606
@Test

0 commit comments

Comments
 (0)