brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [30/50] brooklyn-server git commit: better port availability check routines and test messages
Date Mon, 01 Feb 2016 17:49:00 GMT
better port availability check routines and test messages


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/f8b3954b
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/f8b3954b
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/f8b3954b

Branch: refs/heads/0.4.0
Commit: f8b3954b2ecb413c8d21e5cfbbf393993f8dcd1f
Parents: a6b00ab
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Oct 10 12:47:34 2012 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Oct 10 12:47:34 2012 +0100

----------------------------------------------------------------------
 .../LocalhostMachineProvisioningLocation.java    |  2 +-
 .../main/java/brooklyn/util/NetworkUtils.java    | 19 ++++++++++++++++---
 .../brooklyn/launcher/WebAppRunnerTest.groovy    |  8 ++++++++
 3 files changed, 25 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8b3954b/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
b/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
index 58e33bc..204a6d5 100644
--- a/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
+++ b/core/src/main/java/brooklyn/location/basic/LocalhostMachineProvisioningLocation.java
@@ -142,7 +142,7 @@ public class LocalhostMachineProvisioningLocation extends FixedListMachineProvis
             return true;
         }
     }
-    /** checks the actual availability of the port on localhost, ie by binding to it */
+    /** checks the actual availability of the port on localhost, ie by binding to it; cf
{@link NetworkUtils#isPortAvailable(int)} */
     public static boolean checkPortAvailable(InetAddress localAddress, int portNumber) {
         if (portNumber<1024) {
             if (LOG.isDebugEnabled()) LOG.debug("Skipping system availability check for privileged
localhost port "+portNumber);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8b3954b/core/src/main/java/brooklyn/util/NetworkUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/NetworkUtils.java b/core/src/main/java/brooklyn/util/NetworkUtils.java
index c399c2b..c9a147e 100644
--- a/core/src/main/java/brooklyn/util/NetworkUtils.java
+++ b/core/src/main/java/brooklyn/util/NetworkUtils.java
@@ -5,6 +5,7 @@ import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.UnknownHostException;
 import java.util.Map;
 
 import org.slf4j.Logger;
@@ -18,13 +19,25 @@ public class NetworkUtils {
     
     public static final int MIN_PORT_NUMBER = 1;
     public static final int MAX_PORT_NUMBER = 65535;
-    
+
+    private static boolean loggedLocalhostNotAvailable = false;
     public static boolean isPortAvailable(int port) {
+        try {
+            return isPortAvailable(InetAddress.getByName("localhost"), port);
+        } catch (UnknownHostException e) {
+            if (!loggedLocalhostNotAvailable) {
+                loggedLocalhostNotAvailable = true;
+                log.warn("localhost unavailable during port availability check for "+port+":
"+e+"; ignoring, but this may be a sign of network misconfiguration");
+            }
+            return isPortAvailable(null, port);
+        }
+    }
+    public static boolean isPortAvailable(InetAddress localAddress, int port) {
         if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
             throw new IllegalArgumentException("Invalid start port: " + port);
         }
         try {
-            Socket s = new Socket(InetAddress.getByName("localhost"), port);
+            Socket s = new Socket(localAddress, port);
             try {
                 s.close();
             } catch (Exception e) {}
@@ -70,7 +83,7 @@ public class NetworkUtils {
         return port;
     }
 
-    public static void checkPortsValid(Map ports) {
+    public static void checkPortsValid(@SuppressWarnings("rawtypes") Map ports) {
         for (Object ppo : ports.entrySet()) {
             Map.Entry<?,?> pp = (Map.Entry<?,?>)ppo;
             Object val = pp.getValue();

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f8b3954b/usage/launcher/src/test/java/brooklyn/launcher/WebAppRunnerTest.groovy
----------------------------------------------------------------------
diff --git a/usage/launcher/src/test/java/brooklyn/launcher/WebAppRunnerTest.groovy b/usage/launcher/src/test/java/brooklyn/launcher/WebAppRunnerTest.groovy
index 202a51b..a860df3 100644
--- a/usage/launcher/src/test/java/brooklyn/launcher/WebAppRunnerTest.groovy
+++ b/usage/launcher/src/test/java/brooklyn/launcher/WebAppRunnerTest.groovy
@@ -10,9 +10,11 @@ import org.slf4j.LoggerFactory
 import org.testng.annotations.Test
 
 import brooklyn.config.BrooklynServiceAttributes
+import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
 import brooklyn.management.internal.LocalManagementContext
 import brooklyn.test.TestUtils;
 import brooklyn.util.BrooklynLanguageExtensions
+import brooklyn.util.NetworkUtils;
 import brooklyn.util.internal.BrooklynSystemProperties;
 import brooklyn.util.internal.TimeExtras
 
@@ -47,6 +49,8 @@ public class WebAppRunnerTest {
     
     @Test
     public void testStartWar1() {
+        if (!NetworkUtils.isPortAvailable(8090))
+            fail("Another process is using port 8090 which is required for this test.");
         BrooklynWebServer server = createWebServer(port:8090);
         assertNotNull(server);
         
@@ -81,6 +85,8 @@ public class WebAppRunnerTest {
         
     @Test
     public void testStartSecondaryWar() {
+        if (!NetworkUtils.isPortAvailable(8090))
+            fail("Another process is using port 8090 which is required for this test.");
         BrooklynWebServer server = createWebServer(port:8090, war:"brooklyn.war", wars:["hello":"hello-world.war"]);
         assertNotNull(server);
         
@@ -98,6 +104,8 @@ public class WebAppRunnerTest {
 
     @Test
     public void testStartSecondaryWarAfter() {
+        if (!NetworkUtils.isPortAvailable(8090))
+            fail("Another process is using port 8090 which is required for this test.");
         BrooklynWebServer server = createWebServer(port:8090, war:"brooklyn.war");
         assertNotNull(server);
         


Mime
View raw message