zhaohaidao commented on code in PR #14271:
URL: https://github.com/apache/kafka/pull/14271#discussion_r1324791489


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupCoordinatorServiceTest.java:
##########
@@ -599,6 +608,164 @@ public void testHeartbeatCoordinatorException() throws 
Exception {
         );
     }
 
+    @Test
+    public void testListGroups() throws ExecutionException, 
InterruptedException, TimeoutException {
+        CoordinatorRuntime<GroupCoordinatorShard, Record> runtime = 
mockRuntime();
+        GroupCoordinatorService service = new GroupCoordinatorService(
+            new LogContext(),
+            createConfig(),
+            runtime
+        );
+        int partitionCount = 3;
+        service.startup(() -> partitionCount);
+
+        ListGroupsRequestData request = new ListGroupsRequestData();
+
+        List<ListGroupsResponseData.ListedGroup> expectedResults = 
Arrays.asList(
+            new ListGroupsResponseData.ListedGroup()
+                .setGroupId("group0")
+                .setGroupState("Stable")
+                .setProtocolType("protocol1"),
+            new ListGroupsResponseData.ListedGroup()
+                .setGroupId("group1")
+                .setGroupState("Empty")
+                .setProtocolType(ConsumerProtocol.PROTOCOL_TYPE),
+            new ListGroupsResponseData.ListedGroup()
+                .setGroupId("group2")
+                .setGroupState("Dead")
+                .setProtocolType(ConsumerProtocol.PROTOCOL_TYPE)
+        );
+        Map<String, List<ListGroupsResponseData.ListedGroup>> expectResultMap 
= new HashMap<>();
+        for (ListGroupsResponseData.ListedGroup result : expectedResults) {
+            expectResultMap.put(result.groupId(), 
Collections.singletonList(result));
+        }
+        when(runtime.partitions()).thenReturn(Sets.newSet(
+            new TopicPartition("__consumer_offsets", 0),
+            new TopicPartition("__consumer_offsets", 1),
+            new TopicPartition("__consumer_offsets", 2)));
+        for (int i = 0; i < partitionCount; i++) {
+            when(runtime.scheduleReadOperation(
+                ArgumentMatchers.eq("list-groups"),
+                ArgumentMatchers.eq(new TopicPartition("__consumer_offsets", 
i)),
+                ArgumentMatchers.any()
+            
)).thenReturn(CompletableFuture.completedFuture(Collections.singletonList(expectedResults.get(i))));
+        }
+
+        CompletableFuture<ListGroupsResponseData> responseFuture = 
service.listGroups(
+            requestContext(ApiKeys.LIST_GROUPS),
+            request
+        );
+
+        List<ListGroupsResponseData.ListedGroup> actualResults = 
responseFuture.get(5, TimeUnit.SECONDS).groups();
+        assertEquals(expectedResults, actualResults);
+        assertEquals(expectResultMap.size(), actualResults.size());
+        for (ListGroupsResponseData.ListedGroup result : actualResults) {
+            assertEquals(expectResultMap.get(result.groupId()), 
Collections.singletonList(result));
+        }

Review Comment:
   Map is used here because the order of the list returned by the function may 
be different from the expected one.



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