Repository: curator
Updated Branches:
  refs/heads/CURATOR-253 [created] 145da217f


Switch to new injectSessionExpiration()


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

Branch: refs/heads/CURATOR-253
Commit: d1b4cbf070046ace5c047cc46d99c0ae71f749a5
Parents: 0f5668b
Author: randgalt <randg...@apache.org>
Authored: Fri Aug 28 13:39:22 2015 -0500
Committer: randgalt <randg...@apache.org>
Committed: Fri Aug 28 13:39:22 2015 -0500

----------------------------------------------------------------------
 .../curator/TestSessionFailRetryLoop.java       |  9 +-
 .../framework/recipes/cache/TestTreeCache.java  |  1 -
 .../org/apache/curator/test/KillSession.java    | 87 ++++----------------
 3 files changed, 21 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/d1b4cbf0/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
----------------------------------------------------------------------
diff --git 
a/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java 
b/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
index c17b5bf..937726e 100644
--- 
a/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
+++ 
b/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
@@ -18,6 +18,7 @@
  */
 package org.apache.curator;
 
+import org.apache.curator.retry.ExponentialBackoffRetry;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.curator.retry.RetryOneTime;
@@ -34,7 +35,7 @@ public class TestSessionFailRetryLoop extends 
BaseClassForTests
     public void     testRetry() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = 
client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
         retryLoop.start();
         try
@@ -103,7 +104,7 @@ public class TestSessionFailRetryLoop extends 
BaseClassForTests
     public void     testRetryStatic() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = 
client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
         retryLoop.start();
         try
@@ -175,7 +176,7 @@ public class TestSessionFailRetryLoop extends 
BaseClassForTests
     public void     testBasic() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = 
client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
         retryLoop.start();
         try
@@ -230,7 +231,7 @@ public class TestSessionFailRetryLoop extends 
BaseClassForTests
     public void     testBasicStatic() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new 
CuratorZookeeperClient(server.getConnectString(), timing.session(), 
timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = 
client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
         retryLoop.start();
         try

http://git-wip-us.apache.org/repos/asf/curator/blob/d1b4cbf0/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
----------------------------------------------------------------------
diff --git 
a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
 
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
index 0bccb54..151ea7e 100644
--- 
a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
+++ 
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
@@ -377,7 +377,6 @@ public class TestTreeCache extends BaseTestTreeCache
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/me");
 
         KillSession.kill(client.getZookeeperClient().getZooKeeper(), 
server.getConnectString());
-        assertEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED);
         assertEvent(TreeCacheEvent.Type.CONNECTION_LOST);
         assertEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED);
         assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/me");

http://git-wip-us.apache.org/repos/asf/curator/blob/d1b4cbf0/curator-test/src/main/java/org/apache/curator/test/KillSession.java
----------------------------------------------------------------------
diff --git 
a/curator-test/src/main/java/org/apache/curator/test/KillSession.java 
b/curator-test/src/main/java/org/apache/curator/test/KillSession.java
index 63b7f2a..ce2b7e6 100644
--- a/curator-test/src/main/java/org/apache/curator/test/KillSession.java
+++ b/curator-test/src/main/java/org/apache/curator/test/KillSession.java
@@ -18,24 +18,12 @@
  */
 package org.apache.curator.test;
 
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
 
 /**
  * <p>
- *     Utility to simulate a ZK session dying. See: <a 
href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4";>ZooKeeper FAQ</a>
+ *     Utility to simulate a ZK session dying.
  * </p>
- *
- * <blockquote>
- *     In the case of testing we want to cause a problem, so to explicitly 
expire a session an
- *     application connects to ZooKeeper, saves the session id and password, 
creates another
- *     ZooKeeper handle with that id and password, and then closes the new 
handle. Since both
- *     handles reference the same session, the close on second handle will 
invalidate the session
- *     causing a SESSION_EXPIRED on the first handle.
- * </blockquote>
  */
 public class KillSession
 {
@@ -43,12 +31,24 @@ public class KillSession
      * Kill the given ZK session
      *
      * @param client the client to kill
+     * @since 3.0.0
+     */
+    public static void     kill(ZooKeeper client)
+    {
+        client.getTestable().injectSessionExpiration();
+    }
+
+    /**
+     * Kill the given ZK session
+     *
+     * @param client the client to kill
      * @param connectString server connection string
      * @throws Exception errors
+     * @deprecated use {@link #kill(ZooKeeper)} instead
      */
     public static void     kill(ZooKeeper client, String connectString) throws 
Exception
     {
-        kill(client, connectString, new Timing().forWaiting().milliseconds());
+        kill(client);
     }
 
     /**
@@ -58,65 +58,10 @@ public class KillSession
      * @param connectString server connection string
      * @param maxMs max time ms to wait for kill
      * @throws Exception errors
+     * @deprecated use {@link #kill(ZooKeeper)} instead
      */
     public static void     kill(ZooKeeper client, String connectString, int 
maxMs) throws Exception
     {
-        long                    startTicks = System.currentTimeMillis();
-
-        final CountDownLatch    sessionLostLatch = new CountDownLatch(1);
-        Watcher                 sessionLostWatch = new Watcher()
-        {
-            @Override
-            public void process(WatchedEvent event)
-            {
-                sessionLostLatch.countDown();
-            }
-        };
-        client.exists("/___CURATOR_KILL_SESSION___" + System.nanoTime(), 
sessionLostWatch);
-
-        final CountDownLatch    connectionLatch = new CountDownLatch(1);
-        Watcher                 connectionWatcher = new Watcher()
-        {
-            @Override
-            public void process(WatchedEvent event)
-            {
-                if ( event.getState() == Event.KeeperState.SyncConnected )
-                {
-                    connectionLatch.countDown();
-                }
-            }
-        };
-        ZooKeeper zk = new ZooKeeper(connectString, maxMs, connectionWatcher, 
client.getSessionId(), client.getSessionPasswd());
-        try
-        {
-            if ( !connectionLatch.await(maxMs, TimeUnit.MILLISECONDS) )
-            {
-                throw new Exception("KillSession could not establish duplicate 
session");
-            }
-            try
-            {
-                zk.close();
-            }
-            finally
-            {
-                zk = null;
-            }
-
-            while ( client.getState().isConnected() && 
!sessionLostLatch.await(100, TimeUnit.MILLISECONDS) )
-            {
-                long        elapsed = System.currentTimeMillis() - startTicks;
-                if ( elapsed > maxMs )
-                {
-                    throw new Exception("KillSession timed out waiting for 
session to expire");
-                }
-            }
-        }
-        finally
-        {
-            if ( zk != null )
-            {
-                zk.close();
-            }
-        }
+        kill(client);
     }
 }

Reply via email to