Search in sources :

Example 21 with LocalChannel

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

the class DefaultChannelPipelineTest method setUp.

private void setUp(final ChannelHandler... handlers) throws Exception {
    final AtomicReference<Channel> peerRef = new AtomicReference<Channel>();
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class);
    sb.childHandler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            peerRef.set(ctx.channel());
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ReferenceCountUtil.release(msg);
        }
    });
    ChannelFuture bindFuture = sb.bind(LocalAddress.ANY).sync();
    Bootstrap b = new Bootstrap();
    b.group(group).channel(LocalChannel.class);
    b.handler(new ChannelInitializer<LocalChannel>() {

        @Override
        protected void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(handlers);
        }
    });
    self = b.connect(bindFuture.channel().localAddress()).sync().channel();
    peer = peerRef.get();
    bindFuture.channel().close().sync();
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 22 with LocalChannel

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

the class DefaultChannelPipelineTest method testHandlerAddedThrowsAndRemovedThrowsException.

@Test(timeout = 3000)
public void testHandlerAddedThrowsAndRemovedThrowsException() throws InterruptedException {
    final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final Promise<Void> promise = group1.next().newPromise();
        final Exception exceptionAdded = new RuntimeException();
        final Exception exceptionRemoved = new RuntimeException();
        String handlerName = "foo";
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(group1, new CheckExceptionHandler(exceptionAdded, promise));
        pipeline.addFirst(handlerName, new ChannelHandlerAdapter() {

            @Override
            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                throw exceptionAdded;
            }

            @Override
            public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
                // Execute this later so we are sure the exception is handled first.
                ctx.executor().execute(new Runnable() {

                    @Override
                    public void run() {
                        latch.countDown();
                    }
                });
                throw exceptionRemoved;
            }
        });
        group.register(pipeline.channel()).syncUninterruptibly();
        latch.await();
        assertNull(pipeline.context(handlerName));
        promise.syncUninterruptibly();
    } finally {
        group1.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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with LocalChannel

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

the class DefaultChannelPipelineTest method testCancelClose.

@Test
public void testCancelClose() throws Exception {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel());
    ChannelPromise promise = pipeline.channel().newPromise();
    assertTrue(promise.cancel(false));
    ChannelFuture future = pipeline.close(promise);
    assertTrue(future.isCancelled());
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.Test)

Example 24 with LocalChannel

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

the class DefaultChannelPipelineTest method testCancelBind.

// Tests for https://github.com/netty/netty/issues/2349
@Test
public void testCancelBind() throws Exception {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel());
    ChannelPromise promise = pipeline.channel().newPromise();
    assertTrue(promise.cancel(false));
    ChannelFuture future = pipeline.bind(new LocalAddress("test"), promise);
    assertTrue(future.isCancelled());
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.Test)

Example 25 with LocalChannel

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

the class ChannelInitializerTest method testChannelRegisteredEventPropagation.

private void testChannelRegisteredEventPropagation(ChannelInitializer<LocalChannel> init) {
    Channel clientChannel = null, serverChannel = null;
    try {
        server.childHandler(init);
        serverChannel = server.bind().syncUninterruptibly().channel();
        clientChannel = client.connect(SERVER_ADDRESS).syncUninterruptibly().channel();
        assertEquals(1, testHandler.channelRegisteredCount.get());
    } finally {
        closeChannel(clientChannel);
        closeChannel(serverChannel);
    }
}
Also used : LocalServerChannel(io.netty.channel.local.LocalServerChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LocalChannel(io.netty.channel.local.LocalChannel)

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