Search in sources :

Example 81 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SSLEngineTest method clientInitiatedRenegotiationWithFatalAlertDoesNotInfiniteLoopServer.

@Test(timeout = 30000)
public void clientInitiatedRenegotiationWithFatalAlertDoesNotInfiniteLoopServer() throws CertificateException, SSLException, InterruptedException, ExecutionException {
    final SelfSignedCertificate ssc = new SelfSignedCertificate();
    serverSslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(sslServerProvider()).build();
    sb = new ServerBootstrap().group(new NioEventLoopGroup(1)).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) {
            ch.config().setAllocator(new TestByteBufAllocator(ch.config().getAllocator(), type));
            ChannelPipeline p = ch.pipeline();
            p.addLast(serverSslCtx.newHandler(ch.alloc()));
            p.addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                    if (evt instanceof SslHandshakeCompletionEvent && ((SslHandshakeCompletionEvent) evt).isSuccess()) {
                        // This data will be sent to the client before any of the re-negotiation data can be
                        // sent. The client will read this, detect that it is not the response to
                        // renegotiation which was expected, and respond with a fatal alert.
                        ctx.writeAndFlush(ctx.alloc().buffer(1).writeByte(100));
                    }
                    ctx.fireUserEventTriggered(evt);
                }

                @Override
                public void channelRead(final ChannelHandlerContext ctx, Object msg) {
                    ReferenceCountUtil.release(msg);
                    // The server then attempts to trigger a flush operation once the application data is
                    // received from the client. The flush will encrypt all data and should not result in
                    // deadlock.
                    ctx.channel().eventLoop().schedule(new Runnable() {

                        @Override
                        public void run() {
                            ctx.writeAndFlush(ctx.alloc().buffer(1).writeByte(101));
                        }
                    }, 500, TimeUnit.MILLISECONDS);
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) {
                    serverLatch.countDown();
                }
            });
            serverConnectedChannel = ch;
        }
    });
    serverChannel = sb.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
    clientSslCtx = SslContextBuilder.forClient().sslProvider(// OpenSslEngine doesn't support renegotiation on client side
    SslProvider.JDK).trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    cb = new Bootstrap();
    cb.group(new NioEventLoopGroup(1)).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) {
            ch.config().setAllocator(new TestByteBufAllocator(ch.config().getAllocator(), type));
            ChannelPipeline p = ch.pipeline();
            SslHandler sslHandler = clientSslCtx.newHandler(ch.alloc());
            // The renegotiate is not expected to succeed, so we should stop trying in a timely manner so
            // the unit test can terminate relativley quicly.
            sslHandler.setHandshakeTimeout(1, TimeUnit.SECONDS);
            p.addLast(sslHandler);
            p.addLast(new ChannelInboundHandlerAdapter() {

                private int handshakeCount;

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                    // completed the first renegotiation handshake (which is the second handshake).
                    if (evt instanceof SslHandshakeCompletionEvent && ++handshakeCount == 2) {
                        ctx.close();
                        return;
                    }
                    ctx.fireUserEventTriggered(evt);
                }

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                    ReferenceCountUtil.release(msg);
                    // Simulate a request that the server's application logic will think is invalid.
                    ctx.writeAndFlush(ctx.alloc().buffer(1).writeByte(102));
                    ctx.pipeline().get(SslHandler.class).renegotiate();
                }
            });
        }
    });
    ChannelFuture ccf = cb.connect(serverChannel.localAddress());
    assertTrue(ccf.syncUninterruptibly().isSuccess());
    clientChannel = ccf.channel();
    serverLatch.await();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 82 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SslErrorTest method testCorrectAlert.

@Test(timeout = 30000)
public void testCorrectAlert() throws Exception {
    // As this only works correctly at the moment when OpenSslEngine is used on the server-side there is
    // no need to run it if there is no openssl is available at all.
    Assume.assumeTrue(OpenSsl.isAvailable());
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(serverProvider).trustManager(new SimpleTrustManagerFactory() {

        @Override
        protected void engineInit(KeyStore keyStore) {
        }

        @Override
        protected void engineInit(ManagerFactoryParameters managerFactoryParameters) {
        }

        @Override
        protected TrustManager[] engineGetTrustManagers() {
            return new TrustManager[] { new X509TrustManager() {

                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    throw exception;
                }

                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                // NOOP
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return EmptyArrays.EMPTY_X509_CERTIFICATES;
                }
            } };
        }
    }).clientAuth(ClientAuth.REQUIRE).build();
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).keyManager(new File(getClass().getResource("test.crt").getFile()), new File(getClass().getResource("test_unencrypted.pem").getFile())).sslProvider(clientProvider).build();
    Channel serverChannel = null;
    Channel clientChannel = null;
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        serverChannel = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        }).bind(0).sync().channel();
        final Promise<Void> promise = group.next().newPromise();
        clientChannel = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslClientCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        // Unwrap as its wrapped by a DecoderException
                        Throwable unwrappedCause = cause.getCause();
                        if (unwrappedCause instanceof SSLException) {
                            if (exception instanceof TestCertificateException) {
                                CertPathValidatorException.Reason reason = ((CertPathValidatorException) exception.getCause()).getReason();
                                if (reason == CertPathValidatorException.BasicReason.EXPIRED) {
                                    verifyException(unwrappedCause, "expired", promise);
                                } else if (reason == CertPathValidatorException.BasicReason.NOT_YET_VALID) {
                                    verifyException(unwrappedCause, "bad", promise);
                                } else if (reason == CertPathValidatorException.BasicReason.REVOKED) {
                                    verifyException(unwrappedCause, "revoked", promise);
                                }
                            } else if (exception instanceof CertificateExpiredException) {
                                verifyException(unwrappedCause, "expired", promise);
                            } else if (exception instanceof CertificateNotYetValidException) {
                                verifyException(unwrappedCause, "bad", promise);
                            } else if (exception instanceof CertificateRevokedException) {
                                verifyException(unwrappedCause, "revoked", promise);
                            }
                        }
                    }
                });
            }
        }).connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        // Block until we received the correct exception
        promise.syncUninterruptibly();
    } finally {
        if (clientChannel != null) {
            clientChannel.close().syncUninterruptibly();
        }
        if (serverChannel != null) {
            serverChannel.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) CertificateExpiredException(java.security.cert.CertificateExpiredException) CertificateException(java.security.cert.CertificateException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLException(javax.net.ssl.SSLException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CertificateRevokedException(java.security.cert.CertificateRevokedException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SimpleTrustManagerFactory(io.netty.handler.ssl.util.SimpleTrustManagerFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateExpiredException(java.security.cert.CertificateExpiredException) CertPathValidatorException(java.security.cert.CertPathValidatorException) CertificateRevokedException(java.security.cert.CertificateRevokedException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) CertPathValidatorException(java.security.cert.CertPathValidatorException) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) X509TrustManager(javax.net.ssl.X509TrustManager) File(java.io.File) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 83 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SslHandlerTest method compositeBufSizeEstimationGuaranteesSynchronousWrite.

private void compositeBufSizeEstimationGuaranteesSynchronousWrite(SslProvider serverProvider, SslProvider clientProvider) throws CertificateException, SSLException, ExecutionException, InterruptedException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(serverProvider).build();
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(clientProvider).build();
    EventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    Channel cc = null;
    try {
        final Promise<Void> donePromise = group.next().newPromise();
        final int expectedBytes = 469 + 1024 + 1024;
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt instanceof SslHandshakeCompletionEvent) {
                            SslHandshakeCompletionEvent sslEvt = (SslHandshakeCompletionEvent) evt;
                            if (sslEvt.isSuccess()) {
                                final ByteBuf input = ctx.alloc().buffer();
                                input.writeBytes(new byte[expectedBytes]);
                                CompositeByteBuf content = ctx.alloc().compositeBuffer();
                                content.addComponent(true, input.readRetainedSlice(469));
                                content.addComponent(true, input.readRetainedSlice(1024));
                                content.addComponent(true, input.readRetainedSlice(1024));
                                ctx.writeAndFlush(content).addListener(new ChannelFutureListener() {

                                    @Override
                                    public void operationComplete(ChannelFuture future) {
                                        input.release();
                                    }
                                });
                            } else {
                                donePromise.tryFailure(sslEvt.cause());
                            }
                        }
                        ctx.fireUserEventTriggered(evt);
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        donePromise.tryFailure(cause);
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        donePromise.tryFailure(new IllegalStateException("server closed"));
                    }
                });
            }
        }).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        cc = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslClientCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    private int bytesSeen;

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        if (msg instanceof ByteBuf) {
                            bytesSeen += ((ByteBuf) msg).readableBytes();
                            if (bytesSeen == expectedBytes) {
                                donePromise.trySuccess(null);
                            }
                        }
                        ReferenceCountUtil.release(msg);
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        donePromise.tryFailure(cause);
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        donePromise.tryFailure(new IllegalStateException("client closed"));
                    }
                });
            }
        }).connect(sc.localAddress()).syncUninterruptibly().channel();
        donePromise.get();
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
        ssc.delete();
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) CodecException(io.netty.handler.codec.CodecException) SSLProtocolException(javax.net.ssl.SSLProtocolException) DecoderException(io.netty.handler.codec.DecoderException) SSLException(javax.net.ssl.SSLException) ClosedChannelException(java.nio.channels.ClosedChannelException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 84 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SslHandlerTest method testEventsFired.

@Test(timeout = 5000)
public void testEventsFired() throws Exception {
    SSLEngine engine = SSLContext.getDefault().createSSLEngine();
    final BlockingQueue<SslCompletionEvent> events = new LinkedBlockingQueue<SslCompletionEvent>();
    EmbeddedChannel channel = new EmbeddedChannel(new SslHandler(engine), new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof SslCompletionEvent) {
                events.add((SslCompletionEvent) evt);
            }
        }
    });
    assertTrue(events.isEmpty());
    assertTrue(channel.finishAndReleaseAll());
    SslCompletionEvent evt = events.take();
    assertTrue(evt instanceof SslHandshakeCompletionEvent);
    assertTrue(evt.cause() instanceof ClosedChannelException);
    evt = events.take();
    assertTrue(evt instanceof SslCloseCompletionEvent);
    assertTrue(evt.cause() instanceof ClosedChannelException);
    assertTrue(events.isEmpty());
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SSLEngine(javax.net.ssl.SSLEngine) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) CodecException(io.netty.handler.codec.CodecException) SSLProtocolException(javax.net.ssl.SSLProtocolException) DecoderException(io.netty.handler.codec.DecoderException) SSLException(javax.net.ssl.SSLException) ClosedChannelException(java.nio.channels.ClosedChannelException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 85 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class IdleStateHandlerTest method observeOutputIdle.

private void observeOutputIdle(boolean writer) throws Exception {
    long writerIdleTime = 0L;
    long allIdleTime = 0L;
    IdleStateEvent expeced = null;
    if (writer) {
        writerIdleTime = 5L;
        expeced = IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT;
    } else {
        allIdleTime = 5L;
        expeced = IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT;
    }
    TestableIdleStateHandler idleStateHandler = new TestableIdleStateHandler(true, 0L, writerIdleTime, allIdleTime, TimeUnit.SECONDS);
    final List<Object> events = new ArrayList<Object>();
    ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            events.add(evt);
        }
    };
    ObservableChannel channel = new ObservableChannel(idleStateHandler, handler);
    try {
        // We're writing 3 messages that will be consumed at different rates!
        channel.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 1 }));
        channel.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 2 }));
        channel.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 3 }));
        // Establish a baseline. We're not consuming anything and let it idle once.
        idleStateHandler.tickRun();
        assertEquals(1, events.size());
        assertSame(expeced, events.get(0));
        events.clear();
        // Our ticker should be at second 5
        assertEquals(5L, idleStateHandler.tick(TimeUnit.SECONDS));
        // Consume one message in 4 seconds, then be idle for 2 seconds,
        // then run the task and we shouldn't get an IdleStateEvent because
        // we haven't been idle for long enough!
        idleStateHandler.tick(4L, TimeUnit.SECONDS);
        assertNotNullAndRelease(channel.consume());
        idleStateHandler.tickRun(2L, TimeUnit.SECONDS);
        assertEquals(0, events.size());
        // 5s + 4s + 2s
        assertEquals(11L, idleStateHandler.tick(TimeUnit.SECONDS));
        // Consume one message in 3 seconds, then be idle for 4 seconds,
        // then run the task and we shouldn't get an IdleStateEvent because
        // we haven't been idle for long enough!
        idleStateHandler.tick(3L, TimeUnit.SECONDS);
        assertNotNullAndRelease(channel.consume());
        idleStateHandler.tickRun(4L, TimeUnit.SECONDS);
        assertEquals(0, events.size());
        // 11s + 3s + 4s
        assertEquals(18L, idleStateHandler.tick(TimeUnit.SECONDS));
        // Don't consume a message and be idle for 5 seconds.
        // We should get an IdleStateEvent!
        idleStateHandler.tickRun(5L, TimeUnit.SECONDS);
        assertEquals(1, events.size());
        // 18s + 5s
        assertEquals(23L, idleStateHandler.tick(TimeUnit.SECONDS));
        events.clear();
        // Consume one message in 2 seconds, then be idle for 1 seconds,
        // then run the task and we shouldn't get an IdleStateEvent because
        // we haven't been idle for long enough!
        idleStateHandler.tick(2L, TimeUnit.SECONDS);
        assertNotNullAndRelease(channel.consume());
        idleStateHandler.tickRun(1L, TimeUnit.SECONDS);
        assertEquals(0, events.size());
        // 23s + 2s + 1s
        assertEquals(26L, idleStateHandler.tick(TimeUnit.SECONDS));
        // There are no messages left! Advance the ticker by 3 seconds,
        // attempt a consume() but it will be null, then advance the
        // ticker by an another 2 seconds and we should get an IdleStateEvent
        // because we've been idle for 5 seconds.
        idleStateHandler.tick(3L, TimeUnit.SECONDS);
        assertNull(channel.consume());
        idleStateHandler.tickRun(2L, TimeUnit.SECONDS);
        assertEquals(1, events.size());
        // 26s + 3s + 2s
        assertEquals(31L, idleStateHandler.tick(TimeUnit.SECONDS));
    // q.e.d.
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)103 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)75 Channel (io.netty.channel.Channel)65 Test (org.junit.Test)62 Bootstrap (io.netty.bootstrap.Bootstrap)51 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)48 ChannelFuture (io.netty.channel.ChannelFuture)33 CountDownLatch (java.util.concurrent.CountDownLatch)32 EventLoopGroup (io.netty.channel.EventLoopGroup)28 InetSocketAddress (java.net.InetSocketAddress)26 ClosedChannelException (java.nio.channels.ClosedChannelException)26 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)23 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)23 ByteBuf (io.netty.buffer.ByteBuf)22 ChannelFutureListener (io.netty.channel.ChannelFutureListener)17 LocalServerChannel (io.netty.channel.local.LocalServerChannel)17 SocketChannel (io.netty.channel.socket.SocketChannel)17 LocalAddress (io.netty.channel.local.LocalAddress)16 LocalChannel (io.netty.channel.local.LocalChannel)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)15