|
19 | 19 |
|
20 | 20 | package org.elasticsearch.http.netty4; |
21 | 21 |
|
| 22 | +import io.netty.bootstrap.Bootstrap; |
22 | 23 | import io.netty.buffer.ByteBufUtil; |
23 | 24 | import io.netty.buffer.Unpooled; |
| 25 | +import io.netty.channel.ChannelFuture; |
| 26 | +import io.netty.channel.ChannelHandlerAdapter; |
| 27 | +import io.netty.channel.ChannelInitializer; |
| 28 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 29 | +import io.netty.channel.socket.SocketChannel; |
| 30 | +import io.netty.channel.socket.nio.NioSocketChannel; |
24 | 31 | import io.netty.handler.codec.TooLongFrameException; |
25 | 32 | import io.netty.handler.codec.http.DefaultFullHttpRequest; |
26 | 33 | import io.netty.handler.codec.http.FullHttpRequest; |
|
39 | 46 | import org.elasticsearch.common.settings.Settings; |
40 | 47 | import org.elasticsearch.common.transport.TransportAddress; |
41 | 48 | import org.elasticsearch.common.unit.ByteSizeValue; |
| 49 | +import org.elasticsearch.common.unit.TimeValue; |
42 | 50 | import org.elasticsearch.common.util.MockBigArrays; |
43 | 51 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
44 | 52 | import org.elasticsearch.http.BindHttpException; |
|
63 | 71 | import java.util.Collections; |
64 | 72 | import java.util.HashSet; |
65 | 73 | import java.util.Set; |
| 74 | +import java.util.concurrent.TimeUnit; |
| 75 | +import java.util.concurrent.atomic.AtomicBoolean; |
66 | 76 | import java.util.concurrent.atomic.AtomicReference; |
67 | 77 | import java.util.stream.Collectors; |
68 | 78 |
|
@@ -313,4 +323,53 @@ public void dispatchBadRequest(final RestRequest request, |
313 | 323 | assertNull(threadPool.getThreadContext().getTransient("bar_bad")); |
314 | 324 | } |
315 | 325 | } |
| 326 | + |
| 327 | + public void testReadTimeout() throws Exception { |
| 328 | + final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() { |
| 329 | + |
| 330 | + @Override |
| 331 | + public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) { |
| 332 | + throw new AssertionError("Should not have received a dispatched request"); |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public void dispatchBadRequest(final RestRequest request, |
| 337 | + final RestChannel channel, |
| 338 | + final ThreadContext threadContext, |
| 339 | + final Throwable cause) { |
| 340 | + throw new AssertionError("Should not have received a dispatched request"); |
| 341 | + } |
| 342 | + |
| 343 | + }; |
| 344 | + |
| 345 | + Settings settings = Settings.builder() |
| 346 | + .put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300))) |
| 347 | + .build(); |
| 348 | + |
| 349 | + |
| 350 | + NioEventLoopGroup group = new NioEventLoopGroup(); |
| 351 | + try (Netty4HttpServerTransport transport = |
| 352 | + new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher)) { |
| 353 | + transport.start(); |
| 354 | + final TransportAddress remoteAddress = randomFrom(transport.boundAddress.boundAddresses()); |
| 355 | + |
| 356 | + AtomicBoolean channelClosed = new AtomicBoolean(false); |
| 357 | + |
| 358 | + Bootstrap clientBootstrap = new Bootstrap().channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { |
| 359 | + |
| 360 | + @Override |
| 361 | + protected void initChannel(SocketChannel ch) { |
| 362 | + ch.pipeline().addLast(new ChannelHandlerAdapter() {}); |
| 363 | + |
| 364 | + } |
| 365 | + }).group(group); |
| 366 | + ChannelFuture connect = clientBootstrap.connect(remoteAddress.address()); |
| 367 | + connect.channel().closeFuture().addListener(future -> channelClosed.set(true)); |
| 368 | + |
| 369 | + assertBusy(() -> assertTrue("Channel should be closed due to read timeout", channelClosed.get()), 5, TimeUnit.SECONDS); |
| 370 | + |
| 371 | + } finally { |
| 372 | + group.shutdownGracefully().await(); |
| 373 | + } |
| 374 | + } |
316 | 375 | } |
0 commit comments