jsancio commented on a change in pull request #9476:
URL: https://github.com/apache/kafka/pull/9476#discussion_r509857105



##########
File path: raft/src/test/java/org/apache/kafka/raft/KafkaRaftClientTest.java
##########
@@ -1729,126 +1695,156 @@ public void testLeaderGracefulShutdownTimeout() 
throws Exception {
     public void testFollowerGracefulShutdown() throws Exception {
         int otherNodeId = 1;
         int epoch = 5;
+        Set<Integer> voters = Utils.mkSet(LOCAL_ID, otherNodeId);
 
-        Set<Integer> voters = Utils.mkSet(localId, otherNodeId);
-        
quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, 
otherNodeId, voters));
-        KafkaRaftClient client = buildClient(voters);
-        assertEquals(ElectionState.withElectedLeader(epoch, otherNodeId, 
voters), quorumStateStore.readElectionState());
+        RaftClientTestContext context = new RaftClientTestContext.Builder()
+            .updateQuorumStateStore(quorumStateStore -> {
+                assertDoesNotThrow(() -> {
+                    
quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, 
otherNodeId, voters));
+                });
+            })
+            .build(voters);
 
-        client.poll();
+        assertEquals(ElectionState.withElectedLeader(epoch, otherNodeId, 
voters), context.quorumStateStore.readElectionState());
+
+        context.client.poll();
 
         int shutdownTimeoutMs = 5000;
-        CompletableFuture<Void> shutdownFuture = 
client.shutdown(shutdownTimeoutMs);
-        assertTrue(client.isRunning());
+        CompletableFuture<Void> shutdownFuture = 
context.client.shutdown(shutdownTimeoutMs);
+        assertTrue(context.client.isRunning());
         assertFalse(shutdownFuture.isDone());
 
-        client.poll();
-        assertFalse(client.isRunning());
+        context.client.poll();
+        assertFalse(context.client.isRunning());
         assertTrue(shutdownFuture.isDone());
         assertNull(shutdownFuture.get());
     }
 
     @Test
     public void testGracefulShutdownSingleMemberQuorum() throws IOException {
-        KafkaRaftClient client = buildClient(Collections.singleton(localId));
+        RaftClientTestContext context = 
RaftClientTestContext.build(Collections.singleton(LOCAL_ID));
+
         assertEquals(ElectionState.withElectedLeader(
-            1, localId, Collections.singleton(localId)), 
quorumStateStore.readElectionState());
-        client.poll();
-        assertEquals(0, channel.drainSendQueue().size());
+            1, LOCAL_ID, Collections.singleton(LOCAL_ID)), 
context.quorumStateStore.readElectionState());
+        context.client.poll();
+        assertEquals(0, context.channel.drainSendQueue().size());
         int shutdownTimeoutMs = 5000;
-        client.shutdown(shutdownTimeoutMs);
-        assertTrue(client.isRunning());
-        client.poll();
-        assertFalse(client.isRunning());
+        context.client.shutdown(shutdownTimeoutMs);
+        assertTrue(context.client.isRunning());
+        context.client.poll();
+        assertFalse(context.client.isRunning());
     }
 
     @Test
     public void testFollowerReplication() throws Exception {
         int otherNodeId = 1;
         int epoch = 5;
-        Set<Integer> voters = Utils.mkSet(localId, otherNodeId);
-        
quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, 
otherNodeId, voters));
-        KafkaRaftClient client = buildClient(voters);
-        assertEquals(ElectionState.withElectedLeader(epoch, otherNodeId, 
voters), quorumStateStore.readElectionState());
+        Set<Integer> voters = Utils.mkSet(LOCAL_ID, otherNodeId);
+
+        RaftClientTestContext context = new RaftClientTestContext.Builder()
+            .updateQuorumStateStore(quorumStateStore -> {
+                assertDoesNotThrow(() -> {
+                    
quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, 
otherNodeId, voters));
+                });
+            })
+            .build(voters);
+
+        assertEquals(ElectionState.withElectedLeader(epoch, otherNodeId, 
voters), context.quorumStateStore.readElectionState());
 
-        pollUntilSend(client);
+        context.pollUntilSend();
 
-        int fetchQuorumCorrelationId = assertSentFetchRequest(epoch, 0L, 0);
+        int fetchQuorumCorrelationId = context.assertSentFetchRequest(epoch, 
0L, 0);
         Records records = MemoryRecords.withRecords(0L, CompressionType.NONE,
             3, new SimpleRecord("a".getBytes()), new 
SimpleRecord("b".getBytes()));
         FetchResponseData response = fetchResponse(epoch, otherNodeId, 
records, 0L, Errors.NONE);
-        deliverResponse(fetchQuorumCorrelationId, otherNodeId, response);
+        context.deliverResponse(fetchQuorumCorrelationId, otherNodeId, 
response);
 
-        client.poll();
-        assertEquals(2L, log.endOffset().offset);
-        assertEquals(2L, log.lastFlushedOffset());
+        context.client.poll();
+        assertEquals(2L, context.log.endOffset().offset);
+        assertEquals(2L, context.log.lastFlushedOffset());
     }
 
     @Test
     public void testEmptyRecordSetInFetchResponse() throws Exception {
         int otherNodeId = 1;
         int epoch = 5;
-        Set<Integer> voters = Utils.mkSet(localId, otherNodeId);
-        
quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, 
otherNodeId, voters));
-        KafkaRaftClient client = buildClient(voters);
+        Set<Integer> voters = Utils.mkSet(LOCAL_ID, otherNodeId);
 
-        assertEquals(ElectionState.withElectedLeader(epoch, otherNodeId, 
voters), quorumStateStore.readElectionState());
+        RaftClientTestContext context = new RaftClientTestContext.Builder()
+            .updateQuorumStateStore(quorumStateStore -> {
+                assertDoesNotThrow(() -> {
+                    
quorumStateStore.writeElectionState(ElectionState.withElectedLeader(epoch, 
otherNodeId, voters));
+                });
+            })
+            .build(voters);
+
+        assertEquals(ElectionState.withElectedLeader(epoch, otherNodeId, 
voters), context.quorumStateStore.readElectionState());

Review comment:
       Thanks for the suggestion. I implemented this.




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to