chia7712 commented on code in PR #23708:
URL: https://github.com/apache/kafka/pull/23708#discussion_r4246068518


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerAssignorsTest.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.UnsupportedAssignorException;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Integration tests for client-side and server-side consumer assignors.
+ */
+@Timeout(600)
+@ClusterTestDefaults(
+    types = {Type.KRAFT},
+    brokers = 3,
+    serverProperties = {
+        @ClusterConfigProperty(key = "offsets.topic.replication.factor", value 
= "3"),
+        @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = 
"1"),
+        @ClusterConfigProperty(key = "group.min.session.timeout.ms", value = 
"100"),
+        @ClusterConfigProperty(key = "group.max.session.timeout.ms", value = 
"60000"),
+        @ClusterConfigProperty(key = "group.initial.rebalance.delay.ms", value 
= "10")
+    }
+)
+public class PlaintextConsumerAssignorsTest {
+    private final ClusterInstance cluster;
+    private final List<Consumer<byte[], byte[]>> consumers = new ArrayList<>();

Review Comment:
   Could we move those variables into each test case? As class-level fields, 
they are tied to every test even though some tests (e.g. 
testSubscriptionChanges, testRemoteAssignorInvalid) don’t use them at all.



##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerAssignorsTest.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.UnsupportedAssignorException;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Integration tests for client-side and server-side consumer assignors.
+ */
+@Timeout(600)
+@ClusterTestDefaults(
+    types = {Type.KRAFT},
+    brokers = 3,
+    serverProperties = {
+        @ClusterConfigProperty(key = "offsets.topic.replication.factor", value 
= "3"),
+        @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = 
"1"),
+        @ClusterConfigProperty(key = "group.min.session.timeout.ms", value = 
"100"),
+        @ClusterConfigProperty(key = "group.max.session.timeout.ms", value = 
"60000"),
+        @ClusterConfigProperty(key = "group.initial.rebalance.delay.ms", value 
= "10")
+    }
+)
+public class PlaintextConsumerAssignorsTest {
+    private final ClusterInstance cluster;
+    private final List<Consumer<byte[], byte[]>> consumers = new ArrayList<>();
+    private final List<ConsumerAssignmentPoller> pollers = new ArrayList<>();
+
+    public PlaintextConsumerAssignorsTest(ClusterInstance cluster) {
+        this.cluster = cluster;
+    }
+
+    private boolean isPartitionAssignmentValid(List<Set<TopicPartition>> 
assignments,
+                                               Set<TopicPartition> partitions,
+                                               List<Set<TopicPartition>> 
expectedAssignments) {
+        if (assignments.stream().anyMatch(Set::isEmpty)
+            || assignments.stream().mapToInt(Set::size).sum() != 
partitions.size()) {
+            return false;
+        }
+        var assigned = new HashSet<TopicPartition>();
+        assignments.forEach(assigned::addAll);
+        return assigned.equals(partitions)
+            && (expectedAssignments.isEmpty()
+                || (assignments.size() == expectedAssignments.size()
+                    && new HashSet<>(assignments).equals(new 
HashSet<>(expectedAssignments))));
+    }
+
+    @ClusterTest
+    public void testRoundRobinAssignment() throws Exception {
+        testSubscriptionChanges(Map.of(
+            ConsumerConfig.GROUP_PROTOCOL_CONFIG, GroupProtocol.CLASSIC.name,
+            ConsumerConfig.GROUP_ID_CONFIG, "roundrobin-group",
+            ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, 
RoundRobinAssignor.class.getName()
+        ));
+    }
+
+    @ClusterTest
+    public void testRemoteAssignorRange() throws Exception {
+        testSubscriptionChanges(Map.of(
+            ConsumerConfig.GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name,
+            ConsumerConfig.GROUP_ID_CONFIG, "range-group",
+            ConsumerConfig.GROUP_REMOTE_ASSIGNOR_CONFIG, "range",
+            ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, "30000"
+        ));
+    }
+
+    private void testSubscriptionChanges(Map<String, Object> configs) throws 
Exception {
+        var consumer = createConsumer(configs);
+        var expected = createTopicAndSendRecords("topic1", 2);
+        expected.addAll(createTopicAndSendRecords("topic2", 2));
+        assertTrue(consumer.assignment().isEmpty());
+        consumer.subscribe(List.of("topic1", "topic2"));
+        ClientsTestUtils.awaitAssignment(consumer, expected);
+
+        var expanded = new HashSet<>(expected);
+        expanded.addAll(createTopicAndSendRecords("topic3", 2));
+        consumer.subscribe(List.of("topic1", "topic2", "topic3"));
+        ClientsTestUtils.awaitAssignment(consumer, expanded);
+        consumer.subscribe(List.of("topic1", "topic2"));
+        ClientsTestUtils.awaitAssignment(consumer, expected);
+        consumer.unsubscribe();
+        assertTrue(consumer.assignment().isEmpty());
+    }
+
+    @ClusterTest
+    public void testRemoteAssignorInvalid() throws Exception {
+        var consumer = createConsumer(Map.of(
+            ConsumerConfig.GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name,
+            ConsumerConfig.GROUP_ID_CONFIG, "invalid-assignor-group",
+            ConsumerConfig.GROUP_REMOTE_ASSIGNOR_CONFIG, "invalid"
+        ));
+        createTopicAndSendRecords("topic1", 2);
+        assertTrue(consumer.assignment().isEmpty());
+        consumer.subscribe(List.of("topic1"));
+        var failure = new AtomicReference<UnsupportedAssignorException>();
+        // Assert inside the retry loop so the polling exception is not 
swallowed by the wait helper.
+        TestUtils.waitForCondition(() -> {
+            failure.set(assertThrows(UnsupportedAssignorException.class,
+                () -> consumer.poll(Duration.ofMillis(100))));
+            return true;
+        }, "Consumer did not reject the invalid remote assignor");
+        assertTrue(failure.get().getMessage().startsWith(
+            "ServerAssignor invalid is not supported. Supported assignors: "));
+    }
+
+    @ClusterTest
+    public void testMultiConsumerRoundRobinAssignor() throws Exception {
+        var configs = Map.<String, Object>of(

Review Comment:
   Why do you use `<String, Object>` instead of `<String, String>`? It causes a 
log of weird type declaration.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to