### Question:
ChunkedWriteHandler extends ChannelDuplexHandler { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { queue.add(new PendingWrite(msg, promise)); } ChunkedWriteHandler(); @Deprecated  ChunkedWriteHandler(int maxPendingWrites); @Override void handlerAdded(ChannelHandlerContext ctx); void resumeTransfer(); @Override void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise); @Override void flush(ChannelHandlerContext ctx); @Override void channelInactive(ChannelHandlerContext ctx); @Override void channelWritabilityChanged(ChannelHandlerContext ctx);  }### Answer:
@Test public void testFailureWhenLastChunkFailed() throws IOException { ChannelOutboundHandlerAdapter failLast = new ChannelOutboundHandlerAdapter() { private int passedWrites; @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { if (++this.passedWrites < 4) { ctx.write(msg, promise); } else { ReferenceCountUtil.release(msg); promise.tryFailure(new RuntimeException()); } } }; EmbeddedChannel ch = new EmbeddedChannel(failLast, new ChunkedWriteHandler()); ChannelFuture r = ch.writeAndFlush(new ChunkedFile(TMP, 1024 * 16)); assertTrue(ch.finish()); assertFalse(r.isSuccess()); assertTrue(r.cause() instanceof RuntimeException); int read = 0; for (;;) { ByteBuf buffer = ch.readOutbound(); if (buffer == null) { break; } read += buffer.readableBytes(); buffer.release(); } assertEquals(1024 * 16 * 3, read); }
@Test public void testDiscardPendingWritesOnInactive() throws IOException { final AtomicBoolean closeWasCalled = new AtomicBoolean(false); ChunkedInput<ByteBuf> notifiableInput = new ChunkedInput<ByteBuf>() { private boolean done; private final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1); @Override public boolean isEndOfInput() throws Exception { return done; } @Override public void close() throws Exception { buffer.release(); closeWasCalled.set(true); } @Deprecated @Override public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception { return readChunk(ctx.alloc()); } @Override public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception { if (done) { return null; } done = true; return buffer.retainedDuplicate(); } @Override public long length() { return -1; } @Override public long progress() { return 1; } }; EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler()); ChannelFuture r1 = ch.write(new ChunkedFile(TMP)); ChannelFuture r2 = ch.write(new ChunkedNioFile(TMP)); ch.write(notifiableInput); assertFalse(ch.finish()); assertFalse(r1.isSuccess()); assertFalse(r2.isSuccess()); assertTrue(closeWasCalled.get()); }