|
18 | 18 | import io.netty.buffer.ByteBuf; |
19 | 19 | import io.netty.buffer.ByteBufAllocator; |
20 | 20 | import io.netty.buffer.Unpooled; |
| 21 | +import io.netty.channel.Channel; |
21 | 22 | import io.netty.channel.ChannelFuture; |
22 | 23 | import io.netty.channel.ChannelFutureListener; |
23 | 24 | import io.netty.channel.ChannelHandlerContext; |
@@ -826,4 +827,178 @@ boolean isClosed() { |
826 | 827 | return closed; |
827 | 828 | } |
828 | 829 | } |
| 830 | + |
| 831 | + @Test |
| 832 | + public void testResumeTransferReenteredFromReadChunkMustNotBeLostWhenSuspending() { |
| 833 | + final ChunkedWriteHandler streamer = new ChunkedWriteHandler(); |
| 834 | + final EmbeddedChannel ch = new EmbeddedChannel(streamer); |
| 835 | + SuspendResumeChunkedInput input = new SuspendResumeChunkedInput(streamer); |
| 836 | + ChannelFuture future = ch.write(input); |
| 837 | + ch.flush(); |
| 838 | + |
| 839 | + // The input suspends (first readChunk(...) returns null) after its implementation has |
| 840 | + // already produced data and synchronously called resumeTransfer() from within |
| 841 | + // readChunk(...). A plain re-entrancy guard would drop that nested call and leave the |
| 842 | + // transfer suspended forever; the guard must re-run the drain loop instead. |
| 843 | + ByteBuf chunk = ch.readOutbound(); |
| 844 | + assertNull(ch.readOutbound()); |
| 845 | + assertFalse(ch.finishAndReleaseAll()); |
| 846 | + |
| 847 | + assertEquals(42, chunk.readInt()); |
| 848 | + chunk.release(); |
| 849 | + assertEquals(2, input.readChunkCalls()); |
| 850 | + assertTrue(input.isClosed()); |
| 851 | + assertTrue(future.isSuccess()); |
| 852 | + } |
| 853 | + |
| 854 | + private static final class SuspendResumeChunkedInput implements ChunkedInput<ByteBuf> { |
| 855 | + private final ChunkedWriteHandler streamer; |
| 856 | + private final AtomicInteger readChunkCalls = new AtomicInteger(); |
| 857 | + private final AtomicBoolean closed = new AtomicBoolean(); |
| 858 | + |
| 859 | + SuspendResumeChunkedInput(ChunkedWriteHandler streamer) { |
| 860 | + this.streamer = streamer; |
| 861 | + } |
| 862 | + |
| 863 | + int readChunkCalls() { |
| 864 | + return readChunkCalls.get(); |
| 865 | + } |
| 866 | + |
| 867 | + boolean isClosed() { |
| 868 | + return closed.get(); |
| 869 | + } |
| 870 | + |
| 871 | + @Override |
| 872 | + public boolean isEndOfInput() { |
| 873 | + return readChunkCalls.get() >= 2; |
| 874 | + } |
| 875 | + |
| 876 | + @Override |
| 877 | + public void close() { |
| 878 | + closed.set(true); |
| 879 | + } |
| 880 | + |
| 881 | + @Override |
| 882 | + public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception { |
| 883 | + return readChunk(ctx.alloc()); |
| 884 | + } |
| 885 | + |
| 886 | + @Override |
| 887 | + public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception { |
| 888 | + if (readChunkCalls.incrementAndGet() == 1) { |
| 889 | + // Data becomes available "now", so the producer resumes synchronously, but this |
| 890 | + // call still reports "no chunk yet": doFlush(...) is about to suspend when the |
| 891 | + // nested resumeTransfer() arrives, and that call must not be dropped. |
| 892 | + streamer.resumeTransfer(); |
| 893 | + return null; |
| 894 | + } |
| 895 | + ByteBuf buffer = allocator.buffer(4); |
| 896 | + buffer.writeInt(42); |
| 897 | + return buffer; |
| 898 | + } |
| 899 | + |
| 900 | + @Override |
| 901 | + public long length() { |
| 902 | + return -1; |
| 903 | + } |
| 904 | + |
| 905 | + @Override |
| 906 | + public long progress() { |
| 907 | + return 0; |
| 908 | + } |
| 909 | + } |
| 910 | + |
| 911 | + @Test |
| 912 | + public void testFlushReenteredFromReadChunkMustNotConsumeQueueEntryTwice() { |
| 913 | + final EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler()); |
| 914 | + ReentrantFlushChunkedInput input = new ReentrantFlushChunkedInput(ch); |
| 915 | + ChannelFuture future = ch.write(input); |
| 916 | + ch.flush(); |
| 917 | + |
| 918 | + ByteBuf first = ch.readOutbound(); |
| 919 | + ByteBuf second = ch.readOutbound(); |
| 920 | + assertNull(ch.readOutbound()); |
| 921 | + // Before the re-entrancy guard, the nested flush() consumed the queue entry and both |
| 922 | + // invocations read from the same input; the outer doFlush(...) then failed with |
| 923 | + // NoSuchElementException from ArrayDeque.remove() on the already-empty queue. |
| 924 | + // Both chunks were read above, so no message must be left in the buffers. |
| 925 | + assertFalse(ch.finishAndReleaseAll()); |
| 926 | + |
| 927 | + assertEquals(1, first.readInt()); |
| 928 | + first.release(); |
| 929 | + assertEquals(2, second.readInt()); |
| 930 | + second.release(); |
| 931 | + // The input must have been read exactly twice, and closed exactly once, after the last |
| 932 | + // read: before the guard, close() happened from the nested invocation while the outer |
| 933 | + // readChunk(...) call had not even returned yet. |
| 934 | + assertEquals(2, input.readChunkCalls()); |
| 935 | + assertTrue(input.isClosed()); |
| 936 | + assertEquals(2, input.readChunkCallsAtClose()); |
| 937 | + assertTrue(future.isSuccess()); |
| 938 | + } |
| 939 | + |
| 940 | + private static final class ReentrantFlushChunkedInput implements ChunkedInput<ByteBuf> { |
| 941 | + private final Channel channel; |
| 942 | + private final AtomicInteger readChunkCalls = new AtomicInteger(); |
| 943 | + private final AtomicInteger readChunkCallsAtClose = new AtomicInteger(-1); |
| 944 | + private final AtomicBoolean closed = new AtomicBoolean(); |
| 945 | + private int chunkNo; |
| 946 | + |
| 947 | + ReentrantFlushChunkedInput(Channel channel) { |
| 948 | + this.channel = channel; |
| 949 | + } |
| 950 | + |
| 951 | + int readChunkCalls() { |
| 952 | + return readChunkCalls.get(); |
| 953 | + } |
| 954 | + |
| 955 | + int readChunkCallsAtClose() { |
| 956 | + return readChunkCallsAtClose.get(); |
| 957 | + } |
| 958 | + |
| 959 | + boolean isClosed() { |
| 960 | + return closed.get(); |
| 961 | + } |
| 962 | + |
| 963 | + @Override |
| 964 | + public boolean isEndOfInput() { |
| 965 | + return chunkNo >= 2; |
| 966 | + } |
| 967 | + |
| 968 | + @Override |
| 969 | + public void close() { |
| 970 | + closed.set(true); |
| 971 | + readChunkCallsAtClose.set(readChunkCalls.get()); |
| 972 | + } |
| 973 | + |
| 974 | + @Override |
| 975 | + public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception { |
| 976 | + return readChunk(ctx.alloc()); |
| 977 | + } |
| 978 | + |
| 979 | + @Override |
| 980 | + public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception { |
| 981 | + int call = readChunkCalls.incrementAndGet(); |
| 982 | + if (call == 1) { |
| 983 | + // Runs between queue.peek() and queue.remove() in doFlush(...). Re-enter the |
| 984 | + // handler synchronously, like incidental pipeline activity triggered from user |
| 985 | + // code (a flush, a writability change, a close notification) would. |
| 986 | + channel.flush(); |
| 987 | + } |
| 988 | + chunkNo++; |
| 989 | + ByteBuf buffer = allocator.buffer(4); |
| 990 | + buffer.writeInt(chunkNo); |
| 991 | + return buffer; |
| 992 | + } |
| 993 | + |
| 994 | + @Override |
| 995 | + public long length() { |
| 996 | + return -1; |
| 997 | + } |
| 998 | + |
| 999 | + @Override |
| 1000 | + public long progress() { |
| 1001 | + return 0; |
| 1002 | + } |
| 1003 | + } |
829 | 1004 | } |