Skip to content

Commit 24484ae

Browse files
committed
Fix http read timeout test by releasing response (#41801)
This fixes #41794. Currently the read timeout test queues up responses in the netty pipeline. These responses are immediately returned in the write call, but they are not released. This commit releases the responses. This will cause the leak detector to quit throwing exceptions.
1 parent 241c4ef commit 24484ae

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/HttpReadWriteHandlerTests.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ public void testDecodeHttpRequestContentLengthToLongGeneratesOutboundMessage() t
189189
@SuppressWarnings("unchecked")
190190
public void testEncodeHttpResponse() throws IOException {
191191
prepareHandlerForResponse(handler);
192-
193-
DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
194-
NioHttpRequest nioHttpRequest = new NioHttpRequest(nettyRequest, 0);
195-
NioHttpResponse httpResponse = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
196-
httpResponse.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), "0");
192+
NioHttpResponse httpResponse = emptyGetResponse(0);
197193

198194
SocketChannelContext context = mock(SocketChannelContext.class);
199195
HttpWriteOperation writeOperation = new HttpWriteOperation(context, httpResponse, mock(BiConsumer.class));
@@ -332,10 +328,6 @@ public void testReadTimeout() throws IOException {
332328
TimeValue timeValue = TimeValue.timeValueMillis(500);
333329
Settings settings = Settings.builder().put(SETTING_HTTP_READ_TIMEOUT.getKey(), timeValue).build();
334330
HttpHandlingSettings httpHandlingSettings = HttpHandlingSettings.fromSettings(settings);
335-
DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
336-
NioHttpRequest nioHttpRequest = new NioHttpRequest(nettyRequest, 0);
337-
NioHttpResponse httpResponse = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
338-
httpResponse.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), "0");
339331

340332
NioCorsConfig corsConfig = NioCorsConfigBuilder.forAnyOrigin().build();
341333
TaskScheduler taskScheduler = new TaskScheduler();
@@ -346,8 +338,8 @@ public void testReadTimeout() throws IOException {
346338

347339
prepareHandlerForResponse(handler);
348340
SocketChannelContext context = mock(SocketChannelContext.class);
349-
HttpWriteOperation writeOperation = new HttpWriteOperation(context, httpResponse, mock(BiConsumer.class));
350-
handler.writeToBytes(writeOperation);
341+
HttpWriteOperation writeOperation0 = new HttpWriteOperation(context, emptyGetResponse(0), mock(BiConsumer.class));
342+
((ChannelPromise) handler.writeToBytes(writeOperation0).get(0).getListener()).setSuccess();
351343

352344
taskScheduler.pollTask(timeValue.getNanos() + 1).run();
353345
// There was a read. Do not close.
@@ -360,20 +352,30 @@ public void testReadTimeout() throws IOException {
360352
// There was a read. Do not close.
361353
verify(transport, times(0)).onException(eq(channel), any(HttpReadTimeoutException.class));
362354

363-
handler.writeToBytes(writeOperation);
355+
HttpWriteOperation writeOperation1 = new HttpWriteOperation(context, emptyGetResponse(1), mock(BiConsumer.class));
356+
((ChannelPromise) handler.writeToBytes(writeOperation1).get(0).getListener()).setSuccess();
364357

365358
taskScheduler.pollTask(timeValue.getNanos() + 5).run();
366359
// There has not been a read, however there is still an inflight request. Do not close.
367360
verify(transport, times(0)).onException(eq(channel), any(HttpReadTimeoutException.class));
368361

369-
handler.writeToBytes(writeOperation);
362+
HttpWriteOperation writeOperation2 = new HttpWriteOperation(context, emptyGetResponse(2), mock(BiConsumer.class));
363+
((ChannelPromise) handler.writeToBytes(writeOperation2).get(0).getListener()).setSuccess();
370364

371365
taskScheduler.pollTask(timeValue.getNanos() + 7).run();
372366
// No reads and no inflight requests, close
373367
verify(transport, times(1)).onException(eq(channel), any(HttpReadTimeoutException.class));
374368
assertNull(taskScheduler.pollTask(timeValue.getNanos() + 9));
375369
}
376370

371+
private static NioHttpResponse emptyGetResponse(int sequenceNumber) {
372+
DefaultFullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
373+
NioHttpRequest nioHttpRequest = new NioHttpRequest(nettyRequest, sequenceNumber);
374+
NioHttpResponse httpResponse = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
375+
httpResponse.addHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), "0");
376+
return httpResponse;
377+
}
378+
377379
private FullHttpResponse executeCorsRequest(final Settings settings, final String originValue, final String host) throws IOException {
378380
HttpHandlingSettings httpSettings = HttpHandlingSettings.fromSettings(settings);
379381
NioCorsConfig corsConfig = NioHttpServerTransport.buildCorsConfig(settings);

0 commit comments

Comments
 (0)