use of org.apache.kafka.raft.BatchReader in project kafka by apache.
the class RecordsBatchReaderTest method testBatchReader.
private void testBatchReader(long baseOffset, Records records, List<TestBatch<String>> expectedBatches) {
BufferSupplier bufferSupplier = Mockito.mock(BufferSupplier.class);
Set<ByteBuffer> allocatedBuffers = Collections.newSetFromMap(new IdentityHashMap<>());
Mockito.when(bufferSupplier.get(Mockito.anyInt())).thenAnswer(invocation -> {
int size = invocation.getArgument(0);
ByteBuffer buffer = ByteBuffer.allocate(size);
allocatedBuffers.add(buffer);
return buffer;
});
Mockito.doAnswer(invocation -> {
ByteBuffer released = invocation.getArgument(0);
allocatedBuffers.remove(released);
return null;
}).when(bufferSupplier).release(Mockito.any(ByteBuffer.class));
@SuppressWarnings("unchecked") CloseListener<BatchReader<String>> closeListener = Mockito.mock(CloseListener.class);
RecordsBatchReader<String> reader = RecordsBatchReader.of(baseOffset, records, serde, bufferSupplier, MAX_BATCH_BYTES, closeListener);
for (TestBatch<String> batch : expectedBatches) {
assertTrue(reader.hasNext());
assertEquals(batch, TestBatch.from(reader.next()));
}
assertFalse(reader.hasNext());
assertThrows(NoSuchElementException.class, reader::next);
reader.close();
Mockito.verify(closeListener).onClose(reader);
assertEquals(Collections.emptySet(), allocatedBuffers);
}
Aggregations