Search in sources :

Example 11 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class ChunkedWriteHandlerTest method checkFirstFailed.

private static void checkFirstFailed(Object input) {
    ChannelOutboundHandlerAdapter noOpWrites = new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            ReferenceCountUtil.release(msg);
            promise.tryFailure(new RuntimeException());
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(noOpWrites, new ChunkedWriteHandler());
    ChannelFuture r = ch.writeAndFlush(input);
    // Should be `false` as we do not expect any messages to be written
    assertFalse(ch.finish());
    assertTrue(r.cause() instanceof RuntimeException);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise)

Example 12 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class ChunkedWriteHandlerTest method testEndOfInputWhenChannelIsClosedwhenWrite.

@Test
public void testEndOfInputWhenChannelIsClosedwhenWrite() {
    ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {

        @Override
        public boolean isEndOfInput() {
            return true;
        }

        @Override
        public void close() {
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) {
            return null;
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) {
            return null;
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            ReferenceCountUtil.release(msg);
            // Calling close so we will drop all queued messages in the ChunkedWriteHandler.
            ctx.close();
            promise.setSuccess();
        }
    }, new ChunkedWriteHandler());
    ch.writeAndFlush(input).syncUninterruptibly();
    assertFalse(ch.finishAndReleaseAll());
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 13 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeHttpsSchemeWhenSslHandlerExists.

@Test
public void testEncodeHttpsSchemeWhenSslHandlerExists() throws Exception {
    final Queue<Http2StreamFrame> frames = new ConcurrentLinkedQueue<Http2StreamFrame>();
    final SslContext ctx = SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build();
    EmbeddedChannel ch = new EmbeddedChannel(ctx.newHandler(ByteBufAllocator.DEFAULT), new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (msg instanceof Http2StreamFrame) {
                frames.add((Http2StreamFrame) msg);
                ctx.write(Unpooled.EMPTY_BUFFER, promise);
            } else {
                ctx.write(msg, promise);
            }
        }
    }, new Http2StreamFrameToHttpObjectCodec(false));
    try {
        FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello/world");
        assertTrue(ch.writeOutbound(req));
        ch.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());
    } finally {
        ch.finishAndReleaseAll();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) EncoderException(io.netty.handler.codec.EncoderException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Example 14 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class WebSocketProtocolHandlerTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    final AtomicReference<ChannelPromise> ref = new AtomicReference<ChannelPromise>();
    WebSocketProtocolHandler handler = new WebSocketProtocolHandler(false, WebSocketCloseStatus.NORMAL_CLOSURE, 1) {
    };
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            ref.set(promise);
            ReferenceCountUtil.release(msg);
        }
    }, handler);
    ChannelFuture future = channel.writeAndFlush(new CloseWebSocketFrame());
    ChannelHandlerContext ctx = channel.pipeline().context(WebSocketProtocolHandler.class);
    handler.close(ctx, ctx.newPromise());
    do {
        Thread.sleep(10);
        channel.runPendingTasks();
    } while (!future.isDone());
    assertThat(future.cause(), Matchers.instanceOf(WebSocketHandshakeException.class));
    assertFalse(ref.get().isDone());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelPromise(io.netty.channel.ChannelPromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.jupiter.api.Test)

Example 15 with ChannelOutboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter in project netty by netty.

the class EmbeddedChannelTest method testWriteLater.

@Test
public void testWriteLater() {
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
            ctx.executor().execute(new Runnable() {

                @Override
                public void run() {
                    ctx.write(msg, promise);
                }
            });
        }
    });
    Object msg = new Object();
    assertTrue(channel.writeOutbound(msg));
    assertTrue(channel.finish());
    assertSame(msg, channel.readOutbound());
    assertNull(channel.readOutbound());
}
Also used : ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)32 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)32 ChannelPromise (io.netty.channel.ChannelPromise)30 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)18 Test (org.junit.jupiter.api.Test)17 ByteBuf (io.netty.buffer.ByteBuf)10 Test (org.junit.Test)9 IOException (java.io.IOException)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 ChannelFuture (io.netty.channel.ChannelFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Channel (io.netty.channel.Channel)3 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 SslContext (io.netty.handler.ssl.SslContext)3 ByteBuffer (java.nio.ByteBuffer)3 WritableByteChannel (java.nio.channels.WritableByteChannel)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ApiErrorWithMetadata (com.nike.backstopper.apierror.ApiErrorWithMetadata)2 NettyHttpClientRequestBuilder (com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder)2 NettyHttpClientResponse (com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse)2