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 2015/08/19 01:18:20 UTC

[04/31] curator git commit: CURATOR-161 - Updates to unit tests.

CURATOR-161 - Updates to unit tests.

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

Branch: refs/heads/CURATOR-3.0
Commit: b2b9af31c181d7e4f790dbfd9f8850a7b948822d
Parents: 9ff9ccd
Author: Cameron McKenzie <ca...@unico.com.au>
Authored: Tue Dec 2 09:56:02 2014 +1100
Committer: Cameron McKenzie <ca...@unico.com.au>
Committed: Tue Dec 2 09:56:02 2014 +1100

----------------------------------------------------------------------
 .../framework/imps/TestRemoveWatches.java       | 121 ++++++++++++++-----
 1 file changed, 93 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/b2b9af31/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java
index d7e8886..15fb24c 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java
@@ -7,6 +7,7 @@ import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.api.BackgroundCallback;
 import org.apache.curator.framework.api.CuratorEvent;
+import org.apache.curator.framework.api.CuratorEventType;
 import org.apache.curator.framework.api.CuratorListener;
 import org.apache.curator.framework.api.CuratorWatcher;
 import org.apache.curator.retry.RetryOneTime;
@@ -15,16 +16,56 @@ import org.apache.curator.test.Timing;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.Watcher.Event.EventType;
 import org.apache.zookeeper.Watcher.WatcherType;
-import org.apache.zookeeper.ZooKeeper;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
 public class TestRemoveWatches extends BaseClassForTests
 {
     @Test
+    public void testRemoveCuratorDefaultWatcher() throws Exception
+    {
+        Timing timing = new Timing();
+        CuratorFramework client = CuratorFrameworkFactory.builder().
+                connectString(server.getConnectString()).
+                retryPolicy(new RetryOneTime(1)).
+                build();
+        try
+        {
+            client.start();
+            
+            final CountDownLatch removedLatch = new CountDownLatch(1);
+            
+            final String path = "/";            
+            client.getCuratorListenable().addListener(new CuratorListener()
+            {                
+                @Override
+                public void eventReceived(CuratorFramework client, CuratorEvent event)
+                        throws Exception
+                {
+                    if(event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getType() == EventType.DataWatchRemoved) {                        
+                        removedLatch.countDown();
+                    }        
+                }
+            });
+                        
+            client.checkExists().watched().forPath(path);
+            
+            client.removeWatches().allWatches().ofType(WatcherType.Data).forPath(path);
+            
+            Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal");
+        }
+        finally
+        {
+            CloseableUtils.closeQuietly(client);
+        }
+    }
+    
+    @Test
     public void testRemoveCuratorWatch() throws Exception
     {       
+        Timing timing = new Timing();
         CuratorFramework client = CuratorFrameworkFactory.builder().
                 connectString(server.getConnectString()).
                 retryPolicy(new RetryOneTime(1)).
@@ -33,21 +74,26 @@ public class TestRemoveWatches extends BaseClassForTests
         {
             client.start();
             
+            final CountDownLatch removedLatch = new CountDownLatch(1);
+            
+            final String path = "/";            
             CuratorWatcher watcher = new CuratorWatcher()
             {
                 
                 @Override
                 public void process(WatchedEvent event) throws Exception
                 {
-                    // TODO Auto-generated method stub
-                    
+                    if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) {
+                        removedLatch.countDown();
+                    }
                 }
             };
-            
-            String path = "/";
+                        
             client.checkExists().usingWatcher(watcher).forPath(path);
             
-            client.removeWatches().watcher(watcher).ofType(WatcherType.Any).forPath(path);
+            client.removeWatches().watcher(watcher).ofType(WatcherType.Data).forPath(path);
+            
+            Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal");
         }
         finally
         {
@@ -58,6 +104,7 @@ public class TestRemoveWatches extends BaseClassForTests
     @Test
     public void testRemoveWatch() throws Exception
     {       
+        Timing timing = new Timing();
         CuratorFramework client = CuratorFrameworkFactory.builder().
                 connectString(server.getConnectString()).
                 retryPolicy(new RetryOneTime(1)).
@@ -66,18 +113,25 @@ public class TestRemoveWatches extends BaseClassForTests
         {
             client.start();
             
+            final CountDownLatch removedLatch = new CountDownLatch(1);
+            
+            final String path = "/";    
             Watcher watcher = new Watcher()
             {                
                 @Override
                 public void process(WatchedEvent event)
                 {
+                    if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) {
+                        removedLatch.countDown();
+                    }                    
                 }
             };
             
-            String path = "/";
             client.checkExists().usingWatcher(watcher).forPath(path);
             
-            client.removeWatches().watcher(watcher).ofType(WatcherType.Any).forPath(path);
+            client.removeWatches().watcher(watcher).ofType(WatcherType.Data).forPath(path);
+            
+            Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal");
         }
         finally
         {
@@ -94,18 +148,23 @@ public class TestRemoveWatches extends BaseClassForTests
                 retryPolicy(new RetryOneTime(1)).
                 build();
         try
-        {
+        {            
             client.start();
-            
+         
+            //Make sure that the event fires on both the watcher and the callback.
+            final CountDownLatch removedLatch = new CountDownLatch(2);
+            final String path = "/";
             Watcher watcher = new Watcher()
             {                
                 @Override
                 public void process(WatchedEvent event)
                 {
+                    if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) {
+                        removedLatch.countDown();
+                    }                        
                 }
             };
-
-            final CountDownLatch removedLatch = new CountDownLatch(1);
+            
             BackgroundCallback callback = new BackgroundCallback()
             {
                 
@@ -113,11 +172,13 @@ public class TestRemoveWatches extends BaseClassForTests
                 public void processResult(CuratorFramework client, CuratorEvent event)
                         throws Exception
                 {
-                    removedLatch.countDown();
+                    if(event.getType() == CuratorEventType.REMOVE_WATCHES && event.getPath().equals(path)) {
+                        removedLatch.countDown();
+                    }
                 }
             };
             
-            String path = "/";
+            
             client.checkExists().usingWatcher(watcher).forPath(path);
             
             client.removeWatches().watcher(watcher).ofType(WatcherType.Any).inBackground(callback).forPath(path);
@@ -143,27 +204,19 @@ public class TestRemoveWatches extends BaseClassForTests
         {
             client.start();
             
+            final String path = "/";
+            final CountDownLatch removedLatch = new CountDownLatch(1);
             Watcher watcher = new Watcher()
             {                
                 @Override
                 public void process(WatchedEvent event)
                 {
+                    if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) {
+                        removedLatch.countDown();
+                    }                    
                 }
             };
-
-            final CountDownLatch removedLatch = new CountDownLatch(1);
-            client.getCuratorListenable().addListener(new CuratorListener()
-            {
-                
-                @Override
-                public void eventReceived(CuratorFramework client, CuratorEvent event)
-                        throws Exception
-                {
-                    removedLatch.countDown();
-                }
-            });
             
-            String path = "/";
             client.checkExists().usingWatcher(watcher).forPath(path);
             
             client.removeWatches().watcher(watcher).ofType(WatcherType.Any).inBackground().forPath(path);
@@ -180,6 +233,7 @@ public class TestRemoveWatches extends BaseClassForTests
     @Test
     public void testRemoveAllWatches() throws Exception
     {       
+        Timing timing = new Timing();
         CuratorFramework client = CuratorFrameworkFactory.builder().
                 connectString(server.getConnectString()).
                 retryPolicy(new RetryOneTime(1)).
@@ -188,11 +242,17 @@ public class TestRemoveWatches extends BaseClassForTests
         {
             client.start();
             
+            final String path = "/";
+            final CountDownLatch removedLatch = new CountDownLatch(2);
+            
             Watcher watcher1 = new Watcher()
             {                
                 @Override
                 public void process(WatchedEvent event)
                 {
+                    if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) {
+                        removedLatch.countDown();
+                    }
                 }
             };
             
@@ -201,14 +261,19 @@ public class TestRemoveWatches extends BaseClassForTests
                 @Override
                 public void process(WatchedEvent event)
                 {
+                    if(event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) {
+                        removedLatch.countDown();
+                    }                    
                 }
             };            
             
-            String path = "/";
+            
             client.checkExists().usingWatcher(watcher1).forPath(path);
             client.checkExists().usingWatcher(watcher2).forPath(path);
             
             client.removeWatches().allWatches().ofType(WatcherType.Any).forPath(path);
+            
+            Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal");
         }
         finally
         {