XComp commented on code in PR #22422:
URL: https://github.com/apache/flink/pull/22422#discussion_r1173929303
##########
flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultLeaderElectionServiceTest.java:
##########
@@ -130,22 +137,101 @@ void testLeaderInformationChangedAndShouldBeCorrected()
throws Exception {
}
@Test
- void testHasLeadership() throws Exception {
+ void testHasLeadershipWithLeadershipButNoGrantEventProcessed() throws
Exception {
new Context() {
{
- runTest(
- () -> {
- testingLeaderElectionDriver.isLeader();
- final UUID currentLeaderSessionId =
- leaderElectionService.getLeaderSessionID();
- assertThat(currentLeaderSessionId).isNotNull();
-
assertThat(leaderElectionService.hasLeadership(currentLeaderSessionId))
+ runTestWithManuallyTriggeredEvents(
+ executorService -> {
+ final UUID expectedSessionID = UUID.randomUUID();
+
+
testingLeaderElectionDriver.isLeader(expectedSessionID);
+
+
assertThat(leaderElectionService.hasLeadership(expectedSessionID))
+ .isFalse();
+
assertThat(leaderElectionService.hasLeadership(UUID.randomUUID()))
+ .isFalse();
+ });
+ }
+ };
+ }
+
+ @Test
+ void testHasLeadershipWithLeadershipAndGrantEventProcessed() throws
Exception {
+ new Context() {
+ {
+ runTestWithManuallyTriggeredEvents(
+ executorService -> {
+ final UUID expectedSessionID = UUID.randomUUID();
+
+
testingLeaderElectionDriver.isLeader(expectedSessionID);
+ executorService.trigger();
+
+
assertThat(leaderElectionService.hasLeadership(expectedSessionID))
.isTrue();
assertThat(leaderElectionService.hasLeadership(UUID.randomUUID()))
.isFalse();
+ });
+ }
+ };
+ }
+
+ @Test
+ void testHasLeadershipWithLeadershipLostButNoRevokeEventProcessed() throws
Exception {
+ new Context() {
+ {
+ runTestWithManuallyTriggeredEvents(
+ executorService -> {
+ final UUID expectedSessionID = UUID.randomUUID();
+
+
testingLeaderElectionDriver.isLeader(expectedSessionID);
+ executorService.trigger();
+ testingLeaderElectionDriver.notLeader();
+
+
assertThat(leaderElectionService.hasLeadership(expectedSessionID))
+ .isFalse();
Review Comment:
I was puzzled about that one as well at the start. But it makes sense: The
HA backend (i.e. the driver) holds the ground truth of leadership. If the HA
backend indicates the leadership loss, no operation should be executed that
relies on `hasLeadership(UUID)` because another leader could have picked up in
the meantime already. The `onRevokeLeadership` call that follows is used for
cleaning up the component's leadership state only. Does that make sense?
I could add a description to that assert to avoid other's wonder the same
when reading the test code.
--
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]