philipnee commented on code in PR #14364: URL: https://github.com/apache/kafka/pull/14364#discussion_r1330480862
########## clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java: ########## @@ -0,0 +1,269 @@ +/* + * 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.internals; + +import org.apache.kafka.clients.ClientResponse; +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.ConsumerGroupHeartbeatResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.serialization.StringDeserializer; +import org.apache.kafka.common.utils.LogContext; +import org.apache.kafka.common.utils.MockTime; +import org.apache.kafka.common.utils.Time; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; +import java.util.Properties; + +import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.RETRY_BACKOFF_MS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class HeartbeatRequestManagerTest { + + private final int heartbeatInterval = 1000; + private final long retryBackoffMaxMs = 3000; + private final long retryBackoffMs = 100; + private final String groupId = "group-id"; + + private Time mockTime; + private LogContext mockLogContext; + private CoordinatorRequestManager mockCoordinatorRequestManager; + private SubscriptionState mockSubscriptionState; + private HeartbeatRequestManager heartbeatRequestManager; + private MembershipManager mockMembershipManager; + private HeartbeatRequestManager.HeartbeatRequestState heartbeatRequestState; + private ConsumerConfig config; + + private String memberId = "member-id"; + private int memberEpoch = 1; + private ConsumerGroupHeartbeatResponseData.Assignment memberAssignment = mockAssignment(); + private ErrorEventHandler errorEventHandler; + + private ConsumerGroupHeartbeatResponseData.Assignment mockAssignment() { + return new ConsumerGroupHeartbeatResponseData.Assignment() + .setAssignedTopicPartitions(Arrays.asList( + new ConsumerGroupHeartbeatResponseData.TopicPartitions() + .setTopicId(Uuid.randomUuid()) + .setPartitions(Arrays.asList(0, 1, 2)), + new ConsumerGroupHeartbeatResponseData.TopicPartitions() + .setTopicId(Uuid.randomUuid()) + .setPartitions(Arrays.asList(3, 4, 5)) + )); + } + + @BeforeEach + public void setUp() { + mockTime = new MockTime(); + mockLogContext = new LogContext(); + Properties properties = new Properties(); + properties.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); + properties.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + properties.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + properties.put(RETRY_BACKOFF_MS_CONFIG, "100"); + properties.put(HEARTBEAT_INTERVAL_MS_CONFIG, heartbeatInterval); + config = new ConsumerConfig(properties); + mockCoordinatorRequestManager = mock(CoordinatorRequestManager.class); + when(mockCoordinatorRequestManager.coordinator()).thenReturn(Optional.of(new Node(1, "localhost", 9999))); + mockSubscriptionState = mock(SubscriptionState.class); + mockMembershipManager = spy(new MembershipManagerImpl(groupId)); + heartbeatRequestState = mock(HeartbeatRequestManager.HeartbeatRequestState.class); + errorEventHandler = mock(ErrorEventHandler.class); + heartbeatRequestManager = new HeartbeatRequestManager( + mockTime, + mockLogContext, + config, + mockCoordinatorRequestManager, + mockSubscriptionState, + mockMembershipManager, + heartbeatRequestState, + errorEventHandler); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testPoll_sendHeartbeatOnMemberState(boolean notInGroup) { Review Comment: I've seen it in quite a few places so I thought we don't have a conventions 😅 ########## clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java: ########## @@ -0,0 +1,269 @@ +/* + * 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.internals; + +import org.apache.kafka.clients.ClientResponse; +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.message.ConsumerGroupHeartbeatResponseData; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.requests.ConsumerGroupHeartbeatResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.serialization.StringDeserializer; +import org.apache.kafka.common.utils.LogContext; +import org.apache.kafka.common.utils.MockTime; +import org.apache.kafka.common.utils.Time; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; +import java.util.Properties; + +import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.RETRY_BACKOFF_MS_CONFIG; +import static org.apache.kafka.clients.consumer.ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class HeartbeatRequestManagerTest { + + private final int heartbeatInterval = 1000; + private final long retryBackoffMaxMs = 3000; + private final long retryBackoffMs = 100; + private final String groupId = "group-id"; + + private Time mockTime; + private LogContext mockLogContext; + private CoordinatorRequestManager mockCoordinatorRequestManager; + private SubscriptionState mockSubscriptionState; + private HeartbeatRequestManager heartbeatRequestManager; + private MembershipManager mockMembershipManager; + private HeartbeatRequestManager.HeartbeatRequestState heartbeatRequestState; + private ConsumerConfig config; + + private String memberId = "member-id"; + private int memberEpoch = 1; + private ConsumerGroupHeartbeatResponseData.Assignment memberAssignment = mockAssignment(); + private ErrorEventHandler errorEventHandler; + + private ConsumerGroupHeartbeatResponseData.Assignment mockAssignment() { + return new ConsumerGroupHeartbeatResponseData.Assignment() + .setAssignedTopicPartitions(Arrays.asList( + new ConsumerGroupHeartbeatResponseData.TopicPartitions() + .setTopicId(Uuid.randomUuid()) + .setPartitions(Arrays.asList(0, 1, 2)), + new ConsumerGroupHeartbeatResponseData.TopicPartitions() + .setTopicId(Uuid.randomUuid()) + .setPartitions(Arrays.asList(3, 4, 5)) + )); + } + + @BeforeEach + public void setUp() { + mockTime = new MockTime(); + mockLogContext = new LogContext(); + Properties properties = new Properties(); + properties.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); + properties.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + properties.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); + properties.put(RETRY_BACKOFF_MS_CONFIG, "100"); + properties.put(HEARTBEAT_INTERVAL_MS_CONFIG, heartbeatInterval); + config = new ConsumerConfig(properties); + mockCoordinatorRequestManager = mock(CoordinatorRequestManager.class); + when(mockCoordinatorRequestManager.coordinator()).thenReturn(Optional.of(new Node(1, "localhost", 9999))); + mockSubscriptionState = mock(SubscriptionState.class); + mockMembershipManager = spy(new MembershipManagerImpl(groupId)); + heartbeatRequestState = mock(HeartbeatRequestManager.HeartbeatRequestState.class); + errorEventHandler = mock(ErrorEventHandler.class); + heartbeatRequestManager = new HeartbeatRequestManager( + mockTime, + mockLogContext, + config, + mockCoordinatorRequestManager, + mockSubscriptionState, + mockMembershipManager, + heartbeatRequestState, + errorEventHandler); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testPoll_sendHeartbeatOnMemberState(boolean notInGroup) { Review Comment: I've seen it in quite a few places so I thought we don't have a conventions 😅 -- 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