curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [4/4] git commit: Added setData() method
Date Mon, 10 Jun 2013 20:56:51 GMT
Added setData() method


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

Branch: refs/heads/CURATOR-25
Commit: 415ec2e8500e3a47ef1232c927e17f3e5cf2ef9a
Parents: f7e75e0
Author: randgalt <randgalt@apache.org>
Authored: Mon Jun 10 13:56:43 2013 -0700
Committer: randgalt <randgalt@apache.org>
Committed: Mon Jun 10 13:56:43 2013 -0700

----------------------------------------------------------------------
 .../recipes/nodes/PersistentEphemeralNode.java  | 22 ++++++--
 .../nodes/TestPersistentEphemeralNode.java      | 53 +++++++++++++++++++-
 2 files changed, 71 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/415ec2e8/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
index d86af92..7e92bdf 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
@@ -47,7 +47,7 @@ public class PersistentEphemeralNode implements Closeable
     private final AtomicReference<String>   nodePath = new AtomicReference<String>(null);
     private final String                    basePath;
     private final Mode                      mode;
-    private final byte[]                    data;
+    private final AtomicReference<byte[]>   data = new AtomicReference<byte[]>();
     private final AtomicReference<State>    state = new AtomicReference<State>(State.LATENT);
     private final AtomicBoolean             isSuspended = new AtomicBoolean(false);
     private final BackgroundCallback        backgroundCallback;
@@ -227,7 +227,7 @@ public class PersistentEphemeralNode implements Closeable
         };
 
         createMethod = mode.isProtected() ? client.create().withProtection() : client.create();
-        this.data = Arrays.copyOf(data, data.length);
+        this.data.set(Arrays.copyOf(data, data.length));
     }
 
     /**
@@ -281,6 +281,22 @@ public class PersistentEphemeralNode implements Closeable
         return nodePath.get();
     }
 
+    /**
+     * Set data that ephemeral node should set in ZK also writes the data to the node
+     *
+     * @param data new data value
+     * @throws Exception errors
+     */
+    public void setData(byte[] data) throws Exception
+    {
+        data = Preconditions.checkNotNull(data, "data cannot be null");
+        this.data.set(Arrays.copyOf(data, data.length));
+        if ( isActive() )
+        {
+            client.setData().inBackground().forPath(basePath, this.data.get());
+        }
+    }
+
     private void deleteNode()
     {
         String          localNodePath = nodePath.getAndSet(null);
@@ -313,7 +329,7 @@ public class PersistentEphemeralNode implements Closeable
             String      existingPath = nodePath.get();
             String      createPath = (existingPath != null) ? existingPath : basePath;
             ensurePath.ensure(client.getZookeeperClient());
-            createMethod.withMode(mode.getCreateMode(existingPath != null)).inBackground(backgroundCallback).forPath(createPath,
data);
+            createMethod.withMode(mode.getCreateMode(existingPath != null)).inBackground(backgroundCallback).forPath(createPath,
data.get());
         }
         catch ( Exception e )
         {

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/415ec2e8/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
index 033ec1f..84c1cf7 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
@@ -12,12 +12,14 @@ import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.data.Stat;
+import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.Test;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 
 import static org.testng.Assert.*;
@@ -74,6 +76,54 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
     }
 
     @Test
+    public void testSettingData() throws Exception
+    {
+        PersistentEphemeralNode node = null;
+        Timing timing = new Timing();
+        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
timing.session(), timing.connection(), new RetryOneTime(1));
+        try
+        {
+            client.start();
+            node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL,
PATH, "a".getBytes());
+            node.start();
+            Assert.assertTrue(node.waitForInitialCreate(5, TimeUnit.SECONDS));
+
+            Assert.assertEquals(node.getActualPath(), PATH);
+            Assert.assertEquals(client.getData().forPath(PATH), "a".getBytes());
+
+            final Semaphore semaphore = new Semaphore(0);
+            Watcher watcher = new Watcher()
+            {
+                @Override
+                public void process(WatchedEvent arg0)
+                {
+                    semaphore.release();
+                }
+            };
+            client.checkExists().usingWatcher(watcher).forPath(PATH);
+            node.setData("b".getBytes());
+            Assert.assertTrue(timing.acquireSemaphore(semaphore));
+            Assert.assertEquals(node.getActualPath(), PATH);
+            Assert.assertEquals(client.getData().usingWatcher(watcher).forPath(PATH), "b".getBytes());
+            node.setData("c".getBytes());
+            Assert.assertTrue(timing.acquireSemaphore(semaphore));
+            Assert.assertEquals(node.getActualPath(), PATH);
+            Assert.assertEquals(client.getData().usingWatcher(watcher).forPath(PATH), "c".getBytes());
+            node.close();
+            Assert.assertTrue(timing.acquireSemaphore(semaphore));
+            Assert.assertTrue(client.checkExists().forPath(PATH) == null);
+        }
+        finally
+        {
+            if ( node != null )
+            {
+                node.close();
+            }
+            client.close();
+        }
+    }
+
+    @Test
     public void testDeletesNodeWhenClosed() throws Exception
     {
         CuratorFramework curator = newCurator();
@@ -342,7 +392,8 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
             try
             {
                 return latch.await(duration, unit);
-            } catch ( InterruptedException e )
+            }
+            catch ( InterruptedException e )
             {
                 throw Throwables.propagate(e);
             }


Mime
View raw message