-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor(showcase): migrate Thread.sleep to Awaitility in integration tests #13519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ | |
| import java.io.InputStream; | ||
| import java.time.Duration; | ||
| import java.util.Collection; | ||
| import org.awaitility.Awaitility; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -106,7 +107,10 @@ void testMetrics_successfulEcho_grpc() throws Exception { | |
| // This is implemented by adding a TraceFinisher to ApiFuture as a callback in | ||
| // TracedUnaryCallable, | ||
| // which could be executed in a different thread. | ||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
|
Comment on lines
+110
to
+113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Efficiency & Readability Improvement: Calling You can use Awaitility's ability to return the evaluated value that satisfies a Hamcrest matcher to avoid the duplicate call: Collection<MetricData> metrics =
Awaitility.await()
.atMost(Duration.ofSeconds(5))
.pollInterval(Duration.ofMillis(10))
.until(metricReader::collectAllMetrics, org.hamcrest.Matchers.not(org.hamcrest.Matchers.empty()));Note: This pattern occurs in multiple places throughout this file (e.g., lines 200, 235, 380, 432, 478, 561, 735). Consider applying this improvement to all of them. |
||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).isNotEmpty(); | ||
|
|
||
|
|
@@ -192,7 +196,10 @@ public void sendMessage(ReqT message) {} | |
| UnavailableException.class, | ||
| () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).isNotEmpty(); | ||
|
|
||
|
|
@@ -224,7 +231,10 @@ void testMetrics_successfulEcho_httpjson() throws Exception { | |
|
|
||
| client.echo(EchoRequest.newBuilder().setContent("metrics-test").build()); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).isNotEmpty(); | ||
|
|
||
|
|
@@ -366,7 +376,10 @@ public String getHeaderValue(int index) { | |
| UnavailableException.class, | ||
| () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).isNotEmpty(); | ||
|
|
||
|
|
@@ -415,7 +428,10 @@ void testMetrics_clientTimeout_grpc() throws Exception { | |
| Exception.class, | ||
| () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).isNotEmpty(); | ||
|
|
||
|
|
@@ -458,7 +474,10 @@ void testMetrics_clientTimeout_httpjson() throws Exception { | |
| Exception.class, | ||
| () -> client.echo(EchoRequest.newBuilder().setContent("metrics-test").build())); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).isNotEmpty(); | ||
|
|
||
|
|
@@ -538,7 +557,10 @@ public void sendMessage(ReqT message) {} | |
|
|
||
| assertThat(attemptCount.get()).isEqualTo(3); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).hasSize(1); | ||
|
|
||
|
|
@@ -709,7 +731,10 @@ public String getHeaderValue(int index) { | |
|
|
||
| assertThat(requestCount.get()).isEqualTo(3); | ||
|
|
||
| Thread.sleep(100); | ||
| Awaitility.await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .pollInterval(Duration.ofMillis(10)) | ||
| .until(() -> !metricReader.collectAllMetrics().isEmpty()); | ||
| Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
| assertThat(metrics).hasSize(1); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -83,6 +83,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.function.Predicate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.awaitility.Awaitility; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.junit.jupiter.api.AfterEach; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.junit.jupiter.api.Assertions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.junit.jupiter.api.BeforeEach; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -296,18 +297,21 @@ private List<MetricData> getMetricDataList() throws InterruptedException { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private List<MetricData> getMetricDataList(InMemoryMetricReader metricReader) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throws InterruptedException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < NUM_DEFAULT_FLUSH_ATTEMPTS; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thread.sleep(1000L); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<MetricData> metricData = new ArrayList<>(metricReader.collectAllMetrics()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Depending on the OpenTelemetry instance (i.e. OpenTelemetry, GrpcOpenTelemetry, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // there may be additional metrics recorded. Only check to ensure the Gax Metrics | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // are recorded properly. Any additional metrics are fine to be passed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (metricData.size() >= NUM_GAX_OTEL_METRICS && areAllGaxMetricsRecorded(metricData)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return metricData; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Awaitility.await() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .atMost(java.time.Duration.ofSeconds(NUM_DEFAULT_FLUSH_ATTEMPTS)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .pollInterval(java.time.Duration.ofSeconds(1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .until( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| () -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<MetricData> metricData = new ArrayList<>(metricReader.collectAllMetrics()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return metricData.size() >= NUM_GAX_OTEL_METRICS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && areAllGaxMetricsRecorded(metricData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new ArrayList<>(metricReader.collectAllMetrics()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (org.awaitility.core.ConditionTimeoutException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assertions.fail("Unable to collect all the GAX metrics required for the test", e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+300
to
314
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Efficiency & Readability Improvement: Calling We can use an
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assertions.fail("Unable to collect all the GAX metrics required for the test"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private boolean areAllGaxMetricsRecorded(List<MetricData> metricData) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -370,7 +374,7 @@ void testGrpc_operationCancelled_recordsMetrics() throws Exception { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UnaryCallable<BlockRequest, BlockResponse> blockCallable = grpcClient.blockCallable(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ApiFuture<BlockResponse> blockResponseApiFuture = blockCallable.futureCall(blockRequest); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Sleep 1s before cancelling to let the request go through | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thread.sleep(1000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Awaitility.await().pollDelay(java.time.Duration.ofSeconds(1)).until(() -> true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blockResponseApiFuture.cancel(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<MetricData> actualMetricDataList = getMetricDataList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -397,7 +401,7 @@ void testHttpJson_operationCancelled_recordsMetrics() throws Exception { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UnaryCallable<BlockRequest, BlockResponse> blockCallable = httpClient.blockCallable(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ApiFuture<BlockResponse> blockResponseApiFuture = blockCallable.futureCall(blockRequest); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Sleep 1s before cancelling to let the request go through | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thread.sleep(1000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Awaitility.await().pollDelay(java.time.Duration.ofSeconds(1)).until(() -> true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| blockResponseApiFuture.cancel(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<MetricData> actualMetricDataList = getMetricDataList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Efficiency & Readability Improvement: Calling
metricReader.collectAllMetrics()inside theuntilcondition and then calling it again immediately after to assign toactualMetricsis redundant and inefficient.InMemoryMetricReader.collectAllMetrics()can be an expensive operation.Instead, you can use Awaitility's ability to return the evaluated value that satisfies a Hamcrest matcher, which avoids the duplicate call entirely: