Repository: ant-ivy Updated Branches: refs/heads/master 910fe1482 -> 23cf1fb45
Reduce the chance to have a conflict of port binding by being able to bind to one, even briefly. Project: http://git-wip-us.apache.org/repos/asf/ant-ivy/repo Commit: http://git-wip-us.apache.org/repos/asf/ant-ivy/commit/23cf1fb4 Tree: http://git-wip-us.apache.org/repos/asf/ant-ivy/tree/23cf1fb4 Diff: http://git-wip-us.apache.org/repos/asf/ant-ivy/diff/23cf1fb4 Branch: refs/heads/master Commit: 23cf1fb45ed47e5c729346ce91a55f92087749a7 Parents: 910fe14 Author: Nicolas LaleveÌe <nicolas.lale...@hibnet.org> Authored: Thu Apr 12 00:27:20 2018 +0200 Committer: Nicolas LaleveÌe <nicolas.lale...@hibnet.org> Committed: Thu Apr 12 00:27:20 2018 +0200 ---------------------------------------------------------------------- test/java/org/apache/ivy/TestHelper.java | 33 ++++++++++++++++++++ .../ivy/plugins/resolver/URLResolverTest.java | 6 ++-- .../ivy/util/url/HttpclientURLHandlerTest.java | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/23cf1fb4/test/java/org/apache/ivy/TestHelper.java ---------------------------------------------------------------------- diff --git a/test/java/org/apache/ivy/TestHelper.java b/test/java/org/apache/ivy/TestHelper.java index 76846f9..5ffe8ac 100644 --- a/test/java/org/apache/ivy/TestHelper.java +++ b/test/java/org/apache/ivy/TestHelper.java @@ -51,6 +51,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; @@ -488,4 +489,36 @@ public class TestHelper { return "AuthFilter"; } } + + /** + * Find a TCP/IP port which may continue to be available. + * <br /> + * The returned port is available since a socket has successfully bind to it, but this availability is not ensured + * after this method since the associated socket is released and some other process can now use it. + */ + public static int getMaybeAvailablePort() { + ServerSocket s = null; + try { + s = new ServerSocket(0); + s.setReuseAddress(true); + int port = s.getLocalPort(); + try { + s.close(); + } catch (IOException e) { + // ignore + } + return port; + } catch (IOException e) { + // ignore + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + // ignore + } + } + } + throw new IllegalStateException("Not TCP/IP port available"); + } } http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/23cf1fb4/test/java/org/apache/ivy/plugins/resolver/URLResolverTest.java ---------------------------------------------------------------------- diff --git a/test/java/org/apache/ivy/plugins/resolver/URLResolverTest.java b/test/java/org/apache/ivy/plugins/resolver/URLResolverTest.java index 97e98b5..eaada3e 100644 --- a/test/java/org/apache/ivy/plugins/resolver/URLResolverTest.java +++ b/test/java/org/apache/ivy/plugins/resolver/URLResolverTest.java @@ -223,9 +223,7 @@ public class URLResolverTest extends AbstractDependencyResolverTest { settings.addConfigured(extremelyLowTimeout); // setup a HTTP backed repo - // TODO: Right now the port is hard coded, but we need to find a "available" port to which can be bind to. - // Else this can lead to occasional bind failures - final InetSocketAddress fastServerBindAddr = new InetSocketAddress("localhost", 12345); + final InetSocketAddress fastServerBindAddr = new InetSocketAddress("localhost", TestHelper.getMaybeAvailablePort()); final String contextRoot = "/testTimeouts"; final Path repoRoot = new File("test/repositories/1").toPath(); assertTrue(repoRoot + " is not a directory", Files.isDirectory(repoRoot)); @@ -264,7 +262,7 @@ public class URLResolverTest extends AbstractDependencyResolverTest { settings.getDefaultRepositoryCacheManager().clean(); settings.getResolutionCacheManager().clean(); - final InetSocketAddress slowServerAddr = new InetSocketAddress("localhost", 23456); + final InetSocketAddress slowServerAddr = new InetSocketAddress("localhost", TestHelper.getMaybeAvailablePort()); final String ivyPattern = "http://" + slowServerAddr.getHostName() + ":" + slowServerAddr.getPort() + "/testTimeouts/[organisation]/[module]/ivys/ivy-[revision].xml"; final String artifactPattern = "http://" + slowServerAddr.getHostName() + ":" + slowServerAddr.getPort() http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/23cf1fb4/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java ---------------------------------------------------------------------- diff --git a/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java b/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java index 598508c..5f99b3e 100644 --- a/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java +++ b/test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java @@ -148,7 +148,7 @@ public class HttpclientURLHandlerTest { final String userName = "test-http-user-" + random.nextInt(); final String password = "pass-" + random.nextInt(); credentialsStore.addCredentials(realm, host, userName, password); - final InetSocketAddress serverBindAddr = new InetSocketAddress("localhost", 12345); + final InetSocketAddress serverBindAddr = new InetSocketAddress("localhost", TestHelper.getMaybeAvailablePort()); final String contextRoot = "/testHttpClientHandler"; final Path repoRoot = new File("test/repositories").toPath(); assertTrue(repoRoot + " is not a directory", Files.isDirectory(repoRoot));