You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2017/02/28 22:33:18 UTC

[3/6] curator git commit: Use debug flag instead of assertion

Use debug flag instead of assertion


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/7f53bc17
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/7f53bc17
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/7f53bc17

Branch: refs/heads/master
Commit: 7f53bc179aa738e847c9d097ef4e0ad8b76de4f9
Parents: 7c35e44
Author: Zoltan Szekeres <Zo...@morganstanley.com>
Authored: Tue Jan 31 17:29:48 2017 +0000
Committer: Zoltan Szekeres <Zo...@morganstanley.com>
Committed: Tue Jan 31 17:29:48 2017 +0000

----------------------------------------------------------------------
 .../org/apache/curator/ConnectionState.java     | 25 ++++++++-------
 .../apache/curator/ConnectionStateAccessor.java | 33 ++++++++++++++++++++
 .../framework/imps/TestBlockUntilConnected.java |  9 +++++-
 3 files changed, 55 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/7f53bc17/curator-client/src/main/java/org/apache/curator/ConnectionState.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/ConnectionState.java b/curator-client/src/main/java/org/apache/curator/ConnectionState.java
index bb4f08e..d7bf6d8 100644
--- a/curator-client/src/main/java/org/apache/curator/ConnectionState.java
+++ b/curator-client/src/main/java/org/apache/curator/ConnectionState.java
@@ -18,6 +18,7 @@
  */
 package org.apache.curator;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.curator.drivers.EventTrace;
 import org.apache.curator.drivers.OperationTrace;
@@ -56,6 +57,9 @@ class ConnectionState implements Watcher, Closeable
     private final AtomicLong instanceIndex = new AtomicLong();
     private volatile long connectionStartMs = 0;
 
+    @VisibleForTesting
+    volatile boolean debugWaitOnExpiredEvent = false;
+
     ConnectionState(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher parentWatcher, AtomicReference<TracerDriver> tracer, boolean canBeReadOnly)
     {
         this.ensembleProvider = ensembleProvider;
@@ -163,7 +167,10 @@ class ConnectionState implements Watcher, Closeable
         }
 
         // only wait during tests
-        assert waitOnExpiredEvent(event.getState());
+        if (debugWaitOnExpiredEvent && event.getState() == Event.KeeperState.Expired)
+        {
+            waitOnExpiredEvent();
+        }
 
         for ( Watcher parentWatcher : parentWatchers )
         {
@@ -176,19 +183,15 @@ class ConnectionState implements Watcher, Closeable
     }
 
     // only for testing
-    private boolean waitOnExpiredEvent(Event.KeeperState currentState)
+    private void waitOnExpiredEvent()
     {
-        if (currentState == Event.KeeperState.Expired)
+        log.debug("Waiting on Expired event for testing");
+        try
         {
-            log.debug("Waiting on Expired event for testing");
-            try
-            {
-                Thread.sleep(1000);
-            }
-            catch(InterruptedException e) {}
-            log.debug("Continue processing");
+            Thread.sleep(1000);
         }
-        return true;
+        catch(InterruptedException e) {}
+        log.debug("Continue processing");
     }
 
     EnsembleProvider getEnsembleProvider()

http://git-wip-us.apache.org/repos/asf/curator/blob/7f53bc17/curator-framework/src/test/java/org/apache/curator/ConnectionStateAccessor.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/ConnectionStateAccessor.java b/curator-framework/src/test/java/org/apache/curator/ConnectionStateAccessor.java
new file mode 100755
index 0000000..5b01166
--- /dev/null
+++ b/curator-framework/src/test/java/org/apache/curator/ConnectionStateAccessor.java
@@ -0,0 +1,33 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.curator;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.mockito.internal.util.reflection.Whitebox;
+
+public final class ConnectionStateAccessor
+{
+    public static final void setDebugWaitOnExpiredForClient(CuratorFramework client)
+    {
+        CuratorZookeeperClient zookeeperClient = client.getZookeeperClient();
+        ConnectionState state = (ConnectionState)Whitebox.getInternalState(zookeeperClient, "state");
+        state.debugWaitOnExpiredEvent = true;
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/7f53bc17/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
index debf7f9..3597f95 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
@@ -19,6 +19,8 @@
 
 package org.apache.curator.framework.imps;
 
+import org.apache.curator.ConnectionStateAccessor;
+import org.apache.curator.CuratorZookeeperClient;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.api.CuratorEvent;
@@ -32,6 +34,7 @@ import org.apache.curator.test.TestingServer;
 import org.apache.curator.test.Timing;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.zookeeper.Watcher;
+import org.mockito.internal.util.reflection.Whitebox;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 import java.util.Timer;
@@ -298,6 +301,8 @@ public class TestBlockUntilConnected extends BaseClassForTests
             }
         });
 
+        ConnectionStateAccessor.setDebugWaitOnExpiredForClient(client);
+
         try
         {
             client.start();
@@ -320,7 +325,9 @@ public class TestBlockUntilConnected extends BaseClassForTests
             //Wait until we get expired event
             Assert.assertTrue(timing.awaitLatch(expiredLatch), "Failed to get Expired event");
 
-            Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Not connected");
+            final boolean blockUntilConnected5Seconds = client.blockUntilConnected(5, TimeUnit.SECONDS);
+            Assert.assertTrue(client.getZookeeperClient().isConnected(), "ConnectionState.isConnected returned false");
+            Assert.assertTrue(blockUntilConnected5Seconds, "BlockUntilConnected returned false");
         }
         catch ( Exception e )
         {