6363import java .io .InputStream ;
6464import java .time .Duration ;
6565import java .util .Collection ;
66+ import org .awaitility .Awaitility ;
6667import org .junit .jupiter .api .AfterEach ;
6768import org .junit .jupiter .api .BeforeEach ;
6869import org .junit .jupiter .api .Test ;
@@ -106,8 +107,7 @@ void testMetrics_successfulEcho_grpc() throws Exception {
106107 // This is implemented by adding a TraceFinisher to ApiFuture as a callback in
107108 // TracedUnaryCallable,
108109 // which could be executed in a different thread.
109- Thread .sleep (100 );
110- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
110+ Collection <MetricData > metrics = waitAndCollectMetrics ();
111111 assertThat (metrics ).isNotEmpty ();
112112
113113 MetricData durationMetric =
@@ -192,8 +192,7 @@ public void sendMessage(ReqT message) {}
192192 UnavailableException .class ,
193193 () -> client .echo (EchoRequest .newBuilder ().setContent ("metrics-test" ).build ()));
194194
195- Thread .sleep (100 );
196- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
195+ Collection <MetricData > metrics = waitAndCollectMetrics ();
197196 assertThat (metrics ).isNotEmpty ();
198197
199198 MetricData durationMetric =
@@ -224,8 +223,7 @@ void testMetrics_successfulEcho_httpjson() throws Exception {
224223
225224 client .echo (EchoRequest .newBuilder ().setContent ("metrics-test" ).build ());
226225
227- Thread .sleep (100 );
228- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
226+ Collection <MetricData > metrics = waitAndCollectMetrics ();
229227 assertThat (metrics ).isNotEmpty ();
230228
231229 MetricData durationMetric =
@@ -366,8 +364,7 @@ public String getHeaderValue(int index) {
366364 UnavailableException .class ,
367365 () -> client .echo (EchoRequest .newBuilder ().setContent ("metrics-test" ).build ()));
368366
369- Thread .sleep (100 );
370- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
367+ Collection <MetricData > metrics = waitAndCollectMetrics ();
371368 assertThat (metrics ).isNotEmpty ();
372369
373370 MetricData durationMetric =
@@ -415,8 +412,7 @@ void testMetrics_clientTimeout_grpc() throws Exception {
415412 Exception .class ,
416413 () -> client .echo (EchoRequest .newBuilder ().setContent ("metrics-test" ).build ()));
417414
418- Thread .sleep (100 );
419- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
415+ Collection <MetricData > metrics = waitAndCollectMetrics ();
420416 assertThat (metrics ).isNotEmpty ();
421417
422418 MetricData durationMetric =
@@ -458,8 +454,7 @@ void testMetrics_clientTimeout_httpjson() throws Exception {
458454 Exception .class ,
459455 () -> client .echo (EchoRequest .newBuilder ().setContent ("metrics-test" ).build ()));
460456
461- Thread .sleep (100 );
462- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
457+ Collection <MetricData > metrics = waitAndCollectMetrics ();
463458 assertThat (metrics ).isNotEmpty ();
464459
465460 MetricData durationMetric =
@@ -538,8 +533,7 @@ public void sendMessage(ReqT message) {}
538533
539534 assertThat (attemptCount .get ()).isEqualTo (3 );
540535
541- Thread .sleep (100 );
542- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
536+ Collection <MetricData > metrics = waitAndCollectMetrics ();
543537 assertThat (metrics ).hasSize (1 );
544538
545539 MetricData durationMetric =
@@ -709,8 +703,7 @@ public String getHeaderValue(int index) {
709703
710704 assertThat (requestCount .get ()).isEqualTo (3 );
711705
712- Thread .sleep (100 );
713- Collection <MetricData > metrics = metricReader .collectAllMetrics ();
706+ Collection <MetricData > metrics = waitAndCollectMetrics ();
714707 assertThat (metrics ).hasSize (1 );
715708
716709 MetricData durationMetric =
@@ -730,4 +723,11 @@ public String getHeaderValue(int index) {
730723 .isEqualTo ("OK" );
731724 }
732725 }
726+
727+ private Collection <MetricData > waitAndCollectMetrics () {
728+ Awaitility .await ()
729+ .atMost (Duration .ofSeconds (5 ))
730+ .until (() -> !metricReader .collectAllMetrics ().isEmpty ());
731+ return metricReader .collectAllMetrics ();
732+ }
733733}
0 commit comments