Search in sources :

Example 16 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class SimpleChannelPoolTest method testUnhealthyChannelIsNotOffered.

/**
     * Tests that if channel was unhealthy it is not offered back to the pool.
     *
     * @throws Exception
     */
@Test
public void testUnhealthyChannelIsNotOffered() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    ChannelPoolHandler handler = new CountingChannelPoolHandler();
    ChannelPool pool = new SimpleChannelPool(cb, handler);
    Channel channel1 = pool.acquire().syncUninterruptibly().getNow();
    pool.release(channel1).syncUninterruptibly();
    Channel channel2 = pool.acquire().syncUninterruptibly().getNow();
    //first check that when returned healthy then it actually offered back to the pool.
    assertSame(channel1, channel2);
    expectedException.expect(IllegalStateException.class);
    channel1.close().syncUninterruptibly();
    try {
        pool.release(channel1).syncUninterruptibly();
    } finally {
        sc.close().syncUninterruptibly();
        channel2.close().syncUninterruptibly();
        group.shutdownGracefully();
    }
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ExpectedException(org.junit.rules.ExpectedException) EventLoopGroup(io.netty.channel.EventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 17 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class DefaultChannelPipelineTest method testWrongPromiseChannel.

@Test(expected = IllegalArgumentException.class)
public void testWrongPromiseChannel() throws Exception {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel()).sync();
    ChannelPipeline pipeline2 = new LocalChannel().pipeline();
    group.register(pipeline2.channel()).sync();
    try {
        ChannelPromise promise2 = pipeline2.channel().newPromise();
        pipeline.close(promise2);
    } finally {
        pipeline.close();
        pipeline2.close();
    }
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.Test)

Example 18 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class DefaultChannelPipelineTest method testReplaceChannelHandler.

@Test
public void testReplaceChannelHandler() {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    ChannelHandler handler1 = newHandler();
    pipeline.addLast("handler1", handler1);
    pipeline.addLast("handler2", handler1);
    pipeline.addLast("handler3", handler1);
    assertSame(pipeline.get("handler1"), handler1);
    assertSame(pipeline.get("handler2"), handler1);
    assertSame(pipeline.get("handler3"), handler1);
    ChannelHandler newHandler1 = newHandler();
    pipeline.replace("handler1", "handler1", newHandler1);
    assertSame(pipeline.get("handler1"), newHandler1);
    ChannelHandler newHandler3 = newHandler();
    pipeline.replace("handler3", "handler3", newHandler3);
    assertSame(pipeline.get("handler3"), newHandler3);
    ChannelHandler newHandler2 = newHandler();
    pipeline.replace("handler2", "handler2", newHandler2);
    assertSame(pipeline.get("handler2"), newHandler2);
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.Test)

Example 19 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class DefaultChannelPipelineTest method testRemoveChannelHandler.

@Test
public void testRemoveChannelHandler() {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    ChannelHandler handler1 = newHandler();
    ChannelHandler handler2 = newHandler();
    ChannelHandler handler3 = newHandler();
    pipeline.addLast("handler1", handler1);
    pipeline.addLast("handler2", handler2);
    pipeline.addLast("handler3", handler3);
    assertSame(pipeline.get("handler1"), handler1);
    assertSame(pipeline.get("handler2"), handler2);
    assertSame(pipeline.get("handler3"), handler3);
    pipeline.remove(handler1);
    assertNull(pipeline.get("handler1"));
    pipeline.remove(handler2);
    assertNull(pipeline.get("handler2"));
    pipeline.remove(handler3);
    assertNull(pipeline.get("handler3"));
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.Test)

Example 20 with LocalChannel

use of io.netty.channel.local.LocalChannel in project netty by netty.

the class DefaultChannelPipelineTest method testHandlerAddedAndRemovedCalledInCorrectOrder.

@Test(timeout = 3000)
public void testHandlerAddedAndRemovedCalledInCorrectOrder() throws Throwable {
    final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
    final EventExecutorGroup group2 = new DefaultEventExecutorGroup(1);
    try {
        BlockingQueue<CheckOrderHandler> addedQueue = new LinkedBlockingQueue<CheckOrderHandler>();
        BlockingQueue<CheckOrderHandler> removedQueue = new LinkedBlockingQueue<CheckOrderHandler>();
        CheckOrderHandler handler1 = new CheckOrderHandler(addedQueue, removedQueue);
        CheckOrderHandler handler2 = new CheckOrderHandler(addedQueue, removedQueue);
        CheckOrderHandler handler3 = new CheckOrderHandler(addedQueue, removedQueue);
        CheckOrderHandler handler4 = new CheckOrderHandler(addedQueue, removedQueue);
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(handler1);
        group.register(pipeline.channel()).syncUninterruptibly();
        pipeline.addLast(group1, handler2);
        pipeline.addLast(group2, handler3);
        pipeline.addLast(handler4);
        assertTrue(removedQueue.isEmpty());
        pipeline.channel().close().syncUninterruptibly();
        assertHandler(addedQueue.take(), handler1);
        // Depending on timing this can be handler2 or handler3 as these use different EventExecutorGroups.
        assertHandler(addedQueue.take(), handler2, handler3, handler4);
        assertHandler(addedQueue.take(), handler2, handler3, handler4);
        assertHandler(addedQueue.take(), handler2, handler3, handler4);
        assertTrue(addedQueue.isEmpty());
        assertHandler(removedQueue.take(), handler4);
        assertHandler(removedQueue.take(), handler3);
        assertHandler(removedQueue.take(), handler2);
        assertHandler(removedQueue.take(), handler1);
        assertTrue(removedQueue.isEmpty());
    } finally {
        group1.shutdownGracefully();
        group2.shutdownGracefully();
    }
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) LocalChannel(io.netty.channel.local.LocalChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Aggregations

LocalChannel (io.netty.channel.local.LocalChannel)53 Test (org.junit.Test)49 LocalServerChannel (io.netty.channel.local.LocalServerChannel)16 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)14 LocalAddress (io.netty.channel.local.LocalAddress)14 Bootstrap (io.netty.bootstrap.Bootstrap)13 Channel (io.netty.channel.Channel)12 EventLoopGroup (io.netty.channel.EventLoopGroup)12 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)11 LocalEventLoopGroup (io.netty.channel.local.LocalEventLoopGroup)11 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)8 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)7 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeoutException (java.util.concurrent.TimeoutException)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 ExpectedException (org.junit.rules.ExpectedException)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 OioSocketChannel (io.netty.channel.socket.oio.OioSocketChannel)3