Skip to content

Commit a6233b9

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 63127b1 commit a6233b9

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.nio.entity.NStringEntity;
3636
import org.apache.http.util.EntityUtils;
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;
@@ -69,20 +71,20 @@
6971
*/
7072
public class RestClientSingleHostIntegTests extends RestClientTestCase {
7173

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

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

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

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

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

0 commit comments

Comments
 (0)