From 72d2f5174ea253cda4548c618d88d8bdddf4530d Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Mon, 30 Mar 2015 14:22:26 -0400 Subject: [PATCH] Fix HttpClientTest If port 9200 isn't available inc the port and try again until we reach 9300. Original commit: elastic/x-pack-elasticsearch@90ca05544f1d7d3a7491ba0ac34d97bfb5e6cb9d --- .../watcher/support/http/HttpClientTest.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTest.java b/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTest.java index 5ef7a5b1c4e..babe93349f6 100644 --- a/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTest.java +++ b/src/test/java/org/elasticsearch/watcher/support/http/HttpClientTest.java @@ -9,6 +9,7 @@ import com.carrotsearch.randomizedtesting.annotations.Repeat; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; import com.squareup.okhttp.mockwebserver.RecordedRequest; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.base.Charsets; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.ImmutableSettings; @@ -18,6 +19,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.net.BindException; + import static org.hamcrest.Matchers.equalTo; /** @@ -27,10 +30,23 @@ public class HttpClientTest extends ElasticsearchTestCase { private MockWebServer webServer; private HttpClient httpClient; + private int webPort = 9200; + @Before public void init() throws Exception { webServer = new MockWebServer(); - webServer.start(9200); + while (webPort < 9300) { + try { + webServer.start(webPort); + break; + } catch (BindException be) { + logger.warn("port [{}] was already in use trying next port", webPort); + ++webPort; + } + } + if (webPort == 9300) { + throw new ElasticsearchException("unable to find open port between 9200 and 9300"); + } httpClient = new HttpClient(ImmutableSettings.EMPTY); } @@ -50,7 +66,7 @@ public class HttpClientTest extends ElasticsearchTestCase { HttpRequest request = new HttpRequest(); request.method(HttpMethod.POST); request.host("localhost"); - request.port(9200); + request.port(webPort); request.path("/" + randomAsciiOfLength(5)); String paramKey; String paramValue; @@ -81,7 +97,7 @@ public class HttpClientTest extends ElasticsearchTestCase { HttpRequest request = new HttpRequest(); request.method(HttpMethod.POST); request.host("localhost"); - request.port(9200); + request.port(webPort); request.path("/test"); request.auth(new BasicAuth("user", "pass")); request.body("body");