Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7E0FE118DA for ; Tue, 22 Jul 2014 21:05:31 +0000 (UTC) Received: (qmail 59343 invoked by uid 500); 22 Jul 2014 21:05:31 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 59313 invoked by uid 500); 22 Jul 2014 21:05:31 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 59299 invoked by uid 99); 22 Jul 2014 21:05:31 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 Jul 2014 21:05:31 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id EF6BB9B0170; Tue, 22 Jul 2014 21:05:30 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: git commit: Test case that shows the problem Date: Tue, 22 Jul 2014 21:05:30 +0000 (UTC) Repository: curator Updated Branches: refs/heads/CURATOR-123 [created] feb13e1c1 Test case that shows the problem Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/feb13e1c Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/feb13e1c Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/feb13e1c Branch: refs/heads/CURATOR-123 Commit: feb13e1c16de7078dfc1310d9403441d1b2dae9e Parents: 5df92bf Author: randgalt Authored: Tue Jul 22 16:05:19 2014 -0500 Committer: randgalt Committed: Tue Jul 22 16:05:19 2014 -0500 ---------------------------------------------------------------------- .../curator/framework/imps/TestReadOnly.java | 165 +++++++++++++------ 1 file changed, 115 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/feb13e1c/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReadOnly.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReadOnly.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReadOnly.java index 537617c..d6d63f8 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReadOnly.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReadOnly.java @@ -16,93 +16,158 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.curator.framework.imps; -import org.apache.curator.utils.CloseableUtils; +import com.google.common.collect.Queues; 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.ExponentialBackoffRetry; +import org.apache.curator.retry.RetryNTimes; +import org.apache.curator.retry.RetryOneTime; import org.apache.curator.test.InstanceSpec; import org.apache.curator.test.TestingCluster; import org.apache.curator.test.Timing; +import org.apache.curator.utils.CloseableUtils; import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.Iterator; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; public class TestReadOnly { - @Test - public void testReadOnly() throws Exception + @BeforeMethod + public void setup() { System.setProperty("readonlymode.enabled", "true"); + } + + @AfterMethod + public void tearDown() + { + System.setProperty("readonlymode.enabled", "false"); + } + + @Test + public void testConnectionStateNewClient() throws Exception + { + Timing timing = new Timing(); + TestingCluster cluster = new TestingCluster(3); + CuratorFramework client = null; try { - Timing timing = new Timing(); + cluster.start(); + + client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(100)); + client.start(); + client.checkExists().forPath("/"); + client.close(); + client = null; - CuratorFramework client = null; - TestingCluster cluster = new TestingCluster(2); - try + System.out.println("killing 2 instances"); + Iterator iterator = cluster.getInstances().iterator(); + for ( int i = 0; i < 2; ++i ) { - cluster.start(); - - client = CuratorFrameworkFactory.builder() - .connectString(cluster.getConnectString()) - .canBeReadOnly(true) - .connectionTimeoutMs(timing.connection()) - .sessionTimeoutMs(timing.session()) - .retryPolicy(new ExponentialBackoffRetry(100, 3)) - .build(); - client.start(); - - client.create().forPath("/test"); - - final CountDownLatch readOnlyLatch = new CountDownLatch(1); - final CountDownLatch reconnectedLatch = new CountDownLatch(1); - ConnectionStateListener listener = new ConnectionStateListener() + cluster.killServer(iterator.next()); + } + + System.out.println("reconnecting client"); + client = CuratorFrameworkFactory.builder() + .connectString(cluster.getConnectString()) + .sessionTimeoutMs(timing.session()) + .connectionTimeoutMs(timing.connection()) + .retryPolicy(new RetryNTimes(3, timing.milliseconds())) + .canBeReadOnly(true) + .build(); + + final BlockingQueue states = Queues.newLinkedBlockingQueue(); + client.getConnectionStateListenable().addListener + ( + new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { - if ( newState == ConnectionState.READ_ONLY ) - { - readOnlyLatch.countDown(); - } - else if ( newState == ConnectionState.RECONNECTED ) - { - reconnectedLatch.countDown(); - } + states.add(newState); } - }; - client.getConnectionStateListenable().addListener(listener); + } + ); + client.start(); + + System.out.println("making api call"); + client.checkExists().forPath("/"); + + ConnectionState state = states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS); + Assert.assertEquals(state, ConnectionState.READ_ONLY); + } + finally + { + CloseableUtils.closeQuietly(client); + CloseableUtils.closeQuietly(cluster); + } + } + + @Test + public void testReadOnly() throws Exception + { + Timing timing = new Timing(); + + CuratorFramework client = null; + TestingCluster cluster = new TestingCluster(2); + try + { + cluster.start(); + + client = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).canBeReadOnly(true).connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build(); + client.start(); - InstanceSpec ourInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper()); - Iterator iterator = cluster.getInstances().iterator(); - InstanceSpec killInstance = iterator.next(); - if ( killInstance.equals(ourInstance) ) + client.create().forPath("/test"); + + final CountDownLatch readOnlyLatch = new CountDownLatch(1); + final CountDownLatch reconnectedLatch = new CountDownLatch(1); + ConnectionStateListener listener = new ConnectionStateListener() + { + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) { - killInstance = iterator.next(); // kill the instance we're not connected to + if ( newState == ConnectionState.READ_ONLY ) + { + readOnlyLatch.countDown(); + } + else if ( newState == ConnectionState.RECONNECTED ) + { + reconnectedLatch.countDown(); + } } - cluster.killServer(killInstance); - - Assert.assertEquals(reconnectedLatch.getCount(), 1); - Assert.assertTrue(timing.awaitLatch(readOnlyLatch)); + }; + client.getConnectionStateListenable().addListener(listener); - Assert.assertEquals(reconnectedLatch.getCount(), 1); - cluster.restartServer(killInstance); - Assert.assertTrue(timing.awaitLatch(reconnectedLatch)); - } - finally + InstanceSpec ourInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper()); + Iterator iterator = cluster.getInstances().iterator(); + InstanceSpec killInstance = iterator.next(); + if ( killInstance.equals(ourInstance) ) { - CloseableUtils.closeQuietly(client); - CloseableUtils.closeQuietly(cluster); + killInstance = iterator.next(); // kill the instance we're not connected to } + cluster.killServer(killInstance); + + Assert.assertEquals(reconnectedLatch.getCount(), 1); + Assert.assertTrue(timing.awaitLatch(readOnlyLatch)); + + Assert.assertEquals(reconnectedLatch.getCount(), 1); + cluster.restartServer(killInstance); + Assert.assertTrue(timing.awaitLatch(reconnectedLatch)); } finally { - System.clearProperty("readonlymode.enabled"); + CloseableUtils.closeQuietly(client); + CloseableUtils.closeQuietly(cluster); } } }