@Test
public void testNotLeakBuffersWhenCloseByRemotePeer() throws Exception {

    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    cb.group(sharedGroup)
            .channel(LocalChannel.class)
            .handler(new SimpleChannelInboundHandler<ByteBuf>() {
                @Override
                public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                    ctx.writeAndFlush(ctx.alloc().buffer().writeZero(100));
                }
                @Override
                public void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
                    // Just drop the buffer
                }
            });
    sb.group(sharedGroup)
            .channel(LocalServerChannel.class)
            .childHandler(new ChannelInitializer<LocalChannel>() {
                @Override
                public void initChannel(LocalChannel ch) throws Exception {
                    ch.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {
                        @Override
                        public void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
                            while (buffer.isReadable()) {
                                // Fill the ChannelOutboundBuffer with multiple buffers
                                ctx.write(buffer.readRetainedSlice(1));
                            }
                            // Flush and so transfer the written buffers to the inboundBuffer of the remote peer.
                            // After this point the remote peer is responsible to release all the buffers.
                            ctx.flush();
                            // This close call will trigger the remote peer close as well.
                            ctx.close();
                        }
                    });
                }
            });
    Channel sc = null;
    LocalChannel cc = null;
    try {
        // Start server
        sc = sb.bind(TEST_ADDRESS).sync().channel();
        // Connect to the server
        cc = (LocalChannel) cb.connect(sc.localAddress()).sync().channel();
        // Close the channel
        closeChannel(cc);
        assertTrue(cc.inboundBuffer.isEmpty());
        closeChannel(sc);
    } finally {
        closeChannel(cc);
        closeChannel(sc);
    }
}