curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [11/32] git commit: some additional functionality in testing code
Date Mon, 22 Jul 2013 23:26:46 GMT
some additional functionality in testing code


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

Branch: refs/heads/CURATOR-14
Commit: 6a26d4fa3ff431bbc0a5a166e4f92e8b3691f0f7
Parents: c676e3e
Author: randgalt <randgalt@apache.org>
Authored: Sat Jun 15 16:39:14 2013 -0700
Committer: randgalt <randgalt@apache.org>
Committed: Sat Jun 15 16:39:14 2013 -0700

----------------------------------------------------------------------
 .../org/apache/curator/test/TestingCluster.java | 66 +++++++++++++++++---
 .../curator/test/TestingQuorumPeerMain.java     |  6 ++
 .../curator/test/TestingZooKeeperMain.java      |  7 +++
 .../curator/test/TestingZooKeeperServer.java    | 12 ++++
 .../apache/curator/test/ZooKeeperMainFace.java  |  3 +
 5 files changed, 85 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/6a26d4fa/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java
----------------------------------------------------------------------
diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java b/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java
index 442cb66..cd86b72 100644
--- a/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java
+++ b/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java
@@ -18,7 +18,12 @@
  */
 package org.apache.curator.test;
 
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import org.apache.zookeeper.ZooKeeper;
 import java.io.Closeable;
 import java.io.IOException;
@@ -26,6 +31,7 @@ import java.lang.reflect.Method;
 import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 /**
  * manages an internally running ensemble of ZooKeeper servers. FOR TESTING PURPOSES ONLY
@@ -37,7 +43,6 @@ public class TestingCluster implements Closeable
         ByteCodeRewrite.apply();
     }
 
-    private final QuorumConfigBuilder           builder;
     private final List<TestingZooKeeperServer>  servers;
 
     /**
@@ -58,7 +63,7 @@ public class TestingCluster implements Closeable
      */
     public TestingCluster(InstanceSpec... specs)
     {
-        this(ImmutableList.copyOf(specs));
+        this(listToMap(ImmutableList.copyOf(specs)));
     }
 
     /**
@@ -68,11 +73,25 @@ public class TestingCluster implements Closeable
      */
     public TestingCluster(Collection<InstanceSpec> specs)
     {
-        builder = new QuorumConfigBuilder(specs);
+        this(listToMap(specs));
+    }
+
+    /**
+     * Creates an ensemble using the given server specs
+     *
+     * @param specs map of an instance spec to its set of quorum instances. Allows simulation
of an ensemble with instances
+     *              having different config peers
+     */
+    public TestingCluster(Map<InstanceSpec, Collection<InstanceSpec>> specs)
+    {
         ImmutableList.Builder<TestingZooKeeperServer> serverBuilder = ImmutableList.builder();
-        for ( int i = 0; i < specs.size(); ++i )
+        for ( Map.Entry<InstanceSpec, Collection<InstanceSpec>> entry : specs.entrySet()
)
         {
-            serverBuilder.add(new TestingZooKeeperServer(builder, i));
+            List<InstanceSpec> instanceSpecs = Lists.newArrayList(entry.getValue());
+            int index = instanceSpecs.indexOf(entry.getKey());
+            Preconditions.checkState(index >= 0, entry.getKey() + " not found in specs");
+            QuorumConfigBuilder builder = new QuorumConfigBuilder(instanceSpecs);
+            serverBuilder.add(new TestingZooKeeperServer(builder, index));
         }
         servers = serverBuilder.build();
     }
@@ -84,7 +103,24 @@ public class TestingCluster implements Closeable
      */
     public Collection<InstanceSpec> getInstances()
     {
-        return builder.getInstanceSpecs();
+        Iterable<InstanceSpec> transformed = Iterables.transform
+        (
+            servers,
+            new Function<TestingZooKeeperServer, InstanceSpec>()
+            {
+                @Override
+                public InstanceSpec apply(TestingZooKeeperServer server)
+                {
+                    return server.getInstanceSpec();
+                }
+            }
+        );
+        return Lists.newArrayList(transformed);
+    }
+
+    public List<TestingZooKeeperServer> getServers()
+    {
+        return Lists.newArrayList(servers);
     }
 
     /**
@@ -95,7 +131,7 @@ public class TestingCluster implements Closeable
     public String   getConnectString()
     {
         StringBuilder       str = new StringBuilder();
-        for ( InstanceSpec spec : builder.getInstanceSpecs() )
+        for ( InstanceSpec spec : getInstances() )
         {
             if ( str.length() > 0 )
             {
@@ -211,13 +247,25 @@ public class TestingCluster implements Closeable
         return null;
     }
 
-    private static Collection<InstanceSpec> makeSpecs(int instanceQty)
+    private static Map<InstanceSpec, Collection<InstanceSpec>> makeSpecs(int
instanceQty)
     {
         ImmutableList.Builder<InstanceSpec> builder = ImmutableList.builder();
         for ( int i = 0; i < instanceQty; ++i )
         {
             builder.add(InstanceSpec.newInstanceSpec());
         }
-        return builder.build();
+
+        return listToMap(builder.build());
+    }
+
+    private static Map<InstanceSpec, Collection<InstanceSpec>> listToMap(Collection<InstanceSpec>
list)
+    {
+        ImmutableMap.Builder<InstanceSpec, Collection<InstanceSpec>> mapBuilder
= ImmutableMap.builder();
+        for ( InstanceSpec spec : list )
+        {
+            mapBuilder.put(spec, list);
+        }
+
+        return mapBuilder.build();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/6a26d4fa/curator-test/src/main/java/org/apache/curator/test/TestingQuorumPeerMain.java
----------------------------------------------------------------------
diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingQuorumPeerMain.java
b/curator-test/src/main/java/org/apache/curator/test/TestingQuorumPeerMain.java
index 0048099..9f9f302 100644
--- a/curator-test/src/main/java/org/apache/curator/test/TestingQuorumPeerMain.java
+++ b/curator-test/src/main/java/org/apache/curator/test/TestingQuorumPeerMain.java
@@ -53,6 +53,12 @@ class TestingQuorumPeerMain extends QuorumPeerMain implements ZooKeeperMainFace
     }
 
     @Override
+    public QuorumPeer getQuorumPeer()
+    {
+        return quorumPeer;
+    }
+
+    @Override
     public void close() throws IOException
     {
         if ( quorumPeer != null )

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/6a26d4fa/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java
----------------------------------------------------------------------
diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java
b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java
index 21bc57f..2193acb 100644
--- a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java
+++ b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java
@@ -23,6 +23,7 @@ import org.apache.zookeeper.server.ServerConfig;
 import org.apache.zookeeper.server.ZKDatabase;
 import org.apache.zookeeper.server.ZooKeeperServer;
 import org.apache.zookeeper.server.ZooKeeperServerMain;
+import org.apache.zookeeper.server.quorum.QuorumPeer;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 import java.io.IOException;
 import java.lang.reflect.Field;
@@ -67,6 +68,12 @@ public class TestingZooKeeperMain extends ZooKeeperServerMain implements
ZooKeep
         super.runFromConfig(serverConfig);
     }
 
+    @Override
+    public QuorumPeer getQuorumPeer()
+    {
+        throw new UnsupportedOperationException();
+    }
+
     @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
     @Override
     public void blockUntilStarted() throws Exception

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/6a26d4fa/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
----------------------------------------------------------------------
diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
index b9e7dc1..be1a1b6 100644
--- a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
+++ b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java
@@ -18,12 +18,14 @@
  */
 package org.apache.curator.test;
 
+import org.apache.zookeeper.server.quorum.QuorumPeer;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 import org.apache.zookeeper.server.quorum.QuorumPeerMain;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import java.io.Closeable;
 import java.io.IOException;
+import java.util.Collection;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -58,6 +60,16 @@ public class TestingZooKeeperServer extends QuorumPeerMain implements Closeable
         main = (configBuilder.size() > 1) ? new TestingQuorumPeerMain() : new TestingZooKeeperMain();
     }
 
+    public QuorumPeer getQuorumPeer()
+    {
+        return main.getQuorumPeer();
+    }
+
+    public Collection<InstanceSpec> getInstanceSpecs()
+    {
+        return configBuilder.getInstanceSpecs();
+    }
+
     public void     kill()
     {
         main.kill();

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/6a26d4fa/curator-test/src/main/java/org/apache/curator/test/ZooKeeperMainFace.java
----------------------------------------------------------------------
diff --git a/curator-test/src/main/java/org/apache/curator/test/ZooKeeperMainFace.java b/curator-test/src/main/java/org/apache/curator/test/ZooKeeperMainFace.java
index 946ac1a..6da29a4 100644
--- a/curator-test/src/main/java/org/apache/curator/test/ZooKeeperMainFace.java
+++ b/curator-test/src/main/java/org/apache/curator/test/ZooKeeperMainFace.java
@@ -18,6 +18,7 @@
  */
 package org.apache.curator.test;
 
+import org.apache.zookeeper.server.quorum.QuorumPeer;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 import java.io.Closeable;
 
@@ -28,4 +29,6 @@ public interface ZooKeeperMainFace extends Closeable
     public void blockUntilStarted() throws Exception;
 
     public void kill();
+
+    public QuorumPeer getQuorumPeer();
 }


Mime
View raw message