Skip to content

Commit

Permalink
chore: flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Feb 19, 2024
1 parent 62319f0 commit ac11f63
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
5 changes: 5 additions & 0 deletions google-cloud-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<properties>
<site.installationModule>google-cloud-spanner</site.installationModule>
<opencensus.version>0.31.1</opencensus.version>
<opentelemetry.version>1.34.1</opentelemetry.version>
<spanner.testenv.config.class>com.google.cloud.spanner.GceTestEnvConfig</spanner.testenv.config.class>
<spanner.testenv.instance>projects/gcloud-devel/instances/spanner-testing-east1</spanner.testenv.instance>
<spanner.gce.config.project_id>gcloud-devel</spanner.gce.config.project_id>
Expand Down Expand Up @@ -418,21 +419,25 @@
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>${opentelemetry.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-metrics</artifactId>
<version>${opentelemetry.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-trace</artifactId>
<version>${opentelemetry.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<version>${opentelemetry.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@Category(TracerTest.class)
@RunWith(JUnit4.class)
Expand Down Expand Up @@ -103,11 +104,11 @@ public class OpenTelemetrySpanTest {
Statement.of("UPDATE NON_EXISTENT_TABLE SET BAR=1 WHERE BAZ=2");

private List<String> expectedBatchCreateSessionsRequestEvents =
ImmutableList.of("Requesting 25 sessions", "Request for 25 sessions returned 25 sessions");
ImmutableList.of("Requesting 2 sessions", "Request for 2 sessions returned 2 sessions");

private int expectedBatchCreateSessionsRequestEventsCount = 2;

private List<String> expectedBatchCreateSessionsEvents = ImmutableList.of("Creating 25 sessions");
private List<String> expectedBatchCreateSessionsEvents = ImmutableList.of("Creating 2 sessions");

private int expectedBatchCreateSessionsEventsCount = 1;

Expand All @@ -117,58 +118,45 @@ public class OpenTelemetrySpanTest {
private int expectedExecuteStreamingQueryEventsCount = 1;

private List<String> expectedReadOnlyTransactionSingleUseEvents =
ImmutableList.of(
"Acquiring session",
"No session available",
"Creating sessions",
"Waiting for a session to come available",
"Using Session");
ImmutableList.of("Acquiring session", "Acquired session", "Using Session");

private int expectedReadOnlyTransactionSingleUseEventsCount = 5;
private int expectedReadOnlyTransactionSingleUseEventsCount = 3;

private List<String> expectedReadOnlyTransactionMultiUseEvents =
ImmutableList.of(
"Acquiring session",
"No session available",
"Creating sessions",
"Waiting for a session to come available",
"Acquired session",
"Using Session",
"Creating Transaction",
"Transaction Creation Done");

private int expectedReadOnlyTransactionMultiUseEventsCount = 7;
private int expectedReadOnlyTransactionMultiUseEventsCount = 5;

private List<String> expectedReadWriteTransactionErrorEvents =
ImmutableList.of(
"Acquiring session",
"No session available",
"Creating sessions",
"Waiting for a session to come available",
"Acquired session",
"Using Session",
"Starting Transaction Attempt",
"Transaction Attempt Failed in user operation",
"exception");

private int expectedReadWriteTransactionErrorEventsCount = 8;
private int expectedReadWriteTransactionErrorEventsCount = 6;
private List<String> expectedReadWriteTransactionEvents =
ImmutableList.of(
"Acquiring session",
"No session available",
"Creating sessions",
"Waiting for a session to come available",
"Acquired session",
"Using Session",
"Starting Transaction Attempt",
"Starting Commit",
"Commit Done",
"Transaction Attempt Succeeded");

private int expectedReadWriteTransactionCount = 9;
private int expectedReadWriteTransactionCount = 7;
private List<String> expectedReadWriteTransactionErrorWithBeginTransactionEvents =
ImmutableList.of(
"Acquiring session",
"No session available",
"Creating sessions",
"Waiting for a session to come available",
"Acquired session",
"Using Session",
"Starting Transaction Attempt",
"Transaction Attempt Aborted in user operation. Retrying",
Expand All @@ -178,38 +166,34 @@ public class OpenTelemetrySpanTest {
"Commit Done",
"Transaction Attempt Succeeded");

private int expectedReadWriteTransactionErrorWithBeginTransactionEventsCount = 13;
private int expectedReadWriteTransactionErrorWithBeginTransactionEventsCount = 11;
private List<String> expectedReadOnlyTransactionSpans =
ImmutableList.of(
"CloudSpannerOperation.BatchCreateSessionsRequest",
"CloudSpannerOperation.ExecuteStreamingQuery",
"CloudSpannerOperation.BatchCreateSessions",
"CloudSpanner.ReadOnlyTransaction",
"SessionPool.WaitForSession");
"CloudSpanner.ReadOnlyTransaction");

private List<String> expectedReadWriteTransactionWithCommitSpans =
ImmutableList.of(
"CloudSpannerOperation.BatchCreateSessionsRequest",
"CloudSpannerOperation.Commit",
"CloudSpannerOperation.BatchCreateSessions",
"CloudSpanner.ReadWriteTransaction",
"SessionPool.WaitForSession");
"CloudSpanner.ReadWriteTransaction");

private List<String> expectedReadWriteTransactionSpans =
ImmutableList.of(
"CloudSpannerOperation.BatchCreateSessionsRequest",
"CloudSpannerOperation.BatchCreateSessions",
"CloudSpanner.ReadWriteTransaction",
"SessionPool.WaitForSession");
"CloudSpanner.ReadWriteTransaction");

private List<String> expectedReadWriteTransactionWithCommitAndBeginTransactionSpans =
ImmutableList.of(
"CloudSpannerOperation.BeginTransaction",
"CloudSpannerOperation.BatchCreateSessionsRequest",
"CloudSpannerOperation.Commit",
"CloudSpannerOperation.BatchCreateSessions",
"CloudSpanner.ReadWriteTransaction",
"SessionPool.WaitForSession");
"CloudSpanner.ReadWriteTransaction");

@BeforeClass
public static void setupOpenTelemetry() {
Expand Down Expand Up @@ -282,7 +266,11 @@ public void setUp() throws Exception {
.setChannelProvider(channelProvider)
.setOpenTelemetry(openTelemetry)
.setCredentials(NoCredentials.getInstance())
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMinSessions(0).build());
.setSessionPoolOption(
SessionPoolOptions.newBuilder()
.setMinSessions(2)
.setWaitForMinSessions(Duration.ofSeconds(10L))
.build());

spanner = builder.build().getService();

Expand Down Expand Up @@ -339,9 +327,6 @@ public void singleUse() {
expectedReadOnlyTransactionSingleUseEvents,
expectedReadOnlyTransactionSingleUseEventsCount);
break;
case "SessionPool.WaitForSession":
assertEquals(0, spanItem.getEvents().size());
break;
default:
assert false;
}
Expand Down Expand Up @@ -385,9 +370,6 @@ public void multiUse() {
expectedExecuteStreamingQueryEvents,
expectedExecuteStreamingQueryEventsCount);
break;
case "SessionPool.WaitForSession":
assertEquals(0, spanItem.getEvents().size());
break;
case "CloudSpanner.ReadOnlyTransaction":
verifyRequestEvents(
spanItem,
Expand All @@ -403,10 +385,9 @@ public void multiUse() {
}

@Test
public void transactionRunner() {
public void transactionRunner() throws InterruptedException {
TransactionRunner runner = client.readWriteTransaction();
runner.run(transaction -> transaction.executeUpdate(UPDATE_STATEMENT));

List<String> actualSpanItems = new ArrayList<>();
spanExporter
.getFinishedSpanItems()
Expand All @@ -426,7 +407,6 @@ public void transactionRunner() {
expectedBatchCreateSessionsEvents,
expectedBatchCreateSessionsEventsCount);
break;
case "SessionPool.WaitForSession":
case "CloudSpannerOperation.Commit":
assertEquals(0, spanItem.getEvents().size());
break;
Expand Down Expand Up @@ -472,9 +452,6 @@ public void transactionRunnerWithError() {
expectedBatchCreateSessionsEvents,
expectedBatchCreateSessionsEventsCount);
break;
case "SessionPool.WaitForSession":
assertEquals(0, spanItem.getEvents().size());
break;
case "CloudSpanner.ReadWriteTransaction":
verifyRequestEvents(
spanItem,
Expand Down Expand Up @@ -527,7 +504,6 @@ public void transactionRunnerWithFailedAndBeginTransaction() {
expectedBatchCreateSessionsEvents,
expectedBatchCreateSessionsEventsCount);
break;
case "SessionPool.WaitForSession":
case "CloudSpannerOperation.Commit":
case "CloudSpannerOperation.BeginTransaction":
assertEquals(0, spanItem.getEvents().size());
Expand All @@ -551,13 +527,13 @@ private void verifyRequestEvents(SpanData spanItem, List<String> expectedEvents,
spanItem.getEvents().stream().map(EventData::getName).collect(Collectors.toList());
assertEquals(eventCount, spanItem.getEvents().size());
assertEquals(
eventNames.stream().distinct().sorted().collect(Collectors.toList()),
expectedEvents.stream().sorted().collect(Collectors.toList()));
expectedEvents.stream().sorted().collect(Collectors.toList()),
eventNames.stream().distinct().sorted().collect(Collectors.toList()));
}

private static void verifySpans(List<String> actualSpanItems, List<String> expectedSpansItems) {
assertEquals(
actualSpanItems.stream().distinct().sorted().collect(Collectors.toList()),
expectedSpansItems.stream().sorted().collect(Collectors.toList()));
expectedSpansItems.stream().sorted().collect(Collectors.toList()),
actualSpanItems.stream().distinct().sorted().collect(Collectors.toList()));
}
}
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@
<version>6.59.1-SNAPSHOT</version><!-- {x-version-update:google-cloud-spanner:current} -->
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.34.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-shared-dependencies</artifactId>
Expand Down

0 comments on commit ac11f63

Please sign in to comment.