curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject git commit: Test case that shows the problem
Date Tue, 22 Jul 2014 21:05:30 GMT
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 <randgalt@apache.org>
Authored: Tue Jul 22 16:05:19 2014 -0500
Committer: randgalt <randgalt@apache.org>
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<InstanceSpec> 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<ConnectionState> 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<InstanceSpec>      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<InstanceSpec> 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);
         }
     }
 }


Mime
View raw message