|
19 | 19 | import io.netty.buffer.ByteBufAllocator; |
20 | 20 | import io.netty.buffer.Unpooled; |
21 | 21 | import io.netty.channel.embedded.EmbeddedChannel; |
| 22 | +import io.netty.util.IllegalReferenceCountException; |
22 | 23 | import io.netty.util.ReferenceCountUtil; |
23 | 24 | import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.function.Executable; |
24 | 26 |
|
25 | 27 | import java.nio.channels.ClosedChannelException; |
26 | 28 |
|
27 | 29 | import static org.junit.jupiter.api.Assertions.assertEquals; |
28 | 30 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
29 | 33 | import static org.junit.jupiter.api.Assertions.assertTrue; |
30 | 34 |
|
31 | 35 | public class AbstractCoalescingBufferQueueTest { |
@@ -88,4 +92,66 @@ public void operationComplete(ChannelFuture future) { |
88 | 92 |
|
89 | 93 | assertFalse(channel.finish()); |
90 | 94 | } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testKeepStateConsistentOnError() { |
| 98 | + final IllegalReferenceCountException exception = new IllegalReferenceCountException(); |
| 99 | + final EmbeddedChannel channel = new EmbeddedChannel(); |
| 100 | + final AbstractCoalescingBufferQueue queue = new AbstractCoalescingBufferQueue(channel, 128) { |
| 101 | + @Override |
| 102 | + protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) { |
| 103 | + // Simulate throwing an IllegalReferenceCountException. |
| 104 | + throw exception; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) { |
| 109 | + // Simulate throwing an IllegalReferenceCountException. |
| 110 | + throw exception; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected ByteBuf removeEmptyValue() { |
| 115 | + return Unpooled.EMPTY_BUFFER; |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + ChannelPromise promise = channel.newPromise(); |
| 120 | + ByteBuf buffer = Unpooled.buffer().writeLong(0); |
| 121 | + queue.add(buffer, promise); |
| 122 | + |
| 123 | + ChannelPromise promise2 = channel.newPromise(); |
| 124 | + ByteBuf buffer2 = Unpooled.buffer().writeLong(0); |
| 125 | + queue.add(buffer2, promise2); |
| 126 | + |
| 127 | + // Size should be 4 as its 2 buffers and 2 listeners. |
| 128 | + assertEquals(4, queue.size()); |
| 129 | + assertEquals(16, queue.readableBytes()); |
| 130 | + |
| 131 | + assertThrows(IllegalStateException.class, new Executable() { |
| 132 | + @Override |
| 133 | + public void execute() throws Throwable { |
| 134 | + queue.remove(channel.alloc(), 128, channel.newPromise()); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + // This should result in have the first buf and first listener removed from the queue. |
| 139 | + assertEquals(2, queue.size()); |
| 140 | + assertEquals(8, queue.readableBytes()); |
| 141 | + assertSame(exception, promise.cause()); |
| 142 | + assertEquals(0, buffer.refCnt()); |
| 143 | + |
| 144 | + assertFalse(promise2.isDone()); |
| 145 | + assertEquals(1, buffer2.refCnt()); |
| 146 | + ByteBuf removed = queue.remove(channel.alloc(), 128, channel.newPromise()); |
| 147 | + assertEquals(8, removed.readableBytes()); |
| 148 | + removed.release(); |
| 149 | + |
| 150 | + assertTrue(queue.isEmpty()); |
| 151 | + assertEquals(0, queue.size()); |
| 152 | + assertEquals(0, queue.readableBytes()); |
| 153 | + ByteBuf removed2 = queue.remove(channel.alloc(), 128, channel.newPromise()); |
| 154 | + assertEquals(0, removed2.readableBytes()); |
| 155 | + removed2.release(); |
| 156 | + } |
91 | 157 | } |