|
63 | 63 | import java.io.InputStream; |
64 | 64 | import java.time.Duration; |
65 | 65 | import java.util.Collection; |
| 66 | +import org.awaitility.Awaitility; |
66 | 67 | import org.junit.jupiter.api.AfterEach; |
67 | 68 | import org.junit.jupiter.api.BeforeEach; |
68 | 69 | import org.junit.jupiter.api.Test; |
@@ -106,7 +107,10 @@ void testMetrics_successfulEcho_grpc() throws Exception { |
106 | 107 | // This is implemented by adding a TraceFinisher to ApiFuture as a callback in |
107 | 108 | // TracedUnaryCallable, |
108 | 109 | // which could be executed in a different thread. |
109 | | - Thread.sleep(100); |
| 110 | + Awaitility.await() |
| 111 | + .atMost(Duration.ofSeconds(5)) |
| 112 | + .pollInterval(Duration.ofMillis(10)) |
| 113 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
110 | 114 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
111 | 115 | assertThat(metrics).isNotEmpty(); |
112 | 116 |
|
@@ -192,7 +196,10 @@ public void sendMessage(ReqT message) {} |
192 | 196 | UnavailableException.class, |
193 | 197 | () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); |
194 | 198 |
|
195 | | - Thread.sleep(100); |
| 199 | + Awaitility.await() |
| 200 | + .atMost(Duration.ofSeconds(5)) |
| 201 | + .pollInterval(Duration.ofMillis(10)) |
| 202 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
196 | 203 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
197 | 204 | assertThat(metrics).isNotEmpty(); |
198 | 205 |
|
@@ -224,7 +231,10 @@ void testMetrics_successfulEcho_httpjson() throws Exception { |
224 | 231 |
|
225 | 232 | client.echo(EchoRequest.newBuilder().setContent("metrics-test").build()); |
226 | 233 |
|
227 | | - Thread.sleep(100); |
| 234 | + Awaitility.await() |
| 235 | + .atMost(Duration.ofSeconds(5)) |
| 236 | + .pollInterval(Duration.ofMillis(10)) |
| 237 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
228 | 238 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
229 | 239 | assertThat(metrics).isNotEmpty(); |
230 | 240 |
|
@@ -366,7 +376,10 @@ public String getHeaderValue(int index) { |
366 | 376 | UnavailableException.class, |
367 | 377 | () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); |
368 | 378 |
|
369 | | - Thread.sleep(100); |
| 379 | + Awaitility.await() |
| 380 | + .atMost(Duration.ofSeconds(5)) |
| 381 | + .pollInterval(Duration.ofMillis(10)) |
| 382 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
370 | 383 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
371 | 384 | assertThat(metrics).isNotEmpty(); |
372 | 385 |
|
@@ -415,7 +428,10 @@ void testMetrics_clientTimeout_grpc() throws Exception { |
415 | 428 | Exception.class, |
416 | 429 | () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); |
417 | 430 |
|
418 | | - Thread.sleep(100); |
| 431 | + Awaitility.await() |
| 432 | + .atMost(Duration.ofSeconds(5)) |
| 433 | + .pollInterval(Duration.ofMillis(10)) |
| 434 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
419 | 435 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
420 | 436 | assertThat(metrics).isNotEmpty(); |
421 | 437 |
|
@@ -458,7 +474,10 @@ void testMetrics_clientTimeout_httpjson() throws Exception { |
458 | 474 | Exception.class, |
459 | 475 | () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); |
460 | 476 |
|
461 | | - Thread.sleep(100); |
| 477 | + Awaitility.await() |
| 478 | + .atMost(Duration.ofSeconds(5)) |
| 479 | + .pollInterval(Duration.ofMillis(10)) |
| 480 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
462 | 481 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
463 | 482 | assertThat(metrics).isNotEmpty(); |
464 | 483 |
|
@@ -538,7 +557,10 @@ public void sendMessage(ReqT message) {} |
538 | 557 |
|
539 | 558 | assertThat(attemptCount.get()).isEqualTo(3); |
540 | 559 |
|
541 | | - Thread.sleep(100); |
| 560 | + Awaitility.await() |
| 561 | + .atMost(Duration.ofSeconds(5)) |
| 562 | + .pollInterval(Duration.ofMillis(10)) |
| 563 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
542 | 564 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
543 | 565 | assertThat(metrics).hasSize(1); |
544 | 566 |
|
@@ -709,7 +731,10 @@ public String getHeaderValue(int index) { |
709 | 731 |
|
710 | 732 | assertThat(requestCount.get()).isEqualTo(3); |
711 | 733 |
|
712 | | - Thread.sleep(100); |
| 734 | + Awaitility.await() |
| 735 | + .atMost(Duration.ofSeconds(5)) |
| 736 | + .pollInterval(Duration.ofMillis(10)) |
| 737 | + .until(() -> !metricReader.collectAllMetrics().isEmpty()); |
713 | 738 | Collection<MetricData> metrics = metricReader.collectAllMetrics(); |
714 | 739 | assertThat(metrics).hasSize(1); |
715 | 740 |
|
|
0 commit comments