From fd64661844eadbce52506f169ad8d396905da15b Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 14 Sep 2015 09:15:42 +0200 Subject: [PATCH] Cleanup code --- .../sonar/orchestrator/util/NetworkUtils.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/it/it-tests/src/test/java/com/sonar/orchestrator/util/NetworkUtils.java b/it/it-tests/src/test/java/com/sonar/orchestrator/util/NetworkUtils.java index 9274dab3483..ebe398646d0 100644 --- a/it/it-tests/src/test/java/com/sonar/orchestrator/util/NetworkUtils.java +++ b/it/it-tests/src/test/java/com/sonar/orchestrator/util/NetworkUtils.java @@ -25,28 +25,27 @@ import java.net.ServerSocket; import java.util.concurrent.atomic.AtomicInteger; public final class NetworkUtils { + private static final int MAX_TRY = 10; private static final AtomicInteger nextPort = new AtomicInteger(20000); private NetworkUtils() { } public static int getNextAvailablePort() { - System.out.println("=== Override method provided by orchestrator"); - - if ("true".equals(System.getenv("TRAVIS"))) { - for (int i = 0; i < 10; i++) { + if (isOnTravisCI()) { + for (int i = 0; i < MAX_TRY; i++) { int port = nextPort.getAndIncrement(); + // Check that the port is really available. + // (On Travis, if the build is single threaded, it should be) + // try { - System.out.println("=== Trying port " + port); Process process = new ProcessBuilder("nc", "-z", "localhost", Integer.toString(port)).start(); if (process.waitFor() == 1) { - System.out.println("=== Using port " + port); return port; } } catch (Exception e) { - // Ignore. will try again - System.out.println(e); + throw new IllegalStateException("Can't test that a network port is available", e); } } @@ -60,4 +59,8 @@ public final class NetworkUtils { throw new IllegalStateException("Can't find a free network port", e); } } + + private static boolean isOnTravisCI() { + return "true".equals(System.getenv("TRAVIS")); + } } -- 2.39.5