Skip to content
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

Expand test coverage for getDatabaseClient() #3686

Merged
merged 2 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions google-cloud-clients/google-cloud-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,29 @@
</parent>
<properties>
<site.installationModule>google-cloud-spanner</site.installationModule>
<jacoco.skip>true</jacoco.skip>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
snehashah16 marked this conversation as resolved.
Show resolved Hide resolved
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.spanner.spi.v1.SpannerRpc;

import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
Expand Down Expand Up @@ -70,4 +71,49 @@ public void createAndCloseSession() {
// The same channelHint is passed for deleteSession (contained in "options").
Mockito.verify(rpc).deleteSession(sessionName, options.getValue());
}

@Test
public void getDbclientAgainGivesSame() {
snehashah16 marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> labels = new HashMap<>();
labels.put("env", "dev");
Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels);
String dbName = "projects/p1/instances/i1/databases/d1";
DatabaseId db = DatabaseId.of(dbName);

Mockito.when(spannerOptions.getTransportOptions())
.thenReturn(GrpcTransportOptions.newBuilder().build());
Mockito.when(spannerOptions.getSessionPoolOptions())
.thenReturn(SessionPoolOptions.newBuilder().build());

DatabaseClient databaseClient = impl.getDatabaseClient(db);

// Get db client again
DatabaseClient databaseClient1 = impl.getDatabaseClient(db);

assertThat(databaseClient1).isSameAs(databaseClient);
}

@Test
public void getDbclientAfterCloseThrows() {
SpannerImpl imp = new SpannerImpl(rpc, 1, spannerOptions);
Map<String, String> labels = new HashMap<>();
labels.put("env", "dev");
Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels);
String dbName = "projects/p1/instances/i1/databases/d1";
DatabaseId db = DatabaseId.of(dbName);

Mockito.when(spannerOptions.getTransportOptions())
.thenReturn(GrpcTransportOptions.newBuilder().build());
Mockito.when(spannerOptions.getSessionPoolOptions())
.thenReturn(SessionPoolOptions.newBuilder().build());

imp.close();

try {
imp.getDatabaseClient(db);
fail("Expected exception");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("Cloud Spanner client has been closed");
}
}
}