Search in sources :

Example 21 with LocalAddress

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

the class ReentrantChannelTest method testFlushFailure.

@Test
public void testFlushFailure() throws Exception {
    LocalAddress addr = new LocalAddress("testFlushFailure");
    ServerBootstrap sb = getLocalServerBootstrap();
    sb.bind(addr).sync().channel();
    Bootstrap cb = getLocalClientBootstrap();
    setInterest(Event.WRITE, Event.FLUSH, Event.CLOSE, Event.EXCEPTION);
    Channel clientChannel = cb.connect(addr).sync().channel();
    clientChannel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            throw new Exception("intentional failure");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
        }
    });
    try {
        clientChannel.writeAndFlush(createTestBuf(2000)).sync();
        fail();
    } catch (Throwable cce) {
        // FIXME:  shouldn't this contain the "intentional failure" exception?
        assertEquals(ClosedChannelException.class, cce.getClass());
    }
    clientChannel.closeFuture().sync();
    assertLog("WRITE\nCLOSE\n");
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) LocalAddress(io.netty.channel.local.LocalAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 22 with LocalAddress

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

the class ReentrantChannelTest method testFlushInWritabilityChanged.

/**
     * Similar to {@link #testWritabilityChanged()} with slight variation.
     */
@Test
public void testFlushInWritabilityChanged() throws Exception {
    LocalAddress addr = new LocalAddress("testFlushInWritabilityChanged");
    ServerBootstrap sb = getLocalServerBootstrap();
    sb.bind(addr).sync().channel();
    Bootstrap cb = getLocalClientBootstrap();
    setInterest(Event.WRITE, Event.FLUSH, Event.WRITABILITY);
    Channel clientChannel = cb.connect(addr).sync().channel();
    clientChannel.config().setWriteBufferLowWaterMark(512);
    clientChannel.config().setWriteBufferHighWaterMark(1024);
    clientChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            if (!ctx.channel().isWritable()) {
                ctx.channel().flush();
            }
            ctx.fireChannelWritabilityChanged();
        }
    });
    assertTrue(clientChannel.isWritable());
    clientChannel.write(createTestBuf(2000)).sync();
    clientChannel.close().sync();
    assertLog(// Case 1:
    "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITE\n" + "WRITABILITY: writable=false\n" + "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITABILITY: writable=true\n", // Case 2:
    "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITE\n" + "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITABILITY: writable=true\n" + "WRITABILITY: writable=true\n");
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 23 with LocalAddress

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

the class BootstrapTest method testLateRegisterSuccess.

@Test
public void testLateRegisterSuccess() throws Exception {
    TestEventLoopGroup group = new TestEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group);
        bootstrap.channel(LocalServerChannel.class);
        bootstrap.childHandler(new DummyHandler());
        bootstrap.localAddress(new LocalAddress("1"));
        ChannelFuture future = bootstrap.bind();
        assertFalse(future.isDone());
        group.promise.setSuccess();
        final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>();
        future.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                queue.add(future.channel().eventLoop().inEventLoop(Thread.currentThread()));
                queue.add(future.isSuccess());
            }
        });
        assertTrue(queue.take());
        assertTrue(queue.take());
    } finally {
        group.shutdownGracefully();
        group.terminationFuture().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 24 with LocalAddress

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

the class ServerBootstrapTest method testParentHandler.

private static void testParentHandler(boolean channelInitializer) throws Exception {
    final LocalAddress addr = new LocalAddress(UUID.randomUUID().toString());
    final CountDownLatch readLatch = new CountDownLatch(1);
    final CountDownLatch initLatch = new CountDownLatch(1);
    final ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            initLatch.countDown();
            super.handlerAdded(ctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            readLatch.countDown();
            super.channelRead(ctx, msg);
        }
    };
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    Channel sch = null;
    Channel cch = null;
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.channel(LocalServerChannel.class).group(group).childHandler(new ChannelInboundHandlerAdapter());
        if (channelInitializer) {
            sb.handler(new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline().addLast(handler);
                }
            });
        } else {
            sb.handler(handler);
        }
        Bootstrap cb = new Bootstrap();
        cb.group(group).channel(LocalChannel.class).handler(new ChannelInboundHandlerAdapter());
        sch = sb.bind(addr).syncUninterruptibly().channel();
        cch = cb.connect(addr).syncUninterruptibly().channel();
        initLatch.await();
        readLatch.await();
    } finally {
        if (sch != null) {
            sch.close().syncUninterruptibly();
        }
        if (cch != null) {
            cch.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
    }
}
Also used : 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) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 25 with LocalAddress

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

the class DefaultChannelPipelineTest method testCancelConnect.

@Test
public void testCancelConnect() throws Exception {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel());
    ChannelPromise promise = pipeline.channel().newPromise();
    assertTrue(promise.cancel(false));
    ChannelFuture future = pipeline.connect(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)

Aggregations

LocalAddress (io.netty.channel.local.LocalAddress)34 Bootstrap (io.netty.bootstrap.Bootstrap)26 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)25 LocalChannel (io.netty.channel.local.LocalChannel)24 Test (org.junit.Test)24 LocalServerChannel (io.netty.channel.local.LocalServerChannel)23 Channel (io.netty.channel.Channel)22 EventLoopGroup (io.netty.channel.EventLoopGroup)18 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)16 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)15 LocalEventLoopGroup (io.netty.channel.local.LocalEventLoopGroup)14 ChannelFuture (io.netty.channel.ChannelFuture)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 TimeoutException (java.util.concurrent.TimeoutException)6 ClosedChannelException (java.nio.channels.ClosedChannelException)4 ChannelPipeline (io.netty.channel.ChannelPipeline)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 InetSocketAddress (java.net.InetSocketAddress)3