Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D184F118B0 for ; Tue, 17 Jun 2014 22:11:58 +0000 (UTC) Received: (qmail 47777 invoked by uid 500); 17 Jun 2014 22:11:58 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 47746 invoked by uid 500); 17 Jun 2014 22:11:58 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 47736 invoked by uid 99); 17 Jun 2014 22:11:58 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Jun 2014 22:11:58 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 81E93981776; Tue, 17 Jun 2014 22:11:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Message-Id: <7074880d3287493bb843472d30227ac7@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: Added testProperCloseWithoutConnectionEstablished Date: Tue, 17 Jun 2014 22:11:58 +0000 (UTC) Repository: curator Updated Branches: refs/heads/CURATOR-110 03e736c66 -> bac23d3af 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/CURATOR-110 Commit: bac23d3af7abf4bf25d5acfca7c5f2cb53b739d0 Parents: 03e736c Author: randgalt Authored: Tue Jun 17 17:11:53 2014 -0500 Committer: randgalt 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 *

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();