Skip to content

Commit

Permalink
spanner: expand test coverage for getDatabaseClient() (googleapis#3686)
Browse files Browse the repository at this point in the history
This change also adds the jacoco coverage plugin in the spanner pom.xml
and sets jacoco.skip to true to disable it by default. It can be enabled
by passing -Djacoco.skip=false to the mvn command.
  • Loading branch information
nithinsujir authored and pongad committed Sep 17, 2018
1 parent ae614b3 commit a175980
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
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>
<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() {
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");
}
}
}

0 comments on commit a175980

Please sign in to comment.