tillrohrmann commented on a change in pull request #17485:
URL: https://github.com/apache/flink/pull/17485#discussion_r791059723



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/leaderelection/TestingMultipleComponentLeaderElectionDriver.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.apache.flink.util.Preconditions;
+import org.apache.flink.util.function.BiConsumerWithException;
+import org.apache.flink.util.function.ThrowingConsumer;
+
+import java.util.Optional;
+
+/** Testing implementation of {@link MultipleComponentLeaderElectionDriver}. */
+public class TestingMultipleComponentLeaderElectionDriver
+        implements MultipleComponentLeaderElectionDriver {
+
+    private final BiConsumerWithException<String, LeaderInformation, Exception>
+            publishLeaderInformationConsumer;
+    private final ThrowingConsumer<String, Exception> 
deleteLeaderInformationConsumer;
+    private boolean hasLeadership;
+
+    private Optional<Listener> listener;
+
+    private TestingMultipleComponentLeaderElectionDriver(
+            BiConsumerWithException<String, LeaderInformation, Exception>
+                    publishLeaderInformationConsumer,
+            ThrowingConsumer<String, Exception> 
deleteLeaderInformationConsumer) {
+        this.publishLeaderInformationConsumer = 
publishLeaderInformationConsumer;
+        this.deleteLeaderInformationConsumer = deleteLeaderInformationConsumer;
+        hasLeadership = false;
+        listener = Optional.empty();
+    }
+
+    public void grantLeadership() {
+        if (!hasLeadership) {
+            hasLeadership = true;
+            listener.ifPresent(Listener::isLeader);
+        }
+    }
+
+    public void revokeLeadership() {
+        if (hasLeadership) {
+            hasLeadership = false;
+            listener.ifPresent(Listener::notLeader);
+        }
+    }
+
+    public void setListener(Listener listener) {
+        Preconditions.checkState(!this.listener.isPresent(), "Can only set a 
single listener.");
+        this.listener = Optional.of(listener);
+    }
+
+    @Override
+    public void close() throws Exception {}
+
+    @Override
+    public boolean hasLeadership() {
+        return hasLeadership;
+    }
+
+    @Override
+    public void publishLeaderInformation(String componentId, LeaderInformation 
leaderInformation)
+            throws Exception {
+        publishLeaderInformationConsumer.accept(componentId, 
leaderInformation);
+    }
+
+    @Override
+    public void deleteLeaderInformation(String componentId) throws Exception {
+        deleteLeaderInformationConsumer.accept(componentId);
+    }
+
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public static final class Builder {
+        private BiConsumerWithException<String, LeaderInformation, Exception>
+                publishLeaderInformationConsumer = (ignoredA, ignoredB) -> {};
+        private ThrowingConsumer<String, Exception> 
deleteLeaderInformationConsumer =
+                (ignored) -> {};

Review comment:
       True. Will update 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.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to