You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gobblin.apache.org by su...@apache.org on 2020/10/13 18:54:06 UTC

[incubator-gobblin] branch master updated: [GOBBLIN-1287] Cleanup code duplication across tests that choose a random open port for spinning up an embedded mysql server[]

This is an automated email from the ASF dual-hosted git repository.

suvasude pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-gobblin.git


The following commit(s) were added to refs/heads/master by this push:
     new 655b8eb  [GOBBLIN-1287] Cleanup code duplication across tests that choose a random open port for spinning up an embedded mysql server[]
655b8eb is described below

commit 655b8ebcf5a570d34559003ced7005531d04fcb3
Author: suvasude <su...@linkedin.biz>
AuthorDate: Tue Oct 13 11:53:52 2020 -0700

    [GOBBLIN-1287] Cleanup code duplication across tests that choose a random open port for spinning up an embedded mysql server[]
    
    Closes #3126 from sv2000/randomPort
---
 .../metastore/testing/TestMetastoreDatabaseServer.java   | 15 ++-------------
 .../apache/gobblin/rest/JobExecutionInfoServerTest.java  | 16 +++-------------
 .../java/org/apache/gobblin/service/FlowConfigTest.java  | 12 ------------
 .../src/main/java/org/apache/gobblin/util/PortUtils.java |  2 +-
 4 files changed, 6 insertions(+), 39 deletions(-)

diff --git a/gobblin-metastore/src/test/java/org/apache/gobblin/metastore/testing/TestMetastoreDatabaseServer.java b/gobblin-metastore/src/test/java/org/apache/gobblin/metastore/testing/TestMetastoreDatabaseServer.java
index 40b5a11..f2afd86 100644
--- a/gobblin-metastore/src/test/java/org/apache/gobblin/metastore/testing/TestMetastoreDatabaseServer.java
+++ b/gobblin-metastore/src/test/java/org/apache/gobblin/metastore/testing/TestMetastoreDatabaseServer.java
@@ -45,6 +45,7 @@ import org.apache.gobblin.configuration.ConfigurationKeys;
 import org.apache.gobblin.metastore.MetaStoreModule;
 import org.apache.gobblin.metastore.util.DatabaseJobHistoryStoreSchemaManager;
 import org.apache.gobblin.metastore.util.MySqlJdbcUrl;
+import org.apache.gobblin.util.PortUtils;
 
 
 class TestMetastoreDatabaseServer implements Closeable {
@@ -83,7 +84,7 @@ class TestMetastoreDatabaseServer implements Closeable {
     this.dbUserName = realConfig.getString(DBUSER_NAME_KEY);
     this.dbUserPassword = realConfig.getString(DBUSER_PASSWORD_KEY);
     this.dbHost = this.embeddedMysqlEnabled ? "localhost" : realConfig.getString(DBHOST_KEY);
-    this.dbPort = this.embeddedMysqlEnabled ? chooseRandomPort() : realConfig.getInt(DBPORT_KEY);
+    this.dbPort = this.embeddedMysqlEnabled ? new PortUtils.ServerSocketPortLocator().random() : realConfig.getInt(DBPORT_KEY);
 
     this.log.error("Starting with config: embeddedMysqlEnabled={} dbUserName={} dbHost={} dbPort={}",
                   this.embeddedMysqlEnabled,
@@ -134,18 +135,6 @@ class TestMetastoreDatabaseServer implements Closeable {
     }
   }
 
-  private int chooseRandomPort() throws IOException {
-    ServerSocket socket = null;
-    try {
-      socket = new ServerSocket(0);
-      return socket.getLocalPort();
-    } finally {
-      if (socket != null) {
-        socket.close();
-      }
-    }
-  }
-
   MySqlJdbcUrl getJdbcUrl(String database) throws URISyntaxException {
     return getBaseJdbcUrl()
         .setPath(database)
diff --git a/gobblin-rest-service/gobblin-rest-server/src/test/java/org/apache/gobblin/rest/JobExecutionInfoServerTest.java b/gobblin-rest-service/gobblin-rest-server/src/test/java/org/apache/gobblin/rest/JobExecutionInfoServerTest.java
index c3c38f4..2c8002a 100644
--- a/gobblin-rest-service/gobblin-rest-server/src/test/java/org/apache/gobblin/rest/JobExecutionInfoServerTest.java
+++ b/gobblin-rest-service/gobblin-rest-server/src/test/java/org/apache/gobblin/rest/JobExecutionInfoServerTest.java
@@ -40,6 +40,7 @@ import org.apache.gobblin.metastore.JobHistoryStore;
 import org.apache.gobblin.metastore.MetaStoreModule;
 import org.apache.gobblin.metastore.testing.ITestMetastoreDatabase;
 import org.apache.gobblin.metastore.testing.TestMetastoreDatabaseFactory;
+import org.apache.gobblin.util.PortUtils;
 
 
 /**
@@ -70,7 +71,8 @@ public class JobExecutionInfoServerTest {
     Properties properties = new Properties();
     properties.setProperty(ConfigurationKeys.JOB_HISTORY_STORE_URL_KEY, testMetastoreDatabase.getJdbcUrl());
 
-    int randomPort = chooseRandomPort();
+    int randomPort = new PortUtils.ServerSocketPortLocator().random();
+
     properties.setProperty(ConfigurationKeys.REST_SERVER_PORT_KEY, Integer.toString(randomPort));
 
     Injector injector = Guice.createInjector(new MetaStoreModule(properties));
@@ -153,18 +155,6 @@ public class JobExecutionInfoServerTest {
     }
   }
 
-  private static int chooseRandomPort() throws IOException {
-    ServerSocket socket = null;
-    try {
-      socket = new ServerSocket(0);
-      return socket.getLocalPort();
-    } finally {
-      if (socket != null) {
-        socket.close();
-      }
-    }
-  }
-
   private static JobExecutionInfo createJobExecutionInfo(int index) {
     JobExecutionInfo jobExecutionInfo = new JobExecutionInfo();
     jobExecutionInfo.setJobName("TestJob" + index);
diff --git a/gobblin-restli/gobblin-flow-config-service/gobblin-flow-config-service-client/src/test/java/org/apache/gobblin/service/FlowConfigTest.java b/gobblin-restli/gobblin-flow-config-service/gobblin-flow-config-service-client/src/test/java/org/apache/gobblin/service/FlowConfigTest.java
index e89f65f..2da8bdf 100644
--- a/gobblin-restli/gobblin-flow-config-service/gobblin-flow-config-service-client/src/test/java/org/apache/gobblin/service/FlowConfigTest.java
+++ b/gobblin-restli/gobblin-flow-config-service/gobblin-flow-config-service-client/src/test/java/org/apache/gobblin/service/FlowConfigTest.java
@@ -329,16 +329,4 @@ public class FlowConfigTest {
     _testDirectory.delete();
     cleanUpDir(TEST_SPEC_STORE_DIR);
   }
-
-  private static int chooseRandomPort() throws IOException {
-    ServerSocket socket = null;
-    try {
-      socket = new ServerSocket(0);
-      return socket.getLocalPort();
-    } finally {
-      if (socket != null) {
-        socket.close();
-      }
-    }
-  }
 }
diff --git a/gobblin-utility/src/main/java/org/apache/gobblin/util/PortUtils.java b/gobblin-utility/src/main/java/org/apache/gobblin/util/PortUtils.java
index 045afe4..c60e2f7 100644
--- a/gobblin-utility/src/main/java/org/apache/gobblin/util/PortUtils.java
+++ b/gobblin-utility/src/main/java/org/apache/gobblin/util/PortUtils.java
@@ -151,7 +151,7 @@ public class PortUtils {
     int specific(int port) throws Exception;
   }
 
-  private static class ServerSocketPortLocator implements PortLocator {
+  public static class ServerSocketPortLocator implements PortLocator {
     @Override
     public int random() throws Exception {
         try (ServerSocket serverSocket = new ServerSocket(0)) {