Search in sources :

Example 31 with ByteBufferOutputStream

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

the class FetcherTest method testInvalidDefaultRecordBatch.

@Test
public void testInvalidDefaultRecordBatch() {
    buildFetcher();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    ByteBufferOutputStream out = new ByteBufferOutputStream(buffer);
    MemoryRecordsBuilder builder = new MemoryRecordsBuilder(out, DefaultRecordBatch.CURRENT_MAGIC_VALUE, CompressionType.NONE, TimestampType.CREATE_TIME, 0L, 10L, 0L, (short) 0, 0, false, false, 0, 1024);
    builder.append(10L, "key".getBytes(), "value".getBytes());
    builder.close();
    buffer.flip();
    // Garble the CRC
    buffer.position(17);
    buffer.put("beef".getBytes());
    buffer.position(0);
    assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    // normal fetch
    assertEquals(1, fetcher.sendFetches());
    client.prepareResponse(fullFetchResponse(tidp0, MemoryRecords.readableRecords(buffer), Errors.NONE, 100L, 0));
    consumerClient.poll(time.timer(0));
    // the fetchedRecords() should always throw exception due to the bad batch.
    for (int i = 0; i < 2; i++) {
        try {
            fetcher.collectFetch();
            fail("fetchedRecords should have raised KafkaException");
        } catch (KafkaException e) {
            assertEquals(0, subscriptions.position(tp0).offset);
        }
    }
}
Also used : ByteBufferOutputStream(org.apache.kafka.common.utils.ByteBufferOutputStream) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) KafkaException(org.apache.kafka.common.KafkaException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufferOutputStream (org.apache.kafka.common.utils.ByteBufferOutputStream)31 ByteBuffer (java.nio.ByteBuffer)26 DataOutputStream (java.io.DataOutputStream)20 Test (org.junit.Test)13 Test (org.junit.jupiter.api.Test)10 KafkaException (org.apache.kafka.common.KafkaException)8 Header (org.apache.kafka.common.header.Header)6 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)6 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)2 KafkaLZ4BlockInputStream (org.apache.kafka.common.compress.KafkaLZ4BlockInputStream)2 KafkaLZ4BlockOutputStream (org.apache.kafka.common.compress.KafkaLZ4BlockOutputStream)2 BatchRetention (org.apache.kafka.common.record.MemoryRecords.RecordFilter.BatchRetention)2 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)2 SnapshotFooterRecord (org.apache.kafka.common.message.SnapshotFooterRecord)1 SnapshotHeaderRecord (org.apache.kafka.common.message.SnapshotHeaderRecord)1 BatchRetentionResult (org.apache.kafka.common.record.MemoryRecords.RecordFilter.BatchRetentionResult)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)1