use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.
the class EmbeddedChannelTest method testWriteScheduled.
@Test
public void testWriteScheduled() throws InterruptedException {
final int delay = 500;
EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
ctx.executor().schedule(new Runnable() {
@Override
public void run() {
ctx.writeAndFlush(msg, promise);
}
}, delay, TimeUnit.MILLISECONDS);
}
});
Object msg = new Object();
assertFalse(channel.writeOutbound(msg));
Thread.sleep(delay * 2);
assertTrue(channel.finish());
assertSame(msg, channel.readOutbound());
assertNull(channel.readOutbound());
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.
the class HttpContentCompressorTest method testExecutorPreserveOrdering.
@Test
public void testExecutorPreserveOrdering() throws Exception {
final EventLoopGroup compressorGroup = new DefaultEventLoopGroup(1);
EventLoopGroup localGroup = new DefaultEventLoopGroup(1);
Channel server = null;
Channel client = null;
try {
ServerBootstrap bootstrap = new ServerBootstrap().channel(LocalServerChannel.class).group(localGroup).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new HttpServerCodec()).addLast(new HttpObjectAggregator(1024)).addLast(compressorGroup, new HttpContentCompressor()).addLast(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
super.write(ctx, msg, promise);
}
}).addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("Hello, World", CharsetUtil.US_ASCII));
ctx.writeAndFlush(res);
ReferenceCountUtil.release(msg);
return;
}
super.channelRead(ctx, msg);
}
});
}
});
LocalAddress address = new LocalAddress(UUID.randomUUID().toString());
server = bootstrap.bind(address).sync().channel();
final BlockingQueue<HttpObject> responses = new LinkedBlockingQueue<HttpObject>();
client = new Bootstrap().channel(LocalChannel.class).remoteAddress(address).group(localGroup).handler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new HttpClientCodec()).addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpObject) {
responses.put((HttpObject) msg);
return;
}
super.channelRead(ctx, msg);
}
});
}
}).connect().sync().channel();
client.writeAndFlush(newRequest()).sync();
assertEncodedResponse((HttpResponse) responses.poll(1, TimeUnit.SECONDS));
HttpContent c = (HttpContent) responses.poll(1, TimeUnit.SECONDS);
assertNotNull(c);
assertThat(ByteBufUtil.hexDump(c.content()), is("1f8b0800000000000000f248cdc9c9d75108cf2fca4901000000ffff"));
c.release();
c = (HttpContent) responses.poll(1, TimeUnit.SECONDS);
assertNotNull(c);
assertThat(ByteBufUtil.hexDump(c.content()), is("0300c6865b260c000000"));
c.release();
LastHttpContent last = (LastHttpContent) responses.poll(1, TimeUnit.SECONDS);
assertNotNull(last);
assertThat(last.content().readableBytes(), is(0));
last.release();
assertNull(responses.poll(1, TimeUnit.SECONDS));
} finally {
if (client != null) {
client.close().sync();
}
if (server != null) {
server.close().sync();
}
compressorGroup.shutdownGracefully();
localGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.
the class HttpContentDecompressorTest method testInvokeReadWhenNotProduceMessage.
// See https://github.com/netty/netty/issues/8915.
@Test
public void testInvokeReadWhenNotProduceMessage() {
final AtomicInteger readCalled = new AtomicInteger();
EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void read(ChannelHandlerContext ctx) {
readCalled.incrementAndGet();
ctx.read();
}
}, new HttpContentDecompressor(), new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.fireChannelRead(msg);
ctx.read();
}
});
channel.config().setAutoRead(false);
readCalled.set(0);
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_ENCODING, "gzip");
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json;charset=UTF-8");
response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
assertTrue(channel.writeInbound(response));
// we triggered read explicitly
assertEquals(1, readCalled.get());
assertTrue(channel.readInbound() instanceof HttpResponse);
assertFalse(channel.writeInbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)));
// read was triggered by the HttpContentDecompressor itself as it did not produce any message to the next
// inbound handler.
assertEquals(2, readCalled.get());
assertFalse(channel.finishAndReleaseAll());
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.
the class Http2ConnectionRoundtripTest method writeFailureFlowControllerRemoveFrame.
@Test
public void writeFailureFlowControllerRemoveFrame() throws Exception {
bootstrapEnv(1, 1, 2, 1);
final ChannelPromise dataPromise = newPromise();
final ChannelPromise assertPromise = newPromise();
runInChannel(clientChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
http2Client.encoder().writeHeaders(ctx(), 3, EmptyHttp2Headers.INSTANCE, 0, (short) 16, false, 0, false, newPromise());
clientChannel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
ReferenceCountUtil.release(msg);
// Ensure we update the window size so we will try to write the rest of the frame while
// processing the flush.
http2Client.encoder().flowController().initialWindowSize(8);
promise.setFailure(new IllegalStateException());
}
});
http2Client.encoder().flowController().initialWindowSize(4);
http2Client.encoder().writeData(ctx(), 3, randomBytes(8), 0, false, dataPromise);
assertTrue(http2Client.encoder().flowController().hasFlowControlled(http2Client.connection().stream(3)));
http2Client.flush(ctx());
try {
// The Frame should have been removed after the write failed.
assertFalse(http2Client.encoder().flowController().hasFlowControlled(http2Client.connection().stream(3)));
assertPromise.setSuccess();
} catch (Throwable error) {
assertPromise.setFailure(error);
}
}
});
ExecutionException e = assertThrows(ExecutionException.class, new Executable() {
@Override
public void execute() throws Throwable {
dataPromise.get();
}
});
assertThat(e.getCause(), is(instanceOf(IllegalStateException.class)));
assertPromise.sync();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.
the class Http2StreamFrameToHttpObjectCodecTest method testIsSharableBetweenChannels.
@Test
public void testIsSharableBetweenChannels() throws Exception {
final Queue<Http2StreamFrame> frames = new ConcurrentLinkedQueue<Http2StreamFrame>();
final ChannelHandler sharedHandler = new Http2StreamFrameToHttpObjectCodec(false);
final SslContext ctx = SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build();
EmbeddedChannel tlsCh = new EmbeddedChannel(ctx.newHandler(ByteBufAllocator.DEFAULT), new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof Http2StreamFrame) {
frames.add((Http2StreamFrame) msg);
promise.setSuccess();
} else {
ctx.write(msg, promise);
}
}
}, sharedHandler);
EmbeddedChannel plaintextCh = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof Http2StreamFrame) {
frames.add((Http2StreamFrame) msg);
promise.setSuccess();
} else {
ctx.write(msg, promise);
}
}
}, sharedHandler);
FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello/world");
assertTrue(tlsCh.writeOutbound(req));
assertTrue(tlsCh.finishAndReleaseAll());
Http2HeadersFrame headersFrame = (Http2HeadersFrame) frames.poll();
Http2Headers headers = headersFrame.headers();
assertThat(headers.scheme().toString(), is("https"));
assertThat(headers.method().toString(), is("GET"));
assertThat(headers.path().toString(), is("/hello/world"));
assertTrue(headersFrame.isEndStream());
assertNull(frames.poll());
// Run the plaintext channel
req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello/world");
assertFalse(plaintextCh.writeOutbound(req));
assertFalse(plaintextCh.finishAndReleaseAll());
headersFrame = (Http2HeadersFrame) frames.poll();
headers = headersFrame.headers();
assertThat(headers.scheme().toString(), is("http"));
assertThat(headers.method().toString(), is("GET"));
assertThat(headers.path().toString(), is("/hello/world"));
assertTrue(headersFrame.isEndStream());
assertNull(frames.poll());
}
Aggregations