Skip to content

Commit 1dc90b0

Browse files
authored
tests: add additional tests for client ids (#212)
* tests: add additional tests for client ids * fix: close Spanner to prevent resource leak
1 parent a2fbbed commit 1dc90b0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.grpc.StatusRuntimeException;
4242
import io.grpc.inprocess.InProcessServerBuilder;
4343
import java.io.IOException;
44+
import java.util.Arrays;
4445
import java.util.List;
4546
import java.util.concurrent.ScheduledThreadPoolExecutor;
4647
import java.util.concurrent.TimeUnit;
@@ -927,4 +928,36 @@ public void testBackendPartitionQueryOptions() {
927928
assertThat(request.getQueryOptions().getOptimizerVersion()).isEqualTo("1");
928929
}
929930
}
931+
932+
@Test
933+
public void testClientIdReusedOnDatabaseNotFound() {
934+
mockSpanner.setBatchCreateSessionsExecutionTime(
935+
SimulatedExecutionTime.ofStickyException(
936+
SpannerExceptionFactoryTest.newStatusResourceNotFoundException(
937+
"my-database",
938+
SpannerExceptionFactory.DATABASE_RESOURCE_TYPE,
939+
"project/my-project/instances/my-instance/databases/my-database")));
940+
try (Spanner spanner =
941+
SpannerOptions.newBuilder()
942+
.setProjectId("my-project")
943+
.setChannelProvider(channelProvider)
944+
.setCredentials(NoCredentials.getInstance())
945+
.build()
946+
.getService()) {
947+
DatabaseId databaseId = DatabaseId.of("my-project", "my-instance", "my-database");
948+
String prevClientId = null;
949+
for (int i = 0; i < 100; i++) {
950+
try {
951+
DatabaseClientImpl client = (DatabaseClientImpl) spanner.getDatabaseClient(databaseId);
952+
if (prevClientId != null) {
953+
assertThat(client.clientId).isEqualTo(prevClientId);
954+
}
955+
prevClientId = client.clientId;
956+
client.singleUse().readRow("MyTable", Key.of(0), Arrays.asList("MyColumn"));
957+
} catch (Exception e) {
958+
// ignore
959+
}
960+
}
961+
}
962+
}
930963
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ public void testClientId() throws Exception {
231231
assertThat(revalidated).isNotSameInstanceAs(databaseClient);
232232
assertThat(revalidated.clientId).isEqualTo(databaseClient.clientId);
233233

234+
// Now invalidate the second client and request a new one.
235+
revalidated.pool.setResourceNotFoundException(
236+
new DatabaseNotFoundException(DoNotConstructDirectly.ALLOWED, "not found", null, null));
237+
DatabaseClientImpl revalidated2 = (DatabaseClientImpl) impl.getDatabaseClient(db);
238+
assertThat(revalidated2).isNotSameInstanceAs(revalidated);
239+
assertThat(revalidated2.clientId).isEqualTo(revalidated.clientId);
240+
234241
// Create a new Spanner instance. This will generate new database clients with new ids.
235242
try (Spanner spanner =
236243
SpannerOptions.newBuilder()

0 commit comments

Comments
 (0)