aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-start
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-07-16 09:19:06 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-07-16 10:26:38 +0200
commite5a8f05ad4d779984e8aad215a083e2d206b6b90 (patch)
tree68a0b01261da215073ade70ff74a56f2894b47a7 /sonar-start
parent92dfaad539e0116847f6e927bc05aa05ab784277 (diff)
downloadsonarqube-e5a8f05ad4d779984e8aad215a083e2d206b6b90.tar.gz
sonarqube-e5a8f05ad4d779984e8aad215a083e2d206b6b90.zip
Updated NetworkUtils for free port generation (steps over last generated port);
Diffstat (limited to 'sonar-start')
-rw-r--r--sonar-start/src/main/java/org/sonar/start/NetworkUtils.java4
-rw-r--r--sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java42
2 files changed, 45 insertions, 1 deletions
diff --git a/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java b/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java
index 7876b8dddad..e0c1caa5a46 100644
--- a/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java
+++ b/sonar-start/src/main/java/org/sonar/start/NetworkUtils.java
@@ -24,9 +24,11 @@ import java.net.ServerSocket;
class NetworkUtils {
+ static private int lastPort = -1;
+
static int freePort() {
try {
- ServerSocket s = new ServerSocket(0);
+ ServerSocket s = new ServerSocket(lastPort + 1);
int port = s.getLocalPort();
s.close();
return port;
diff --git a/sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java b/sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java
new file mode 100644
index 00000000000..f8690d6514e
--- /dev/null
+++ b/sonar-start/src/test/java/org/sonar/start/NetworkUtilsTest.java
@@ -0,0 +1,42 @@
+package org.sonar.start;
+
+import org.junit.Test;
+
+import java.net.ServerSocket;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class NetworkUtilsTest {
+
+
+ @Test
+ public void find_free_port() throws Exception {
+ int port = NetworkUtils.freePort();
+ assertThat(port).isGreaterThan(1024);
+ }
+
+ @Test
+ public void find_multiple_free_port() throws Exception {
+ int port1 = NetworkUtils.freePort();
+ int port2 = NetworkUtils.freePort();
+
+ assertThat(port1).isGreaterThan(1024);
+ assertThat(port2).isGreaterThan(1024);
+
+ assertThat(port1).isNotSameAs(port2);
+ }
+
+ @Test
+ public void find_multiple_free_non_adjacent_port() throws Exception {
+ int port1 = NetworkUtils.freePort();
+
+ ServerSocket socket = new ServerSocket(port1 + 1);
+
+ int port2 = NetworkUtils.freePort();
+
+ assertThat(port1).isGreaterThan(1024);
+ assertThat(port2).isGreaterThan(1024);
+
+ assertThat(port1).isNotSameAs(port2);
+ }
+} \ No newline at end of file