Skip to content

Commit

Permalink
Fixing checkstyle issues (3) and new unit test case (BufferedChannelT…
Browse files Browse the repository at this point in the history
…est)
  • Loading branch information
StefanoBelli committed Sep 19, 2024
1 parent bcd5d66 commit 3353ee7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public long forceWrite(boolean forceMetadata) throws IOException {
@Override
public synchronized int read(ByteBuf dest, long pos, int length) throws IOException {
if (dest.writableBytes() < length) {
throw new IllegalArgumentException("dest buffer remaining capacity is not enough"
throw new IllegalArgumentException("dest buffer remaining capacity is not enough"
+ "(must be at least as \"length\"=" + length + ")");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

package org.apache.bookkeeper.bookie;

import static org.junit.Assert.assertThrows;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Random;
Expand Down Expand Up @@ -126,6 +129,41 @@ public void testBufferedChannel(int byteBufLength, int numOfWrites, int unpersis
fileChannel.close();
}

@Test
public void testBufferedChannelReadWhenDestBufSizeExceedsReadLength() throws IOException {
doTestBufferedChannelReadThrowing(100, 60);
}

@Test
public void testBufferedChannelReadWhenDestBufSizeDoesNotExceedReadLength() throws IOException {
doTestBufferedChannelReadThrowing(100, 110);
}

private void doTestBufferedChannelReadThrowing(int destBufSize, int readLength) throws IOException {
File newLogFile = File.createTempFile("test", "log");
newLogFile.deleteOnExit();

try (RandomAccessFile raf = new RandomAccessFile(newLogFile, "rw")) {
FileChannel fileChannel = raf.getChannel();

try (BufferedChannel bufferedChannel = new BufferedChannel(
UnpooledByteBufAllocator.DEFAULT, fileChannel,
INTERNAL_BUFFER_WRITE_CAPACITY, INTERNAL_BUFFER_READ_CAPACITY, 0)) {

bufferedChannel.write(generateEntry(500));

ByteBuf destBuf = UnpooledByteBufAllocator.DEFAULT.buffer(destBufSize);

if (destBufSize < readLength) {
assertThrows(IllegalArgumentException.class,
() -> bufferedChannel.read(destBuf, 0, readLength));
} else {
bufferedChannel.read(destBuf, 0, readLength);
}
}
}
}

private static ByteBuf generateEntry(int length) {
byte[] data = new byte[length];
ByteBuf bb = Unpooled.buffer(length);
Expand Down

0 comments on commit 3353ee7

Please sign in to comment.