apoorvmittal10 commented on code in PR #18651: URL: https://github.com/apache/kafka/pull/18651#discussion_r1931142635
########## server/src/test/java/org/apache/kafka/server/share/fetch/ShareFetchTestUtils.java: ########## @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.kafka.server.share.fetch; + +import org.apache.kafka.common.TopicIdPartition; + +import java.util.LinkedHashMap; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Helper functions for writing share fetch unit tests. + */ +public class ShareFetchTestUtils { + + /** + * Create an ordered map of TopicIdPartition to partition max bytes. + * + * @param partitionMaxBytes The maximum number of bytes that can be fetched for each partition. + * @param topicIdPartitions The topic partitions to create the map for. + * + * @return The ordered map of TopicIdPartition to partition max bytes. + */ + public static LinkedHashMap<TopicIdPartition, Integer> orderedMap(int partitionMaxBytes, TopicIdPartition... topicIdPartitions) { + LinkedHashMap<TopicIdPartition, Integer> map = new LinkedHashMap<>(); + for (TopicIdPartition tp : topicIdPartitions) { + map.put(tp, partitionMaxBytes); + } + return map; + } + + /** + * Validate that the rotated map is equal to the original map with the keys rotated by the given position. + * + * @param original The original map. + * @param result The rotated map. + * Review Comment: Done. ########## server/src/test/java/org/apache/kafka/server/share/fetch/PartitionRotateStrategyTest.java: ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.kafka.server.share.fetch; + +import org.apache.kafka.common.TopicIdPartition; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.requests.ShareRequestMetadata; +import org.apache.kafka.server.share.fetch.PartitionRotateStrategy.PartitionRotateMetadata; +import org.apache.kafka.server.share.fetch.PartitionRotateStrategy.StrategyType; + +import org.junit.jupiter.api.Test; + +import java.util.LinkedHashMap; + +import static org.apache.kafka.server.share.fetch.ShareFetchTestUtils.validateRotatedMapEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PartitionRotateStrategyTest { + + @Test + public void testRoundRobinStrategy() { + PartitionRotateStrategy strategy = PartitionRotateStrategy.type(StrategyType.ROUND_ROBIN); + LinkedHashMap<TopicIdPartition, Integer> partitions = createPartitions(3); + + LinkedHashMap<TopicIdPartition, Integer> result = strategy.rotate(partitions, new PartitionRotateMetadata(1)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 1); + + // Session epoch is greater than the number of partitions. + result = strategy.rotate(partitions, new PartitionRotateMetadata(5)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 2); + + // Session epoch is at Integer.MAX_VALUE. + result = strategy.rotate(partitions, new PartitionRotateMetadata(Integer.MAX_VALUE)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 1); + + // No rotation at same size as epoch. + result = strategy.rotate(partitions, new PartitionRotateMetadata(3)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 0); + } + + @Test + public void testRoundRobinStrategyWithSpecialSessionEpochs() { + PartitionRotateStrategy strategy = PartitionRotateStrategy.type(StrategyType.ROUND_ROBIN); + + LinkedHashMap<TopicIdPartition, Integer> partitions = createPartitions(3); + LinkedHashMap<TopicIdPartition, Integer> result = strategy.rotate( + partitions, + new PartitionRotateMetadata(ShareRequestMetadata.INITIAL_EPOCH)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 0); + + result = strategy.rotate( + partitions, + new PartitionRotateMetadata(ShareRequestMetadata.FINAL_EPOCH)); + assertEquals(3, result.size()); + validateRotatedMapEquals(partitions, result, 0); + } + + @Test + public void testRoundRobinStrategyWithEmptyPartitions() { + PartitionRotateStrategy strategy = PartitionRotateStrategy.type(StrategyType.ROUND_ROBIN); + // Empty partitions. + LinkedHashMap<TopicIdPartition, Integer> result = strategy.rotate(new LinkedHashMap<>(), new PartitionRotateMetadata(5)); + // The result should be empty. + assertTrue(result.isEmpty()); + } + + private LinkedHashMap<TopicIdPartition, Integer> createPartitions(int size) { Review Comment: Added comments. -- 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. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org