Added test to expose issue CURATOR-72
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/28b63b0a Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/28b63b0a Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/28b63b0a Branch: refs/heads/master Commit: 28b63b0a1ef5f56d8d40e276b75b7e66a70c335b Parents: d4135ae Author: randgalt <randg...@apache.org> Authored: Sat Nov 9 18:45:41 2013 -0800 Committer: randgalt <randg...@apache.org> Committed: Sat Nov 9 18:45:41 2013 -0800 ---------------------------------------------------------------------- .../framework/imps/TestFrameworkBackground.java | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/28b63b0a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java index 5aa1353..0439e4a 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkBackground.java @@ -25,8 +25,11 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.api.BackgroundCallback; import org.apache.curator.framework.api.CuratorEvent; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.retry.RetryNTimes; import org.apache.curator.retry.RetryOneTime; +import org.apache.curator.test.TestingServer; import org.apache.curator.test.Timing; import org.apache.zookeeper.KeeperException.Code; import org.testng.Assert; @@ -34,11 +37,60 @@ import org.testng.annotations.Test; import java.util.Arrays; import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; public class TestFrameworkBackground extends BaseClassForTests { @Test + public void testListenerConnectedAtStart() throws Exception + { + server.close(); + + Timing timing = new Timing(2); + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(0, 0)); + try + { + client.start(); + + final CountDownLatch connectedLatch = new CountDownLatch(1); + final AtomicBoolean firstListenerAction = new AtomicBoolean(true); + final AtomicReference<ConnectionState> firstListenerState = new AtomicReference<ConnectionState>(); + ConnectionStateListener listener = new ConnectionStateListener() + { + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) + { + if ( firstListenerAction.compareAndSet(true, false) ) { + firstListenerState.set(newState); + System.out.println("First listener state is " + newState); + } + if ( newState == ConnectionState.CONNECTED ) + { + connectedLatch.countDown(); + } + } + }; + client.getConnectionStateListenable().addListener(listener); + + // due to CURATOR-72, this was causing a LOST event to precede the CONNECTED event + client.create().inBackground().forPath("/foo"); + + server = new TestingServer(server.getPort()); + + Assert.assertTrue(timing.awaitLatch(connectedLatch)); + Assert.assertFalse(firstListenerAction.get()); + ConnectionState firstconnectionState = firstListenerState.get(); + Assert.assertEquals(firstconnectionState, ConnectionState.CONNECTED, "First listener state MUST BE CONNECTED but is " + firstconnectionState); + } + finally + { + Closeables.closeQuietly(client); + } + } + + @Test public void testRetries() throws Exception { final int SLEEP = 1000;