CURATOR-460 Move test from TestLeaderLatch to TestConnectionStateManager
authorjavando <antonio.rafael.ar@gmail.com>
Thu, 5 Apr 2018 02:10:12 +0000 (23:10 -0300)
committerjavando <antonio.rafael.ar@gmail.com>
Thu, 5 Apr 2018 02:10:12 +0000 (23:10 -0300)
curator-framework/src/test/java/org/apache/curator/framework/state/TestConnectionStateManager.java [new file with mode: 0644]
curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java

diff --git a/curator-framework/src/test/java/org/apache/curator/framework/state/TestConnectionStateManager.java b/curator-framework/src/test/java/org/apache/curator/framework/state/TestConnectionStateManager.java
new file mode 100644 (file)
index 0000000..e313c56
--- /dev/null
@@ -0,0 +1,54 @@
+package org.apache.curator.framework.state;
+
+import com.google.common.collect.Queues;
+import org.apache.curator.connection.StandardConnectionHandlingPolicy;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.RetryOneTime;
+import org.apache.curator.test.BaseClassForTests;
+import org.apache.curator.test.compatibility.Timing2;
+import org.apache.curator.utils.CloseableUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+public class TestConnectionStateManager extends BaseClassForTests {
+
+    @Test
+    public void testSessionConnectionStateErrorPolicyWithExpirationPercent30() throws Exception {
+        Timing2 timing = new Timing2();
+        CuratorFramework client = CuratorFrameworkFactory.builder()
+            .connectString(server.getConnectString())
+            .connectionTimeoutMs(1000)
+            .sessionTimeoutMs(timing.session())
+            .retryPolicy(new RetryOneTime(1))
+            .connectionStateErrorPolicy(new SessionConnectionStateErrorPolicy())
+            .connectionHandlingPolicy(new StandardConnectionHandlingPolicy(30))
+            .build();
+
+        try {
+            final BlockingQueue<String> states = Queues.newLinkedBlockingQueue();
+            ConnectionStateListener stateListener = new ConnectionStateListener() {
+                @Override
+                public void stateChanged(CuratorFramework client, ConnectionState newState) {
+                states.add(newState.name());
+                }
+            };
+
+            timing.sleepABit();
+
+            client.getConnectionStateListenable().addListener(stateListener);
+            client.start();
+            Assert.assertEquals(states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED.name());
+            server.close();
+
+            Assert.assertEquals(states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED.name());
+            Assert.assertEquals(states.poll(timing.session() / 3, TimeUnit.MILLISECONDS), ConnectionState.LOST.name());
+        }
+        finally {
+            CloseableUtils.closeQuietly(client);
+        }
+    }
+}
\ No newline at end of file
index af1475d..011e4a0 100644 (file)
@@ -23,7 +23,6 @@ import com.google.common.base.Throwables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Queues;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import org.apache.curator.connection.StandardConnectionHandlingPolicy;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.imps.TestCleanState;
@@ -223,35 +222,6 @@ public class TestLeaderLatch extends BaseClassForTests
             next.add(states.poll(timing.forSessionSleep().milliseconds(), TimeUnit.MILLISECONDS));
             next.add(states.poll(timing.forSessionSleep().milliseconds(), TimeUnit.MILLISECONDS));
             Assert.assertTrue(next.equals(Arrays.asList(ConnectionState.LOST.name(), "false")) || next.equals(Arrays.asList("false", ConnectionState.LOST.name())), next.toString());
-
-            latch.close();
-            client.close();
-
-            timing.sleepABit();
-            states.clear();
-            server = new TestingServer();
-            client = CuratorFrameworkFactory.builder()
-                    .connectString(server.getConnectString())
-                    .connectionTimeoutMs(1000)
-                    .sessionTimeoutMs(timing.session())
-                    .retryPolicy(new RetryOneTime(1))
-                    .connectionStateErrorPolicy(new SessionConnectionStateErrorPolicy())
-                    .connectionHandlingPolicy(new StandardConnectionHandlingPolicy(30))
-                    .build();
-            client.getConnectionStateListenable().addListener(stateListener);
-            client.start();
-            latch = new LeaderLatch(client, "/test");
-            latch.addListener(listener);
-            latch.start();
-            Assert.assertEquals(states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED.name());
-            Assert.assertEquals(states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS), "true");
-            server.close();
-            Assert.assertEquals(states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED.name());
-            next = Lists.newArrayList();
-
-            next.add(states.poll(timing.session() / 3, TimeUnit.MILLISECONDS));
-            next.add(states.poll(timing.forSleepingABit().milliseconds(), TimeUnit.MILLISECONDS));
-            Assert.assertTrue(next.equals(Arrays.asList(ConnectionState.LOST.name(), "false")) || next.equals(Arrays.asList("false", ConnectionState.LOST.name())), next.toString());
         }
         finally
         {