diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-03-07 12:08:56 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-03-07 12:08:56 +0100 |
commit | 6368d2292614f67492fb28ff5486b7aa3ef6c699 (patch) | |
tree | 8d1d885267f959d711a6da9338ce20c14ed8cd00 /it | |
parent | 597a4fed424d895bbd9b54287f90ad67ae5b5161 (diff) | |
download | sonarqube-6368d2292614f67492fb28ff5486b7aa3ef6c699.tar.gz sonarqube-6368d2292614f67492fb28ff5486b7aa3ef6c699.zip |
Improve allocation of new ports in integration tests
Diffstat (limited to 'it')
3 files changed, 38 insertions, 25 deletions
diff --git a/it/it-tests/src/test/java/it/issue/IssueNotificationsTest.java b/it/it-tests/src/test/java/it/issue/IssueNotificationsTest.java index d634e7e46b2..e6b2dcce31f 100644 --- a/it/it-tests/src/test/java/it/issue/IssueNotificationsTest.java +++ b/it/it-tests/src/test/java/it/issue/IssueNotificationsTest.java @@ -61,7 +61,7 @@ public class IssueNotificationsTest extends AbstractIssueTest { @BeforeClass public static void before() throws Exception { - smtpServer = new Wiser(NetworkUtils.getNextAvailablePort()); + smtpServer = new Wiser(0); smtpServer.start(); System.out.println("SMTP Server port: " + smtpServer.getServer().getPort()); diff --git a/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java b/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java index 4b6756fdf85..3fd287f22b7 100644 --- a/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java +++ b/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java @@ -29,7 +29,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.wsclient.Sonar; @@ -43,7 +42,6 @@ import org.sonar.wsclient.services.ResourceQuery; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; import util.ItUtils; -import util.NetworkUtils; import util.QaOnly; import util.selenium.SeleneseTest; @@ -82,7 +80,7 @@ public class QualityGateNotificationTest { @Test public void status_on_metric_variation_and_send_notifications() throws Exception { - Wiser smtpServer = new Wiser(NetworkUtils.getNextAvailablePort()); + Wiser smtpServer = new Wiser(0); try { // Configure SMTP smtpServer.start(); diff --git a/it/it-tests/src/test/java/util/NetworkUtils.java b/it/it-tests/src/test/java/util/NetworkUtils.java index d4934e903fb..39c35286a5f 100644 --- a/it/it-tests/src/test/java/util/NetworkUtils.java +++ b/it/it-tests/src/test/java/util/NetworkUtils.java @@ -22,45 +22,60 @@ package util; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.HashSet; +import java.util.Set; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.ArrayUtils; public final class NetworkUtils { - private static final int MAX_TRY = 10; - private static final AtomicInteger nextPort = new AtomicInteger(20000); + + private static final RandomPortFinder RANDOM_PORT_FINDER = new RandomPortFinder(); private NetworkUtils() { + // only statics } public static int getNextAvailablePort() { - if (isOnTravisCI()) { - for (int i = 0; i < MAX_TRY; i++) { - int port = nextPort.getAndIncrement(); + return RANDOM_PORT_FINDER.getNextAvailablePort(); + } + + static class RandomPortFinder { + private static final int MAX_TRY = 10; + // Firefox blocks some reserved ports : http://www-archive.mozilla.org/projects/netlib/PortBanning.html + private static final int[] BLOCKED_PORTS = {2049, 4045, 6000}; + private final Set<Integer> usedPorts = new HashSet<>(); - // Check that the port is really available. - // (On Travis, if the build is single threaded, it should be) - // + public int getNextAvailablePort() { + for (int i = 0; i < MAX_TRY; i++) { try { - Process process = new ProcessBuilder("nc", "-z", "localhost", Integer.toString(port)).start(); - if (process.waitFor() == 1) { + int port = getRandomUnusedPort(); + if (isValidPort(port) && !usedPorts.contains(port)) { + usedPorts.add(port); return port; } } catch (Exception e) { - throw new IllegalStateException("Can't test that a network port is available", e); + throw new IllegalStateException("Can't find an open network port", e); } } - throw new IllegalStateException("Can't find a free network port"); + throw new IllegalStateException("Can't find an open network port"); } - try (ServerSocket socket = new ServerSocket()) { - socket.bind(new InetSocketAddress("localhost", 0)); - return socket.getLocalPort(); - } catch (IOException e) { - throw new IllegalStateException("Can't find a free network port", e); + public int getRandomUnusedPort() throws IOException { + ServerSocket socket = null; + try { + socket = new ServerSocket(); + socket.bind(new InetSocketAddress("localhost", 0)); + return socket.getLocalPort(); + } catch (IOException e) { + throw new IllegalStateException("Can not find a free network port", e); + } finally { + IOUtils.closeQuietly(socket); + } } - } - private static boolean isOnTravisCI() { - return "true".equals(System.getenv("TRAVIS")); + public static boolean isValidPort(int port) { + return port > 1023 && !ArrayUtils.contains(BLOCKED_PORTS, port); + } } } |