Skip to content

Commit

Permalink
Allow a default port to be passed in as a parameter, and also fix names
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Sep 30, 2015
1 parent 1f617b7 commit 22a9e99
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class DatastoreOptionsTest {

private static final String PROJECT_ID = "project_id";
private static final int PORT = LocalGcdHelper.findOpenPort();
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
private DatastoreRpcFactory datastoreRpcFactory;
private DatastoreRpc datastoreRpc;
private DatastoreOptions.Builder options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class DatastoreTest {
private Datastore datastore;

private static LocalGcdHelper gcdHelper;
private static final int PORT = LocalGcdHelper.findOpenPort();
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);

@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class LocalGcdHelper {
private final int port;

public static final String DEFAULT_PROJECT_ID = "projectid1";
private static final int DEFAULT_PORT = 8080;
public static final int DEFAULT_PORT = 8080;
private static final String GCD_VERSION = "v1beta2";
private static final String GCD_BUILD = "rev1-2.1.2b";
private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD;
Expand All @@ -96,15 +96,12 @@ public class LocalGcdHelper {
}
}

public static int findOpenPort() {
int port;
try (ServerSocket temp_socket = new ServerSocket(0)) {
port = temp_socket.getLocalPort();
temp_socket.close();
public static int findAvailablePort(int defaultPort) {
try (ServerSocket tempSocket = new ServerSocket(0)) {
return tempSocket.getLocalPort();
} catch (IOException e) {
port = DEFAULT_PORT;
return defaultPort;
}
return port;
}

private static Path installedGcdPath() {
Expand Down Expand Up @@ -491,8 +488,8 @@ public static void main(String... args) throws IOException, InterruptedException
if (args.length == 1) {
switch (args[0]) {
case "START":
if (!isActive(DEFAULT_PROJECT_ID, DEFAULT_PORT)) {
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, DEFAULT_PORT);
if (!isActive(DEFAULT_PROJECT_ID, findAvailablePort(DEFAULT_PORT))) {
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, findAvailablePort(DEFAULT_PORT));
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
writer.write(helper.gcdPath.toAbsolutePath().toString());
}
Expand Down

0 comments on commit 22a9e99

Please sign in to comment.