From f4dcec3e6f80616f76f428c25787ff3293273a5a Mon Sep 17 00:00:00 2001 From: randgalt Date: Mon, 28 Mar 2016 20:32:55 -0500 Subject: [PATCH 1/2] Check for initial create in setData() --- .../recipes/nodes/PersistentNode.java | 1 + .../recipes/nodes/TestPersistentNode.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java index c472fdd3df..bf11f81efe 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java @@ -317,6 +317,7 @@ public String getActualPath() public void setData(byte[] data) throws Exception { data = Preconditions.checkNotNull(data, "data cannot be null"); + Preconditions.checkState(nodePath.get() != null, "initial create has not been processed. Call waitForInitialCreate() to ensure."); this.data.set(Arrays.copyOf(data, data.length)); if ( isActive() ) { diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentNode.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentNode.java index 386a0febe3..ac8c65d304 100644 --- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentNode.java +++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentNode.java @@ -31,6 +31,37 @@ public class TestPersistentNode extends BaseClassForTests { + @Test + public void testQuickSetData() throws Exception + { + final byte[] TEST_DATA = "hey".getBytes(); + final byte[] ALT_TEST_DATA = "there".getBytes(); + + Timing timing = new Timing(); + PersistentNode pen = null; + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + try + { + client.start(); + pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test", TEST_DATA); + pen.start(); + try + { + pen.setData(ALT_TEST_DATA); + Assert.fail("IllegalStateException should have been thrown"); + } + catch ( IllegalStateException dummy ) + { + // expected + } + } + finally + { + CloseableUtils.closeQuietly(pen); + CloseableUtils.closeQuietly(client); + } + } + @Test public void testBasic() throws Exception { From 73cd00aed8e42cf38dcbf5b1dd36826b3041ee13 Mon Sep 17 00:00:00 2001 From: randgalt Date: Mon, 28 Mar 2016 21:18:58 -0500 Subject: [PATCH 2/2] Added comment --- .../curator/framework/recipes/nodes/PersistentNode.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java index bf11f81efe..5f94ea197b 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentNode.java @@ -309,7 +309,10 @@ public String getActualPath() } /** - * Set data that node should set in ZK also writes the data to the node + * Set data that node should set in ZK also writes the data to the node. NOTE: it + * is an error to call this method after {@link #start()} but before the initial create + * has completed. Use {@link #waitForInitialCreate(long, TimeUnit)} to ensure initial + * creation. * * @param data new data value * @throws Exception errors