Skip to content

Commit

Permalink
tests: add additional tests for client ids (#212)
Browse files Browse the repository at this point in the history
* tests: add additional tests for client ids

* fix: close Spanner to prevent resource leak
  • Loading branch information
olavloite committed May 21, 2020
1 parent a2fbbed commit 1dc90b0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Expand Up @@ -41,6 +41,7 @@
import io.grpc.StatusRuntimeException;
import io.grpc.inprocess.InProcessServerBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -927,4 +928,36 @@ public void testBackendPartitionQueryOptions() {
assertThat(request.getQueryOptions().getOptimizerVersion()).isEqualTo("1");
}
}

@Test
public void testClientIdReusedOnDatabaseNotFound() {
mockSpanner.setBatchCreateSessionsExecutionTime(
SimulatedExecutionTime.ofStickyException(
SpannerExceptionFactoryTest.newStatusResourceNotFoundException(
"my-database",
SpannerExceptionFactory.DATABASE_RESOURCE_TYPE,
"project/my-project/instances/my-instance/databases/my-database")));
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId("my-project")
.setChannelProvider(channelProvider)
.setCredentials(NoCredentials.getInstance())
.build()
.getService()) {
DatabaseId databaseId = DatabaseId.of("my-project", "my-instance", "my-database");
String prevClientId = null;
for (int i = 0; i < 100; i++) {
try {
DatabaseClientImpl client = (DatabaseClientImpl) spanner.getDatabaseClient(databaseId);
if (prevClientId != null) {
assertThat(client.clientId).isEqualTo(prevClientId);
}
prevClientId = client.clientId;
client.singleUse().readRow("MyTable", Key.of(0), Arrays.asList("MyColumn"));
} catch (Exception e) {
// ignore
}
}
}
}
}
Expand Up @@ -231,6 +231,13 @@ public void testClientId() throws Exception {
assertThat(revalidated).isNotSameInstanceAs(databaseClient);
assertThat(revalidated.clientId).isEqualTo(databaseClient.clientId);

// Now invalidate the second client and request a new one.
revalidated.pool.setResourceNotFoundException(
new DatabaseNotFoundException(DoNotConstructDirectly.ALLOWED, "not found", null, null));
DatabaseClientImpl revalidated2 = (DatabaseClientImpl) impl.getDatabaseClient(db);
assertThat(revalidated2).isNotSameInstanceAs(revalidated);
assertThat(revalidated2.clientId).isEqualTo(revalidated.clientId);

// Create a new Spanner instance. This will generate new database clients with new ids.
try (Spanner spanner =
SpannerOptions.newBuilder()
Expand Down

0 comments on commit 1dc90b0

Please sign in to comment.