wangyang0918 commented on a change in pull request #13644:
URL: https://github.com/apache/flink/pull/13644#discussion_r513218321



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/DefaultLeaderElectionServiceTest.java
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.leaderelection;
+
+import org.junit.Test;
+
+import java.util.UUID;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@link DefaultLeaderElectionService}.
+ */
+public class DefaultLeaderElectionServiceTest {
+
+       private static final String TEST_URL = "akka//user/jobmanager";
+       private static final long timeout = 30L * 1000L;
+
+       @Test
+       public void testOnGrantAndRevokeLeadership() throws Exception {
+               final 
TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory 
testingLeaderElectionDriverFactory =
+                       new 
TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory();
+               final DefaultLeaderElectionService leaderElectionService = new 
DefaultLeaderElectionService(
+                       testingLeaderElectionDriverFactory);
+               final TestingContender testingContender = new 
TestingContender(TEST_URL, leaderElectionService);
+               leaderElectionService.start(testingContender);
+
+               // grant leadership
+               final TestingLeaderElectionDriver testingLeaderElectionDriver =
+                       
testingLeaderElectionDriverFactory.getCurrentLeaderDriver();
+               assertThat(testingLeaderElectionDriver, is(notNullValue()));
+               testingLeaderElectionDriver.isLeader();
+
+               testingContender.waitForLeader(timeout);
+               assertThat(testingContender.isLeader(), is(true));
+               assertThat(testingContender.getDescription(), is(TEST_URL));
+
+               // Check the external storage
+               
assertThat(testingLeaderElectionDriver.getLeaderInformation().getLeaderAddress(),
 is(TEST_URL));
+
+               // revoke leadership
+               testingLeaderElectionDriver.notLeader();
+               testingContender.waitForRevokeLeader(timeout);
+               assertThat(testingContender.isLeader(), is(false));
+
+               leaderElectionService.stop();
+       }
+
+       @Test
+       public void testLeaderInformationChangedAndShouldBeCorrected() throws 
Exception {
+               final 
TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory 
testingLeaderElectionDriverFactory =
+                       new 
TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory();
+               final DefaultLeaderElectionService leaderElectionService = new 
DefaultLeaderElectionService(
+                       testingLeaderElectionDriverFactory);
+               final TestingContender testingContender = new 
TestingContender(TEST_URL, leaderElectionService);
+               leaderElectionService.start(testingContender);
+
+               final TestingLeaderElectionDriver testingLeaderElectionDriver =
+                       
testingLeaderElectionDriverFactory.getCurrentLeaderDriver();
+               assertThat(testingLeaderElectionDriver, is(notNullValue()));
+               testingLeaderElectionDriver.isLeader();
+               testingContender.waitForLeader(timeout);
+
+               // Leader information changed and should be corrected
+               
testingLeaderElectionDriver.leaderInformationChanged(LeaderInformation.empty());
+               
assertThat(testingLeaderElectionDriver.getLeaderInformation().getLeaderAddress(),
 is(TEST_URL));

Review comment:
       Will check it.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to