From e2eed55985b1d5efbc74ca04afcdac28c8400fe6 Mon Sep 17 00:00:00 2001 From: Patrick Peralta Date: Tue, 22 Jul 2014 13:23:43 -0400 Subject: [PATCH 1/2] CURATOR-124 - PathChildrenCache StartMode doc Updated PathChildrenCache.StartMode.NORMAL JavaDoc to accurately depict the cache startup behavior. Added test testChildrenInitializedNormal to assert the startup behavior indicated in the JavaDoc. --- .../recipes/cache/PathChildrenCache.java | 15 ++--- .../recipes/cache/TestPathChildrenCache.java | 59 +++++++++++++++++-- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java index ad433d8b34..18e008b979 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java @@ -253,20 +253,21 @@ public void start(boolean buildInitial) throws Exception public enum StartMode { /** - * cache will _not_ be primed. i.e. it will start empty and you will receive - * events for all nodes added, etc. + * The cache will be primed (in the background) with initial values. + * Events for existing and new nodes will be posted. */ NORMAL, /** - * {@link PathChildrenCache#rebuild()} will be called before this method returns in - * order to get an initial view of the node. + * The cache will be primed (in the foreground) with initial values. + * {@link PathChildrenCache#rebuild()} will be called before this + * method returns in order to get an initial view of the node. */ BUILD_INITIAL_CACHE, /** * After cache is primed with initial values (in the background) a - * {@link PathChildrenCacheEvent.Type#INITIALIZED} will be posted + * {@link PathChildrenCacheEvent.Type#INITIALIZED} will be posted. */ POST_INITIALIZED_EVENT } @@ -767,9 +768,9 @@ public void run() //so just ignore these events if ( state.get() != State.CLOSED ) { - handleException(e); + handleException(e); } - + Thread.currentThread().interrupt(); } catch ( Exception e ) diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestPathChildrenCache.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestPathChildrenCache.java index 653a8b1428..60f2e8839e 100644 --- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestPathChildrenCache.java +++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestPathChildrenCache.java @@ -171,6 +171,55 @@ else if ( event.getType() == PathChildrenCacheEvent.Type.INITIALIZED ) } } + @Test + public void testChildrenInitializedNormal() throws Exception + { + Timing timing = new Timing(); + PathChildrenCache cache = null; + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + try + { + client.start(); + client.create().forPath("/test"); + + cache = new PathChildrenCache(client, "/test", true); + + final CountDownLatch addedLatch = new CountDownLatch(3); + cache.getListenable().addListener + ( + new PathChildrenCacheListener() + { + @Override + public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception + { + Assert.assertNotEquals(event.getType(), PathChildrenCacheEvent.Type.INITIALIZED); + if ( event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED ) + { + addedLatch.countDown(); + } + } + } + ); + + client.create().forPath("/test/1", "1".getBytes()); + client.create().forPath("/test/2", "2".getBytes()); + client.create().forPath("/test/3", "3".getBytes()); + + cache.start(PathChildrenCache.StartMode.NORMAL); + + Assert.assertTrue(timing.awaitLatch(addedLatch)); + Assert.assertEquals(cache.getCurrentData().size(), 3); + Assert.assertEquals(cache.getCurrentData().get(0).getData(), "1".getBytes()); + Assert.assertEquals(cache.getCurrentData().get(1).getData(), "2".getBytes()); + Assert.assertEquals(cache.getCurrentData().get(2).getData(), "3".getBytes()); + } + finally + { + CloseableUtils.closeQuietly(cache); + CloseableUtils.closeQuietly(client); + } + } + @Test public void testUpdateWhenNotCachingData() throws Exception { @@ -841,7 +890,7 @@ public void testDeleteNodeAfterCloseDoesntCallExecutor() } } - + /** * Tests the case where there's an outstanding operation being executed when the cache is * shut down. See CURATOR-121, this was causing misleading warning messages to be logged. @@ -874,19 +923,19 @@ public void invoke() throws Exception Thread.sleep(5000); } }); - + Thread.sleep(1000); cache.close(); - + latch.await(5, TimeUnit.SECONDS); - + Assert.assertTrue(latch.getCount() == 1, "Unexpected exception occurred"); } finally { CloseableUtils.closeQuietly(client); } - } + } public static class ExecuteCalledWatchingExecutorService extends DelegatingExecutorService { From c71a5a7aacf64b2a639d7cbdef462dd2c4d50de9 Mon Sep 17 00:00:00 2001 From: Patrick Peralta Date: Wed, 23 Jul 2014 15:06:49 -0400 Subject: [PATCH 2/2] Updated per review comments Updated BUILD_INITIAL_CACHE doc to indicate that the start() method with this mode will cause a synchronous rebuilding of the cache. Removed extra line. --- .../curator/framework/recipes/cache/PathChildrenCache.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java index 18e008b979..d5555085a3 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java @@ -260,8 +260,9 @@ public enum StartMode /** * The cache will be primed (in the foreground) with initial values. - * {@link PathChildrenCache#rebuild()} will be called before this - * method returns in order to get an initial view of the node. + * {@link PathChildrenCache#rebuild()} will be called before + * the {@link PathChildrenCache#start(StartMode)} method returns + * in order to get an initial view of the node. */ BUILD_INITIAL_CACHE, @@ -770,7 +771,6 @@ public void run() { handleException(e); } - Thread.currentThread().interrupt(); } catch ( Exception e )