Fix HttpClientTest

If port 9200 isn't available inc the port and try again until we reach 9300.

Original commit: elastic/x-pack-elasticsearch@90ca05544f
This commit is contained in:
Brian Murphy 2015-03-30 14:22:26 -04:00
parent e00bb69982
commit 72d2f5174e
1 changed files with 19 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import com.carrotsearch.randomizedtesting.annotations.Repeat;
import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer; import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest; import com.squareup.okhttp.mockwebserver.RecordedRequest;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.base.Charsets; import org.elasticsearch.common.base.Charsets;
import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
@ -18,6 +19,8 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.net.BindException;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
/** /**
@ -27,10 +30,23 @@ public class HttpClientTest extends ElasticsearchTestCase {
private MockWebServer webServer; private MockWebServer webServer;
private HttpClient httpClient; private HttpClient httpClient;
private int webPort = 9200;
@Before @Before
public void init() throws Exception { public void init() throws Exception {
webServer = new MockWebServer(); 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); httpClient = new HttpClient(ImmutableSettings.EMPTY);
} }
@ -50,7 +66,7 @@ public class HttpClientTest extends ElasticsearchTestCase {
HttpRequest request = new HttpRequest(); HttpRequest request = new HttpRequest();
request.method(HttpMethod.POST); request.method(HttpMethod.POST);
request.host("localhost"); request.host("localhost");
request.port(9200); request.port(webPort);
request.path("/" + randomAsciiOfLength(5)); request.path("/" + randomAsciiOfLength(5));
String paramKey; String paramKey;
String paramValue; String paramValue;
@ -81,7 +97,7 @@ public class HttpClientTest extends ElasticsearchTestCase {
HttpRequest request = new HttpRequest(); HttpRequest request = new HttpRequest();
request.method(HttpMethod.POST); request.method(HttpMethod.POST);
request.host("localhost"); request.host("localhost");
request.port(9200); request.port(webPort);
request.path("/test"); request.path("/test");
request.auth(new BasicAuth("user", "pass")); request.auth(new BasicAuth("user", "pass"));
request.body("body"); request.body("body");