|
21 | 21 |
|
22 | 22 | package org.apache.bookkeeper.bookie; |
23 | 23 |
|
| 24 | +import static org.junit.Assert.assertThrows; |
| 25 | + |
24 | 26 | import io.netty.buffer.ByteBuf; |
25 | 27 | import io.netty.buffer.Unpooled; |
26 | 28 | import io.netty.buffer.UnpooledByteBufAllocator; |
27 | 29 | import java.io.File; |
| 30 | +import java.io.IOException; |
28 | 31 | import java.io.RandomAccessFile; |
29 | 32 | import java.nio.channels.FileChannel; |
30 | 33 | import java.util.Random; |
@@ -126,6 +129,41 @@ public void testBufferedChannel(int byteBufLength, int numOfWrites, int unpersis |
126 | 129 | fileChannel.close(); |
127 | 130 | } |
128 | 131 |
|
| 132 | + @Test |
| 133 | + public void testBufferedChannelReadWhenDestBufSizeExceedsReadLength() throws IOException { |
| 134 | + doTestBufferedChannelReadThrowing(100, 60); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testBufferedChannelReadWhenDestBufSizeDoesNotExceedReadLength() throws IOException { |
| 139 | + doTestBufferedChannelReadThrowing(100, 110); |
| 140 | + } |
| 141 | + |
| 142 | + private void doTestBufferedChannelReadThrowing(int destBufSize, int readLength) throws IOException { |
| 143 | + File newLogFile = File.createTempFile("test", "log"); |
| 144 | + newLogFile.deleteOnExit(); |
| 145 | + |
| 146 | + try (RandomAccessFile raf = new RandomAccessFile(newLogFile, "rw")) { |
| 147 | + FileChannel fileChannel = raf.getChannel(); |
| 148 | + |
| 149 | + try (BufferedChannel bufferedChannel = new BufferedChannel( |
| 150 | + UnpooledByteBufAllocator.DEFAULT, fileChannel, |
| 151 | + INTERNAL_BUFFER_WRITE_CAPACITY, INTERNAL_BUFFER_READ_CAPACITY, 0)) { |
| 152 | + |
| 153 | + bufferedChannel.write(generateEntry(500)); |
| 154 | + |
| 155 | + ByteBuf destBuf = UnpooledByteBufAllocator.DEFAULT.buffer(destBufSize); |
| 156 | + |
| 157 | + if (destBufSize < readLength) { |
| 158 | + assertThrows(IllegalArgumentException.class, |
| 159 | + () -> bufferedChannel.read(destBuf, 0, readLength)); |
| 160 | + } else { |
| 161 | + bufferedChannel.read(destBuf, 0, readLength); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
129 | 167 | private static ByteBuf generateEntry(int length) { |
130 | 168 | byte[] data = new byte[length]; |
131 | 169 | ByteBuf bb = Unpooled.buffer(length); |
|
0 commit comments