Skip to content

Commit

Permalink
Adding support to custom ports on LocalDatastoreHelper (#4933) (#4935)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasoares authored and sduskis committed Apr 11, 2019
1 parent f9ffc7a commit 47884a3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
}
}

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 {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 47884a3

Please sign in to comment.