diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java index 3b61e0cb92..ac57476c12 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java @@ -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; @@ -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 + } + } + } + } } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java index 25f3381bd1..25a77016b8 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java @@ -223,6 +223,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()