Search in sources :

Example 1 with BufferSupplier

use of org.apache.kafka.common.utils.BufferSupplier 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);
}
Also used : BufferSupplier(org.apache.kafka.common.utils.BufferSupplier) ByteBuffer(java.nio.ByteBuffer) BatchReader(org.apache.kafka.raft.BatchReader)

Example 2 with BufferSupplier

use of org.apache.kafka.common.utils.BufferSupplier in project kafka by apache.

the class RecordsIteratorTest method mockBufferSupplier.

static BufferSupplier mockBufferSupplier(Set<ByteBuffer> buffers) {
    BufferSupplier bufferSupplier = Mockito.mock(BufferSupplier.class);
    Mockito.when(bufferSupplier.get(Mockito.anyInt())).thenAnswer(invocation -> {
        int size = invocation.getArgument(0);
        ByteBuffer buffer = ByteBuffer.allocate(size);
        buffers.add(buffer);
        return buffer;
    });
    Mockito.doAnswer(invocation -> {
        ByteBuffer released = invocation.getArgument(0);
        buffers.remove(released);
        return null;
    }).when(bufferSupplier).release(Mockito.any(ByteBuffer.class));
    return bufferSupplier;
}
Also used : BufferSupplier(org.apache.kafka.common.utils.BufferSupplier) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 BufferSupplier (org.apache.kafka.common.utils.BufferSupplier)2 BatchReader (org.apache.kafka.raft.BatchReader)1