-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(datastore): Migrate datastore tests to use awaitility #13668
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
Changes from 3 commits
56db71a
f3f8061
09b3879
2d4b814
73a9fe2
63676b5
ddabb38
3521f3f
effb38f
57f9f33
5f8943f
2695dc5
9b3b541
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |||||
| import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_RUN_QUERY; | ||||||
| import static com.google.common.truth.Truth.assertThat; | ||||||
| import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME; | ||||||
| import static org.awaitility.Awaitility.await; | ||||||
| import static org.junit.Assert.assertEquals; | ||||||
| import static org.junit.Assert.assertFalse; | ||||||
| import static org.junit.Assert.assertNotNull; | ||||||
|
|
@@ -81,6 +82,7 @@ | |||||
| import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||||||
| import io.opentelemetry.sdk.trace.samplers.Sampler; | ||||||
| import java.io.IOException; | ||||||
| import java.time.Duration; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.Arrays; | ||||||
| import java.util.Collections; | ||||||
|
|
@@ -91,6 +93,7 @@ | |||||
| import java.util.concurrent.TimeUnit; | ||||||
| import java.util.logging.Level; | ||||||
| import java.util.logging.Logger; | ||||||
| import org.awaitility.core.ConditionTimeoutException; | ||||||
| import org.junit.After; | ||||||
| import org.junit.AfterClass; | ||||||
| import org.junit.Before; | ||||||
|
|
@@ -452,44 +455,37 @@ protected void waitForTracesToComplete() throws Exception { | |||||
| protected void fetchAndValidateTrace( | ||||||
| String traceId, int numExpectedSpans, List<List<String>> callStackList) | ||||||
| throws InterruptedException { | ||||||
| // Large enough count to accommodate eventually consistent Cloud Trace backend | ||||||
| int numRetries = GET_TRACE_RETRY_COUNT; | ||||||
| // Account for rootSpanName | ||||||
| numExpectedSpans++; | ||||||
|
|
||||||
| // Fetch traces | ||||||
| do { | ||||||
| try { | ||||||
| retrievedTrace = traceClient.getTrace(projectId, traceId); | ||||||
| assertEquals(traceId, retrievedTrace.getTraceId()); | ||||||
|
|
||||||
| logger.info( | ||||||
| "expectedSpanCount=" | ||||||
| + numExpectedSpans | ||||||
| + ", retrievedSpanCount=" | ||||||
| + retrievedTrace.getSpansCount()); | ||||||
| } catch (NotFoundException | DeadlineExceededException e) { | ||||||
| logger.info( | ||||||
| "Trace not found or deadline exceeded, retrying in " | ||||||
| + GET_TRACE_RETRY_BACKOFF_MILLIS | ||||||
| + " ms"); | ||||||
| } catch (IndexOutOfBoundsException outOfBoundsException) { | ||||||
| logger.info("Call stack not found in trace. Retrying."); | ||||||
| } | ||||||
| if (retrievedTrace == null || numExpectedSpans != retrievedTrace.getSpansCount()) { | ||||||
| Thread.sleep(GET_TRACE_RETRY_BACKOFF_MILLIS); | ||||||
| } | ||||||
| } while (numRetries-- > 0 | ||||||
| && (retrievedTrace == null || numExpectedSpans != retrievedTrace.getSpansCount())); | ||||||
|
|
||||||
| if (retrievedTrace == null || numExpectedSpans != retrievedTrace.getSpansCount()) { | ||||||
| final int expectedSpanCount = numExpectedSpans + 1; | ||||||
|
|
||||||
| try { | ||||||
| await() | ||||||
| .atMost(Duration.ofSeconds(GET_TRACE_RETRY_COUNT)) | ||||||
|
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.
Suggested change
|
||||||
| .pollInterval(Duration.ofMillis(GET_TRACE_RETRY_BACKOFF_MILLIS)) | ||||||
| .ignoreExceptionsInstanceOf(NotFoundException.class) | ||||||
| .ignoreExceptionsInstanceOf(DeadlineExceededException.class) | ||||||
| .ignoreExceptionsInstanceOf(IndexOutOfBoundsException.class) | ||||||
| .until( | ||||||
| () -> { | ||||||
| retrievedTrace = traceClient.getTrace(projectId, traceId); | ||||||
| assertEquals(traceId, retrievedTrace.getTraceId()); | ||||||
| logger.info( | ||||||
| "expectedSpanCount=" | ||||||
| + expectedSpanCount | ||||||
| + ", retrievedSpanCount=" | ||||||
| + retrievedTrace.getSpansCount()); | ||||||
| return retrievedTrace != null | ||||||
| && expectedSpanCount == retrievedTrace.getSpansCount(); | ||||||
| }); | ||||||
| } catch (ConditionTimeoutException e) { | ||||||
| throw new RuntimeException( | ||||||
| "Expected number of spans: " | ||||||
| + numExpectedSpans | ||||||
| + expectedSpanCount | ||||||
| + ", Actual number of spans: " | ||||||
| + (retrievedTrace != null | ||||||
| ? retrievedTrace.getSpansList().toString() | ||||||
| : "Trace NOT_FOUND")); | ||||||
| : "Trace NOT_FOUND"), | ||||||
| e); | ||||||
| } | ||||||
|
|
||||||
| TraceContainer traceContainer = new TraceContainer(rootSpanName, retrievedTrace); | ||||||
|
|
@@ -541,27 +537,32 @@ public void traceContainerTest() throws Exception { | |||||
| } | ||||||
| waitForTracesToComplete(); | ||||||
|
|
||||||
| Trace traceResp = null; | ||||||
| final Trace[] traceRespHolder = new Trace[1]; | ||||||
| int expectedSpanCount = 2; | ||||||
|
|
||||||
| int numRetries = GET_TRACE_RETRY_COUNT; | ||||||
| do { | ||||||
| try { | ||||||
| traceResp = traceClient.getTrace(projectId, customSpanContext.getTraceId()); | ||||||
| if (traceResp.getSpansCount() == expectedSpanCount) { | ||||||
| logger.info("Success: Got " + expectedSpanCount + " spans."); | ||||||
| break; | ||||||
| } | ||||||
| } catch (NotFoundException notFoundException) { | ||||||
| Thread.sleep(GET_TRACE_RETRY_BACKOFF_MILLIS); | ||||||
| logger.info("Trace not found, retrying in " + GET_TRACE_RETRY_BACKOFF_MILLIS + " ms"); | ||||||
| } | ||||||
| logger.info( | ||||||
| "Trace Found. The trace did not contain " | ||||||
| + expectedSpanCount | ||||||
| + " spans. Going to retry."); | ||||||
| numRetries--; | ||||||
| } while (numRetries > 0); | ||||||
| try { | ||||||
| await() | ||||||
| .atMost(Duration.ofSeconds(GET_TRACE_RETRY_COUNT)) | ||||||
| .pollInterval(Duration.ofMillis(GET_TRACE_RETRY_BACKOFF_MILLIS)) | ||||||
| .ignoreExceptionsInstanceOf(NotFoundException.class) | ||||||
| .until( | ||||||
| () -> { | ||||||
| Trace trace = traceClient.getTrace(projectId, customSpanContext.getTraceId()); | ||||||
| traceRespHolder[0] = trace; | ||||||
| if (trace.getSpansCount() == expectedSpanCount) { | ||||||
| logger.info("Success: Got " + expectedSpanCount + " spans."); | ||||||
| return true; | ||||||
| } | ||||||
| logger.info( | ||||||
| "Trace Found. The trace did not contain " | ||||||
| + expectedSpanCount | ||||||
| + " spans. Going to retry."); | ||||||
| return false; | ||||||
| }); | ||||||
| } catch (ConditionTimeoutException ignored) { | ||||||
| // Ignore to let assertions below run and fail with descriptive messages | ||||||
| } | ||||||
|
lqiu96 marked this conversation as resolved.
|
||||||
| Trace traceResp = traceRespHolder[0]; | ||||||
|
|
||||||
| // Make sure we got as many spans as we expected. | ||||||
| assertNotNull(traceResp); | ||||||
|
|
||||||
This file was deleted.
This file was deleted.
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.
Add
awaitilitytogoogle-cloud-jar-parentso we don't have to manage the version in each module?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.
yeah, will do in a follow up pr
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.
SG, thanks!