Skip to content

Commit dac0698

Browse files
author
Ajay Kannan
committed
Clean up local gcd helper and docs
1 parent 6747274 commit dac0698

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

TESTING.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ This library provides tools to help write tests for code that uses the following
1414
You can test against a temporary local datastore by following these steps:
1515

1616
1. Start the local datastore emulator using `LocalGcdHelper`. This can be done in two ways:
17-
- Run `LocalGcdHelper.java`'s `main` method with arguments `START` and (optionally) `--port=<port number>`. This will create a temporary folder on your computer and bind `localhost:<port number>` for communication with the local datastore. The port number is an optional argument. If no port number is specified, port 8080 will be used.
18-
- Call `LocalGcdHelper.start(<project ID>, <port number>)` before running your tests. Save the `LocalGcdHelper` object returned so that you can stop the emulator later.
19-
20-
2. In your program, create and use a datastore whose host is set host to `localhost:<port number>`. For example,
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+
```
23+
24+
2. In your program, create and use a `Datastore` object with the options given by the `LocalGcdHelper` instance. For example:
2125
```java
22-
DatastoreOptions options = DatastoreOptions.builder()
23-
.projectId(PROJECT_ID)
24-
.host("http://localhost:8080")
25-
.build();
26-
Datastore localDatastore = options.service();
26+
Datastore localDatastore = helper.options().service()
2727
```
2828
3. Run your tests.
2929

3030
4. Stop the local datastore emulator.
3131
- 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 `LocalGcdHelper.start()` to start the emulator, call the `stop()` method on the `LocalGcdHelper` object returned by `LocalGcdHelper.start()`.
32+
- If you ran the `LocalGcdHelper.start` method to start the emulator, call the `stop()` method on the `LocalGcdHelper` object returned by `LocalGcdHelper.start`.
3333

3434
#### On a remote machine
3535

@@ -39,6 +39,7 @@ You can test against a remote datastore emulator as well. To do this, set the `
3939
DatastoreOptions options = DatastoreOptions.builder()
4040
.projectId(PROJECT_ID)
4141
.host("http://<hostname of machine>:<port>")
42+
.authCredentials(AuthCredentials.noAuth())
4243
.build();
4344
Datastore localDatastore = options.service();
4445
```
@@ -134,4 +135,4 @@ Here is an example that clears the dataset created in Step 3.
134135
```
135136

136137
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts
137-
[create-service-account]:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
138+
[create-service-account]:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static java.nio.charset.StandardCharsets.UTF_8;
2222

2323
import com.google.common.base.Strings;
24+
import com.google.gcloud.AuthCredentials;
25+
import com.google.gcloud.datastore.DatastoreOptions;
2426

2527
import java.io.BufferedInputStream;
2628
import java.io.BufferedOutputStream;
@@ -393,6 +395,18 @@ public LocalGcdHelper(String projectId, int port) {
393395
this.port = port;
394396
}
395397

398+
/**
399+
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator
400+
* on localhost.
401+
*/
402+
public DatastoreOptions options() {
403+
return DatastoreOptions.builder()
404+
.projectId(projectId)
405+
.host("http://localhost:" + Integer.toString(port))
406+
.authCredentials(AuthCredentials.noAuth())
407+
.build();
408+
}
409+
396410
/**
397411
* Starts the local datastore for the specific project.
398412
*
@@ -639,10 +653,13 @@ private static Map<String, String> parseArgs(String[] args) {
639653
for (String arg : args) {
640654
if (arg.startsWith("--port=")) {
641655
parsedArgs.put("port", arg.substring("--port=".length()));
656+
} else if (arg.startsWith("--consistency=")) {
657+
parsedArgs.put("consistency", arg.substring("--consistency=".length()));
642658
} else if (arg.equals("START") || arg.equals("STOP")) {
643659
parsedArgs.put("action", arg);
644660
} else {
645-
throw new RuntimeException("Only accepts START, STOP, and --port=<port #> as arguments");
661+
throw new RuntimeException("Only accepts START, STOP, --port=<port #> and "
662+
+ "--consistency=<double in range [0,1]> as arguments.");
646663
}
647664
}
648665
if (parsedArgs.get("action") == null) {

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@
2020
* <p>A simple usage example:
2121
* <p>Before the test:
2222
* <pre> {@code
23-
* LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT_NUMBER);
24-
* DatastoreOptions options = DatastoreOptions.builder()
25-
* .projectId(PROJECT_ID)
26-
* .host("localhost:8080")
27-
* .build();
28-
* Datastore localDatastore = options.service();
23+
* LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT_NUMBER, CONSISTENCY);
24+
* Datastore localDatastore = gcdHelper.options().service();
2925
* } </pre>
3026
*
3127
* <p>After the test:

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.google.api.services.datastore.DatastoreV1.RunQueryResponse;
3333
import com.google.common.collect.Iterators;
3434
import com.google.common.collect.Lists;
35-
import com.google.gcloud.AuthCredentials;
3635
import com.google.gcloud.RetryParams;
3736
import com.google.gcloud.datastore.Query.ResultType;
3837
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
@@ -126,13 +125,8 @@ public static void beforeClass() throws IOException, InterruptedException {
126125

127126
@Before
128127
public void setUp() {
129-
options = DatastoreOptions.builder()
130-
.projectId(PROJECT_ID)
131-
.host("http://localhost:" + PORT)
132-
.authCredentials(AuthCredentials.noAuth())
133-
.retryParams(RetryParams.noRetries())
134-
.build();
135-
datastore = options.service();
128+
datastore =
129+
gcdHelper.options().toBuilder().retryParams(RetryParams.noRetries()).build().service();
136130
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
137131
QueryResults<Key> result = datastore.run(query);
138132
datastore.delete(Iterators.toArray(result, Key.class));

0 commit comments

Comments
 (0)