Search in sources :

Example 46 with ChannelHandler

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

the class HttpServerUpgradeHandlerTest method upgradesPipelineInSameMethodInvocation.

@Test
public void upgradesPipelineInSameMethodInvocation() {
    final HttpServerCodec httpServerCodec = new HttpServerCodec();
    final UpgradeCodecFactory factory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new TestUpgradeCodec();
        }
    };
    ChannelHandler testInStackFrame = new ChannelDuplexHandler() {

        // marker boolean to signal that we're in the `channelRead` method
        private boolean inReadCall;

        private boolean writeUpgradeMessage;

        private boolean writeFlushed;

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            assertFalse(inReadCall);
            assertFalse(writeUpgradeMessage);
            inReadCall = true;
            try {
                super.channelRead(ctx, msg);
                // All in the same call stack, the upgrade codec should receive the message,
                // written the upgrade response, and upgraded the pipeline.
                assertTrue(writeUpgradeMessage);
                assertFalse(writeFlushed);
                assertNull(ctx.pipeline().get(HttpServerCodec.class));
                assertNotNull(ctx.pipeline().get("marker"));
            } finally {
                inReadCall = false;
            }
        }

        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
            // We ensure that we're in the read call and defer the write so we can
            // make sure the pipeline was reformed irrespective of the flush completing.
            assertTrue(inReadCall);
            writeUpgradeMessage = true;
            ctx.channel().eventLoop().execute(new Runnable() {

                @Override
                public void run() {
                    ctx.write(msg, promise);
                }
            });
            promise.addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) {
                    writeFlushed = true;
                }
            });
        }
    };
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory);
    EmbeddedChannel channel = new EmbeddedChannel(testInStackFrame, httpServerCodec, upgradeHandler);
    String upgradeString = "GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade, HTTP2-Settings\r\n" + "Upgrade: nextprotocol\r\n" + "HTTP2-Settings: AAMAAABkAAQAAP__\r\n\r\n";
    ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
    assertFalse(channel.writeInbound(upgrade));
    assertNull(channel.pipeline().get(HttpServerCodec.class));
    assertNotNull(channel.pipeline().get("marker"));
    channel.flushOutbound();
    ByteBuf upgradeMessage = channel.readOutbound();
    String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" + "connection: upgrade\r\n" + "upgrade: nextprotocol\r\n\r\n";
    assertEquals(expectedHttpResponse, upgradeMessage.toString(CharsetUtil.US_ASCII));
    assertTrue(upgradeMessage.release());
    assertFalse(channel.finishAndReleaseAll());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Test(org.junit.jupiter.api.Test)

Example 47 with ChannelHandler

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

the class EmbeddedChannelTest method testHandlerAddedExecutedInEventLoop.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedExecutedInEventLoop() throws Throwable {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final ChannelHandler handler = new ChannelHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            try {
                assertTrue(ctx.executor().inEventLoop());
            } catch (Throwable cause) {
                error.set(cause);
            } finally {
                latch.countDown();
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    assertFalse(channel.finish());
    latch.await();
    Throwable cause = error.get();
    if (cause != null) {
        throw cause;
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 48 with ChannelHandler

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

the class EmbeddedChannelTest method testConstructWithChannelInitializer.

@Test
public void testConstructWithChannelInitializer() {
    final Integer first = 1;
    final Integer second = 2;
    final ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ctx.fireChannelRead(first);
            ctx.fireChannelRead(second);
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(handler);
        }
    });
    ChannelPipeline pipeline = channel.pipeline();
    assertSame(handler, pipeline.firstContext().handler());
    assertTrue(channel.writeInbound(3));
    assertTrue(channel.finish());
    assertSame(first, channel.readInbound());
    assertSame(second, channel.readInbound());
    assertNull(channel.readInbound());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPipeline(io.netty.channel.ChannelPipeline) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 49 with ChannelHandler

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

the class AbstractCompatibleMarshallingDecoderTest method testTooBigObject.

@Test
public void testTooBigObject() throws IOException {
    MarshallerFactory marshallerFactory = createMarshallerFactory();
    MarshallingConfiguration configuration = createMarshallingConfig();
    ChannelHandler mDecoder = createDecoder(4);
    EmbeddedChannel ch = new EmbeddedChannel(mDecoder);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Marshaller marshaller = marshallerFactory.createMarshaller(configuration);
    marshaller.start(Marshalling.createByteOutput(bout));
    marshaller.writeObject(testObject);
    marshaller.finish();
    marshaller.close();
    byte[] testBytes = bout.toByteArray();
    onTooBigFrame(ch, input(testBytes));
}
Also used : MarshallerFactory(org.jboss.marshalling.MarshallerFactory) Marshaller(org.jboss.marshalling.Marshaller) MarshallingConfiguration(org.jboss.marshalling.MarshallingConfiguration) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandler(io.netty.channel.ChannelHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 50 with ChannelHandler

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

the class FlowControlHandlerTest method testRemoveFlowControl.

@Test
public void testRemoveFlowControl() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelDuplexHandler() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            // do the first read
            ctx.read();
            super.channelActive(ctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            latch.countDown();
            super.channelRead(ctx, msg);
        }
    };
    FlowControlHandler flow = new FlowControlHandler() {

        private int num;

        @Override
        public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
            super.channelRead(ctx, msg);
            ++num;
            if (num >= 3) {
                // We have received 3 messages. Remove myself later
                final ChannelHandler handler = this;
                ctx.channel().eventLoop().execute(new Runnable() {

                    @Override
                    public void run() {
                        ctx.pipeline().remove(handler);
                    }
                });
            }
        }
    };
    ChannelInboundHandlerAdapter tail = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            // consume this msg
            ReferenceCountUtil.release(msg);
        }
    };
    Channel server = newServer(false, /* no auto read */
    flow, handler, tail);
    Channel client = newClient(server.localAddress());
    try {
        // Write one message
        client.writeAndFlush(newOneMessage()).sync();
        // We should receive 3 messages
        assertTrue(latch.await(1L, SECONDS));
        assertTrue(flow.isQueueEmpty());
    } finally {
        client.close();
        server.close();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)216 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)27 ChannelPipeline (io.netty.channel.ChannelPipeline)26 SslHandler (io.netty.handler.ssl.SslHandler)25 Test (org.junit.jupiter.api.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)23 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)21 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 InetSocketAddress (java.net.InetSocketAddress)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 TcpIngester (com.wavefront.ingester.TcpIngester)15 ByteBuf (io.netty.buffer.ByteBuf)13