@Test(timeout = 3000)
public void writingAfterClosedChannelDoesNotNPE() throws InterruptedException {

    EventLoopGroup group = new NioEventLoopGroup(2);
    Channel serverChannel = null;
    Channel clientChannel = null;
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> writeFailCauseRef = new AtomicReference<Throwable>();
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(group);
        sb.channel(NioServerSocketChannel.class);
        sb.childHandler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
            }
        });
        Bootstrap bs = new Bootstrap();
        bs.group(group);
        bs.channel(NioSocketChannel.class);
        bs.handler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new Lz4FrameEncoder());
            }
        });
        serverChannel = sb.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        clientChannel = bs.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        final Channel finalClientChannel = clientChannel;
        clientChannel.eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                finalClientChannel.close();
                final int size = 27;
                ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(size, size);
                finalClientChannel.writeAndFlush(buf.writerIndex(buf.writerIndex() + size))
                        .addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        try {
                            writeFailCauseRef.set(future.cause());
                        } finally {
                            latch.countDown();
                        }
                    }
                });
            }
        });
        latch.await();
        Throwable writeFailCause = writeFailCauseRef.get();
        assertNotNull(writeFailCause);
        Throwable writeFailCauseCause = writeFailCause.getCause();
        if (writeFailCauseCause != null) {
            assertThat(writeFailCauseCause, is(not(instanceOf(NullPointerException.class))));
        }
    } finally {
        if (serverChannel != null) {
            serverChannel.close();
        }
        if (clientChannel != null) {
            clientChannel.close();
        }
        group.shutdownGracefully();
    }
}