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