Repository: curator Updated Branches: refs/heads/CURATOR-134 [created] ddcdc844b
CURATOR-134 - Added unit test to reproduce the issue. Modification to the testing cluster to allow restarting. Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/ddcdc844 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/ddcdc844 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/ddcdc844 Branch: refs/heads/CURATOR-134 Commit: ddcdc844b93f5bea19574d92ca1855b67e12d57b Parents: d2c37d0 Author: Cameron McKenzie <[email protected]> Authored: Wed Aug 20 10:34:04 2014 +1000 Committer: Cameron McKenzie <[email protected]> Committed: Wed Aug 20 10:34:04 2014 +1000 ---------------------------------------------------------------------- .../framework/imps/TestConnectionState.java | 90 ++++++++++++++++++++ .../org/apache/curator/test/TestingCluster.java | 12 +++ 2 files changed, 102 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/ddcdc844/curator-framework/src/test/java/org/apache/curator/framework/imps/TestConnectionState.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestConnectionState.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestConnectionState.java new file mode 100644 index 0000000..33a9ee7 --- /dev/null +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestConnectionState.java @@ -0,0 +1,90 @@ +package org.apache.curator.framework.imps; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; +import org.apache.curator.retry.RetryNTimes; +import org.apache.curator.test.BaseClassForTests; +import org.apache.curator.test.TestingCluster; +import org.apache.curator.utils.CloseableUtils; +import org.testng.Assert; +import org.testng.ITestContext; +import org.testng.annotations.Test; + +public class TestConnectionState extends BaseClassForTests +{ + public void beforeSuite(ITestContext context) { + + } + + @Test + public void testLostConnectionState() throws Exception + { + server.close(); + + int sessionTimeout = 30000; + int numRetries = 3; + int msBetweenRetries = 10000; + int connectionTimeoutSeconds = 10000; + + TestingCluster cluster = new TestingCluster(3); + CuratorFramework client = null; + + try { + cluster.start(); + + client = CuratorFrameworkFactory + .builder() + .connectString(cluster.getConnectString()) + .sessionTimeoutMs(sessionTimeout) + .connectionTimeoutMs(connectionTimeoutSeconds) + .retryPolicy(new RetryNTimes(numRetries, msBetweenRetries)) + .canBeReadOnly(true).build(); + + final CountDownLatch lostLatch = new CountDownLatch(1); + client.getConnectionStateListenable().addListener( + new ConnectionStateListener() + { + + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) + { + if(newState == ConnectionState.LOST) { + lostLatch.countDown(); + } + } + }); + + client.start(); + + Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to establish connection"); + + //Restart the server + cluster.restart(); + + //Wait for reconnection + Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to reestablish connection"); + + cluster.stop(); + + long stopTime = System.currentTimeMillis(); + + long maxWaitTimeMS = ((numRetries + 2) * (msBetweenRetries + connectionTimeoutSeconds)); + boolean lost = lostLatch.await(maxWaitTimeMS, TimeUnit.MILLISECONDS); + long lostTime = System.currentTimeMillis(); + Assert.assertTrue(lost, "LOST state was not achieved"); + + //Make sure that the lost event occurred after the right amount of time + Assert.assertTrue(lostTime - stopTime > numRetries * msBetweenRetries, "LOST state was achieved too early"); + + + } finally { + CloseableUtils.closeQuietly(client); + CloseableUtils.closeQuietly(cluster); + } + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/ddcdc844/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java ---------------------------------------------------------------------- diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java b/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java index cd86b72..a189342 100644 --- a/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java +++ b/curator-test/src/main/java/org/apache/curator/test/TestingCluster.java @@ -154,6 +154,18 @@ public class TestingCluster implements Closeable server.start(); } } + + /** + * Restart the ensemble. + * @throws Exception + */ + public void restart() throws Exception + { + for ( TestingZooKeeperServer server : servers ) + { + server.restart(); + } + } /** * Shutdown the ensemble WITHOUT freeing resources, etc.
