mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 02:14:54 +00:00
Use getPortRange
in http server tests (#58794)
Currently we are leaving the settings to default port range in the nio and netty4 http server test. This has recently led to tests failing due to what appears to be a port conflict with other processes. This commit modifies these tests to use the test case helper method to generate port ranges. Fixes #58433 and #58296.
This commit is contained in:
parent
0a6e6ef4be
commit
605e24ed7c
@ -30,6 +30,7 @@ import org.elasticsearch.common.util.MockBigArrays;
|
||||
import org.elasticsearch.common.util.MockPageCacheRecycler;
|
||||
import org.elasticsearch.common.util.concurrent.ThreadContext;
|
||||
import org.elasticsearch.http.HttpServerTransport;
|
||||
import org.elasticsearch.http.HttpTransportSettings;
|
||||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
|
||||
import org.elasticsearch.rest.BytesRestResponse;
|
||||
import org.elasticsearch.rest.RestChannel;
|
||||
@ -87,7 +88,8 @@ public class Netty4BadRequestTests extends ESTestCase {
|
||||
}
|
||||
};
|
||||
|
||||
try (HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool,
|
||||
Settings settings = Settings.builder().put(HttpTransportSettings.SETTING_HTTP_PORT.getKey(), getPortRange()).build();
|
||||
try (HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool,
|
||||
xContentRegistry(), dispatcher, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS),
|
||||
new SharedGroupFactory(Settings.EMPTY))) {
|
||||
httpServerTransport.start();
|
||||
|
@ -122,7 +122,7 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
* @throws InterruptedException if the client communication with the server is interrupted
|
||||
*/
|
||||
public void testExpectContinueHeader() throws InterruptedException {
|
||||
final Settings settings = Settings.EMPTY;
|
||||
final Settings settings = createSettings();
|
||||
final int contentLength = randomIntBetween(1, HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.get(settings).bytesAsInt());
|
||||
runExpectHeaderTest(settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.CONTINUE);
|
||||
}
|
||||
@ -136,7 +136,7 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
public void testExpectContinueHeaderContentLengthTooLong() throws InterruptedException {
|
||||
final String key = HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.getKey();
|
||||
final int maxContentLength = randomIntBetween(1, 104857600);
|
||||
final Settings settings = Settings.builder().put(key, maxContentLength + "b").build();
|
||||
final Settings settings = createBuilderWithPort().put(key, maxContentLength + "b").build();
|
||||
final int contentLength = randomIntBetween(maxContentLength + 1, Integer.MAX_VALUE);
|
||||
runExpectHeaderTest(
|
||||
settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
|
||||
@ -147,7 +147,8 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
* @throws InterruptedException if the client communication with the server is interrupted
|
||||
*/
|
||||
public void testExpectUnsupportedExpectation() throws InterruptedException {
|
||||
runExpectHeaderTest(Settings.EMPTY, "chocolate=yummy", 0, HttpResponseStatus.EXPECTATION_FAILED);
|
||||
Settings settings = createSettings();
|
||||
runExpectHeaderTest(settings, "chocolate=yummy", 0, HttpResponseStatus.EXPECTATION_FAILED);
|
||||
}
|
||||
|
||||
private void runExpectHeaderTest(
|
||||
@ -201,7 +202,8 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
}
|
||||
|
||||
public void testBindUnavailableAddress() {
|
||||
try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool,
|
||||
Settings initialSettings = createSettings();
|
||||
try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(initialSettings, networkService, bigArrays, threadPool,
|
||||
xContentRegistry(), new NullDispatcher(), clusterSettings, new SharedGroupFactory(Settings.EMPTY))) {
|
||||
transport.start();
|
||||
TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
|
||||
@ -248,10 +250,10 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
|
||||
if (randomBoolean()) {
|
||||
maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
|
||||
settings = Settings.EMPTY;
|
||||
settings = createSettings();
|
||||
} else {
|
||||
maxInitialLineLength = randomIntBetween(1, 8192);
|
||||
settings = Settings.builder().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
|
||||
settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
|
||||
}
|
||||
|
||||
try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(
|
||||
@ -300,7 +302,7 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
|
||||
};
|
||||
|
||||
final Settings settings = Settings.builder()
|
||||
final Settings settings = createBuilderWithPort()
|
||||
.put(SETTING_CORS_ENABLED.getKey(), true)
|
||||
.put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co").build();
|
||||
|
||||
@ -362,7 +364,7 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
|
||||
};
|
||||
|
||||
Settings settings = Settings.builder()
|
||||
Settings settings = createBuilderWithPort()
|
||||
.put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300)))
|
||||
.build();
|
||||
|
||||
@ -396,4 +398,11 @@ public class Netty4HttpServerTransportTests extends ESTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private Settings createSettings() {
|
||||
return createBuilderWithPort().build();
|
||||
}
|
||||
|
||||
private Settings.Builder createBuilderWithPort() {
|
||||
return Settings.builder().put(HttpTransportSettings.SETTING_HTTP_PORT.getKey(), getPortRange());
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
* @throws InterruptedException if the client communication with the server is interrupted
|
||||
*/
|
||||
public void testExpectContinueHeader() throws InterruptedException {
|
||||
final Settings settings = Settings.EMPTY;
|
||||
final Settings settings = createSettings();
|
||||
final int contentLength = randomIntBetween(1, HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.get(settings).bytesAsInt());
|
||||
runExpectHeaderTest(settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.CONTINUE);
|
||||
}
|
||||
@ -127,7 +127,7 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
public void testExpectContinueHeaderContentLengthTooLong() throws InterruptedException {
|
||||
final String key = HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.getKey();
|
||||
final int maxContentLength = randomIntBetween(1, 104857600);
|
||||
final Settings settings = Settings.builder().put(key, maxContentLength + "b").build();
|
||||
final Settings settings = createBuilderWithPort().put(key, maxContentLength + "b").build();
|
||||
final int contentLength = randomIntBetween(maxContentLength + 1, Integer.MAX_VALUE);
|
||||
runExpectHeaderTest(
|
||||
settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
|
||||
@ -138,7 +138,8 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
* @throws InterruptedException if the client communication with the server is interrupted
|
||||
*/
|
||||
public void testExpectUnsupportedExpectation() throws InterruptedException {
|
||||
runExpectHeaderTest(Settings.EMPTY, "chocolate=yummy", 0, HttpResponseStatus.EXPECTATION_FAILED);
|
||||
final Settings settings = createSettings();
|
||||
runExpectHeaderTest(settings, "chocolate=yummy", 0, HttpResponseStatus.EXPECTATION_FAILED);
|
||||
}
|
||||
|
||||
private void runExpectHeaderTest(
|
||||
@ -193,7 +194,8 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
}
|
||||
|
||||
public void testBindUnavailableAddress() {
|
||||
try (NioHttpServerTransport transport = new NioHttpServerTransport(Settings.EMPTY, networkService, bigArrays, pageRecycler,
|
||||
final Settings initialSettings = createSettings();
|
||||
try (NioHttpServerTransport transport = new NioHttpServerTransport(initialSettings, networkService, bigArrays, pageRecycler,
|
||||
threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger),
|
||||
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
|
||||
transport.start();
|
||||
@ -234,9 +236,10 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
|
||||
};
|
||||
|
||||
final Settings settings = Settings.builder()
|
||||
final Settings settings = createBuilderWithPort()
|
||||
.put(SETTING_CORS_ENABLED.getKey(), true)
|
||||
.put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co").build();
|
||||
.put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co")
|
||||
.build();
|
||||
|
||||
try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler,
|
||||
threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger),
|
||||
@ -304,10 +307,10 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
|
||||
if (randomBoolean()) {
|
||||
maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
|
||||
settings = Settings.EMPTY;
|
||||
settings = createSettings();
|
||||
} else {
|
||||
maxInitialLineLength = randomIntBetween(1, 8192);
|
||||
settings = Settings.builder().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
|
||||
settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
|
||||
}
|
||||
|
||||
try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler,
|
||||
@ -356,7 +359,7 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
|
||||
};
|
||||
|
||||
Settings settings = Settings.builder()
|
||||
Settings settings = createBuilderWithPort()
|
||||
.put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300)))
|
||||
.build();
|
||||
|
||||
@ -382,4 +385,11 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private Settings createSettings() {
|
||||
return createBuilderWithPort().build();
|
||||
}
|
||||
|
||||
private Settings.Builder createBuilderWithPort() {
|
||||
return Settings.builder().put(HttpTransportSettings.SETTING_HTTP_PORT.getKey(), getPortRange());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user