XComp commented on code in PR #22380:
URL: https://github.com/apache/flink/pull/22380#discussion_r1169775001


##########
flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultLeaderElectionServiceTest.java:
##########
@@ -69,6 +71,118 @@ void testOnGrantAndRevokeLeadership() throws Exception {
         };
     }
 
+    @Test
+    void testDelayedGrantCallAfterContenderRegistration() throws Exception {
+        final TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory 
driverFactory =
+                new 
TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory();
+        try (final DefaultLeaderElectionService testInstance =
+                new DefaultLeaderElectionService(driverFactory)) {
+            testInstance.startLeaderElectionBackend();
+
+            final TestingLeaderElectionDriver driver = 
driverFactory.getCurrentLeaderDriver();
+            assertThat(driver).isNotNull();
+
+            final CompletableFuture<Void> grantLeadershipFuture = new 
CompletableFuture<>();
+            driver.isLeader(grantLeadershipFuture);
+
+            final TestingContender contender = new 
TestingContender("unused-address", testInstance);
+            testInstance.start(contender);
+
+            assertThat(testInstance.getLeaderSessionID())
+                    .as("Leadership grant was not forwarded to the contender, 
yet.")
+                    .isNull();
+
+            grantLeadershipFuture.complete(null);
+
+            contender.waitForLeader();
+
+            testInstance.stop();
+        }
+    }
+
+    /**
+     * Test to cover the issue described in FLINK-31814. This test could be 
removed after
+     * FLINK-31814 is resolved.
+     */
+    @Test
+    void testOnRevokeCallWhileClosingService() throws Exception {
+        final TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory 
driverFactory =
+                new 
TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory(
+                        LeaderElectionEventHandler::onRevokeLeadership);
+
+        try (final DefaultLeaderElectionService testInstance =
+                new DefaultLeaderElectionService(driverFactory)) {
+            testInstance.startLeaderElectionBackend();
+
+            final TestingLeaderElectionDriver driver = 
driverFactory.getCurrentLeaderDriver();
+            assertThat(driver).isNotNull();
+
+            driver.isLeader();
+
+            final TestingContender contender = new 
TestingContender("unused-address", testInstance);
+            testInstance.start(contender);
+
+            contender.waitForLeader();
+
+            testInstance.stop();
+        }
+    }
+
+    /**
+     * This issue can happen when the shutdown of the contender takes too long 
and the leadership is
+     * re-acquired in the meantime (see FLINK-29234).

Review Comment:
   You're right: It doesn't really add much value. I added the test when 
noticing a test failure in 
[JobMasterServiceLeadershipRunnerTest.testJobMasterServiceLeadershipRunnerCloseWhenElectionServiceGrantLeaderShip](https://github.com/apache/flink/pull/21137/files#diff-c010bcd788571261ea6c43a17ad7aaea719a64a7b8a080d76939e7dbbc5a4b32).
 But the test scenario of `testStopWhileHavingLeadership` is actually more 
general and doesn't require additional JavaDoc pointing to FLINK-29234. I'm 
going to remove the JavaDoc.



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