Search in sources :

Example 21 with Exchanger

use of java.util.concurrent.Exchanger in project netty by netty.

the class FlowControlHandlerTest method testFlowToggleAutoRead.

/**
 * The {@link FlowControlHandler} will pass down messages one by one
 * if {@link ChannelConfig#setAutoRead(boolean)} is being toggled.
 */
@Test
public void testFlowToggleAutoRead() throws Exception {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch msgRcvLatch1 = new CountDownLatch(1);
    final CountDownLatch msgRcvLatch2 = new CountDownLatch(1);
    final CountDownLatch msgRcvLatch3 = new CountDownLatch(1);
    final CountDownLatch setAutoReadLatch1 = new CountDownLatch(1);
    final CountDownLatch setAutoReadLatch2 = new CountDownLatch(1);
    ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {

        private int msgRcvCount;

        private int expectedMsgCount;

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

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws InterruptedException {
            ReferenceCountUtil.release(msg);
            // Disable auto reading after each message
            ctx.channel().config().setAutoRead(false);
            if (msgRcvCount++ != expectedMsgCount) {
                return;
            }
            switch(msgRcvCount) {
                case 1:
                    msgRcvLatch1.countDown();
                    if (setAutoReadLatch1.await(1L, SECONDS)) {
                        ++expectedMsgCount;
                    }
                    break;
                case 2:
                    msgRcvLatch2.countDown();
                    if (setAutoReadLatch2.await(1L, SECONDS)) {
                        ++expectedMsgCount;
                    }
                    break;
                default:
                    msgRcvLatch3.countDown();
                    break;
            }
        }
    };
    FlowControlHandler flow = new FlowControlHandler();
    Channel server = newServer(true, flow, handler);
    Channel client = newClient(server.localAddress());
    try {
        // The client connection on the server side
        Channel peer = peerRef.exchange(null, 1L, SECONDS);
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // channelRead(1)
        assertTrue(msgRcvLatch1.await(1L, SECONDS));
        // channelRead(2)
        peer.config().setAutoRead(true);
        setAutoReadLatch1.countDown();
        assertTrue(msgRcvLatch1.await(1L, SECONDS));
        // channelRead(3)
        peer.config().setAutoRead(true);
        setAutoReadLatch2.countDown();
        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) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 22 with Exchanger

use of java.util.concurrent.Exchanger in project netty by netty.

the class FlowControlHandlerTest method testReentranceNotCausesNPE.

@Test
public void testReentranceNotCausesNPE() throws Throwable {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch latch = new CountDownLatch(3);
    final AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
    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) {
            latch.countDown();
            ctx.read();
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            causeRef.set(cause);
        }
    };
    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(latch.await(1L, SECONDS));
        assertTrue(flow.isQueueEmpty());
        Throwable cause = causeRef.get();
        if (cause != null) {
            throw cause;
        }
    } finally {
        client.close();
        server.close();
    }
}
Also used : Exchanger(java.util.concurrent.Exchanger) 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 23 with Exchanger

use of java.util.concurrent.Exchanger in project hbase by apache.

the class TestReadOnlyZKClient method testNotCloseZkWhenPending.

@Test
public void testNotCloseZkWhenPending() throws Exception {
    ZooKeeper mockedZK = mock(ZooKeeper.class);
    Exchanger<AsyncCallback.DataCallback> exchanger = new Exchanger<>();
    doAnswer(i -> {
        exchanger.exchange(i.getArgument(2));
        return null;
    }).when(mockedZK).getData(anyString(), anyBoolean(), any(AsyncCallback.DataCallback.class), any());
    doAnswer(i -> null).when(mockedZK).close();
    when(mockedZK.getState()).thenReturn(ZooKeeper.States.CONNECTED);
    RO_ZK.zookeeper = mockedZK;
    CompletableFuture<byte[]> future = RO_ZK.get(PATH);
    AsyncCallback.DataCallback callback = exchanger.exchange(null);
    // 2 * keep alive time to ensure that we will not close the zk when there are pending requests
    Thread.sleep(6000);
    assertNotNull(RO_ZK.zookeeper);
    verify(mockedZK, never()).close();
    callback.processResult(Code.OK.intValue(), PATH, null, DATA, null);
    assertArrayEquals(DATA, future.get());
    // now we will close the idle connection.
    waitForIdleConnectionClosed();
    verify(mockedZK, times(1)).close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Exchanger(java.util.concurrent.Exchanger) AsyncCallback(org.apache.zookeeper.AsyncCallback) Test(org.junit.Test)

Aggregations

Exchanger (java.util.concurrent.Exchanger)23 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)9 IOException (java.io.IOException)7 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 TimeoutException (java.util.concurrent.TimeoutException)5 Test (org.junit.jupiter.api.Test)5 Channel (io.netty.channel.Channel)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)4 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4 SocketException (java.net.SocketException)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 HashMap (java.util.HashMap)3