Search in sources :

Example 96 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class FlowControlHandlerTest method testFlowAutoReadOff.

/**
     * The {@link FlowControlHandler} will pass down messages one by one
     * if auto reading is off and the user is calling {@code read()} on
     * their own.
     */
@Test
public void testFlowAutoReadOff() throws Exception {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch msgRcvLatch1 = new CountDownLatch(1);
    final CountDownLatch msgRcvLatch2 = new CountDownLatch(2);
    final CountDownLatch msgRcvLatch3 = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelDuplexHandler() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.fireChannelActive();
            peerRef.exchange(ctx.channel(), 1L, SECONDS);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            msgRcvLatch1.countDown();
            msgRcvLatch2.countDown();
            msgRcvLatch3.countDown();
        }
    };
    FlowControlHandler flow = new FlowControlHandler();
    Channel server = newServer(false, flow, handler);
    Channel client = newClient(server.localAddress());
    try {
        // The client connection on the server side
        Channel peer = peerRef.exchange(null, 1L, SECONDS);
        // Write the message
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // channelRead(1)
        peer.read();
        assertTrue(msgRcvLatch1.await(1L, SECONDS));
        // channelRead(2)
        peer.read();
        assertTrue(msgRcvLatch2.await(1L, SECONDS));
        // channelRead(3)
        peer.read();
        assertTrue(msgRcvLatch3.await(1L, SECONDS));
        assertTrue(flow.isQueueEmpty());
    } finally {
        client.close();
        server.close();
    }
}
Also used : Exchanger(java.util.concurrent.Exchanger) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 97 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class FlowControlHandlerTest method newClient.

private static Channel newClient(SocketAddress server) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(GROUP).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000).handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            fail("In this test the client is never receiving a message from the server.");
        }
    });
    return bootstrap.connect(server).syncUninterruptibly().channel();
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 98 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class FlowControlHandlerTest method testFlowAutoReadOn.

/**
     * The {@link FlowControlHandler} will simply pass-through all messages
     * if auto reading is on and remains on.
     */
@Test
public void testFlowAutoReadOn() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelDuplexHandler() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            latch.countDown();
        }
    };
    FlowControlHandler flow = new FlowControlHandler();
    Channel server = newServer(true, flow, handler);
    Channel client = newClient(server.localAddress());
    try {
        // Write the message
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // 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) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 99 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class OptionalSslHandlerTest method handlerReplaced.

@Test
public void handlerReplaced() throws Exception {
    final ChannelHandler nonSslHandler = Mockito.mock(ChannelHandler.class);
    OptionalSslHandler handler = new OptionalSslHandler(sslContext) {

        @Override
        protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
            return nonSslHandler;
        }

        @Override
        protected String newNonSslHandlerName() {
            return HANDLER_NAME;
        }
    };
    final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes());
    try {
        handler.decode(context, payload, null);
        verify(pipeline).replace(handler, HANDLER_NAME, nonSslHandler);
    } finally {
        payload.release();
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 100 with ChannelHandlerContext

use of io.netty.channel.ChannelHandlerContext in project netty by netty.

the class OptionalSslHandlerTest method sslHandlerReplaced.

@Test
public void sslHandlerReplaced() throws Exception {
    final SslHandler sslHandler = Mockito.mock(SslHandler.class);
    OptionalSslHandler handler = new OptionalSslHandler(sslContext) {

        @Override
        protected SslHandler newSslHandler(ChannelHandlerContext context, SslContext sslContext) {
            return sslHandler;
        }

        @Override
        protected String newSslHandlerName() {
            return SSL_HANDLER_NAME;
        }
    };
    final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3, 1, 0, 5 });
    try {
        handler.decode(context, payload, null);
        verify(pipeline).replace(handler, SSL_HANDLER_NAME, sslHandler);
    } finally {
        payload.release();
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)242 Test (org.junit.Test)143 Channel (io.netty.channel.Channel)106 ByteBuf (io.netty.buffer.ByteBuf)85 ChannelFuture (io.netty.channel.ChannelFuture)84 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)76 Bootstrap (io.netty.bootstrap.Bootstrap)66 ChannelPipeline (io.netty.channel.ChannelPipeline)64 ClosedChannelException (java.nio.channels.ClosedChannelException)63 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)55 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)55 InetSocketAddress (java.net.InetSocketAddress)54 AtomicReference (java.util.concurrent.atomic.AtomicReference)53 EventLoopGroup (io.netty.channel.EventLoopGroup)48 CountDownLatch (java.util.concurrent.CountDownLatch)47 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)45 ArrayList (java.util.ArrayList)43 List (java.util.List)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)40 Http2Exception (io.netty.handler.codec.http2.Http2Exception)39