Skip to content
Permalink
Browse files
fix: fixes sample tests
- add missing dependencies for tracing sample
- create test instance when none is set
- id may not end with a hyphen
- restored DB id must also not end with a hyphen
- use formatted name for quick start test
- fix build errors
- updates async example to use java.util.List instead of ImmutableList
  • Loading branch information
olavloite authored and thiagotnunes committed Oct 6, 2020
1 parent f928fd5 commit ed0665c71abbce57a28cb79531783145eccab1fb
@@ -21,6 +21,9 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opencensus.version>0.26.0</opencensus.version>
<trace.version>1.1.0</trace.version>
<cloudmonitoring.version>1.100.1</cloudmonitoring.version>
</properties>


@@ -32,6 +35,60 @@
<version>2.0.1</version>
</dependency>
<!-- [END spanner_install_without_bom] -->

<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>${opencensus.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-zpages</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>${opencensus.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-trace</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
<version>${opencensus.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-trace</artifactId>
<version>${trace.version}</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
<version>${cloudmonitoring.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
@@ -21,6 +21,9 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opencensus.version>0.26.0</opencensus.version>
<trace.version>1.1.0</trace.version>
<cloudmonitoring.version>1.100.1</cloudmonitoring.version>
</properties>

<dependencies>
@@ -31,6 +34,60 @@
<version>2.0.3-SNAPSHOT</version>
</dependency>
<!-- {x-version-update-end} -->

<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>${opencensus.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-zpages</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>${opencensus.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-trace</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
<version>${opencensus.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-trace</artifactId>
<version>${trace.version}</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
<version>${cloudmonitoring.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
@@ -21,6 +21,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opencensus.version>0.26.0</opencensus.version>
</properties>


@@ -43,7 +44,61 @@
<artifactId>google-cloud-spanner</artifactId>
</dependency>
<!-- [END spanner_install_with_bom] -->


<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<version>${opencensus.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-zpages</artifactId>
<version>${opencensus.version}</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-trace-stackdriver</artifactId>
<version>${opencensus.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-trace</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-exporter-stats-stackdriver</artifactId>
<version>${opencensus.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
<version>${opencensus.version}</version>
</dependency>
<!-- Version auto-managed by BOM -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-trace</artifactId>
</dependency>
<!-- Version auto-managed by BOM -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -17,14 +17,15 @@
package com.example.spanner;

// [START spanner_async_query_to_list]

import com.google.api.core.ApiFuture;
import com.google.cloud.spanner.AsyncResultSet;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -62,7 +63,7 @@ static void asyncQueryToList() throws InterruptedException, ExecutionException,
static void asyncQueryToList(DatabaseClient client)
throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService executor = Executors.newSingleThreadExecutor();
ApiFuture<ImmutableList<Album>> albums;
ApiFuture<List<Album>> albums;
try (AsyncResultSet resultSet =
client
.singleUse()
@@ -16,7 +16,7 @@

package com.example.spanner;

//[START spanner_create_instance]
// [START spanner_create_instance]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.spanner.Instance;
import com.google.cloud.spanner.InstanceAdminClient;
@@ -38,34 +38,36 @@ static void createInstance() {
}

static void createInstance(String projectId, String instanceId) {
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();

// Set Instance configuration.
String configId = "regional-us-central1";
int nodeCount = 2;
String displayName = "Descriptive name";
// Set Instance configuration.
String configId = "regional-us-central1";
int nodeCount = 2;
String displayName = "Descriptive name";

// Create an InstanceInfo object that will be used to create the instance.
InstanceInfo instanceInfo =
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
.setNodeCount(nodeCount)
.setDisplayName(displayName)
.build();
OperationFuture<Instance, CreateInstanceMetadata> operation =
instanceAdminClient.createInstance(instanceInfo);
try {
// Wait for the createInstance operation to finish.
Instance instance = operation.get();
System.out.printf("Instance %s was successfully created%n", instance.getId());
} catch (ExecutionException e) {
System.out.printf(
"Error: Creating instance %s failed with error message %s%n",
instanceInfo.getId(), e.getMessage());
} catch (InterruptedException e) {
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
// Create an InstanceInfo object that will be used to create the instance.
InstanceInfo instanceInfo =
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
.setNodeCount(nodeCount)
.setDisplayName(displayName)
.build();
OperationFuture<Instance, CreateInstanceMetadata> operation =
instanceAdminClient.createInstance(instanceInfo);
try {
// Wait for the createInstance operation to finish.
Instance instance = operation.get();
System.out.printf("Instance %s was successfully created%n", instance.getId());
} catch (ExecutionException e) {
System.out.printf(
"Error: Creating instance %s failed with error message %s%n",
instanceInfo.getId(), e.getMessage());
} catch (InterruptedException e) {
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
}
}
}
}
//[END spanner_create_instance]
// [END spanner_create_instance]
@@ -177,6 +177,9 @@ static String createRestoredSampleDbId(DatabaseId database) {
if (restoredDbId.length() > 30) {
restoredDbId = restoredDbId.substring(0, 30);
}
if (restoredDbId.endsWith("-")) {
restoredDbId = restoredDbId.substring(0, restoredDbId.length() - 1);
}
return restoredDbId;
}

0 comments on commit ed0665c

Please sign in to comment.