You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by dr...@apache.org on 2014/10/07 01:52:24 UTC

[1/6] git commit: CURATOR-144: TreeCache.Builder to configure advanced options

Repository: curator
Updated Branches:
  refs/heads/master 86299ede1 -> 861ba15b7


CURATOR-144: TreeCache.Builder to configure advanced options


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

Branch: refs/heads/master
Commit: a8ff472cb0c5e63b7f87a9ebfd22c2c9660d55e5
Parents: 7d30d3d
Author: Scott Blum <sc...@squareup.com>
Authored: Fri Aug 22 16:00:20 2014 -0400
Committer: Scott Blum <sc...@squareup.com>
Committed: Fri Aug 22 16:34:55 2014 -0400

----------------------------------------------------------------------
 .../framework/recipes/cache/TreeCache.java      | 159 ++++++++++++++-----
 .../recipes/cache/BaseTestTreeCache.java        |   9 +-
 .../framework/recipes/cache/TestTreeCache.java  |  30 ++--
 .../recipes/cache/TestTreeCacheRandomTree.java  |   2 +-
 4 files changed, 140 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index a4c252b..3de3a04 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -30,6 +30,7 @@ import org.apache.curator.framework.listen.ListenerContainer;
 import org.apache.curator.framework.state.ConnectionState;
 import org.apache.curator.framework.state.ConnectionStateListener;
 import org.apache.curator.utils.CloseableExecutorService;
+import org.apache.curator.utils.PathUtils;
 import org.apache.curator.utils.ThreadUtils;
 import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.KeeperException;
@@ -50,7 +51,6 @@ import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
-import org.apache.curator.utils.PathUtils;
 
 /**
  * <p>A utility that attempts to keep all data from all children of a ZK path locally cached. This class
@@ -65,6 +65,106 @@ public class TreeCache implements Closeable
 {
     private static final Logger LOG = LoggerFactory.getLogger(TreeCache.class);
 
+    public static final class Builder {
+        private final CuratorFramework client;
+        private final String path;
+        private boolean cacheData = true;
+        private boolean dataIsCompressed = false;
+        private CloseableExecutorService executorService = null;
+
+        private Builder(CuratorFramework client, String path) {
+            this.client = client;
+            this.path = path;
+        }
+
+        /**
+         * Builds the {@link TreeCache} based on configured values.
+         */
+        public TreeCache build()
+        {
+            CloseableExecutorService executor = executorService;
+            if ( executor == null )
+            {
+                executor = new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory));
+            }
+            return new TreeCache(client, path, cacheData, dataIsCompressed, executor);
+        }
+
+        /**
+         * Builds the {@link TreeCache} based on configured values, and starts it.
+         */
+        public TreeCache buildAndStart() throws Exception
+        {
+            TreeCache treeCache = build();
+            treeCache.start();
+            return treeCache;
+        }
+
+        /**
+         * Sets whether or not to cache byte data per node; default {@code true}.
+         */
+        public Builder setCacheData(boolean cacheData)
+        {
+            this.cacheData = cacheData;
+            return this;
+        }
+
+        /**
+         * Sets whether or to decompress node data; default {@code false}.
+         */
+        public Builder setDataIsCompressed(boolean dataIsCompressed)
+        {
+            this.dataIsCompressed = dataIsCompressed;
+            return this;
+        }
+
+        /**
+         * Sets the executor to publish events; a default executor will be created if not specified.
+         */
+        public Builder setExecutor(ThreadFactory threadFactory)
+        {
+            return setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory)));
+        }
+
+        /**
+         * Sets the executor to publish events; a default executor will be created if not specified.
+         */
+        public Builder setExecutor(ExecutorService executorService)
+        {
+            if (executorService instanceof CloseableExecutorService) {
+                return setExecutor((CloseableExecutorService) executorService);
+            } else {
+                return setExecutor(new CloseableExecutorService(executorService));
+            }
+        }
+
+        /**
+         * Sets the executor to publish events; a default executor will be created if not specified.
+         */
+        public Builder setExecutor(CloseableExecutorService executorService)
+        {
+            this.executorService = executorService;
+            return this;
+        }
+    }
+
+    /**
+     * Create a TreeCache builder for the given client and path to configure advanced options.
+     *
+     * If the client is namespaced, all operations on the resulting TreeCache will be in terms of
+     * the namespace, including all published events.  The given path is the root at which the
+     * TreeCache will watch and explore.  If no node exists at the given path, the TreeCache will
+     * be initially empty.
+     *
+     * @param client the client to use; may be namespaced
+     * @param path the path to the root node to watch/explore; this path need not actually exist on
+     *             the server
+     * @return a new builder
+     */
+    public static Builder newBuilder(CuratorFramework client, String path) {
+        return new Builder(client, path);
+    }
+
     private enum NodeState
     {
         PENDING, LIVE, DEAD
@@ -356,51 +456,24 @@ public class TreeCache implements Closeable
         }
     };
 
-    private static final ThreadFactory defaultThreadFactory = ThreadUtils.newThreadFactory("TreeCache");
-
-    /**
-     * @param client    the client
-     * @param path      path to watch
-     * @param cacheData if true, node contents are cached in addition to the stat
-     */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData)
-    {
-        this(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory), true));
-    }
-
-    /**
-     * @param client        the client
-     * @param path          path to watch
-     * @param cacheData     if true, node contents are cached in addition to the stat
-     * @param threadFactory factory to use when creating internal threads
-     */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, ThreadFactory threadFactory)
-    {
-        this(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory), true));
-    }
+    static final ThreadFactory defaultThreadFactory = ThreadUtils.newThreadFactory("TreeCache");
 
     /**
-     * @param client           the client
-     * @param path             path to watch
-     * @param cacheData        if true, node contents are cached in addition to the stat
-     * @param dataIsCompressed if true, data in the path is compressed
-     * @param threadFactory    factory to use when creating internal threads
-     */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, ThreadFactory threadFactory)
-    {
-        this(client, path, cacheData, dataIsCompressed, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory), true));
-    }
-
-    /**
-     * @param client           the client
-     * @param path             path to watch
-     * @param cacheData        if true, node contents are cached in addition to the stat
-     * @param dataIsCompressed if true, data in the path is compressed
-     * @param executorService  ExecutorService to use for the TreeCache's background thread
+     * Create a TreeCache for the given client and path with default options.
+     *
+     * If the client is namespaced, all operations on the resulting TreeCache will be in terms of
+     * the namespace, including all published events.  The given path is the root at which the
+     * TreeCache will watch and explore.  If no node exists at the given path, the TreeCache will
+     * be initially empty.
+     *
+     * @see #newBuilder(CuratorFramework, String)
+     * @param client the client to use; may be namespaced
+     * @param path the path to the root node to watch/explore; this path need not actually exist on
+     *             the server
      */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final ExecutorService executorService)
+    public TreeCache(CuratorFramework client, String path)
     {
-        this(client, path, cacheData, dataIsCompressed, new CloseableExecutorService(executorService));
+        this(client, path, true, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory), true));
     }
 
     /**
@@ -410,7 +483,7 @@ public class TreeCache implements Closeable
      * @param dataIsCompressed if true, data in the path is compressed
      * @param executorService  Closeable ExecutorService to use for the TreeCache's background thread
      */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final CloseableExecutorService executorService)
+    TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final CloseableExecutorService executorService)
     {
         this.root = new TreeNode(PathUtils.validatePath(path), null);
         this.client = client;

http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
index eb903b5..62edf34 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
@@ -25,11 +25,13 @@ import org.apache.curator.framework.api.UnhandledErrorListener;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.curator.test.Timing;
+import org.apache.curator.utils.CloseableExecutorService;
 import org.apache.curator.utils.CloseableUtils;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -62,9 +64,14 @@ public class BaseTestTreeCache extends BaseClassForTests
     class MyTreeCache extends TreeCache
     {
 
+        MyTreeCache(CuratorFramework client, String path)
+        {
+            this(client, path, true);
+        }
+
         MyTreeCache(CuratorFramework client, String path, boolean cacheData)
         {
-            super(client, path, cacheData);
+            super(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(TreeCache.defaultThreadFactory), true));
             getListenable().addListener(eventListener);
         }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
index abdbb8b..4528751 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
@@ -39,7 +39,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test/3", "three".getBytes());
         client.create().forPath("/test/2/sub", "two-sub".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes());
@@ -58,7 +58,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testStartEmpty() throws Exception
     {
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
 
@@ -73,7 +73,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
@@ -87,7 +87,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client, "/", true);
+        cache = new MyTreeCache(client, "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
@@ -109,7 +109,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/outer/test");
         client.create().forPath("/outer/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client.usingNamespace("outer"), "/test", true);
+        cache = new MyTreeCache(client.usingNamespace("outer"), "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
@@ -129,7 +129,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/outer/test");
         client.create().forPath("/outer/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client.usingNamespace("outer"), "/", true);
+        cache = new MyTreeCache(client.usingNamespace("outer"), "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/foo");
@@ -147,7 +147,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testSyncInitialPopulation() throws Exception
     {
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
 
@@ -166,7 +166,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test/2", "2".getBytes());
         client.create().forPath("/test/3", "3".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1");
@@ -200,7 +200,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/foo", "one".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
@@ -219,7 +219,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -243,7 +243,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -269,7 +269,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testBasicsOnTwoCaches() throws Exception
     {
-        MyTreeCache cache2 = new MyTreeCache(client, "/test", true);
+        MyTreeCache cache2 = new MyTreeCache(client, "/test");
         cache2.getListenable().removeListener(eventListener);  // Don't listen on the second cache.
 
         // Just ensures the same event count; enables test flow control on cache2.
@@ -287,7 +287,7 @@ public class TestTreeCache extends BaseTestTreeCache
         {
             client.create().forPath("/test");
 
-            cache = new MyTreeCache(client, "/test", true);
+            cache = new MyTreeCache(client, "/test");
             cache.start();
             cache2.start();
 
@@ -327,7 +327,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -357,7 +357,7 @@ public class TestTreeCache extends BaseTestTreeCache
         initCuratorFramework();
 
         // Start the client disconnected.
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertNoMoreEvents();
 

http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
index 304bfe3..0709330 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
@@ -63,7 +63,7 @@ public class TestTreeCacheRandomTree extends BaseTestTreeCache
     {
         client.create().forPath("/tree", null);
         CuratorFramework cl = client.usingNamespace("tree");
-        cache = new MyTreeCache(cl, "/", true);
+        cache = new MyTreeCache(cl, "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);


[5/6] git commit: TreeCache.getUnhandledErrorListenable() non-public

Posted by dr...@apache.org.
TreeCache.getUnhandledErrorListenable() non-public


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

Branch: refs/heads/master
Commit: 60bd9f1df0df4dc1f538812b332f18583fc31b55
Parents: c7a1ea0
Author: Scott Blum <sc...@squareup.com>
Authored: Tue Sep 2 15:39:53 2014 -0400
Committer: Scott Blum <sc...@squareup.com>
Committed: Tue Sep 2 15:39:53 2014 -0400

----------------------------------------------------------------------
 .../apache/curator/framework/recipes/cache/TreeCache.java   | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/60bd9f1d/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index 03ed982..7d51cd8 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -19,6 +19,7 @@
 
 package org.apache.curator.framework.recipes.cache;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
@@ -543,7 +544,13 @@ public class TreeCache implements Closeable
         return listeners;
     }
 
-    public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
+    /**
+     * Allows catching unhandled errors in asynchornous operations.
+     *
+     * TODO: consider making public.
+     */
+    @VisibleForTesting
+    Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
     {
         return errorListeners;
     }


[2/6] git commit: Add precondition checks.

Posted by dr...@apache.org.
Add precondition checks.


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

Branch: refs/heads/master
Commit: 825a96252d6f6101c1163d340375ad1339f996d3
Parents: a8ff472
Author: Scott Blum <sc...@squareup.com>
Authored: Fri Aug 22 17:14:44 2014 -0400
Committer: Scott Blum <sc...@squareup.com>
Committed: Fri Aug 22 17:14:44 2014 -0400

----------------------------------------------------------------------
 .../apache/curator/utils/CloseableExecutorService.java  |  2 +-
 .../curator/framework/recipes/cache/TreeCache.java      | 12 +++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/825a9625/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java b/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
index e9d2547..721754d 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
@@ -116,7 +116,7 @@ public class CloseableExecutorService implements Closeable
      */
     public CloseableExecutorService(ExecutorService executorService, boolean shutdownOnClose)
     {
-        this.executorService = executorService;
+        this.executorService = Preconditions.checkNotNull(executorService);
         this.shutdownOnClose = shutdownOnClose;
     }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/825a9625/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index 3de3a04..030ea2c 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -30,7 +30,6 @@ import org.apache.curator.framework.listen.ListenerContainer;
 import org.apache.curator.framework.state.ConnectionState;
 import org.apache.curator.framework.state.ConnectionStateListener;
 import org.apache.curator.utils.CloseableExecutorService;
-import org.apache.curator.utils.PathUtils;
 import org.apache.curator.utils.ThreadUtils;
 import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.KeeperException;
@@ -52,6 +51,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.curator.utils.PathUtils.validatePath;
+
 /**
  * <p>A utility that attempts to keep all data from all children of a ZK path locally cached. This class
  * will watch the ZK path, respond to update/create/delete events, pull down the data, etc. You can
@@ -73,8 +75,8 @@ public class TreeCache implements Closeable
         private CloseableExecutorService executorService = null;
 
         private Builder(CuratorFramework client, String path) {
-            this.client = client;
-            this.path = path;
+            this.client = checkNotNull(client);
+            this.path = validatePath(path);
         }
 
         /**
@@ -143,7 +145,7 @@ public class TreeCache implements Closeable
          */
         public Builder setExecutor(CloseableExecutorService executorService)
         {
-            this.executorService = executorService;
+            this.executorService = checkNotNull(executorService);
             return this;
         }
     }
@@ -485,7 +487,7 @@ public class TreeCache implements Closeable
      */
     TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final CloseableExecutorService executorService)
     {
-        this.root = new TreeNode(PathUtils.validatePath(path), null);
+        this.root = new TreeNode(validatePath(path), null);
         this.client = client;
         this.cacheData = cacheData;
         this.dataIsCompressed = dataIsCompressed;


[3/6] git commit: Fix formatting; remove MyTreeCache; error listener

Posted by dr...@apache.org.
Fix formatting; remove MyTreeCache; error listener


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

Branch: refs/heads/master
Commit: 180a1598f7b1550c6cab65bea81053e2d0a0ae60
Parents: 825a962
Author: Scott Blum <sc...@squareup.com>
Authored: Tue Aug 26 19:51:29 2014 -0400
Committer: Scott Blum <sc...@squareup.com>
Committed: Tue Aug 26 19:51:29 2014 -0400

----------------------------------------------------------------------
 .../framework/recipes/cache/TreeCache.java      | 77 ++++++++++++++------
 .../recipes/cache/BaseTestTreeCache.java        | 77 +++++++++++---------
 .../framework/recipes/cache/TestTreeCache.java  | 36 +++++----
 .../recipes/cache/TestTreeCacheRandomTree.java  |  4 +-
 4 files changed, 122 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/180a1598/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index 030ea2c..3a7a1de 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -26,6 +26,8 @@ import com.google.common.collect.Maps;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.api.BackgroundCallback;
 import org.apache.curator.framework.api.CuratorEvent;
+import org.apache.curator.framework.api.UnhandledErrorListener;
+import org.apache.curator.framework.listen.Listenable;
 import org.apache.curator.framework.listen.ListenerContainer;
 import org.apache.curator.framework.state.ConnectionState;
 import org.apache.curator.framework.state.ConnectionStateListener;
@@ -67,14 +69,16 @@ public class TreeCache implements Closeable
 {
     private static final Logger LOG = LoggerFactory.getLogger(TreeCache.class);
 
-    public static final class Builder {
+    public static final class Builder
+    {
         private final CuratorFramework client;
         private final String path;
         private boolean cacheData = true;
         private boolean dataIsCompressed = false;
         private CloseableExecutorService executorService = null;
 
-        private Builder(CuratorFramework client, String path) {
+        private Builder(CuratorFramework client, String path)
+        {
             this.client = checkNotNull(client);
             this.path = validatePath(path);
         }
@@ -133,9 +137,12 @@ public class TreeCache implements Closeable
          */
         public Builder setExecutor(ExecutorService executorService)
         {
-            if (executorService instanceof CloseableExecutorService) {
-                return setExecutor((CloseableExecutorService) executorService);
-            } else {
+            if ( executorService instanceof CloseableExecutorService )
+            {
+                return setExecutor((CloseableExecutorService)executorService);
+            }
+            else
+            {
                 return setExecutor(new CloseableExecutorService(executorService));
             }
         }
@@ -152,18 +159,19 @@ public class TreeCache implements Closeable
 
     /**
      * Create a TreeCache builder for the given client and path to configure advanced options.
-     *
+     * <p/>
      * If the client is namespaced, all operations on the resulting TreeCache will be in terms of
      * the namespace, including all published events.  The given path is the root at which the
      * TreeCache will watch and explore.  If no node exists at the given path, the TreeCache will
      * be initially empty.
      *
      * @param client the client to use; may be namespaced
-     * @param path the path to the root node to watch/explore; this path need not actually exist on
-     *             the server
+     * @param path   the path to the root node to watch/explore; this path need not actually exist on
+     *               the server
      * @return a new builder
      */
-    public static Builder newBuilder(CuratorFramework client, String path) {
+    public static Builder newBuilder(CuratorFramework client, String path)
+    {
         return new Builder(client, path);
     }
 
@@ -333,7 +341,8 @@ public class TreeCache implements Closeable
                 if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
                 {
                     Stat oldStat = stat.get();
-                    if (oldStat != null && oldStat.getMzxid() == newStat.getMzxid()) {
+                    if ( oldStat != null && oldStat.getMzxid() == newStat.getMzxid() )
+                    {
                         // Only update stat if mzxid is different, otherwise we might obscure
                         // GET_DATA event updates.
                         stat.set(newStat);
@@ -447,6 +456,7 @@ public class TreeCache implements Closeable
     private final boolean cacheData;
     private final boolean dataIsCompressed;
     private final ListenerContainer<TreeCacheListener> listeners = new ListenerContainer<TreeCacheListener>();
+    private final ListenerContainer<UnhandledErrorListener> errorListeners = new ListenerContainer<UnhandledErrorListener>();
     private final AtomicReference<TreeState> treeState = new AtomicReference<TreeState>(TreeState.LATENT);
 
     private final ConnectionStateListener connectionStateListener = new ConnectionStateListener()
@@ -462,16 +472,16 @@ public class TreeCache implements Closeable
 
     /**
      * Create a TreeCache for the given client and path with default options.
-     *
+     * <p/>
      * If the client is namespaced, all operations on the resulting TreeCache will be in terms of
      * the namespace, including all published events.  The given path is the root at which the
      * TreeCache will watch and explore.  If no node exists at the given path, the TreeCache will
      * be initially empty.
      *
-     * @see #newBuilder(CuratorFramework, String)
      * @param client the client to use; may be namespaced
-     * @param path the path to the root node to watch/explore; this path need not actually exist on
-     *             the server
+     * @param path   the path to the root node to watch/explore; this path need not actually exist on
+     *               the server
+     * @see #newBuilder(CuratorFramework, String)
      */
     public TreeCache(CuratorFramework client, String path)
     {
@@ -503,7 +513,7 @@ public class TreeCache implements Closeable
     {
         Preconditions.checkState(treeState.compareAndSet(TreeState.LATENT, TreeState.STARTED), "already started");
         client.getConnectionStateListenable().addListener(connectionStateListener);
-        if (client.getZookeeperClient().isConnected())
+        if ( client.getZookeeperClient().isConnected() )
         {
             root.wasCreated();
         }
@@ -536,11 +546,16 @@ public class TreeCache implements Closeable
      *
      * @return listenable
      */
-    public ListenerContainer<TreeCacheListener> getListenable()
+    public Listenable<TreeCacheListener> getListenable()
     {
         return listeners;
     }
 
+    public Listenable<UnhandledErrorListener> getUnhandledErrorListenable()
+    {
+        return errorListeners;
+    }
+
     private TreeNode find(String fullPath)
     {
         if ( !fullPath.startsWith(root.path) )
@@ -655,13 +670,33 @@ public class TreeCache implements Closeable
     }
 
     /**
-     * Default behavior is just to log the exception
-     *
-     * @param e the exception
+     * Send an exception to any listeners, or else log the error if there are none.
      */
-    protected void handleException(Throwable e)
+    private void handleException(final Throwable e)
     {
-        LOG.error("", e);
+        if ( errorListeners.size() == 0 )
+        {
+            LOG.error("", e);
+        }
+        else
+        {
+            errorListeners.forEach(new Function<UnhandledErrorListener, Void>()
+            {
+                @Override
+                public Void apply(UnhandledErrorListener listener)
+                {
+                    try
+                    {
+                        listener.unhandledError("", e);
+                    }
+                    catch ( Exception e )
+                    {
+                        LOG.error("Exception handling exception", e);
+                    }
+                    return null;
+                }
+            });
+        }
     }
 
     private void handleStateChange(ConnectionState newState)

http://git-wip-us.apache.org/repos/asf/curator/blob/180a1598/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
index 62edf34..69d3c34 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
@@ -25,13 +25,11 @@ import org.apache.curator.framework.api.UnhandledErrorListener;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.curator.test.Timing;
-import org.apache.curator.utils.CloseableExecutorService;
 import org.apache.curator.utils.CloseableUtils;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -39,19 +37,22 @@ import java.util.concurrent.atomic.AtomicBoolean;
 public class BaseTestTreeCache extends BaseClassForTests
 {
     CuratorFramework client;
-    MyTreeCache cache;
+    TreeCache cache;
     private final AtomicBoolean hadBackgroundException = new AtomicBoolean(false);
     private final BlockingQueue<TreeCacheEvent> events = new LinkedBlockingQueue<TreeCacheEvent>();
     private final Timing timing = new Timing();
 
+    /**
+     * Automatically records all events into an easily testable event stream.
+     */
     final TreeCacheListener eventListener = new TreeCacheListener()
     {
         @Override
         public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception
         {
+            // Suppress any events related to /zookeeper paths
             if ( event.getData() != null && event.getData().getPath().startsWith("/zookeeper") )
             {
-                // Suppress any events related to /zookeeper paths
                 return;
             }
             events.add(event);
@@ -59,27 +60,38 @@ public class BaseTestTreeCache extends BaseClassForTests
     };
 
     /**
-     * A TreeCache that records exceptions and automatically adds a listener.
+     * Ensures that tests don't cause any background errors.
      */
-    class MyTreeCache extends TreeCache
+    final UnhandledErrorListener errorListener = new UnhandledErrorListener()
     {
-
-        MyTreeCache(CuratorFramework client, String path)
+        @Override
+        public void unhandledError(String message, Throwable e)
         {
-            this(client, path, true);
+            hadBackgroundException.set(true);
+            e.printStackTrace(System.err);
         }
+    };
 
-        MyTreeCache(CuratorFramework client, String path, boolean cacheData)
-        {
-            super(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(TreeCache.defaultThreadFactory), true));
-            getListenable().addListener(eventListener);
-        }
+    /**
+     * Construct a TreeCache that records exceptions and automatically listens.
+     */
+    protected TreeCache newTreeCacheWithListeners(CuratorFramework client, String path)
+    {
+        TreeCache result = new TreeCache(client, path);
+        result.getListenable().addListener(eventListener);
+        result.getUnhandledErrorListenable().addListener(errorListener);
+        return result;
+    }
 
-        @Override
-        protected void handleException(Throwable e)
-        {
-            handleBackgroundException(e);
-        }
+    /**
+     * Finish constructing a TreeCache that records exceptions and automatically listens.
+     */
+    protected TreeCache buildWithListeners(TreeCache.Builder builder)
+    {
+        TreeCache result = builder.build();
+        result.getListenable().addListener(eventListener);
+        result.getUnhandledErrorListenable().addListener(errorListener);
+        return result;
     }
 
     @Override
@@ -94,20 +106,7 @@ public class BaseTestTreeCache extends BaseClassForTests
     {
         client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
         client.start();
-        client.getUnhandledErrorListenable().addListener(new UnhandledErrorListener()
-        {
-            @Override
-            public void unhandledError(String message, Throwable e)
-            {
-                handleBackgroundException(e);
-            }
-        });
-    }
-
-    private void handleBackgroundException(Throwable exception)
-    {
-        hadBackgroundException.set(true);
-        exception.printStackTrace(System.err);
+        client.getUnhandledErrorListenable().addListener(errorListener);
     }
 
     @Override
@@ -132,22 +131,34 @@ public class BaseTestTreeCache extends BaseClassForTests
         }
     }
 
+    /**
+     * Asserts the event queue is empty.
+     */
     void assertNoMoreEvents() throws InterruptedException
     {
         timing.sleepABit();
         Assert.assertTrue(events.isEmpty());
     }
 
+    /**
+     * Asserts the given event is next in the queue, and consumes it from the queue.
+     */
     TreeCacheEvent assertEvent(TreeCacheEvent.Type expectedType) throws InterruptedException
     {
         return assertEvent(expectedType, null);
     }
 
+    /**
+     * Asserts the given event is next in the queue, and consumes it from the queue.
+     */
     TreeCacheEvent assertEvent(TreeCacheEvent.Type expectedType, String expectedPath) throws InterruptedException
     {
         return assertEvent(expectedType, expectedPath, null);
     }
 
+    /**
+     * Asserts the given event is next in the queue, and consumes it from the queue.
+     */
     TreeCacheEvent assertEvent(TreeCacheEvent.Type expectedType, String expectedPath, byte[] expectedData) throws InterruptedException
     {
         TreeCacheEvent event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);

http://git-wip-us.apache.org/repos/asf/curator/blob/180a1598/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
index 4528751..4f3d914 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
@@ -39,7 +39,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test/3", "three".getBytes());
         client.create().forPath("/test/2/sub", "two-sub".getBytes());
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes());
@@ -58,7 +58,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testStartEmpty() throws Exception
     {
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
 
@@ -73,7 +73,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
@@ -87,7 +87,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client, "/");
+        cache = newTreeCacheWithListeners(client, "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
@@ -109,7 +109,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/outer/test");
         client.create().forPath("/outer/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client.usingNamespace("outer"), "/test");
+        cache = newTreeCacheWithListeners(client.usingNamespace("outer"), "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
@@ -129,7 +129,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/outer/test");
         client.create().forPath("/outer/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client.usingNamespace("outer"), "/");
+        cache = newTreeCacheWithListeners(client.usingNamespace("outer"), "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/foo");
@@ -147,7 +147,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testSyncInitialPopulation() throws Exception
     {
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
 
@@ -166,7 +166,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test/2", "2".getBytes());
         client.create().forPath("/test/3", "3".getBytes());
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1");
@@ -181,7 +181,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", false);
+        cache = buildWithListeners(TreeCache.newBuilder(client, "/test").setCacheData(false));
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -192,6 +192,10 @@ public class TestTreeCache extends BaseTestTreeCache
         client.setData().forPath("/test/foo", "something new".getBytes());
         assertEvent(TreeCacheEvent.Type.NODE_UPDATED, "/test/foo");
         assertNoMoreEvents();
+
+        Assert.assertNotNull(cache.getCurrentData("/test/foo"));
+        // No byte data querying the tree because we're not caching data.
+        Assert.assertNull(cache.getCurrentData("/test/foo").getData());
     }
 
     @Test
@@ -200,7 +204,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/foo", "one".getBytes());
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
@@ -219,7 +223,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -243,7 +247,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -269,7 +273,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testBasicsOnTwoCaches() throws Exception
     {
-        MyTreeCache cache2 = new MyTreeCache(client, "/test");
+        TreeCache cache2 = newTreeCacheWithListeners(client, "/test");
         cache2.getListenable().removeListener(eventListener);  // Don't listen on the second cache.
 
         // Just ensures the same event count; enables test flow control on cache2.
@@ -287,7 +291,7 @@ public class TestTreeCache extends BaseTestTreeCache
         {
             client.create().forPath("/test");
 
-            cache = new MyTreeCache(client, "/test");
+            cache = newTreeCacheWithListeners(client, "/test");
             cache.start();
             cache2.start();
 
@@ -327,7 +331,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -357,7 +361,7 @@ public class TestTreeCache extends BaseTestTreeCache
         initCuratorFramework();
 
         // Start the client disconnected.
-        cache = new MyTreeCache(client, "/test");
+        cache = newTreeCacheWithListeners(client, "/test");
         cache.start();
         assertNoMoreEvents();
 

http://git-wip-us.apache.org/repos/asf/curator/blob/180a1598/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
index 0709330..c4501af 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
@@ -63,7 +63,7 @@ public class TestTreeCacheRandomTree extends BaseTestTreeCache
     {
         client.create().forPath("/tree", null);
         CuratorFramework cl = client.usingNamespace("tree");
-        cache = new MyTreeCache(cl, "/");
+        cache = newTreeCacheWithListeners(cl, "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -169,7 +169,7 @@ public class TestTreeCacheRandomTree extends BaseTestTreeCache
     /**
      * Recursively assert that current children equal expected children.
      */
-    private static void assertTreeEquals(MyTreeCache cache, TestNode expectedNode)
+    private static void assertTreeEquals(TreeCache cache, TestNode expectedNode)
     {
         String path = expectedNode.fullPath;
         Map<String, ChildData> cacheChildren = cache.getCurrentChildren(path);


[6/6] git commit: Merge branch 'CURATOR-144'

Posted by dr...@apache.org.
Merge branch 'CURATOR-144'

* CURATOR-144:
  TreeCache.getUnhandledErrorListenable() non-public
  Address feedback
  Fix formatting; remove MyTreeCache; error listener
  Add precondition checks.
  CURATOR-144: TreeCache.Builder to configure advanced options


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

Branch: refs/heads/master
Commit: 861ba15b71c83e6d69d4128c793209882f93d92f
Parents: 86299ed 60bd9f1
Author: Scott Blum <dr...@apache.org>
Authored: Mon Oct 6 19:52:02 2014 -0400
Committer: Scott Blum <dr...@apache.org>
Committed: Mon Oct 6 19:52:02 2014 -0400

----------------------------------------------------------------------
 .../curator/utils/CloseableExecutorService.java |   2 +-
 .../framework/recipes/cache/TreeCache.java      | 215 ++++++++++++++-----
 .../recipes/cache/BaseTestTreeCache.java        |  72 ++++---
 .../framework/recipes/cache/TestTreeCache.java  |  36 ++--
 .../recipes/cache/TestTreeCacheRandomTree.java  |   4 +-
 5 files changed, 230 insertions(+), 99 deletions(-)
----------------------------------------------------------------------



[4/6] git commit: Address feedback

Posted by dr...@apache.org.
Address feedback


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

Branch: refs/heads/master
Commit: c7a1ea0e00a6580fe7c0e0ce4869930b32770ae7
Parents: 180a159
Author: Scott Blum <sc...@squareup.com>
Authored: Fri Aug 29 13:55:36 2014 -0400
Committer: Scott Blum <sc...@squareup.com>
Committed: Fri Aug 29 13:55:36 2014 -0400

----------------------------------------------------------------------
 .../curator/utils/CloseableExecutorService.java       |  2 +-
 .../curator/framework/recipes/cache/TreeCache.java    | 14 +++-----------
 2 files changed, 4 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/c7a1ea0e/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java b/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
index 721754d..6a84bc4 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/CloseableExecutorService.java
@@ -116,7 +116,7 @@ public class CloseableExecutorService implements Closeable
      */
     public CloseableExecutorService(ExecutorService executorService, boolean shutdownOnClose)
     {
-        this.executorService = Preconditions.checkNotNull(executorService);
+        this.executorService = Preconditions.checkNotNull(executorService, "executorService cannot be null");
         this.shutdownOnClose = shutdownOnClose;
     }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/c7a1ea0e/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index 3a7a1de..03ed982 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -97,16 +97,6 @@ public class TreeCache implements Closeable
         }
 
         /**
-         * Builds the {@link TreeCache} based on configured values, and starts it.
-         */
-        public TreeCache buildAndStart() throws Exception
-        {
-            TreeCache treeCache = build();
-            treeCache.start();
-            return treeCache;
-        }
-
-        /**
          * Sets whether or not to cache byte data per node; default {@code true}.
          */
         public Builder setCacheData(boolean cacheData)
@@ -507,9 +497,10 @@ public class TreeCache implements Closeable
     /**
      * Start the cache. The cache is not started automatically. You must call this method.
      *
+     * @return this
      * @throws Exception errors
      */
-    public void start() throws Exception
+    public TreeCache start() throws Exception
     {
         Preconditions.checkState(treeState.compareAndSet(TreeState.LATENT, TreeState.STARTED), "already started");
         client.getConnectionStateListenable().addListener(connectionStateListener);
@@ -517,6 +508,7 @@ public class TreeCache implements Closeable
         {
             root.wasCreated();
         }
+        return this;
     }
 
     /**