Skip to content

Commit

Permalink
GH-1121: Fix KTU.getSingleRecord()
Browse files Browse the repository at this point in the history
Fixes #1121

`getSingleRecord()` didn't work if the consumer was subscribed to
multiple topics that had records.

Change the assert to ensure there is just one message for the
requested topic and seek any records that were retrieved for other
topics.

**cherry-pick to 2.2.x**
  • Loading branch information
garyrussell authored and artembilan committed Jun 12, 2019
1 parent e487ece commit d3f13ef
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.time.Duration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -30,6 +31,7 @@
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import org.apache.kafka.common.serialization.IntegerSerializer;
import org.apache.kafka.common.serialization.StringDeserializer;
Expand Down Expand Up @@ -165,7 +167,19 @@ public static <K, V> ConsumerRecord<K, V> getSingleRecord(Consumer<K, V> consume
*/
public static <K, V> ConsumerRecord<K, V> getSingleRecord(Consumer<K, V> consumer, String topic, long timeout) {
ConsumerRecords<K, V> received = getRecords(consumer, timeout);
assertThat(received.count()).as("Incorrect results returned", received.count()).isEqualTo(1);
Iterator<ConsumerRecord<K, V>> iterator = received.records(topic).iterator();
assertThat(iterator.hasNext()).as("No records found for topic").isTrue();
iterator.next();
assertThat(iterator.hasNext()).as("More than one record for topic found").isFalse();
if (received.count() > 1) {
Map<TopicPartition, Long> reset = new HashMap<>();
received.forEach(rec -> {
if (!rec.topic().equals(topic)) {
reset.computeIfAbsent(new TopicPartition(rec.topic(), rec.partition()), tp -> rec.offset());
}
});
reset.forEach((tp, off) -> consumer.seek(tp, off));
}
return received.records(topic).iterator().next();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.kafka.test.utils;

import java.util.Map;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.jupiter.api.Test;

import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.context.EmbeddedKafka;

/**
* @author Gary Russell
* @since 2.2.7
*
*/
@EmbeddedKafka(topics = { "singleTopic1", "singleTopic2" })
public class KafkaTestUtilsTests {

@Test
void testGetSingleWithMoreThatOneTopic(EmbeddedKafkaBroker broker) {
Map<String, Object> producerProps = KafkaTestUtils.producerProps(broker);
KafkaProducer<Integer, String> producer = new KafkaProducer<>(producerProps);
producer.send(new ProducerRecord<>("singleTopic1", 1, "foo"));
producer.send(new ProducerRecord<>("singleTopic2", 1, "foo"));
producer.close();
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("ktuTests", "false", broker);
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
KafkaConsumer<Integer, String> consumer = new KafkaConsumer<>(consumerProps);
broker.consumeFromAllEmbeddedTopics(consumer);
KafkaTestUtils.getSingleRecord(consumer, "singleTopic1");
KafkaTestUtils.getSingleRecord(consumer, "singleTopic2");
consumer.close();
}

}

0 comments on commit d3f13ef

Please sign in to comment.