gossip-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ecapri...@apache.org
Subject incubator-gossip git commit: GOSSIP-4: Use builder to create RandomGossipManager (Jaideep Dhok via EGC)
Date Tue, 07 Jun 2016 02:44:40 GMT
Repository: incubator-gossip
Updated Branches:
  refs/heads/master 3ca8e0f9c -> fe196cd78


GOSSIP-4: Use builder to create RandomGossipManager (Jaideep Dhok via EGC)


Project: http://git-wip-us.apache.org/repos/asf/incubator-gossip/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-gossip/commit/fe196cd7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-gossip/tree/fe196cd7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-gossip/diff/fe196cd7

Branch: refs/heads/master
Commit: fe196cd788321ac69e7f2bd2c0913752a2247efa
Parents: 3ca8e0f
Author: jdhok <jaideep.dhok@gmail.com>
Authored: Tue Jun 7 00:07:50 2016 +0800
Committer: Edward Capriolo <edlinuxguru@gmail.com>
Committed: Mon Jun 6 22:43:41 2016 -0400

----------------------------------------------------------------------
 .../java/org/apache/gossip/GossipService.java   | 11 ++-
 .../manager/random/RandomGossipManager.java     | 73 ++++++++++++++-
 .../manager/RandomGossipManagerBuilderTest.java | 96 ++++++++++++++++++++
 3 files changed, 177 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-gossip/blob/fe196cd7/src/main/java/org/apache/gossip/GossipService.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/gossip/GossipService.java b/src/main/java/org/apache/gossip/GossipService.java
index 9db740e..cf727a8 100644
--- a/src/main/java/org/apache/gossip/GossipService.java
+++ b/src/main/java/org/apache/gossip/GossipService.java
@@ -59,8 +59,15 @@ public class GossipService {
   public GossipService(String cluster, String ipAddress, int port, String id,
           List<GossipMember> gossipMembers, GossipSettings settings, GossipListener
listener)
           throws InterruptedException, UnknownHostException {
-    gossipManager = new RandomGossipManager(cluster, ipAddress, port, id, settings, gossipMembers,
-            listener);
+    gossipManager = RandomGossipManager.newBuilder()
+        .withId(id)
+        .cluster(cluster)
+        .address(ipAddress)
+        .port(port)
+        .settings(settings)
+        .gossipMembers(gossipMembers)
+        .listener(listener)
+        .build();
   }
 
   public void start() {

http://git-wip-us.apache.org/repos/asf/incubator-gossip/blob/fe196cd7/src/main/java/org/apache/gossip/manager/random/RandomGossipManager.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/gossip/manager/random/RandomGossipManager.java b/src/main/java/org/apache/gossip/manager/random/RandomGossipManager.java
index 0122610..d407d2a 100644
--- a/src/main/java/org/apache/gossip/manager/random/RandomGossipManager.java
+++ b/src/main/java/org/apache/gossip/manager/random/RandomGossipManager.java
@@ -23,10 +23,81 @@ import org.apache.gossip.event.GossipListener;
 import org.apache.gossip.manager.GossipManager;
 import org.apache.gossip.manager.impl.OnlyProcessReceivedPassiveGossipThread;
 
+import java.util.ArrayList;
 import java.util.List;
 
 public class RandomGossipManager extends GossipManager {
-  public RandomGossipManager(String cluster, String address, int port, String id,
+
+  public static ManagerBuilder newBuilder() {
+    return new ManagerBuilder();
+  }
+
+  public static final class ManagerBuilder {
+    private String cluster;
+    private String address;
+    private int port;
+    private String id;
+    private GossipSettings settings;
+    private List<GossipMember> gossipMembers;
+    private GossipListener listener;
+
+    private ManagerBuilder() {}
+
+    private void checkArgument(boolean check, String msg) {
+      if (!check) {
+        throw new IllegalArgumentException(msg);
+      }
+    }
+
+    public ManagerBuilder cluster(String cluster) {
+      this.cluster = cluster;
+      return this;
+    }
+
+    public ManagerBuilder address(String address) {
+      this.address = address;
+      return this;
+    }
+
+    public ManagerBuilder port(int port) {
+      this.port = port;
+      return this;
+    }
+
+    public ManagerBuilder withId(String id) {
+      this.id = id;
+      return this;
+    }
+
+    public ManagerBuilder settings(GossipSettings settings) {
+      this.settings = settings;
+      return this;
+    }
+
+    public ManagerBuilder gossipMembers(List<GossipMember> members) {
+      this.gossipMembers = members;
+      return this;
+    }
+
+    public ManagerBuilder listener(GossipListener listener) {
+      this.listener = listener;
+      return this;
+    }
+
+    public RandomGossipManager build() {
+      checkArgument(id != null, "You must specify an id");
+      checkArgument(cluster != null, "You must specify a cluster name");
+      checkArgument(settings != null, "You must specify gossip settings");
+
+      if (this.gossipMembers == null) {
+        this.gossipMembers = new ArrayList<>();
+      }
+
+      return new RandomGossipManager(cluster, address, port, id, settings, gossipMembers,
listener);
+    }
+  }
+
+  private RandomGossipManager(String cluster, String address, int port, String id,
                              GossipSettings settings, List<GossipMember> gossipMembers,
GossipListener listener) {
     super(OnlyProcessReceivedPassiveGossipThread.class, RandomActiveGossipThread.class, cluster,
             address, port, id, settings, gossipMembers, listener);

http://git-wip-us.apache.org/repos/asf/incubator-gossip/blob/fe196cd7/src/test/java/org/apache/gossip/manager/RandomGossipManagerBuilderTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/gossip/manager/RandomGossipManagerBuilderTest.java b/src/test/java/org/apache/gossip/manager/RandomGossipManagerBuilderTest.java
new file mode 100644
index 0000000..38b8ab4
--- /dev/null
+++ b/src/test/java/org/apache/gossip/manager/RandomGossipManagerBuilderTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.gossip.manager;
+
+import org.apache.gossip.GossipMember;
+import org.apache.gossip.GossipSettings;
+import org.apache.gossip.LocalGossipMember;
+import org.apache.gossip.event.GossipListener;
+import org.apache.gossip.event.GossipState;
+import org.apache.gossip.manager.random.RandomGossipManager;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.management.Notification;
+import javax.management.NotificationListener;
+import java.util.ArrayList;
+import java.util.List;
+
+public class RandomGossipManagerBuilderTest {
+
+  public static class TestGossipListener implements GossipListener {
+    @Override
+    public void gossipEvent(GossipMember member, GossipState state) {
+      System.out.println("Got gossip event");
+    }
+  }
+
+  public static class TestNotificationListener implements NotificationListener {
+    @Override
+    public void handleNotification(Notification notification, Object o) {
+      System.out.println("Got notification event");
+    }
+  }
+
+  @Test(expected=IllegalArgumentException.class)
+  public void idShouldNotBeNull() {
+    RandomGossipManager.newBuilder().cluster("aCluster").build();
+  }
+
+  @Test(expected=IllegalArgumentException.class)
+  public void clusterShouldNotBeNull() {
+    RandomGossipManager.newBuilder().withId("id").build();
+  }
+
+  @Test(expected=IllegalArgumentException.class)
+  public void settingsShouldNotBeNull() {
+    RandomGossipManager.newBuilder().withId("id").cluster("aCluster").build();
+  }
+
+  @Test
+  public void createMembersListIfNull() {
+    RandomGossipManager gossipManager = RandomGossipManager.newBuilder()
+        .withId("id")
+        .cluster("aCluster")
+        .port(8080)
+        .address("localhost")
+        .settings(new GossipSettings())
+        .gossipMembers(null).build();
+
+    Assert.assertNotNull(gossipManager.getMemberList());
+  }
+
+  @Test
+  public void useMemberListIfProvided() {
+    LocalGossipMember member = new LocalGossipMember("aCluster", "localhost", 2000, "aGossipMember",
+        System.currentTimeMillis(), new TestNotificationListener(), 60000);
+
+    List<GossipMember> memberList = new ArrayList<>();
+    memberList.add(member);
+
+    RandomGossipManager gossipManager = RandomGossipManager.newBuilder()
+        .withId("id")
+        .cluster("aCluster")
+        .settings(new GossipSettings())
+        .gossipMembers(memberList).build();
+
+    Assert.assertEquals(1, gossipManager.getMemberList().size());
+    Assert.assertEquals(member.getId(), gossipManager.getMemberList().get(0).getId());
+  }
+
+}
\ No newline at end of file


Mime
View raw message