Search in sources :

Example 56 with MockConsumer

use of org.apache.kafka.clients.consumer.MockConsumer in project incubator-gobblin by apache.

the class Kafka1ConsumerClientTest method testConsume.

@Test
public void testConsume() throws Exception {
    Config testConfig = ConfigFactory.parseMap(ImmutableMap.of(ConfigurationKeys.KAFKA_BROKERS, "test"));
    MockConsumer<String, String> consumer = new MockConsumer<String, String>(OffsetResetStrategy.NONE);
    consumer.assign(Arrays.asList(new TopicPartition("test_topic", 0)));
    HashMap<TopicPartition, Long> beginningOffsets = new HashMap<>();
    beginningOffsets.put(new TopicPartition("test_topic", 0), 0L);
    consumer.updateBeginningOffsets(beginningOffsets);
    ConsumerRecord<String, String> record0 = new ConsumerRecord<>("test_topic", 0, 0L, 10L, TimestampType.CREATE_TIME, 0L, 3, 6, "key", "value0");
    ConsumerRecord<String, String> record1 = new ConsumerRecord<>("test_topic", 0, 1L, 11L, TimestampType.LOG_APPEND_TIME, 1L, 3, 6, "key", "value1");
    ConsumerRecord<String, String> record2 = new ConsumerRecord<>("test_topic", 0, 2L, 12L, TimestampType.LOG_APPEND_TIME, 2L, 3, 6, "key", "value2");
    consumer.addRecord(record0);
    consumer.addRecord(record1);
    consumer.addRecord(record2);
    try (Kafka1ConsumerClient<String, String> kafka1Client = new Kafka1ConsumerClient<>(testConfig, consumer)) {
        // Consume from 0 offset
        Set<KafkaConsumerRecord> consumedRecords = Sets.newHashSet(kafka1Client.consume(new KafkaPartition.Builder().withId(0).withTopicName("test_topic").build(), 0l, 100l));
        Set<Kafka1ConsumerClient.Kafka1ConsumerRecord<String, String>> expected = ImmutableSet.of(new Kafka1ConsumerClient.Kafka1ConsumerRecord<>(record0), new Kafka1ConsumerClient.Kafka1ConsumerRecord<>(record1), new Kafka1ConsumerClient.Kafka1ConsumerRecord<>(record2));
        Assert.assertEquals(consumedRecords, expected);
        Kafka1ConsumerClient.Kafka1ConsumerRecord expected0 = expected.iterator().next();
        Assert.assertEquals(record0.timestamp(), expected0.getTimestamp());
        Assert.assertEquals(record0.timestampType() == TimestampType.LOG_APPEND_TIME, expected0.isTimestampLogAppend());
        Assert.assertEquals(record0.timestampType(), expected0.getTimestampType());
    }
}
Also used : HashMap(java.util.HashMap) Config(com.typesafe.config.Config) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaPartition(org.apache.gobblin.source.extractor.extract.kafka.KafkaPartition) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) Test(org.testng.annotations.Test)

Aggregations

MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)56 Test (org.junit.Test)46 TopicPartition (org.apache.kafka.common.TopicPartition)44 HashMap (java.util.HashMap)27 TaskId (org.apache.kafka.streams.processor.TaskId)27 Set (java.util.Set)24 ArrayList (java.util.ArrayList)20 StreamsConfig (org.apache.kafka.streams.StreamsConfig)20 PartitionInfo (org.apache.kafka.common.PartitionInfo)18 HashSet (java.util.HashSet)17 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)15 Map (java.util.Map)10 Properties (java.util.Properties)10 StreamsException (org.apache.kafka.streams.errors.StreamsException)10 Collections.emptySet (java.util.Collections.emptySet)9 InternalStreamsBuilderTest (org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest)9 KafkaException (org.apache.kafka.common.KafkaException)8 TimeoutException (org.apache.kafka.common.errors.TimeoutException)8 List (java.util.List)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7