From 22a9e9915c1ba5fae4e1d194bf0b92686d4ba7d4 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 29 Sep 2015 18:14:15 -0700 Subject: [PATCH] Allow a default port to be passed in as a parameter, and also fix names --- .../gcloud/datastore/DatastoreOptionsTest.java | 2 +- .../google/gcloud/datastore/DatastoreTest.java | 2 +- .../google/gcloud/datastore/LocalGcdHelper.java | 17 +++++++---------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index 8a0d52aec9c8..b3365876773e 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -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; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 2c7e8f987cb1..b03472a9a6f8 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -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 { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index a78797bb88e4..ce069bd9709d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -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; @@ -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() { @@ -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()); }