chia7712 commented on code in PR #19214:
URL: https://github.com/apache/kafka/pull/19214#discussion_r1997641038


##########
clients/src/test/java/org/apache/kafka/common/record/FileRecordsTest.java:
##########
@@ -518,6 +521,52 @@ public void testBytesLengthOfWriteTo() throws IOException {
         verify(channel).transferFrom(any(), anyLong(), eq((long) size - 
firstWritten));
     }
 
+    @Test
+    public void 
testSearchForOffsetWithSizeLastOffsetCallCountFirstBatchMatch() throws 
IOException {
+        File mockFile = mock(File.class);
+        FileChannel mockChannel = mock(FileChannel.class);
+        FileLogInputStream.FileChannelRecordBatch batch = 
mock(FileLogInputStream.FileChannelRecordBatch.class);
+        when(batch.baseOffset()).thenReturn(10L);
+
+        FileRecords fileRecords = Mockito.spy(new FileRecords(mockFile, 
mockChannel, 0, 100, false));
+
+        List<FileLogInputStream.FileChannelRecordBatch> batches = new 
ArrayList<>();
+        batches.add(batch);
+        doReturn((Iterable<FileLogInputStream.FileChannelRecordBatch>) 
batches::iterator)
+                .when(fileRecords)
+                .batchesFrom(anyInt());
+        
+        FileRecords.LogOffsetPosition result = 
fileRecords.searchForOffsetWithSize(5L, 0);
+
+        assertEquals(new FileRecords.LogOffsetPosition(batch), result);
+        verify(batch, never()).lastOffset();
+    }
+
+    @Test
+    public void 
testSearchForOffsetWithSizeLastOffsetCallCountPrevBatchMatches() throws 
IOException {
+        File mockFile = mock(File.class);
+        FileChannel mockChannel = mock(FileChannel.class);
+        FileLogInputStream.FileChannelRecordBatch prevBatch = 
mock(FileLogInputStream.FileChannelRecordBatch.class);
+        when(prevBatch.baseOffset()).thenReturn(5L);
+        when(prevBatch.lastOffset()).thenReturn(12L); // > targetOffset
+        FileLogInputStream.FileChannelRecordBatch currentBatch = 
mock(FileLogInputStream.FileChannelRecordBatch.class);
+        when(currentBatch.baseOffset()).thenReturn(15L); // >= targetOffset
+
+        FileRecords fileRecords = Mockito.spy(new FileRecords(mockFile, 
mockChannel, 0, 100, false));
+        
+        List<FileLogInputStream.FileChannelRecordBatch> batches = new 
ArrayList<>();
+        batches.add(prevBatch);
+        batches.add(currentBatch);
+        doReturn((Iterable<FileLogInputStream.FileChannelRecordBatch>) 
batches::iterator)
+                .when(fileRecords)
+                .batchesFrom(anyInt());
+
+        FileRecords.LogOffsetPosition result = 
fileRecords.searchForOffsetWithSize(10L, 0);
+
+        assertEquals(new FileRecords.LogOffsetPosition(prevBatch), result);
+        verify(prevBatch, times(1)).lastOffset();

Review Comment:
   please add comments for this verification.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to