slfan1989 commented on code in PR #1621:
URL: https://github.com/apache/ratis/pull/1621#discussion_r4216237438


##########
ratis-server/src/test/java/org/apache/ratis/LogAppenderTests.java:
##########
@@ -128,50 +134,84 @@ public void testUnlimitedElementBuffer() throws Exception 
{
   }
 
   @Test
-  public void testFollowerHeartbeatMetric() throws IOException, 
InterruptedException {
-
-    // Start a 3 node Ratis ring.
+  public void testFollowerHeartbeatMetric() throws Exception {
     final MiniRaftCluster cluster = newCluster(3);
-    cluster.start();
-    final RaftServer.Division leaderServer = waitForLeader(cluster);
+    final Map<RaftPeerId, RatisMetricRegistryImpl> registries = new 
HashMap<>();
+    try {
+      cluster.start();
+      final RaftPeerId originalLeader = waitForLeader(cluster).getId();
+      for (RaftServer.Division server : cluster.iterateDivisions()) {
+        registries.put(server.getId(), (RatisMetricRegistryImpl)
+            ((RaftServerMetricsImpl) 
server.getRaftServerMetrics()).getRegistry());
+      }
 
-    // Write 10 messages to leader.
-    try(RaftClient client = cluster.createClient(leaderServer.getId())) {
-      for (int i = 1; i <= 10; i++) {
-        client.io().send(new RaftTestUtil.SimpleMessage("Msg to make leader 
ready " +  i));
+      // Check initial leadership, step-down, and re-election without 
restarting the servers.
+      for (int round = 0; round < 3; round++) {
+        final RaftPeerId leaderId = waitForLeader(cluster).getId();
+        try (RaftClient client = cluster.createClient(leaderId)) {
+          for (int i = 0; i < 10; i++) {
+            RaftTestUtil.assertSuccessReply(
+                client.io().send(new SimpleMessage("heartbeat metrics round " 
+ round + " message " + i)));
+          }
+          JavaUtils.attempt(() -> assertFollowerHeartbeatMetrics(cluster, 
registries),
+              100, HUNDRED_MILLIS, "check heartbeat metrics", LOG);
+          if (round < 2) {
+            final RatisMetricRegistryImpl previousLeaderRegistry = 
registries.get(leaderId);
+            final SortedMap<String, Gauge> commitIndexGauges = 
previousLeaderRegistry.getGauges(
+                (s, m) -> s.endsWith("_peerCommitIndex"));
+            assertEquals(3, commitIndexGauges.size());
+            final RaftPeerId nextLeader = round == 0 ? 
cluster.getFollowers().get(0).getId() : originalLeader;

Review Comment:
   Good point, thanks! I’ll move `if (round < 2)` below the commit-index gauge 
assertion so it also covers the third round.



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

Reply via email to