Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4933 Adding support to custom ports on LocalDatastoreHelper #4935

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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