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());
}
}
Aggregations