diff --git a/google-cloud-clients/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/google-cloud-clients/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java index 29141cfc641e..2792b494c5ce 100644 --- a/google-cloud-clients/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java +++ b/google-cloud-clients/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java @@ -78,10 +78,10 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper { } } - private LocalDatastoreHelper(double consistency) { + private LocalDatastoreHelper(double consistency, int port) { super( "datastore", - BaseEmulatorHelper.findAvailablePort(DEFAULT_PORT), + port > 0 ? port : BaseEmulatorHelper.findAvailablePort(DEFAULT_PORT), PROJECT_ID_PREFIX + UUID.randomUUID().toString()); Path tmpDirectory = null; try { @@ -162,7 +162,32 @@ public double getConsistency() { * consistency of non-ancestor queries; non-ancestor queries are eventually consistent. */ public static LocalDatastoreHelper create(double consistency) { - return new LocalDatastoreHelper(consistency); + return create(consistency, 0); + } + + /** + * Creates a local Datastore helper with the specified settings for project ID and consistency. + * + * @param consistency the fraction of Datastore writes that are immediately visible to global + * queries, with 0.0 meaning no writes are immediately visible and 1.0 meaning all writes are + * immediately visible. Note that setting this to 1.0 may mask incorrect assumptions about the + * consistency of non-ancestor queries; non-ancestor queries are eventually consistent. + * @param port the port to be used to start the emulator service. Note that setting this to 0 the + * emulator will search for a free random port. + */ + public static LocalDatastoreHelper create(double consistency, int port) { + return new LocalDatastoreHelper(consistency, port); + } + + /** + * Creates a local Datastore helper with a placeholder project ID and the default consistency + * setting of 0.9. + * + * @param port the port to be used to start the emulator service. Note that setting this to 0 the + * emulator will search for a free random port. + */ + public static LocalDatastoreHelper create(int port) { + return new LocalDatastoreHelper(DEFAULT_CONSISTENCY, port); } /** diff --git a/google-cloud-clients/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java b/google-cloud-clients/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java index 4af7f65dba85..44c2854ac3d1 100644 --- a/google-cloud-clients/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java +++ b/google-cloud-clients/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java @@ -17,6 +17,7 @@ package com.google.cloud.datastore.testing; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -56,6 +57,23 @@ public void testCreate() { assertTrue(helper.getProjectId().startsWith(PROJECT_ID_PREFIX)); } + @Test + public void testCreatePort() { + LocalDatastoreHelper helper = LocalDatastoreHelper.create(0.75, 8888); + DatastoreOptions options = helper.getOptions(NAMESPACE); + assertTrue(options.getHost().endsWith("8888")); + assertTrue(Math.abs(0.75 - helper.getConsistency()) < TOLERANCE); + helper = LocalDatastoreHelper.create(); + options = helper.getOptions(NAMESPACE); + assertTrue(Math.abs(0.9 - helper.getConsistency()) < TOLERANCE); + assertFalse(options.getHost().endsWith("8888")); + assertFalse(options.getHost().endsWith("8080")); + helper = LocalDatastoreHelper.create(9999); + options = helper.getOptions(NAMESPACE); + assertTrue(Math.abs(0.9 - helper.getConsistency()) < TOLERANCE); + assertTrue(options.getHost().endsWith("9999")); + } + @Test public void testOptions() { LocalDatastoreHelper helper = LocalDatastoreHelper.create();