chia7712 commented on code in PR #23990:
URL: https://github.com/apache/kafka/pull/23990#discussion_r4433775429
##########
tools/src/test/java/org/apache/kafka/tools/LeaderElectionCommandTest.java:
##########
@@ -388,8 +388,12 @@ private void waitForOnlineBroker(Admin client, int
brokerId) throws InterruptedE
}
private void assertLeader(Admin client, TopicPartition topicPartition, int
expectedLeader) throws Exception {
Review Comment:
How about extracting the helper for both `assertLeader` and `assertNoLeader`?
```java
private static Optional<Integer> currentLeader(Admin client, TopicPartition
topicPartition) throws Exception {
TopicDescription desc =
client.describeTopics(List.of(topicPartition.topic()))
.allTopicNames().get().get(topicPartition.topic());
return desc.partitions().stream()
.filter(p -> p.partition() == topicPartition.partition())
.findFirst()
.map(TopicPartitionInfo::leader)
.filter(leader -> leader != null && leader.id() !=
Node.noNode().id())
.map(Node::id);
}
private void assertLeader(Admin client, TopicPartition topicPartition, int
expectedLeader) throws InterruptedException {
TestUtils.waitForCondition(
() -> currentLeader(client,
topicPartition).equals(Optional.of(expectedLeader)),
"Timed out waiting for leader " + expectedLeader + " of " +
topicPartition
);
}
private void assertNoLeader(Admin client, TopicPartition topicPartition)
throws InterruptedException {
TestUtils.waitForCondition(
() -> currentLeader(client, topicPartition).isEmpty(),
"Timed out waiting for no leader for " + topicPartition
);
}
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]