some tests to help with CURATOR-97
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/e4cb66ac Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/e4cb66ac Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/e4cb66ac Branch: refs/heads/CURATOR-97-OLDER-MUST-HAVE-BEEN-A-MISTAKE Commit: e4cb66acb9112a4291e593b2825744ef4a258d40 Parents: b34aaaa Author: randgalt <randg...@apache.org> Authored: Wed Mar 19 13:20:42 2014 -0500 Committer: randgalt <randg...@apache.org> Committed: Wed Mar 19 13:20:42 2014 -0500 ---------------------------------------------------------------------- .../recipes/nodes/PersistentEphemeralNode.java | 4 +- .../nodes/TestPersistentEphemeralNode.java | 54 ++++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/e4cb66ac/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 71095ba..1a4d38d 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 @@ -19,6 +19,7 @@ package org.apache.curator.framework.recipes.nodes; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.api.ACLBackgroundPathAndBytesable; @@ -50,7 +51,8 @@ import java.util.concurrent.atomic.AtomicReference; */ public class PersistentEphemeralNode implements Closeable { - private final AtomicReference<CountDownLatch> initialCreateLatch = new AtomicReference<CountDownLatch>(new CountDownLatch(1)); + @VisibleForTesting + final AtomicReference<CountDownLatch> initialCreateLatch = new AtomicReference<CountDownLatch>(new CountDownLatch(1)); private final Logger log = LoggerFactory.getLogger(getClass()); private final CuratorFramework client; private final CreateModable<ACLBackgroundPathAndBytesable<String>> createMethod; http://git-wip-us.apache.org/repos/asf/curator/blob/e4cb66ac/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 e312081..5178fe5 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 @@ -20,7 +20,6 @@ package org.apache.curator.framework.recipes.nodes; import com.google.common.base.Throwables; import com.google.common.collect.Lists; -import org.apache.curator.utils.CloseableUtils; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.BaseClassForTests; @@ -30,7 +29,7 @@ import org.apache.curator.retry.RetryOneTime; import org.apache.curator.test.KillSession; import org.apache.curator.test.TestingServer; import org.apache.curator.test.Timing; -import org.apache.curator.utils.DebugUtils; +import org.apache.curator.utils.CloseableUtils; import org.apache.curator.utils.ZKPaths; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; @@ -72,10 +71,57 @@ public class TestPersistentEphemeralNode extends BaseClassForTests } @Test - public void testListenersReconnectedIsFast() throws Exception + public void testRetriesExpireAndReconnect() throws Exception { - System.setProperty(DebugUtils.PROPERTY_LOG_EVENTS, "true"); + Timing timing = new Timing(); + PersistentEphemeralNode node = null; + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + try + { + client.start(); + node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes()); + node.start(); + Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().seconds(), TimeUnit.SECONDS)); + + final CountDownLatch lostLatch = new CountDownLatch(1); + ConnectionStateListener listener = new ConnectionStateListener() + { + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) + { + if ( newState == ConnectionState.LOST ) + { + lostLatch.countDown(); + } + } + }; + client.getConnectionStateListenable().addListener(listener); + + server.stop(); + + Assert.assertTrue(timing.awaitLatch(lostLatch)); + CountDownLatch recreateLatch = new CountDownLatch(1); + node.initialCreateLatch.set(recreateLatch); + + TimeUnit.MILLISECONDS.sleep(2 * timing.session()); // make sure session expires + + server = new TestingServer(server.getPort(), server.getTempDirectory()); + timing.sleepABit(); + KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString()); + timing.sleepABit(); + Assert.assertTrue(timing.awaitLatch(recreateLatch)); + Assert.assertNotNull(client.checkExists().forPath("/abc/node")); + } + finally + { + CloseableUtils.closeQuietly(node); + CloseableUtils.closeQuietly(client); + } + } + @Test + public void testListenersReconnectedIsFast() throws Exception + { server.close(); Timing timing = new Timing();