You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/08/24 12:36:42 UTC

[GitHub] ivankelly closed pull request #1622: Add mock registration client

ivankelly closed pull request #1622: Add mock registration client
URL: https://github.com/apache/bookkeeper/pull/1622
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/MockRegistrationClient.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/MockRegistrationClient.java
new file mode 100644
index 0000000000..40178b98f8
--- /dev/null
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/MockRegistrationClient.java
@@ -0,0 +1,157 @@
+/*
+ * 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.bookkeeper.discover;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.versioning.LongVersion;
+import org.apache.bookkeeper.versioning.Versioned;
+
+/**
+ * Mock implementation of registration client.
+ * All actions take place in a single thread executor, so they are async
+ * w.r.t. the caller.
+ */
+public class MockRegistrationClient implements RegistrationClient {
+    final ExecutorService executor;
+    private long currentVersion = 0;
+    private Set<BookieSocketAddress> bookies = new HashSet<BookieSocketAddress>();
+    private Set<BookieSocketAddress> readOnlyBookies = new HashSet<BookieSocketAddress>();
+    private Set<RegistrationListener> bookieWatchers = new HashSet<RegistrationListener>();
+    private Set<RegistrationListener> readOnlyBookieWatchers = new HashSet<RegistrationListener>();
+
+    public MockRegistrationClient() {
+        this.executor = Executors.newSingleThreadExecutor((r) -> new Thread(r, "MockRegistrationClient"));
+    }
+
+    @Override
+    public void close() {
+        executor.shutdownNow();
+    }
+
+    private static Versioned<Set<BookieSocketAddress>> versioned(Set<BookieSocketAddress> bookies, long version) {
+        return new Versioned<>(Collections.unmodifiableSet(bookies), new LongVersion(version));
+    }
+
+    CompletableFuture<Void> addBookies(BookieSocketAddress... bookies) {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+                currentVersion++;
+                for (BookieSocketAddress b : bookies) {
+                    this.bookies.add(b);
+                }
+                bookieWatchers.forEach(w -> w.onBookiesChanged(versioned(this.bookies, currentVersion)));
+                promise.complete(null);
+            });
+        return promise;
+    }
+
+    CompletableFuture<Void> removeBookies(BookieSocketAddress... bookies) {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+                currentVersion++;
+                for (BookieSocketAddress b : bookies) {
+                    this.bookies.add(b);
+                }
+                bookieWatchers.forEach(w -> w.onBookiesChanged(versioned(this.bookies, currentVersion)));
+                promise.complete(null);
+            });
+        return promise;
+    }
+
+    CompletableFuture<Void> addReadOnlyBookies(BookieSocketAddress... bookies) {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+                currentVersion++;
+                for (BookieSocketAddress b : bookies) {
+                    this.readOnlyBookies.add(b);
+                }
+                readOnlyBookieWatchers.forEach(w -> w.onBookiesChanged(versioned(readOnlyBookies, currentVersion)));
+                promise.complete(null);
+            });
+        return promise;
+    }
+
+    CompletableFuture<Void> removeReadOnlyBookies(BookieSocketAddress... bookies) {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+                currentVersion++;
+                for (BookieSocketAddress b : bookies) {
+                    this.readOnlyBookies.add(b);
+                }
+                readOnlyBookieWatchers.forEach(w -> w.onBookiesChanged(versioned(readOnlyBookies, currentVersion)));
+                promise.complete(null);
+            });
+        return promise;
+    }
+
+    @Override
+    public CompletableFuture<Versioned<Set<BookieSocketAddress>>> getWritableBookies() {
+        CompletableFuture<Versioned<Set<BookieSocketAddress>>> promise = new CompletableFuture<>();
+        executor.submit(() -> promise.complete(versioned(bookies, currentVersion)));
+        return promise;
+    }
+
+    @Override
+    public CompletableFuture<Versioned<Set<BookieSocketAddress>>> getReadOnlyBookies() {
+        CompletableFuture<Versioned<Set<BookieSocketAddress>>> promise = new CompletableFuture<>();
+        executor.submit(() -> promise.complete(versioned(readOnlyBookies, currentVersion)));
+        return promise;
+    }
+
+    @Override
+    public CompletableFuture<Void> watchWritableBookies(RegistrationListener listener) {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+                bookieWatchers.add(listener);
+                promise.complete(null);
+            });
+        return promise;
+    }
+
+    @Override
+    public void unwatchWritableBookies(RegistrationListener listener) {
+        executor.submit(() -> {
+                bookieWatchers.remove(listener);
+            });
+    }
+
+    @Override
+    public CompletableFuture<Void> watchReadOnlyBookies(RegistrationListener listener) {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+                readOnlyBookieWatchers.add(listener);
+                promise.complete(null);
+            });
+        return promise;
+    }
+
+    @Override
+    public void unwatchReadOnlyBookies(RegistrationListener listener) {
+        executor.submit(() -> {
+                readOnlyBookieWatchers.remove(listener);
+            });
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services