urbandan commented on a change in pull request #9430: URL: https://github.com/apache/kafka/pull/9430#discussion_r563683437
########## File path: core/src/test/scala/kafka/tools/GetOffsetShellTest.scala ########## @@ -0,0 +1,194 @@ +package kafka.tools + +import java.time.Duration +import java.util.Properties +import java.util.regex.Pattern + +import kafka.integration.KafkaServerTestHarness +import kafka.server.KafkaConfig +import kafka.utils.{Exit, Logging, TestUtils} +import org.apache.kafka.clients.CommonClientConfigs +import org.apache.kafka.clients.consumer.{ConsumerConfig, KafkaConsumer} +import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord} +import org.apache.kafka.common.serialization.{StringDeserializer, StringSerializer} +import org.junit.Assert.{assertEquals, assertTrue} +import org.junit.{Before, Test} + +class GetOffsetShellTest extends KafkaServerTestHarness with Logging { + private val topicCount = 4 + private val offsetTopicPartitionCount = 4 + private val topicPattern = Pattern.compile("test.*") + + override def generateConfigs: collection.Seq[KafkaConfig] = TestUtils.createBrokerConfigs(1, zkConnect) + .map(p => { + p.put(KafkaConfig.OffsetsTopicPartitionsProp, offsetTopicPartitionCount) + p + }).map(KafkaConfig.fromProps) + + @Before + def createTopicAndConsume(): Unit = { + Range(1, topicCount + 1).foreach(i => createTopic(topicName(i), i)) + + val props = new Properties() + props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, brokerList) + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer]) + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer]) + props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, classOf[StringDeserializer]) + props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, classOf[StringDeserializer]) + props.put(ConsumerConfig.GROUP_ID_CONFIG, "GetOffsetShellTest") + + // Send X messages to each partition of topicX + val producer = new KafkaProducer[String, String](props) + Range(1, topicCount + 1).foreach(i => Range(0, i*i) + .foreach(msgCount => producer.send(new ProducerRecord[String, String](topicName(i), msgCount % i, null, "val" + msgCount)))) + producer.close() + + // Consume so consumer offsets topic is created + val consumer = new KafkaConsumer[String, String](props) + consumer.subscribe(topicPattern) + consumer.poll(Duration.ofMillis(1000)) + consumer.commitSync() + consumer.close() + } + + @Test + def testNoFilterOptions(): Unit = { + val offsets = executeAndParse(Array()) + assertTrue(expectedOffsets() sameElements offsets.filter(r => !isConsumerOffsetTopicPartition(r))) + assertEquals(offsetTopicPartitionCount, offsets.count(isConsumerOffsetTopicPartition)) + } + + @Test + def testInternalExcluded(): Unit = { + val offsets = executeAndParse(Array("--exclude-internal-topics")) + assertTrue(expectedOffsets() sameElements offsets.filter(r => !isConsumerOffsetTopicPartition(r))) Review comment: I wasn't sure if I should define any expectations on the end offsets of the internal topic, that's why I was testing it separately. But I think you are right, so I refactored the code to handle all topics uniformly ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org