jeffkbkim commented on code in PR #13663:
URL: https://github.com/apache/kafka/pull/13663#discussion_r1190199871


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/generic/GenericGroupTest.java:
##########
@@ -0,0 +1,876 @@
+/*
+ * 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.coordinator.group.generic;
+
+import org.apache.kafka.clients.consumer.ConsumerPartitionAssignor;
+import org.apache.kafka.clients.consumer.internals.ConsumerProtocol;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.CompletingRebalance;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.Dead;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.Empty;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.PreparingRebalance;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.Stable;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class GenericGroupTest {
+    private final String protocolType = "consumer";
+    private final String groupInstanceId = "groupInstanceId";
+    private final String memberId = "memberId";
+    private final String clientId = "clientId";
+    private final String clientHost = "clientHost";
+    private final int rebalanceTimeoutMs = 60000;
+    private final int sessionTimeoutMs = 10000;
+
+    
+    private GenericGroup group = null;
+    
+    @BeforeEach
+    public void initialize() {
+        group = new GenericGroup(new LogContext(), "groupId", Empty, 
Time.SYSTEM);

Review Comment:
   i'll keep this as is since this variable is used for all tests.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/generic/GenericGroupTest.java:
##########
@@ -0,0 +1,876 @@
+/*
+ * 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.coordinator.group.generic;
+
+import org.apache.kafka.clients.consumer.ConsumerPartitionAssignor;
+import org.apache.kafka.clients.consumer.internals.ConsumerProtocol;
+import org.apache.kafka.common.message.JoinGroupResponseData;
+import org.apache.kafka.common.message.SyncGroupResponseData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.CompletingRebalance;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.Dead;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.Empty;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.PreparingRebalance;
+import static 
org.apache.kafka.coordinator.group.generic.GenericGroupState.Stable;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class GenericGroupTest {
+    private final String protocolType = "consumer";
+    private final String groupInstanceId = "groupInstanceId";
+    private final String memberId = "memberId";
+    private final String clientId = "clientId";
+    private final String clientHost = "clientHost";
+    private final int rebalanceTimeoutMs = 60000;
+    private final int sessionTimeoutMs = 10000;
+
+    
+    private GenericGroup group = null;
+    
+    @BeforeEach
+    public void initialize() {
+        group = new GenericGroup(new LogContext(), "groupId", Empty, 
Time.SYSTEM);
+    }
+    
+    @Test
+    public void testCanRebalanceWhenStable() {
+        assertTrue(group.canRebalance());
+    }
+    
+    @Test
+    public void testCanRebalanceWhenCompletingRebalance() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(CompletingRebalance);
+        assertTrue(group.canRebalance()); 
+    }
+    
+    @Test
+    public void testCannotRebalanceWhenPreparingRebalance() {
+        group.transitionTo(PreparingRebalance);
+        assertFalse(group.canRebalance());
+    }
+
+    @Test
+    public void testCannotRebalanceWhenDead() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Empty);
+        group.transitionTo(Dead);
+        assertFalse(group.canRebalance());
+    }
+
+    @Test
+    public void testStableToPreparingRebalanceTransition() {
+        group.transitionTo(PreparingRebalance);
+        assertState(group, PreparingRebalance);
+    }
+
+    @Test
+    public void testStableToDeadTransition() {
+        group.transitionTo(Dead);
+        assertState(group, Dead);
+    }
+
+    @Test
+    public void testAwaitingRebalanceToPreparingRebalanceTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(CompletingRebalance);
+        group.transitionTo(PreparingRebalance);
+        assertState(group, PreparingRebalance);
+    }
+
+    @Test
+    public void testPreparingRebalanceToDeadTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Dead);
+        assertState(group, Dead);
+    }
+
+    @Test
+    public void testPreparingRebalanceToEmptyTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Empty);
+        assertState(group, Empty);
+    }
+
+    @Test
+    public void testEmptyToDeadTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Empty);
+        group.transitionTo(Dead);
+        assertState(group, Dead);
+    }
+
+    @Test
+    public void testAwaitingRebalanceToStableTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(CompletingRebalance);
+        group.transitionTo(Stable);
+        assertState(group, Stable);
+    }
+
+    @Test
+    public void testEmptyToStableIllegalTransition() {
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(Stable));
+    }
+
+    @Test
+    public void testStableToStableIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(CompletingRebalance);
+        group.transitionTo(Stable);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(Stable));
+    }
+
+    @Test
+    public void testEmptyToAwaitingRebalanceIllegalTransition() {
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(CompletingRebalance));
+    }
+
+    @Test
+    public void testPreparingRebalanceToPreparingRebalanceIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(PreparingRebalance));
+    }
+
+    @Test
+    public void testPreparingRebalanceToStableIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(Stable));
+    }
+
+    @Test
+    public void testAwaitingRebalanceToAwaitingRebalanceIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(CompletingRebalance);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(CompletingRebalance));
+    }
+
+    @Test
+    public void testDeadToDeadIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Dead);
+        group.transitionTo(Dead);
+        assertState(group, Dead);
+    }
+
+    @Test
+    public void testDeadToStableIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Dead);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(Stable));
+    }
+
+    @Test
+    public void testDeadToPreparingRebalanceIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Dead);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(PreparingRebalance));
+    }
+
+    @Test
+    public void testDeadToAwaitingRebalanceIllegalTransition() {
+        group.transitionTo(PreparingRebalance);
+        group.transitionTo(Dead);
+        assertThrows(IllegalStateException.class, () -> 
group.transitionTo(CompletingRebalance));
+    }
+
+    @Test
+    public void testSelectProtocol() {
+        List<Protocol> member1Protocols = Arrays.asList(
+            new Protocol("range", new byte[0]),
+            new Protocol("roundrobin", new byte[0])
+        );
+
+        GenericGroupMember member1 = new GenericGroupMember(
+            memberId,
+            Optional.empty(),
+            clientId,
+            clientHost,
+            rebalanceTimeoutMs,
+            sessionTimeoutMs,
+            protocolType,
+            member1Protocols
+        );
+        group.add(member1);
+
+        List<Protocol> member2Protocols = Arrays.asList(
+            new Protocol("roundrobin", new byte[0]),
+            new Protocol("range", new byte[0])
+        );
+
+        GenericGroupMember member2 = new GenericGroupMember(
+            "member2",
+            Optional.empty(),
+            clientId,
+            clientHost,
+            rebalanceTimeoutMs,
+            sessionTimeoutMs,
+            protocolType,
+            member2Protocols
+        );
+        group.add(member2);
+
+        // now could be either range or robin since there is no majority 
preference
+        assertTrue(group.selectProtocol().equals("range") ||
+            group.selectProtocol().equals("roundrobin"));
+
+        GenericGroupMember member3 = new GenericGroupMember(
+            "member3",
+            Optional.empty(),
+            clientId,
+            clientHost,
+            rebalanceTimeoutMs,
+            sessionTimeoutMs,
+            protocolType,
+            member2Protocols
+        );
+        group.add(member3);
+
+        // now we should prefer 'roundrobin'
+        assertEquals("roundrobin", group.selectProtocol());
+    }
+
+    @Test
+    public void testSelectProtocolRaisesIfNoMembers() {
+        assertThrows(IllegalStateException.class, () -> 
group.selectProtocol());
+    }
+
+    @Test
+    public void testSelectProtocolChoosesCompatibleProtocol() {
+        List<Protocol> member1Protocols = Arrays.asList(
+            new Protocol("range", new byte[0]),
+            new Protocol("roundrobin", new byte[0])
+        );
+
+        GenericGroupMember member1 = new GenericGroupMember(
+            memberId,
+            Optional.empty(),
+            clientId,
+            clientHost,
+            rebalanceTimeoutMs,
+            sessionTimeoutMs,
+            protocolType,
+            member1Protocols
+        );
+        group.add(member1);
+
+        List<Protocol> member2Protocols = Arrays.asList(
+            new Protocol("roundrobin", new byte[0]),
+            new Protocol("blah", new byte[0])
+        );
+
+        GenericGroupMember member2 = new GenericGroupMember(
+            "member2",
+            Optional.empty(),
+            clientId,
+            clientHost,
+            rebalanceTimeoutMs,
+            sessionTimeoutMs,
+            protocolType,
+            member2Protocols
+        );
+        group.add(member2);
+
+        assertEquals("roundrobin", group.selectProtocol());
+    }
+
+    @Test
+    public void testSupportsProtocols() {
+        List<Protocol> member1Protocols = Arrays.asList(
+            new Protocol("range", new byte[0]),
+            new Protocol("roundrobin", new byte[0])
+        );
+
+        GenericGroupMember member1 = new GenericGroupMember(
+            memberId,
+            Optional.empty(),
+            clientId,
+            clientHost,
+            rebalanceTimeoutMs,
+            sessionTimeoutMs,
+            protocolType,
+            member1Protocols
+        );
+
+        // by public voidault, the group supports everything

Review Comment:
   thanks for the catch!



-- 
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

Reply via email to