use of java.nio.channels.FileChannel in project kafka by apache.
the class FileRecordsTest method testTruncateNotCalledIfSizeIsSameAsTargetSize.
/**
* Test that truncateTo only calls truncate on the FileChannel if the size of the
* FileChannel is bigger than the target size. This is important because some JVMs
* change the mtime of the file, even if truncate should do nothing.
*/
@Test
public void testTruncateNotCalledIfSizeIsSameAsTargetSize() throws IOException {
FileChannel channelMock = EasyMock.createMock(FileChannel.class);
EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
EasyMock.expect(channelMock.position(42L)).andReturn(null);
EasyMock.replay(channelMock);
FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
fileRecords.truncateTo(42);
EasyMock.verify(channelMock);
}
use of java.nio.channels.FileChannel in project kafka by apache.
the class FileRecordsTest method testTruncateIfSizeIsDifferentToTargetSize.
/**
* see #testTruncateNotCalledIfSizeIsSameAsTargetSize
*/
@Test
public void testTruncateIfSizeIsDifferentToTargetSize() throws IOException {
FileChannel channelMock = EasyMock.createMock(FileChannel.class);
EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
EasyMock.expect(channelMock.position(42L)).andReturn(null).once();
EasyMock.expect(channelMock.truncate(23L)).andReturn(null).once();
EasyMock.expect(channelMock.position(23L)).andReturn(null).once();
EasyMock.replay(channelMock);
FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
fileRecords.truncateTo(23);
EasyMock.verify(channelMock);
}
use of java.nio.channels.FileChannel in project kafka by apache.
the class FileRecordsTest method testTruncateNotCalledIfSizeIsBiggerThanTargetSize.
/**
* Expect a KafkaException if targetSize is bigger than the size of
* the FileRecords.
*/
@Test
public void testTruncateNotCalledIfSizeIsBiggerThanTargetSize() throws IOException {
FileChannel channelMock = EasyMock.createMock(FileChannel.class);
EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
EasyMock.expect(channelMock.position(42L)).andReturn(null);
EasyMock.replay(channelMock);
FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
try {
fileRecords.truncateTo(43);
fail("Should throw KafkaException");
} catch (KafkaException e) {
// expected
}
EasyMock.verify(channelMock);
}
use of java.nio.channels.FileChannel in project kafka by apache.
the class UtilsTest method testReadFullyWithPartialFileChannelReads.
/**
* Tests that `readFullyOrFail` behaves correctly if multiple `FileChannel.read` operations are required to fill
* the destination buffer.
*/
@Test
public void testReadFullyWithPartialFileChannelReads() throws IOException {
FileChannel channelMock = EasyMock.createMock(FileChannel.class);
final int bufferSize = 100;
StringBuilder expectedBufferContent = new StringBuilder();
fileChannelMockExpectReadWithRandomBytes(channelMock, expectedBufferContent, bufferSize);
EasyMock.replay(channelMock);
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
Utils.readFully(channelMock, buffer, 0L);
assertEquals("The buffer should be populated correctly.", expectedBufferContent.toString(), new String(buffer.array()));
assertFalse("The buffer should be filled", buffer.hasRemaining());
EasyMock.verify(channelMock);
}
use of java.nio.channels.FileChannel in project kafka by apache.
the class UtilsTest method testReadFullyOrFailWithRealFile.
@Test
public void testReadFullyOrFailWithRealFile() throws IOException {
try (FileChannel channel = FileChannel.open(TestUtils.tempFile().toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
// prepare channel
String msg = "hello, world";
channel.write(ByteBuffer.wrap(msg.getBytes()), 0);
channel.force(true);
assertEquals("Message should be written to the file channel", channel.size(), msg.length());
ByteBuffer perfectBuffer = ByteBuffer.allocate(msg.length());
ByteBuffer smallBuffer = ByteBuffer.allocate(5);
ByteBuffer largeBuffer = ByteBuffer.allocate(msg.length() + 1);
// Scenario 1: test reading into a perfectly-sized buffer
Utils.readFullyOrFail(channel, perfectBuffer, 0, "perfect");
assertFalse("Buffer should be filled up", perfectBuffer.hasRemaining());
assertEquals("Buffer should be populated correctly", msg, new String(perfectBuffer.array()));
// Scenario 2: test reading into a smaller buffer
Utils.readFullyOrFail(channel, smallBuffer, 0, "small");
assertFalse("Buffer should be filled", smallBuffer.hasRemaining());
assertEquals("Buffer should be populated correctly", "hello", new String(smallBuffer.array()));
// Scenario 3: test reading starting from a non-zero position
smallBuffer.clear();
Utils.readFullyOrFail(channel, smallBuffer, 7, "small");
assertFalse("Buffer should be filled", smallBuffer.hasRemaining());
assertEquals("Buffer should be populated correctly", "world", new String(smallBuffer.array()));
// Scenario 4: test end of stream is reached before buffer is filled up
try {
Utils.readFullyOrFail(channel, largeBuffer, 0, "large");
fail("Expected EOFException to be raised");
} catch (EOFException e) {
// expected
}
}
}
Aggregations