Skip to content

Commit 164d737

Browse files
authored
Speed up testRespondAfterClose (#117969)
Since #106511 this test takes 30s because it waits for the client to time out and close the connection before allowing the transport to fully shut down. This commit reinstates the previous behaviour of closing connections quickly, and tests both client-triggered and server-triggered connection closure.
1 parent 2fe6b60 commit 164d737

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.netty.handler.codec.http.HttpUtil;
4141
import io.netty.handler.codec.http.HttpVersion;
4242

43+
import org.apache.http.ConnectionClosedException;
4344
import org.apache.http.HttpHost;
4445
import org.apache.lucene.util.SetOnce;
4546
import org.elasticsearch.ElasticsearchException;
@@ -48,6 +49,7 @@
4849
import org.elasticsearch.action.ActionListener;
4950
import org.elasticsearch.action.bulk.IncrementalBulkService;
5051
import org.elasticsearch.action.support.ActionTestUtils;
52+
import org.elasticsearch.action.support.PlainActionFuture;
5153
import org.elasticsearch.action.support.SubscribableListener;
5254
import org.elasticsearch.client.Request;
5355
import org.elasticsearch.client.RestClient;
@@ -100,6 +102,7 @@
100102
import java.util.Collections;
101103
import java.util.List;
102104
import java.util.Set;
105+
import java.util.concurrent.CancellationException;
103106
import java.util.concurrent.ConcurrentHashMap;
104107
import java.util.concurrent.CountDownLatch;
105108
import java.util.concurrent.TimeUnit;
@@ -110,6 +113,7 @@
110113
import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
111114
import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ALLOW_ORIGIN;
112115
import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ENABLED;
116+
import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_SERVER_SHUTDOWN_GRACE_PERIOD;
113117
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
114118
import static org.elasticsearch.rest.RestStatus.OK;
115119
import static org.elasticsearch.rest.RestStatus.UNAUTHORIZED;
@@ -1039,8 +1043,16 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
10391043
}
10401044
}
10411045

1042-
public void testRespondAfterClose() throws Exception {
1043-
final String url = "/thing";
1046+
public void testRespondAfterServiceCloseWithClientCancel() throws Exception {
1047+
runRespondAfterServiceCloseTest(true);
1048+
}
1049+
1050+
public void testRespondAfterServiceCloseWithServerCancel() throws Exception {
1051+
runRespondAfterServiceCloseTest(false);
1052+
}
1053+
1054+
private void runRespondAfterServiceCloseTest(boolean clientCancel) throws Exception {
1055+
final String url = "/" + randomIdentifier();
10441056
final CountDownLatch responseReleasedLatch = new CountDownLatch(1);
10451057
final SubscribableListener<Void> transportClosedFuture = new SubscribableListener<>();
10461058
final CountDownLatch handlingRequestLatch = new CountDownLatch(1);
@@ -1066,7 +1078,9 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
10661078

10671079
try (
10681080
Netty4HttpServerTransport transport = new Netty4HttpServerTransport(
1069-
Settings.EMPTY,
1081+
clientCancel
1082+
? Settings.EMPTY
1083+
: Settings.builder().put(SETTING_HTTP_SERVER_SHUTDOWN_GRACE_PERIOD.getKey(), TimeValue.timeValueMillis(1)).build(),
10701084
networkService,
10711085
threadPool,
10721086
xContentRegistry(),
@@ -1082,11 +1096,24 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
10821096
transport.start();
10831097
final var address = randomFrom(transport.boundAddress().boundAddresses()).address();
10841098
try (var client = RestClient.builder(new HttpHost(address.getAddress(), address.getPort())).build()) {
1085-
client.performRequestAsync(new Request("GET", url), ActionTestUtils.wrapAsRestResponseListener(ActionListener.noop()));
1099+
final var responseExceptionFuture = new PlainActionFuture<Exception>();
1100+
final var cancellable = client.performRequestAsync(
1101+
new Request("GET", url),
1102+
ActionTestUtils.wrapAsRestResponseListener(ActionTestUtils.assertNoSuccessListener(responseExceptionFuture::onResponse))
1103+
);
10861104
safeAwait(handlingRequestLatch);
1105+
if (clientCancel) {
1106+
threadPool.generic().execute(cancellable::cancel);
1107+
}
10871108
transport.close();
10881109
transportClosedFuture.onResponse(null);
10891110
safeAwait(responseReleasedLatch);
1111+
final var responseException = safeGet(responseExceptionFuture);
1112+
if (clientCancel) {
1113+
assertThat(responseException, instanceOf(CancellationException.class));
1114+
} else {
1115+
assertThat(responseException, instanceOf(ConnectionClosedException.class));
1116+
}
10901117
}
10911118
}
10921119
}

0 commit comments

Comments
 (0)