Skip to content

Commit 577458f

Browse files
author
Ajay Kannan
committed
Add create and start in LocalDatastoreHelper, minor fixes to docs and variable naming
1 parent 7edd544 commit 577458f

File tree

12 files changed

+67
-56
lines changed

12 files changed

+67
-56
lines changed

TESTING.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ This library provides tools to help write tests for code that uses the following
1313

1414
You can test against a temporary local Datastore by following these steps:
1515

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.
16+
1. Start the local Datastore emulator before running your tests using `LocalDatastoreHelper`'s `create` and `start` methods. 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 `create`: project ID and consistency. The consistency setting controls the fraction of Datastore writes that are immediately visible in global queries.
1717
```java
18-
// Use defaults for project ID and consistency
19-
LocalDatastoreHelper helper = LocalDatastoreHelper.start();
18+
// Use a placeholder project ID and the default consistency setting of 0.9
19+
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
2020
// or explicitly provide them
21-
helper = LocalDatastoreHelper.start("my-project-id", 0.6);
21+
helper = LocalDatastoreHelper.create("my-project-id", 0.6);
22+
23+
helper.start(); // Starts the local Datastore emulator in a separate process
2224
```
2325

24-
2. In your program, create and use a `Datastore` object with the options given by the `LocalDatastoreHelper` instance. For example:
26+
2. Create and use a `Datastore` object with the options given by the `LocalDatastoreHelper` instance. For example:
2527
```java
2628
Datastore localDatastore = helper.options().service();
2729
```
2830

31+
Note that you must call `start` before `options()`; otherwise, you will see an `IOException` regarding a refused connection.
32+
2933
3. Run your tests.
3034

3135
4. Stop the local datastore emulator by calling the `stop()` method, like so:
@@ -38,14 +42,19 @@ You can test against a temporary local Datastore by following these steps:
3842
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.
3943

4044
```java
41-
DatastoreOptions options = LocalDatastoreHelper.options()
42-
.toBuilder()
45+
DatastoreOptions options = DatastoreOptions.builder()
46+
.projectId("my-project-id") // must match project ID specified on remote machine
4347
.host("http://<hostname of machine>:<port>")
48+
.authCredentials(AuthCredentials.noAuth())
4449
.build();
4550
Datastore localDatastore = options.service();
4651
```
4752

48-
Note that the remote Datastore emulator must be running before your tests are run.
53+
Note that the remote Datastore emulator must be running before running the code above. We recommend that you start the emulator on the remote machine using the [Google Cloud SDK](https://cloud.google.com/sdk/gcloud/reference/beta/emulators/datastore/) from command line, as shown below:
54+
55+
```
56+
gcloud beta emulators datastore start
57+
```
4958

5059
### Testing code that uses Storage
5160

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ public class ITBigQueryTest {
171171
@BeforeClass
172172
public static void beforeClass() throws InterruptedException {
173173
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
174-
RemoteStorageHelper gcsHelper = RemoteStorageHelper.create();
174+
RemoteStorageHelper storageHelper = RemoteStorageHelper.create();
175175
bigquery = bigqueryHelper.options().service();
176-
storage = gcsHelper.options().service();
176+
storage = storageHelper.options().service();
177177
storage.create(BucketInfo.of(BUCKET));
178178
storage.create(BlobInfo.builder(BUCKET, LOAD_FILE).contentType("text/plain").build(),
179179
CSV_CONTENT.getBytes(StandardCharsets.UTF_8));

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

+23-21
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ private LocalDatastoreHelper(String projectId, double consistency) {
545545
this.port = findAvailablePort();
546546
}
547547

548-
private static int findAvailablePort() {
548+
public static int findAvailablePort() {
549549
try (ServerSocket tempSocket = new ServerSocket(0)) {
550550
return tempSocket.getLocalPort();
551551
} catch (IOException e) {
@@ -555,7 +555,7 @@ private static int findAvailablePort() {
555555

556556
/**
557557
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator
558-
* on localhost.
558+
* on localhost. This should only be called after calling {@link #start}.
559559
*/
560560
public DatastoreOptions options() {
561561
return DatastoreOptions.builder()
@@ -587,36 +587,38 @@ public double consistency() {
587587
}
588588

589589
/**
590-
* Starts the local Datastore for the specific project.
591-
*
592-
* This will unzip the gcd tool, create the project and start it. All content is written to a
593-
* temporary directory that will be deleted when {@link #stop()} is called (or when the program
594-
* terminates) to make sure that no left-over data from prior runs is used.
590+
* Creates a local Datastore helper with the specified settings for project ID and consistency.
595591
*
596592
* @param consistency the fraction of Datastore writes that are immediately visible to global
597-
* queries, with 0.0 resulting in no attempts succeeding, and 1.0 resulting in all attempts succeeding. Note
598-
* that setting this to 1.0 may mask incorrect assumptions about the consistency of
599-
* non-ancestor queries; non-ancestor queries are eventually consistent.
593+
* queries, with 0.0 meaning no writes are immediately visible and 1.0 meaning all writes
594+
* are immediately visible. Note that setting this to 1.0 may mask incorrect assumptions
595+
* about the consistency of non-ancestor queries; non-ancestor queries are eventually
596+
* consistent.
600597
*/
601-
public static LocalDatastoreHelper start(String projectId, double consistency)
602-
throws IOException, InterruptedException {
598+
public static LocalDatastoreHelper create(String projectId, double consistency) {
603599
LocalDatastoreHelper helper = new LocalDatastoreHelper(projectId, consistency);
604-
helper.findAndStartGcd();
605600
return helper;
606601
}
607602

608603
/**
609-
* Starts the local Datastore using defaults for project ID and consistency setting of 0.9.
610-
*
611-
* This will unzip the gcd tool, create the project and start it. All content is written to a
612-
* temporary directory that will be deleted when {@link #stop()} is called (or when the program
613-
* terminates) to make sure that no left-over data from prior runs is used.
604+
* Creates a local Datastore helper with a placeholder project ID and the default consistency
605+
* setting of 0.9. Consistency refers to the fraction of Datastore writes that are immediately
606+
* visible to global queries, with 0.0 meaning no writes are immediately visible and 1.0 meaning
607+
* all writes are immediately visible.
614608
*/
615-
public static LocalDatastoreHelper start() throws IOException, InterruptedException {
616-
return start(DEFAULT_PROJECT_ID, DEFAULT_CONSISTENCY);
609+
public static LocalDatastoreHelper create() {
610+
return create(DEFAULT_PROJECT_ID, DEFAULT_CONSISTENCY);
617611
}
618612

619-
private void findAndStartGcd() throws IOException, InterruptedException {
613+
/**
614+
* Starts the local Datastore emulator. Leftover data from previous uses of the emulator will be
615+
* removed.
616+
*
617+
* @throws InterruptedException if emulator-related tasks are interrupted
618+
* @throws IOException if there are socket exceptions or issues creating/deleting the temporary
619+
* data folder
620+
*/
621+
public void start() throws IOException, InterruptedException {
620622
// send a quick request in case we have a hanging process from a previous run
621623
checkArgument(consistency >= 0.0 && consistency <= 1.0, "Consistency must be between 0 and 1");
622624
sendQuitRequest(port);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* <p>A simple usage example:
2121
* <p>Before the test:
2222
* <pre> {@code
23-
* LocalDatastoreHelper helper = LocalDatastoreHelper.start();
23+
* LocalDatastoreHelper helper = LocalDatastoreHelper.create();
24+
* helper.start();
2425
* Datastore localDatastore = helper.options().service();
2526
* } </pre>
2627
*

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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;
2728

2829
import org.easymock.EasyMock;
2930
import org.junit.Before;
@@ -32,7 +33,7 @@
3233
public class DatastoreOptionsTest {
3334

3435
private static final String PROJECT_ID = "project-id";
35-
private static final int PORT = 8080;
36+
private static final int PORT = LocalDatastoreHelper.findAvailablePort();
3637
private DatastoreRpcFactory datastoreRpcFactory;
3738
private DatastoreRpc datastoreRpc;
3839
private DatastoreOptions.Builder options;

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ public class DatastoreTest {
110110
private DatastoreOptions options;
111111
private Datastore datastore;
112112

113-
private static LocalDatastoreHelper gcdHelper;
113+
private static LocalDatastoreHelper helper = LocalDatastoreHelper.create(PROJECT_ID, 1.0);
114114

115115
@Rule
116116
public ExpectedException thrown = ExpectedException.none();
117117

118118
@BeforeClass
119119
public static void beforeClass() throws IOException, InterruptedException {
120-
gcdHelper = LocalDatastoreHelper.start(PROJECT_ID, 1.0);
120+
helper.start();
121121
}
122122

123123
@Before
124124
public void setUp() {
125-
options = gcdHelper.options().toBuilder().retryParams(RetryParams.noRetries()).build();
125+
options = helper.options().toBuilder().retryParams(RetryParams.noRetries()).build();
126126
datastore = options.service();
127127
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
128128
QueryResults<Key> result = datastore.run(query);
@@ -132,8 +132,8 @@ public void setUp() {
132132

133133
@AfterClass
134134
public static void afterClass() throws IOException, InterruptedException {
135-
if (gcdHelper != null) {
136-
gcdHelper.stop();
135+
if (helper != null) {
136+
helper.stop();
137137
}
138138
}
139139

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,22 @@ public class LocalDatastoreHelperTest {
3636
private static final double TOLERANCE = 0.00001;
3737

3838
@Test
39-
public void testStart() throws IOException, InterruptedException {
40-
LocalDatastoreHelper helper = LocalDatastoreHelper.start(PROJECT_ID, 0.75);
39+
public void testCreate() {
40+
LocalDatastoreHelper helper = LocalDatastoreHelper.create(PROJECT_ID, 0.75);
4141
assertTrue(Math.abs(0.75 - helper.consistency()) < TOLERANCE);
4242
assertEquals(PROJECT_ID, helper.projectId());
43-
helper.stop();
44-
helper = LocalDatastoreHelper.start();
43+
helper = LocalDatastoreHelper.create();
4544
assertTrue(Math.abs(0.9 - helper.consistency()) < TOLERANCE);
4645
assertEquals(LocalDatastoreHelper.DEFAULT_PROJECT_ID, helper.projectId());
47-
helper.stop();
4846
}
4947

5048
@Test
5149
public void testOptions() throws IOException, InterruptedException {
52-
LocalDatastoreHelper helper = LocalDatastoreHelper.start();
50+
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
51+
helper.start();
5352
DatastoreOptions options = helper.options();
5453
assertEquals(LocalDatastoreHelper.DEFAULT_PROJECT_ID, options.projectId());
5554
assertEquals("http://localhost:" + helper.port(), options.host());
5655
assertSame(AuthCredentials.noAuth(), options.authCredentials());
57-
helper.stop();
5856
}
5957
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* // request processing synchronous.
2525
* long delay = 0;
2626
* LocalDnsHelper dnsHelper = LocalDnsHelper.create(delay);
27-
* Dns dns = dnsHelper.options().service();
2827
* dnsHelper.start();
28+
* Dns dns = dnsHelper.options().service();
2929
* }</pre>
3030
*
3131
* <p>After the test:

gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/DatastoreExample.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,14 @@ public String getRequiredParams() {
182182

183183
public static void main(String... args) {
184184
String projectId = args.length > 0 ? args[0] : null;
185-
// If you want to access a local Datastore running via the gcd sdk, do
185+
// If you want to access a local Datastore running via the Google Cloud SDK, do
186186
// DatastoreOptions options = DatastoreOptions.builder()
187187
// .projectId(projectId)
188188
// .namespace(NAMESPACE)
189-
// .host("http://localhost:8080")
189+
// .host("http://localhost:8080") // change 8080 to the port that the emulator listens to
190190
// .build();
191-
DatastoreOptions options = DatastoreOptions.builder()
192-
.projectId(projectId)
193-
.namespace(NAMESPACE)
194-
.build();
191+
DatastoreOptions options =
192+
DatastoreOptions.builder().projectId(projectId).namespace(NAMESPACE).build();
195193
String name = args.length > 1 ? args[1] : System.getProperty("user.name");
196194
Datastore datastore = options.service();
197195
KeyFactory keyFactory = datastore.newKeyFactory().kind(USER_KIND);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* Before the test:
2222
* <pre> {@code
2323
* LocalResourceManagerHelper resourceManagerHelper = LocalResourceManagerHelper.create();
24-
* ResourceManager resourceManager = resourceManagerHelper.options().service();
2524
* resourceManagerHelper.start();
25+
* ResourceManager resourceManager = resourceManagerHelper.options().service();
2626
* }</pre>
2727
*
2828
* <p>After the test:

gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteStorageHelperTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.gcloud.storage;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertTrue;
2122

2223
import com.google.common.collect.ImmutableList;
@@ -139,7 +140,8 @@ public void testForceDeleteTimeout() throws InterruptedException, ExecutionExcep
139140
}
140141
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(RETRYABLE_EXCEPTION).anyTimes();
141142
EasyMock.replay(storageMock);
142-
assertTrue(!RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 50, TimeUnit.MICROSECONDS));
143+
assertFalse(
144+
RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 50, TimeUnit.MICROSECONDS));
143145
EasyMock.verify(storageMock);
144146
}
145147

gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public class ITStorageTest {
8585

8686
@BeforeClass
8787
public static void beforeClass() {
88-
RemoteStorageHelper gcsHelper = RemoteStorageHelper.create();
89-
storage = gcsHelper.options().service();
88+
RemoteStorageHelper helper = RemoteStorageHelper.create();
89+
storage = helper.options().service();
9090
storage.create(BucketInfo.of(BUCKET));
9191
}
9292

0 commit comments

Comments
 (0)