4545import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
4646
4747import static org .assertj .core .api .Assertions .assertThat ;
48+ import static org .awaitility .Awaitility .await ;
4849
4950/**
5051 * @author Soby Chacko
@@ -66,14 +67,16 @@ void verifyMetricsWithoutObservation(@Autowired MetricsListener listener,
6667 template .send (METRICS_TEST_TOPIC , "test" ).get (10 , TimeUnit .SECONDS );
6768 assertThat (listener .latch .await (10 , TimeUnit .SECONDS )).isTrue ();
6869
69- Timer timer = meterRegistry .find ("spring.kafka.listener" )
70- .tags ("name" , "metricsTest-0" )
71- .tag ("result" , "failure" )
72- .timer ();
70+ await ().untilAsserted (() -> {
71+ Timer timer = meterRegistry .find ("spring.kafka.listener" )
72+ .tags ("name" , "metricsTest-0" )
73+ .tag ("result" , "failure" )
74+ .timer ();
7375
74- assertThat (timer ).isNotNull ();
75- assertThat (timer .getId ().getTag ("exception" ))
76- .isEqualTo ("IllegalStateException" );
76+ assertThat (timer ).isNotNull ();
77+ assertThat (timer .getId ().getTag ("exception" ))
78+ .isEqualTo ("IllegalStateException" );
79+ });
7780 }
7881
7982 @ Test
@@ -85,12 +88,14 @@ void verifyMetricsWithObservation(@Autowired ObservationListener observationList
8588 template .send (METRICS_TEST_TOPIC , "test" ).get (10 , TimeUnit .SECONDS );
8689 assertThat (observationListener .latch .await (10 , TimeUnit .SECONDS )).isTrue ();
8790
88- Timer timer = meterRegistry .find ("spring.kafka.listener" )
89- .tag ("spring.kafka.listener.id" , "observationTest-0" )
90- .tag ("error" , "IllegalStateException" )
91- .timer ();
91+ await ().untilAsserted (() -> {
92+ Timer timer = meterRegistry .find ("spring.kafka.listener" )
93+ .tag ("spring.kafka.listener.id" , "observationTest-0" )
94+ .tag ("error" , "IllegalStateException" )
95+ .timer ();
9296
93- assertThat (timer ).isNotNull ();
97+ assertThat (timer ).isNotNull ();
98+ });
9499 }
95100
96101 @ Configuration
0 commit comments