Search in sources :

Example 91 with LocalAddress

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

the class RenegotiateTest method testRenegotiateServer.

@Test
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testRenegotiateServer() throws Throwable {
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(2);
    SelfSignedCertificate cert = new SelfSignedCertificate();
    EventLoopGroup group = new LocalEventLoopGroup();
    try {
        final SslContext context = SslContextBuilder.forServer(cert.key(), cert.cert()).sslProvider(serverSslProvider()).protocols(SslProtocols.TLS_v1_2).build();
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                SslHandler handler = context.newHandler(ch.alloc());
                handler.setHandshakeTimeoutMillis(0);
                ch.pipeline().addLast(handler);
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    private boolean renegotiate;

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        ReferenceCountUtil.release(msg);
                    }

                    @Override
                    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
                        if (!renegotiate && evt instanceof SslHandshakeCompletionEvent) {
                            SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt;
                            if (event.isSuccess()) {
                                final SslHandler handler = ctx.pipeline().get(SslHandler.class);
                                renegotiate = true;
                                handler.renegotiate().addListener(new FutureListener<Channel>() {

                                    @Override
                                    public void operationComplete(Future<Channel> future) throws Exception {
                                        if (!future.isSuccess()) {
                                            error.compareAndSet(null, future.cause());
                                            ctx.close();
                                        }
                                        latch.countDown();
                                    }
                                });
                            } else {
                                error.compareAndSet(null, event.cause());
                                latch.countDown();
                                ctx.close();
                            }
                        }
                    }
                });
            }
        });
        Channel channel = sb.bind(new LocalAddress("test")).syncUninterruptibly().channel();
        final SslContext clientContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(SslProvider.JDK).protocols(SslProtocols.TLS_v1_2).build();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                SslHandler handler = clientContext.newHandler(ch.alloc());
                handler.setHandshakeTimeoutMillis(0);
                ch.pipeline().addLast(handler);
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                        if (evt instanceof SslHandshakeCompletionEvent) {
                            SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt;
                            if (!event.isSuccess()) {
                                error.compareAndSet(null, event.cause());
                                ctx.close();
                            }
                            latch.countDown();
                        }
                    }
                });
            }
        });
        Channel clientChannel = bootstrap.connect(channel.localAddress()).syncUninterruptibly().channel();
        latch.await();
        clientChannel.close().syncUninterruptibly();
        channel.close().syncUninterruptibly();
        verifyResult(error);
    } finally {
        group.shutdownGracefully();
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) 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.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 92 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.jupiter.api.Test)

Example 93 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?
        assertThat(cce, Matchers.instanceOf(ClosedChannelException.class));
    }
    clientChannel.closeFuture().sync();
    assertLog("WRITE\nCLOSE\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.jupiter.api.Test)

Example 94 with LocalAddress

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

the class ReentrantChannelTest method testWriteFlushPingPong.

@Test
public void testWriteFlushPingPong() throws Exception {
    LocalAddress addr = new LocalAddress("testWriteFlushPingPong");
    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() {

        int writeCount;

        int flushCount;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (writeCount < 5) {
                writeCount++;
                ctx.channel().flush();
            }
            super.write(ctx, msg, promise);
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            if (flushCount < 5) {
                flushCount++;
                ctx.channel().write(createTestBuf(2000));
            }
            super.flush(ctx);
        }
    });
    clientChannel.writeAndFlush(createTestBuf(2000));
    clientChannel.close().sync();
    assertLog("WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "CLOSE\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.jupiter.api.Test)

Example 95 with LocalAddress

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

the class FixedChannelPoolMapDeadlockTest method testDeadlockOnRemove.

@Test
public void testDeadlockOnRemove() throws Exception {
    final EventLoop thread1 = new DefaultEventLoop();
    final Bootstrap bootstrap1 = new Bootstrap().channel(LocalChannel.class).group(thread1).localAddress(new LocalAddress("#1"));
    final EventLoop thread2 = new DefaultEventLoop();
    final Bootstrap bootstrap2 = new Bootstrap().channel(LocalChannel.class).group(thread2).localAddress(new LocalAddress("#2"));
    // pool1 runs on thread2, pool2 runs on thread1
    final FixedChannelPool pool1 = new FixedChannelPool(bootstrap2, NOOP_HANDLER, 1);
    final FixedChannelPool pool2 = new FixedChannelPool(bootstrap1, NOOP_HANDLER, 1);
    final AbstractChannelPoolMap<String, FixedChannelPool> channelPoolMap = new AbstractChannelPoolMap<String, FixedChannelPool>() {

        @Override
        protected FixedChannelPool newPool(String key) {
            if ("#1".equals(key)) {
                return pool1;
            } else if ("#2".equals(key)) {
                return pool2;
            } else {
                throw new AssertionError("Unexpected key=" + key);
            }
        }
    };
    assertSame(pool1, channelPoolMap.get("#1"));
    assertSame(pool2, channelPoolMap.get("#2"));
    // thread1 tries to remove pool1 which is running on thread2
    // thread2 tries to remove pool2 which is running on thread1
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Future<?> future1 = thread1.submit(new Runnable() {

        @Override
        public void run() {
            await(barrier);
            channelPoolMap.remove("#1");
        }
    });
    Future<?> future2 = thread2.submit(new Runnable() {

        @Override
        public void run() {
            await(barrier);
            channelPoolMap.remove("#2");
        }
    });
    // A blocking close on remove will cause a deadlock here and the test will time out
    try {
        future1.get(1, TimeUnit.SECONDS);
        future2.get(1, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        // Fail the test on timeout to distinguish from other errors
        throw new AssertionError(e);
    } finally {
        pool1.close();
        pool2.close();
        channelPoolMap.close();
        shutdown(thread1, thread2);
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DefaultEventLoop(io.netty.channel.DefaultEventLoop) CyclicBarrier(java.util.concurrent.CyclicBarrier) DefaultEventLoop(io.netty.channel.DefaultEventLoop) EventLoop(io.netty.channel.EventLoop) Bootstrap(io.netty.bootstrap.Bootstrap) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

LocalAddress (io.netty.channel.local.LocalAddress)116 Bootstrap (io.netty.bootstrap.Bootstrap)66 LocalChannel (io.netty.channel.local.LocalChannel)66 LocalServerChannel (io.netty.channel.local.LocalServerChannel)66 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)63 Channel (io.netty.channel.Channel)61 Test (org.junit.Test)47 Test (org.junit.jupiter.api.Test)39 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)31 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)28 ConnectException (java.net.ConnectException)27 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)27 Subscription (rx.Subscription)27 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)26 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)26 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)25 ChannelFuture (io.netty.channel.ChannelFuture)24 SSLException (javax.net.ssl.SSLException)22