Skip to content

Commit

Permalink
Isolate REST client single host tests (#30504)
Browse files Browse the repository at this point in the history
These tests are sharing the same server and client for every test. Yet,
we are seeing some tests fail with mysterious connection resets. It is
not clear what is happening but one theory is that the tests are
interfering with each other. This commit moves to use a separate server
and client per test.
  • Loading branch information
jasontedor committed May 10, 2018
1 parent 63127b1 commit a6233b9
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.mocksocket.MockHttpServer;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import java.io.IOException;
Expand Down Expand Up @@ -69,20 +71,20 @@
*/
public class RestClientSingleHostIntegTests extends RestClientTestCase {

private static HttpServer httpServer;
private static RestClient restClient;
private static String pathPrefix;
private static Header[] defaultHeaders;
private HttpServer httpServer;
private RestClient restClient;
private String pathPrefix;
private Header[] defaultHeaders;

@BeforeClass
public static void startHttpServer() throws Exception {
@Before
public void startHttpServer() throws Exception {
pathPrefix = randomBoolean() ? "/testPathPrefix/" + randomAsciiLettersOfLengthBetween(1, 5) : "";
httpServer = createHttpServer();
defaultHeaders = RestClientTestUtil.randomHeaders(getRandom(), "Header-default");
restClient = createRestClient(false, true);
}

private static HttpServer createHttpServer() throws Exception {
private HttpServer createHttpServer() throws Exception {
HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
httpServer.start();
//returns a different status code depending on the path
Expand Down Expand Up @@ -127,7 +129,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
}
}

private static RestClient createRestClient(final boolean useAuth, final boolean usePreemptiveAuth) {
private RestClient createRestClient(final boolean useAuth, final boolean usePreemptiveAuth) {
// provide the username/password for every request
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass"));
Expand Down Expand Up @@ -155,8 +157,8 @@ public HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder h
return restClientBuilder.build();
}

@AfterClass
public static void stopHttpServers() throws IOException {
@After
public void stopHttpServers() throws IOException {
restClient.close();
restClient = null;
httpServer.stop(0);
Expand Down

0 comments on commit a6233b9

Please sign in to comment.