lianetm commented on code in PR #16200:
URL: https://github.com/apache/kafka/pull/16200#discussion_r1668775173


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/HeartbeatRequestManagerTest.java:
##########
@@ -106,72 +102,90 @@ public class HeartbeatRequestManagerTest {
     private MembershipManager membershipManager;
     private HeartbeatRequestManager.HeartbeatRequestState 
heartbeatRequestState;
     private HeartbeatRequestManager.HeartbeatState heartbeatState;
-    private final String memberId = "member-id";
-    private final int memberEpoch = 1;
     private BackgroundEventHandler backgroundEventHandler;
-    private Metrics metrics;
+    private LogContext logContext;
 
     @BeforeEach
     public void setUp() {
-        setUp(ConsumerTestBuilder.createDefaultGroupInformation());
-    }
+        this.time = new MockTime();
+        this.logContext = new LogContext();
+        this.pollTimer = mock(Timer.class);
+        this.coordinatorRequestManager = mock(CoordinatorRequestManager.class);
+        this.heartbeatState = mock(HeartbeatState.class);
+        this.backgroundEventHandler = mock(BackgroundEventHandler.class);
+        this.subscriptions = mock(SubscriptionState.class);
+        this.membershipManager = mock(MembershipManagerImpl.class);
+        this.metadata = mock(ConsumerMetadata.class);
+        Metrics metrics = new Metrics(time);
+        ConsumerConfig config = mock(ConsumerConfig.class);
+
+        this.heartbeatRequestState = spy(new HeartbeatRequestState(
+                logContext,
+                time,
+                DEFAULT_HEARTBEAT_INTERVAL_MS,
+                DEFAULT_RETRY_BACKOFF_MS,
+                DEFAULT_RETRY_BACKOFF_MAX_MS,
+                DEFAULT_HEARTBEAT_JITTER_MS));
 
-    private void setUp(Optional<ConsumerTestBuilder.GroupInformation> 
groupInfo) {
-        testBuilder = new ConsumerTestBuilder(groupInfo, true, false);
-        time = testBuilder.time;
-        coordinatorRequestManager = 
testBuilder.coordinatorRequestManager.orElseThrow(IllegalStateException::new);
-        heartbeatRequestManager = 
testBuilder.heartbeatRequestManager.orElseThrow(IllegalStateException::new);
-        heartbeatRequestState = 
testBuilder.heartbeatRequestState.orElseThrow(IllegalStateException::new);
-        heartbeatState = 
testBuilder.heartbeatState.orElseThrow(IllegalStateException::new);
-        backgroundEventHandler = testBuilder.backgroundEventHandler;
-        subscriptions = testBuilder.subscriptions;
-        membershipManager = 
testBuilder.membershipManager.orElseThrow(IllegalStateException::new);
-        metadata = testBuilder.metadata;
-        metrics = new Metrics(time);
+        this.heartbeatRequestManager = new HeartbeatRequestManager(
+                logContext,
+                pollTimer,
+                config,
+                coordinatorRequestManager,
+                membershipManager,
+                heartbeatState,
+                heartbeatRequestState,
+                backgroundEventHandler,
+                metrics);
 
         
when(coordinatorRequestManager.coordinator()).thenReturn(Optional.of(new 
Node(1, "localhost", 9999)));
+        Map<Uuid, SortedSet<Integer>> map = new HashMap<>();
+        LocalAssignment local = new LocalAssignment(0, map);
+        when(membershipManager.currentAssignment()).thenReturn(local);
     }
 
-    private void resetWithZeroHeartbeatInterval(Optional<String> 
groupInstanceId) {
-        cleanup();
-
-        ConsumerTestBuilder.GroupInformation gi = new 
ConsumerTestBuilder.GroupInformation(
-                DEFAULT_GROUP_ID,
-                groupInstanceId,
+    private void createHeartbeatStateWith0HeartbeatInterval() {
+        this.heartbeatRequestState = spy(new HeartbeatRequestState(
+                logContext,
+                time,
                 0,
-                0.0,
-                Optional.of(DEFAULT_REMOTE_ASSIGNOR)
-        );
-
-        setUp(Optional.of(gi));
-    }
+                DEFAULT_RETRY_BACKOFF_MS,
+                DEFAULT_RETRY_BACKOFF_MAX_MS,
+                DEFAULT_HEARTBEAT_JITTER_MS));
 
-    @AfterEach
-    public void cleanup() {
-        if (testBuilder != null) {
-            testBuilder.close();
-        }
+        heartbeatRequestManager = createHeartbeatRequestManager(
+                coordinatorRequestManager,
+                membershipManager,
+                heartbeatState,
+                heartbeatRequestState,
+                backgroundEventHandler);
     }
 
     @Test
     public void testHeartbeatOnStartup() {
+        
when(coordinatorRequestManager.coordinator()).thenReturn(Optional.empty());

Review Comment:
   Not really hurtful but this is changing the path the test runs. Before this 
PR, the first poll on this test would return null because the member was 
unsubscribed (shouldSkipHeartbeat true). Now it's because there is no 
coordinator. Both go down the same path in the poll implementation but if we 
want to be true to what we had before, here we should mock there is a known 
coordinator, and when(membershipMgr.shouldSkipHB).thenReturn(true).  



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