Skip to content

Commit 8ecd043

Browse files
author
Ajay Kannan
committed
Renames LocalGcdHelper --> LocalDatastoreHelper and RemoteGcsHelper --> RemoteStorageHelper, also removes main()-related functions from LocalDatastoreHelper
1 parent c1e71e1 commit 8ecd043

File tree

12 files changed

+151
-155
lines changed

12 files changed

+151
-155
lines changed

TESTING.md

+27-26
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,57 @@ This library provides tools to help write tests for code that uses the following
1111

1212
#### On your machine
1313

14-
You can test against a temporary local datastore by following these steps:
14+
You can test against a temporary local Datastore by following these steps:
1515

16-
1. Start the local datastore emulator using `LocalGcdHelper`. This can be done in two ways:
17-
- Run `LocalGcdHelper.java`'s `main` method with `START` provided as an argument, followed by optional arguments `--port=<port number>` and `--consistency=<float between 0 and 1, inclusive>`. This will create a temporary folder on your computer and bind `localhost:<port number>` for communication with the local datastore. If no port number is specified, port 8080 will be used. The consistency setting controls the fraction of Datastore writes that are immediately visible in global queries.
18-
- Use the `LocalGcdHelper.start(String projectId, int port, double consistency)` method before running your tests. For example, you can use the following code to start the local Datastore on any available port with the consistency set to 0.9:
19-
```java
20-
int port = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
21-
LocalGcdHelper helper = LocalGcdHelper.start("my-project-id", port, 0.9);
22-
```
16+
1. Start the local Datastore emulator before running your tests using `LocalDatastoreHelper`'s `start` method. This will create a temporary folder on your computer and bind a port for communication with the local datastore. There are two optional arguments for `start`: project ID and consistency. The consistency setting controls the fraction of Datastore writes that are immediately visible in global queries.
17+
```java
18+
// Use defaults for project ID and consistency
19+
LocalDatastoreHelper helper = LocalDatastoreHelper.start();
20+
// or explicitly provide them
21+
helper = LocalDatastoreHelper.start("my-project-id", 0.6);
22+
```
2323

24-
2. In your program, create and use a `Datastore` object with the options given by the `LocalGcdHelper` instance. For example:
24+
2. In your program, create and use a `Datastore` object with the options given by the `LocalDatastoreHelper` instance. For example:
2525
```java
26-
Datastore localDatastore = helper.options().service()
26+
Datastore localDatastore = helper.options().service();
2727
```
28+
2829
3. Run your tests.
2930

30-
4. Stop the local datastore emulator.
31-
- If you ran `LocalGcdHelper.java`'s `main` function to start the emulator, run `LocalGcdHelper.java`'s `main` method with arguments `STOP` and (optionally) `--port=<port number>`. If the port is not supplied, the program will attempt to close the last port started.
32-
- If you ran the `LocalGcdHelper.start` method to start the emulator, call the `stop()` method on the `LocalGcdHelper` object returned by `LocalGcdHelper.start`.
31+
4. Stop the local datastore emulator by calling the `stop()` method, like so:
32+
```java
33+
helper.stop();
34+
```
3335

3436
#### On a remote machine
3537

36-
You can test against a remote datastore emulator as well. To do this, set the `DatastoreOptions` project endpoint to the hostname of the remote machine, like the example below.
38+
You can test against a remote Datastore emulator as well. To do this, set the `DatastoreOptions` project endpoint to the hostname of the remote machine, like the example below.
3739

3840
```java
39-
DatastoreOptions options = DatastoreOptions.builder()
40-
.projectId(PROJECT_ID)
41+
DatastoreOptions options = LocalDatastoreHelper.options()
42+
.toBuilder()
4143
.host("http://<hostname of machine>:<port>")
42-
.authCredentials(AuthCredentials.noAuth())
4344
.build();
4445
Datastore localDatastore = options.service();
4546
```
4647

47-
Note that the remote datastore must be running before your tests are run.
48+
Note that the remote Datastore emulator must be running before your tests are run.
4849

4950
### Testing code that uses Storage
5051

51-
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteGcsHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
52+
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
5253

5354
1. Create a test Google Cloud project.
5455

5556
2. Download a JSON service account credentials file from the Google Developer's Console. See more about this on the [Google Cloud Platform Storage Authentication page][cloud-platform-storage-authentication].
5657

57-
3. Create a `RemoteGcsHelper` object using your project ID and JSON key.
58-
Here is an example that uses the `RemoteGcsHelper` to create a bucket.
58+
3. Create a `RemoteStorageHelper` object using your project ID and JSON key.
59+
Here is an example that uses the `RemoteStorageHelper` to create a bucket.
5960
```java
60-
RemoteGcsHelper gcsHelper =
61-
RemoteGcsHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
62-
Storage storage = gcsHelper.options().service();
63-
String bucket = RemoteGcsHelper.generateBucketName();
61+
RemoteStorageHelper helper =
62+
RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
63+
Storage storage = helper.options().service();
64+
String bucket = RemoteStorageHelper.generateBucketName();
6465
storage.create(BucketInfo.of(bucket));
6566
```
6667

@@ -69,7 +70,7 @@ Here is an example that uses the `RemoteGcsHelper` to create a bucket.
6970
5. Clean up the test project by using `forceDelete` to clear any buckets used.
7071
Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
7172
```java
72-
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
73+
RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
7374
```
7475

7576
### Testing code that uses Resource Manager

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
import com.google.gcloud.storage.BlobInfo;
6868
import com.google.gcloud.storage.BucketInfo;
6969
import com.google.gcloud.storage.Storage;
70-
import com.google.gcloud.storage.testing.RemoteGcsHelper;
70+
import com.google.gcloud.storage.testing.RemoteStorageHelper;
7171

7272
import org.junit.AfterClass;
7373
import org.junit.BeforeClass;
@@ -134,7 +134,7 @@ public class ITBigQueryTest {
134134
private static final String LOAD_FILE = "load.csv";
135135
private static final String JSON_LOAD_FILE = "load.json";
136136
private static final String EXTRACT_FILE = "extract.csv";
137-
private static final String BUCKET = RemoteGcsHelper.generateBucketName();
137+
private static final String BUCKET = RemoteStorageHelper.generateBucketName();
138138
private static final TableId TABLE_ID = TableId.of(DATASET, "testing_table");
139139
private static final String CSV_CONTENT = "StringValue1\nStringValue2\n";
140140
private static final String JSON_CONTENT = "{"
@@ -171,7 +171,7 @@ public class ITBigQueryTest {
171171
@BeforeClass
172172
public static void beforeClass() throws InterruptedException {
173173
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
174-
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create();
174+
RemoteStorageHelper gcsHelper = RemoteStorageHelper.create();
175175
bigquery = bigqueryHelper.options().service();
176176
storage = gcsHelper.options().service();
177177
storage.create(BucketInfo.of(BUCKET));
@@ -199,7 +199,7 @@ public static void afterClass() throws ExecutionException, InterruptedException
199199
RemoteBigQueryHelper.forceDelete(bigquery, DATASET);
200200
}
201201
if (storage != null) {
202-
boolean wasDeleted = RemoteGcsHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS);
202+
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS);
203203
if (!wasDeleted && LOG.isLoggable(Level.WARNING)) {
204204
LOG.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
205205
}

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public RestorableState<AuthCredentials> capture() {
260260
/**
261261
* A placeholder for credentials to signify that requests sent to the server should not be
262262
* authenticated. This is typically useful when using the local service emulators, such as
263-
* {@code LocalGcdHelper} and {@code LocalResourceManagerHelper}.
263+
* {@code LocalDatastoreHelper} and {@code LocalResourceManagerHelper}.
264264
*/
265265
public static class NoAuthCredentials extends AuthCredentials {
266266

@@ -341,7 +341,7 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
341341

342342
/**
343343
* Creates a placeholder denoting that no credentials should be used. This is typically useful
344-
* when using the local service emulators, such as {@code LocalGcdHelper} and
344+
* when using the local service emulators, such as {@code LocalDatastoreHelper} and
345345
* {@code LocalResourceManagerHelper}.
346346
*/
347347
public static AuthCredentials noAuth() {

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalDatastoreHelper.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
*/
6565
public class LocalDatastoreHelper {
6666
private static final Logger log = Logger.getLogger(LocalDatastoreHelper.class.getName());
67+
public static final String DEFAULT_PROJECT_ID = "projectid1";
6768
private static final double DEFAULT_CONSISTENCY = 0.9;
6869
private static final String GCD_VERSION = "v1beta2";
6970
private static final String GCD_BUILD = "rev1-2.1.2b";
@@ -81,6 +82,7 @@ public class LocalDatastoreHelper {
8182
private ProcessStreamReader processReader;
8283
private ProcessErrorStreamReader processErrorReader;
8384
private final int port;
85+
private final double consistency;
8486

8587
static {
8688
INSTALLED_GCD_PATH = installedGcdPath();
@@ -537,8 +539,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
537539
});
538540
}
539541

540-
private LocalDatastoreHelper(String projectId) {
542+
private LocalDatastoreHelper(String projectId, double consistency) {
541543
this.projectId = projectId;
544+
this.consistency = consistency;
542545
this.port = findAvailablePort();
543546
}
544547

@@ -576,6 +579,13 @@ public int port() {
576579
return port;
577580
}
578581

582+
/**
583+
* Returns the consistency setting for the local Datastore emulator.
584+
*/
585+
public double consistency() {
586+
return consistency;
587+
}
588+
579589
/**
580590
* Starts the local Datastore for the specific project.
581591
*
@@ -590,24 +600,23 @@ public int port() {
590600
*/
591601
public static LocalDatastoreHelper start(String projectId, double consistency)
592602
throws IOException, InterruptedException {
593-
LocalDatastoreHelper helper = new LocalDatastoreHelper(projectId);
594-
helper.start(consistency);
603+
LocalDatastoreHelper helper = new LocalDatastoreHelper(projectId, consistency);
604+
helper.findAndStartGcd();
595605
return helper;
596606
}
597607

598608
/**
599-
* Starts the local Datastore for the specific project using a default consistency setting of 0.9.
609+
* Starts the local Datastore using defaults for project ID and consistency setting of 0.9.
600610
*
601611
* This will unzip the gcd tool, create the project and start it. All content is written to a
602612
* temporary directory that will be deleted when {@link #stop()} is called (or when the program
603613
* terminates) to make sure that no left-over data from prior runs is used.
604614
*/
605-
public static LocalDatastoreHelper start(String projectId)
606-
throws IOException, InterruptedException {
607-
return start(projectId, DEFAULT_CONSISTENCY);
615+
public static LocalDatastoreHelper start() throws IOException, InterruptedException {
616+
return start(DEFAULT_PROJECT_ID, DEFAULT_CONSISTENCY);
608617
}
609618

610-
private void start(double consistency) throws IOException, InterruptedException {
619+
private void findAndStartGcd() throws IOException, InterruptedException {
611620
// send a quick request in case we have a hanging process from a previous run
612621
checkArgument(consistency >= 0.0 && consistency <= 1.0, "Consistency must be between 0 and 1");
613622
sendQuitRequest(port);

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/package-info.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
* <p>A simple usage example:
2121
* <p>Before the test:
2222
* <pre> {@code
23-
* LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT_NUMBER, CONSISTENCY);
24-
* Datastore localDatastore = gcdHelper.options().service();
23+
* LocalDatastoreHelper helper = LocalDatastoreHelper.start();
24+
* Datastore localDatastore = helper.options().service();
2525
* } </pre>
2626
*
2727
* <p>After the test:
2828
* <pre> {@code
29-
* gcdHelper.stop();
29+
* helper.stop();
3030
* } </pre>
3131
*
3232
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore">

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424

2525
import com.google.gcloud.datastore.spi.DatastoreRpc;
2626
import com.google.gcloud.datastore.spi.DatastoreRpcFactory;
27-
import com.google.gcloud.datastore.testing.LocalDatastoreHelper;
2827

2928
import org.easymock.EasyMock;
3029
import org.junit.Before;
3130
import org.junit.Test;
3231

3332
public class DatastoreOptionsTest {
3433

35-
private static final String PROJECT_ID = "project_id";
36-
private static final int PORT = LocalDatastoreHelper.findAvailablePort(LocalDatastoreHelper.DEFAULT_PORT);
34+
private static final String PROJECT_ID = "project-id";
35+
private static final int PORT = 8080;
3736
private DatastoreRpcFactory datastoreRpcFactory;
3837
private DatastoreRpc datastoreRpc;
3938
private DatastoreOptions.Builder options;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.datastore;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertSame;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import com.google.gcloud.AuthCredentials;
24+
import com.google.gcloud.datastore.testing.LocalDatastoreHelper;
25+
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
import java.io.IOException;
31+
32+
@RunWith(JUnit4.class)
33+
public class LocalDatastoreHelperTest {
34+
35+
private static final String PROJECT_ID = "some-project-id";
36+
private static final double TOLERANCE = 0.00001;
37+
38+
@Test
39+
public void testStart() throws IOException, InterruptedException {
40+
LocalDatastoreHelper helper = LocalDatastoreHelper.start(PROJECT_ID, 0.75);
41+
assertTrue(Math.abs(0.75 - helper.consistency()) < TOLERANCE);
42+
assertEquals(PROJECT_ID, helper.projectId());
43+
helper.stop();
44+
helper = LocalDatastoreHelper.start();
45+
assertTrue(Math.abs(0.9 - helper.consistency()) < TOLERANCE);
46+
assertEquals(LocalDatastoreHelper.DEFAULT_PROJECT_ID, helper.projectId());
47+
helper.stop();
48+
}
49+
50+
@Test
51+
public void testOptions() throws IOException, InterruptedException {
52+
LocalDatastoreHelper helper = LocalDatastoreHelper.start();
53+
DatastoreOptions options = helper.options();
54+
assertEquals(LocalDatastoreHelper.DEFAULT_PROJECT_ID, options.projectId());
55+
assertEquals("http://localhost:" + helper.port(), options.host());
56+
assertSame(AuthCredentials.noAuth(), options.authCredentials());
57+
helper.stop();
58+
}
59+
}

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelperTest.java

-73
This file was deleted.

0 commit comments

Comments
 (0)