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 2014/06/18 01:03:21 UTC

[17/18] git commit: Added testProperCloseWithoutConnectionEstablished

Added testProperCloseWithoutConnectionEstablished


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

Branch: refs/heads/master
Commit: bac23d3af7abf4bf25d5acfca7c5f2cb53b739d0
Parents: 03e736c
Author: randgalt <ra...@apache.org>
Authored: Tue Jun 17 17:11:53 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Tue Jun 17 17:11:53 2014 -0500

----------------------------------------------------------------------
 .../framework/recipes/leader/LeaderLatch.java   | 18 +++++--
 .../recipes/leader/TestLeaderLatch.java         | 49 ++++++++++++++++++++
 2 files changed, 62 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/bac23d3a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
index 6f7636a..af3f895 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
@@ -200,11 +200,7 @@ public class LeaderLatch implements Closeable
         Preconditions.checkState(state.compareAndSet(State.STARTED, State.CLOSED), "Already closed or has not been started");
         Preconditions.checkNotNull(closeMode, "closeMode cannot be null");
 
-        Future<?> localStartTask = startTask.getAndSet(null);
-        if ( localStartTask != null )
-        {
-            localStartTask.cancel(true);
-        }
+        cancelStartTask();
 
         try
         {
@@ -237,6 +233,18 @@ public class LeaderLatch implements Closeable
         }
     }
 
+    @VisibleForTesting
+    protected boolean cancelStartTask()
+    {
+        Future<?> localStartTask = startTask.getAndSet(null);
+        if ( localStartTask != null )
+        {
+            localStartTask.cancel(true);
+            return true;
+        }
+        return false;
+    }
+
     /**
      * Attaches a listener to this LeaderLatch
      * <p/>

http://git-wip-us.apache.org/repos/asf/curator/blob/bac23d3a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java
index f4fb1c7..fecb9e3 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderLatch.java
@@ -52,6 +52,55 @@ public class TestLeaderLatch extends BaseClassForTests
     private static final int MAX_LOOPS = 5;
 
     @Test
+    public void testProperCloseWithoutConnectionEstablished() throws Exception
+    {
+        server.stop();
+
+        Timing timing = new Timing();
+        LeaderLatch latch = null;
+        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
+        try
+        {
+            client.start();
+
+            final AtomicBoolean resetCalled = new AtomicBoolean(false);
+            final CountDownLatch cancelStartTaskLatch = new CountDownLatch(1);
+            latch = new LeaderLatch(client, PATH_NAME)
+            {
+                @Override
+                void reset() throws Exception
+                {
+                    resetCalled.set(true);
+                    super.reset();
+                }
+
+                @Override
+                protected boolean cancelStartTask()
+                {
+                    if ( super.cancelStartTask() )
+                    {
+                        cancelStartTaskLatch.countDown();
+                        return true;
+                    }
+                    return false;
+                }
+            };
+
+            latch.start();
+            latch.close();
+            latch = null;
+
+            Assert.assertTrue(timing.awaitLatch(cancelStartTaskLatch));
+            Assert.assertFalse(resetCalled.get());
+        }
+        finally
+        {
+            CloseableUtils.closeQuietly(latch);
+            CloseableUtils.closeQuietly(client);
+        }
+    }
+
+    @Test
     public void testResetRace() throws Exception
     {
         Timing timing = new Timing();