Search in sources :

Example 1 with ChannelDuplexHandler

use of io.netty.channel.ChannelDuplexHandler 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 2 with ChannelDuplexHandler

use of io.netty.channel.ChannelDuplexHandler 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)

Aggregations

Channel (io.netty.channel.Channel)2 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 Exchanger (java.util.concurrent.Exchanger)1