dongnuo123 commented on code in PR #14656:
URL: https://github.com/apache/kafka/pull/14656#discussion_r1399461488


##########
core/src/test/scala/unit/kafka/server/HeartbeatRequestTest.scala:
##########
@@ -0,0 +1,204 @@
+/**
+ * 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 kafka.server
+
+import kafka.test.ClusterInstance
+import kafka.test.annotation.{ClusterConfigProperty, ClusterTest, 
ClusterTestDefaults, Type}
+import kafka.test.junit.ClusterTestExtensions
+import kafka.utils.TestUtils
+import org.apache.kafka.clients.consumer.ConsumerPartitionAssignor
+import org.apache.kafka.clients.consumer.internals.ConsumerProtocol
+import org.apache.kafka.common.message.SyncGroupRequestData
+import org.apache.kafka.common.protocol.{ApiKeys, Errors}
+import org.apache.kafka.coordinator.group.generic.GenericGroupState
+import org.junit.jupiter.api.{Tag, Timeout}
+import org.junit.jupiter.api.extension.ExtendWith
+
+import java.util.Collections
+import scala.concurrent.ExecutionContext.Implicits.global
+import scala.concurrent.duration.Duration
+import scala.concurrent.{Await, Future}
+
+@Timeout(120)
+@ExtendWith(value = Array(classOf[ClusterTestExtensions]))
+@ClusterTestDefaults(clusterType = Type.KRAFT, brokers = 1)
+@Tag("integration")
+class HeartbeatRequestTest(cluster: ClusterInstance) extends 
GroupCoordinatorBaseRequestTest(cluster) {
+  @ClusterTest(serverProperties = Array(
+    new ClusterConfigProperty(key = "group.coordinator.new.enable", value = 
"true"),
+    new ClusterConfigProperty(key = "offsets.topic.num.partitions", value = 
"1"),
+    new ClusterConfigProperty(key = "offsets.topic.replication.factor", value 
= "1")
+  ))
+  def testHeartbeatWithOldConsumerGroupProtocolAndNewGroupCoordinator(): Unit 
= {
+    testHeartbeat()
+  }
+
+  @ClusterTest(clusterType = Type.ALL, serverProperties = Array(
+    new ClusterConfigProperty(key = "group.coordinator.new.enable", value = 
"false"),
+    new ClusterConfigProperty(key = "offsets.topic.num.partitions", value = 
"1"),
+    new ClusterConfigProperty(key = "offsets.topic.replication.factor", value 
= "1")
+  ))
+  def testHeartbeatWithOldConsumerGroupProtocolAndOldGroupCoordinator(): Unit 
= {
+    testHeartbeat()
+  }
+
+  private def testHeartbeat(): Unit = {
+    // Creates the __consumer_offsets topics because it won't be created 
automatically
+    // in this test because it does not use FindCoordinator API.
+    createOffsetsTopic()
+
+    // Create the topic.
+    createTopic(
+      topic = "foo",
+      numPartitions = 3
+    )
+
+    for (version <- ApiKeys.HEARTBEAT.oldestVersion() to 
ApiKeys.HEARTBEAT.latestVersion(isUnstableApiEnabled)) {
+      val metadata = ConsumerProtocol.serializeSubscription(
+        new 
ConsumerPartitionAssignor.Subscription(Collections.singletonList("foo"))
+      ).array
+
+      val (leaderMemberId, leaderEpoch) = 
joinDynamicConsumerGroupWithOldProtocol(
+        groupId = "grp",
+        metadata = metadata,
+        completeRebalance = false
+      )
+
+      // Heartbeat with unknown group id and unknown member id.
+      heartbeat(
+        groupId = "grp-unknown",
+        memberId = "member-id-unknown",
+        generationId = -1,
+        expectedError = Errors.UNKNOWN_MEMBER_ID,
+        version = version.toShort
+      )
+
+      // Heartbeat with unknown group id.
+      heartbeat(
+        groupId = "grp-unknown",
+        memberId = leaderMemberId,
+        generationId = -1,
+        expectedError = Errors.UNKNOWN_MEMBER_ID,
+        version = version.toShort
+      )
+
+      // Heartbeat with unknown member id.
+      heartbeat(
+        groupId = "grp",
+        memberId = "member-id-unknown",
+        generationId = -1,
+        expectedError = Errors.UNKNOWN_MEMBER_ID,
+        version = version.toShort
+      )
+
+      // Heartbeat with unmatched generation id.
+      heartbeat(
+        groupId = "grp",
+        memberId = leaderMemberId,
+        generationId = -1,
+        expectedError = Errors.ILLEGAL_GENERATION,
+        version = version.toShort
+      )
+
+      // Heartbeat COMPLETING_REBALANCE group.
+      heartbeat(
+        groupId = "grp",
+        memberId = leaderMemberId,
+        generationId = leaderEpoch,
+        version = version.toShort
+      )
+
+      syncGroupWithOldProtocol(
+        groupId = "grp",
+        memberId = leaderMemberId,
+        generationId = leaderEpoch,
+        assignments = List(new 
SyncGroupRequestData.SyncGroupRequestAssignment()
+          .setMemberId(leaderMemberId)
+          .setAssignment(Array[Byte](1))
+        )
+      )
+
+      // Heartbeat STABLE group.
+      heartbeat(
+        groupId = "grp",
+        memberId = leaderMemberId,
+        generationId = leaderEpoch,
+        version = version.toShort
+      )
+
+      // Join the second member.
+      val joinFollowerResponseData = sendJoinRequest(
+        groupId = "grp",
+        metadata = metadata
+      )
+
+      val reJoinFollowerFuture = Future {
+        sendJoinRequest(
+          groupId = "grp",
+          memberId = joinFollowerResponseData.memberId,
+          metadata = metadata
+        )
+      }
+      val verifyAndRejoinLeaderFuture = Future {
+        TestUtils.waitUntilTrue(() => {
+          val described = describeGroups(groupIds = List("grp"), version = 
ApiKeys.DESCRIBE_GROUPS.latestVersion(isUnstableApiEnabled))
+          GenericGroupState.PREPARING_REBALANCE.toString == 
described.head.groupState
+        }, msg = s"The group is not in PREPARING_REBALANCE state.")

Review Comment:
   Yeah, you're right. This future is not necessary, I can remove it.



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