Skip to content

Commit d924ac7

Browse files
committed
Isolate REST client single host tests (#30504)
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.
1 parent 5a12c11 commit d924ac7

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import org.apache.http.util.EntityUtils;
3636
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
3737
import org.elasticsearch.mocksocket.MockHttpServer;
38+
import org.junit.After;
3839
import org.junit.AfterClass;
40+
import org.junit.Before;
3941
import org.junit.BeforeClass;
4042

4143
import java.io.IOException;
@@ -68,20 +70,20 @@
6870
@IgnoreJRERequirement
6971
public class RestClientSingleHostIntegTests extends RestClientTestCase {
7072

71-
private static HttpServer httpServer;
72-
private static RestClient restClient;
73-
private static String pathPrefix;
74-
private static Header[] defaultHeaders;
73+
private HttpServer httpServer;
74+
private RestClient restClient;
75+
private String pathPrefix;
76+
private Header[] defaultHeaders;
7577

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

84-
private static HttpServer createHttpServer() throws Exception {
86+
private HttpServer createHttpServer() throws Exception {
8587
HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
8688
httpServer.start();
8789
//returns a different status code depending on the path
@@ -128,7 +130,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
128130
}
129131
}
130132

131-
private static RestClient createRestClient(final boolean useAuth, final boolean usePreemptiveAuth) {
133+
private RestClient createRestClient(final boolean useAuth, final boolean usePreemptiveAuth) {
132134
// provide the username/password for every request
133135
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
134136
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass"));
@@ -156,8 +158,8 @@ public HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder h
156158
return restClientBuilder.build();
157159
}
158160

159-
@AfterClass
160-
public static void stopHttpServers() throws IOException {
161+
@After
162+
public void stopHttpServers() throws IOException {
161163
restClient.close();
162164
restClient = null;
163165
httpServer.stop(0);

0 commit comments

Comments
 (0)