Repository: curator
Updated Branches:
  refs/heads/CURATOR-476 [created] 895015165


CURATOR-476 PathChildrenCache should check resultCode=-101 for getData(...) 
done on non-existent child and remove it from it's initialSet


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

Branch: refs/heads/CURATOR-476
Commit: 895015165dca24fb58aa23a1949bed0476761910
Parents: 8a75db8
Author: rsouhrain <[email protected]>
Authored: Fri Aug 10 14:10:18 2018 +0100
Committer: rsouhrain <[email protected]>
Committed: Fri Aug 10 14:10:18 2018 +0100

----------------------------------------------------------------------
 .../recipes/cache/PathChildrenCache.java        |  7 ++-
 .../recipes/cache/TestPathChildrenCache.java    | 51 ++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/89501516/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/PathChildrenCache.java
----------------------------------------------------------------------
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 5d418c6..f8c4e93 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
@@ -601,7 +601,7 @@ public class PathChildrenCache implements Closeable
         Map<String, ChildData> localInitialSet = initialSet.get();
         if ( localInitialSet != null )
         {
-            localInitialSet.remove(fullPath);
+            localInitialSet.remove(ZKPaths.getNodeFromPath(fullPath));
             maybeOfferInitializedEvent(localInitialSet);
         }
     }
@@ -713,6 +713,11 @@ public class PathChildrenCache implements Closeable
             }
             updateInitialSet(ZKPaths.getNodeFromPath(fullPath), data);
         }
+        else if ( resultCode == KeeperException.Code.NONODE.intValue() )
+        {
+            log.debug("NoNode at path {}, removing child from initialSet", 
fullPath);
+            remove(fullPath);
+        }
     }
 
     private void updateInitialSet(String name, ChildData data)

http://git-wip-us.apache.org/repos/asf/curator/blob/89501516/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestPathChildrenCache.java
----------------------------------------------------------------------
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 da294c8..d2f8cc0 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
@@ -95,6 +95,57 @@ public class TestPathChildrenCache extends BaseClassForTests
     }
 
     @Test
+    public void testInitializedEvenIfChildDeleted() throws Exception
+    {
+        final CuratorFramework client = 
CuratorFrameworkFactory.newClient(server.getConnectString(), new 
RetryOneTime(1));
+
+        PathChildrenCache cache = new PathChildrenCache(client, "/a/b/test", 
true)
+        {
+            @Override
+            void getDataAndStat(final String fullPath) throws Exception
+            {
+                // before installing a data watcher on the child, let's delete 
this child
+                client.delete().forPath("/a/b/test/one");
+                super.getDataAndStat(fullPath);
+            }
+        };
+
+        Timing timing = new Timing();
+
+        try
+        {
+            client.start();
+
+            final CountDownLatch cacheInitialized = new CountDownLatch(1);
+
+            PathChildrenCacheListener listener = new 
PathChildrenCacheListener()
+            {
+                @Override
+                public void childEvent(CuratorFramework client, 
PathChildrenCacheEvent event) throws Exception
+                {
+                    if ( event.getType() == 
PathChildrenCacheEvent.Type.INITIALIZED )
+                    {
+                        cacheInitialized.countDown();
+                    }
+                }
+            };
+            cache.getListenable().addListener(listener);
+
+            client.create().creatingParentsIfNeeded().forPath("/a/b/test/one");
+
+            cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
+
+            Assert.assertTrue(timing.awaitLatch(cacheInitialized));
+            Assert.assertEquals(cache.getCurrentData().size(), 0);
+        }
+        finally
+        {
+            CloseableUtils.closeQuietly(cache);
+            CloseableUtils.closeQuietly(client);
+        }
+    }
+
+    @Test
     public void testWithBadConnect() throws Exception
     {
         final int serverPort = server.getPort();

Reply via email to