You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2016/05/19 19:02:45 UTC

[30/35] curator git commit: some refinements and more tests

some refinements and more tests


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

Branch: refs/heads/CURATOR-3.0
Commit: 32965e53b1eee4186e5704f2f23326e6f41d1ee8
Parents: b77b7ee
Author: randgalt <ra...@apache.org>
Authored: Wed May 4 15:09:32 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Wed May 4 15:09:32 2016 -0500

----------------------------------------------------------------------
 .../apache/curator/framework/schema/Schema.java |  46 +++++++-
 .../curator/framework/schema/TestSchema.java    |   2 +
 .../recipes/locks/TestInterProcessMutex.java    | 117 ++++++++++++-------
 3 files changed, 121 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/32965e53/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index dff3cfe..6d4d09e 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -20,6 +20,7 @@ package org.apache.curator.framework.schema;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
+import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.data.ACL;
 import java.util.List;
@@ -72,11 +73,41 @@ public class Schema
         return new SchemaBuilder(pathRegex, null);
     }
 
+    /**
+     * Start a schema builder for a typical Curator recipe's parent node
+     *
+     * @param parentPath Path to the parent node
+     * @return builder
+     */
+    public static SchemaBuilder builderForRecipeParent(String parentPath)
+    {
+        return new SchemaBuilder(null, parentPath)
+            .sequential(Allowance.CANNOT)
+            .ephemeral(Allowance.CANNOT)
+            ;
+    }
+
+    /**
+     * Start a schema builder for a typical Curator recipe's children
+     *
+     * @param parentPath Path to the parent node
+     * @return builder
+     */
+    public static SchemaBuilder builderForRecipe(String parentPath)
+    {
+        return new SchemaBuilder(Pattern.compile(ZKPaths.makePath(parentPath, ".*")), null)
+            .sequential(Allowance.MUST)
+            .ephemeral(Allowance.MUST)
+            .watched(Allowance.MUST)
+            .canBeDeleted(true)
+            ;
+    }
+
     Schema(String name, Pattern pathRegex, String path, String documentation, SchemaValidator schemaValidator, Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted, Map<String, String> metadata)
     {
         Preconditions.checkNotNull((pathRegex != null) || (path != null), "pathRegex and path cannot both be null");
         this.pathRegex = pathRegex;
-        this.path = path;
+        this.path = fixPath(path);
         this.metadata = ImmutableMap.copyOf(Preconditions.checkNotNull(metadata, "metadata cannot be null"));
         this.name = Preconditions.checkNotNull(name, "name cannot be null");
         this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
@@ -87,6 +118,19 @@ public class Schema
         this.canBeDeleted = canBeDeleted;
     }
 
+    private String fixPath(String path)
+    {
+        if ( path != null )
+        {
+            if ( path.endsWith(ZKPaths.PATH_SEPARATOR) )
+            {
+                return (path.length() > 1) ? path.substring(0, path.length() - 1) : "";
+            }
+            return path;
+        }
+        return null;
+    }
+
     /**
      * Validate that this schema allows znode deletion
      *

http://git-wip-us.apache.org/repos/asf/curator/blob/32965e53/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index 78ecdf5..482d17f 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -19,6 +19,7 @@
 package org.apache.curator.framework.schema;
 
 import com.google.common.base.Charsets;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.io.Resources;
 import org.apache.curator.framework.CuratorFramework;
@@ -34,6 +35,7 @@ import org.testng.annotations.Test;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 public class TestSchema extends BaseClassForTests
 {

http://git-wip-us.apache.org/repos/asf/curator/blob/32965e53/curator-recipes/src/test/java/org/apache/curator/framework/recipes/locks/TestInterProcessMutex.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/locks/TestInterProcessMutex.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/locks/TestInterProcessMutex.java
index c37d88d..68daeb7 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/locks/TestInterProcessMutex.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/locks/TestInterProcessMutex.java
@@ -16,13 +16,18 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.curator.framework.recipes.locks;
 
+import com.google.common.collect.Lists;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.imps.TestCleanState;
+import org.apache.curator.framework.schema.Schema;
+import org.apache.curator.framework.schema.SchemaSet;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.KillSession;
+import org.apache.curator.utils.CloseableUtils;
 import org.apache.zookeeper.CreateMode;
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -33,6 +38,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
 
 public class TestInterProcessMutex extends TestInterProcessMutexBase
 {
@@ -45,62 +51,87 @@ public class TestInterProcessMutex extends TestInterProcessMutexBase
     }
 
     @Test
-    public void     testRevoking() throws Exception
+    public void testWithSchema() throws Exception
     {
-        final CuratorFramework        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
+        Schema schemaRoot = Schema.builderForRecipeParent("/foo").name("root").build();
+        Schema schemaLocks = Schema.builderForRecipe("/foo").name("locks").build();
+        SchemaSet schemaSet = new SchemaSet(Lists.newArrayList(schemaRoot, schemaLocks), false);
+        CuratorFramework client = CuratorFrameworkFactory.builder()
+            .connectString(server.getConnectString())
+            .retryPolicy(new RetryOneTime(1))
+            .schemaSet(schemaSet)
+            .build();
         try
         {
             client.start();
-            final InterProcessMutex       lock = new InterProcessMutex(client, LOCK_PATH);
 
-            ExecutorService               executorService = Executors.newCachedThreadPool();
+            InterProcessMutex lock = new InterProcessMutex(client, "/foo");
+            lock.acquire();
+            lock.release();
+        }
+        finally
+        {
+            CloseableUtils.closeQuietly(client);
+        }
+    }
 
-            final CountDownLatch          revokeLatch = new CountDownLatch(1);
-            final CountDownLatch          lockLatch = new CountDownLatch(1);
-            Future<Void>                  f1 = executorService.submit
-            (
-                new Callable<Void>()
-                {
-                    @Override
-                    public Void call() throws Exception
+    @Test
+    public void testRevoking() throws Exception
+    {
+        final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
+        try
+        {
+            client.start();
+            final InterProcessMutex lock = new InterProcessMutex(client, LOCK_PATH);
+
+            ExecutorService executorService = Executors.newCachedThreadPool();
+
+            final CountDownLatch revokeLatch = new CountDownLatch(1);
+            final CountDownLatch lockLatch = new CountDownLatch(1);
+            Future<Void> f1 = executorService.submit
+                (
+                    new Callable<Void>()
                     {
-                        RevocationListener<InterProcessMutex> listener = new RevocationListener<InterProcessMutex>()
+                        @Override
+                        public Void call() throws Exception
                         {
-                            @Override
-                            public void revocationRequested(InterProcessMutex lock)
+                            RevocationListener<InterProcessMutex> listener = new RevocationListener<InterProcessMutex>()
                             {
-                                revokeLatch.countDown();
-                            }
-                        };
-                        lock.makeRevocable(listener);
-                        lock.acquire();
-                        lockLatch.countDown();
-                        revokeLatch.await();
-                        lock.release();
-                        return null;
+                                @Override
+                                public void revocationRequested(InterProcessMutex lock)
+                                {
+                                    revokeLatch.countDown();
+                                }
+                            };
+                            lock.makeRevocable(listener);
+                            lock.acquire();
+                            lockLatch.countDown();
+                            revokeLatch.await();
+                            lock.release();
+                            return null;
+                        }
                     }
-                }
-            );
+                );
 
-            Future<Void>                  f2 = executorService.submit
-            (
-                new Callable<Void>()
-                {
-                    @Override
-                    public Void call() throws Exception
+            Future<Void> f2 = executorService.submit
+                (
+                    new Callable<Void>()
                     {
-                        Assert.assertTrue(lockLatch.await(10, TimeUnit.SECONDS));
-                        Collection<String> nodes = lock.getParticipantNodes();
-                        Assert.assertEquals(nodes.size(), 1);
-                        Revoker.attemptRevoke(client, nodes.iterator().next());
-
-                        InterProcessMutex       l2 = new InterProcessMutex(client, LOCK_PATH);
-                        Assert.assertTrue(l2.acquire(5, TimeUnit.SECONDS));
-                        l2.release();
-                        return null;
+                        @Override
+                        public Void call() throws Exception
+                        {
+                            Assert.assertTrue(lockLatch.await(10, TimeUnit.SECONDS));
+                            Collection<String> nodes = lock.getParticipantNodes();
+                            Assert.assertEquals(nodes.size(), 1);
+                            Revoker.attemptRevoke(client, nodes.iterator().next());
+
+                            InterProcessMutex l2 = new InterProcessMutex(client, LOCK_PATH);
+                            Assert.assertTrue(l2.acquire(5, TimeUnit.SECONDS));
+                            l2.release();
+                            return null;
+                        }
                     }
-                }
-            );
+                );
 
             f2.get();
             f1.get();