Search in sources :

Example 21 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project elastic-job by dangdangdotcom.

the class JobNodeStorageTest method assertAddConnectionStateListener.

@Test
public void assertAddConnectionStateListener() {
    CuratorFramework client = mock(CuratorFramework.class);
    @SuppressWarnings("unchecked") Listenable<ConnectionStateListener> listeners = mock(Listenable.class);
    ConnectionStateListener listener = mock(ConnectionStateListener.class);
    when(client.getConnectionStateListenable()).thenReturn(listeners);
    when(regCenter.getRawClient()).thenReturn(client);
    jobNodeStorage.addConnectionStateListener(listener);
    verify(listeners).addListener(listener);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.junit.Test)

Example 22 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestFrameworkEdges method testReconnectAfterLoss.

@Test
public void testReconnectAfterLoss() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch lostLatch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    lostLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.checkExists().forPath("/");
        server.stop();
        Assert.assertTrue(timing.awaitLatch(lostLatch));
        try {
            client.checkExists().forPath("/");
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // correct
        }
        server.restart();
        client.checkExists().forPath("/");
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 23 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestNeverConnected method testNeverConnected.

@Test
public void testNeverConnected() throws Exception {
    Timing timing = new Timing();
    // use a connection string to a non-existent server
    CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:1111", 100, 100, new RetryOneTime(1));
    try {
        final BlockingQueue<ConnectionState> queue = Queues.newLinkedBlockingQueue();
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState state) {
                queue.add(state);
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.start();
        client.create().inBackground().forPath("/");
        ConnectionState polled = queue.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertEquals(polled, ConnectionState.SUSPENDED);
        polled = queue.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertEquals(polled, ConnectionState.LOST);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 24 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestBackgroundStates method testConnectionStateListener.

@Test
public void testConnectionStateListener() throws Exception {
    server.close();
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(timing.milliseconds()));
    try {
        client.start();
        final BlockingQueue<ConnectionState> stateVector = Queues.newLinkedBlockingQueue(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                stateVector.offer(newState);
            }
        };
        Timing waitingTiming = timing.forWaiting();
        client.getConnectionStateListenable().addListener(listener);
        server = new TestingServer(server.getPort());
        Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
        server.stop();
        Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
        server.restart();
        Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
        server.close();
        Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 25 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestBackgroundStates method testListenersReconnectedIsOK.

@Test
public void testListenersReconnectedIsOK() throws Exception {
    server.close();
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    PersistentEphemeralNode node = null;
    try {
        client.start();
        node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
        node.start();
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        final AtomicReference<ConnectionState> lastState = new AtomicReference<ConnectionState>();
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                lastState.set(newState);
                if (newState == ConnectionState.CONNECTED) {
                    connectedLatch.countDown();
                }
                if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        timing.sleepABit();
        server = new TestingServer(server.getPort());
        Assert.assertTrue(timing.awaitLatch(connectedLatch));
        timing.sleepABit();
        Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
        server.restart();
        timing.sleepABit();
        Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
        timing.sleepABit();
        Assert.assertEquals(lastState.get(), ConnectionState.RECONNECTED);
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(node);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) PersistentEphemeralNode(org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)37 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)37 ConnectionState (org.apache.curator.framework.state.ConnectionState)36 Test (org.testng.annotations.Test)29 Timing (org.apache.curator.test.Timing)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 RetryOneTime (org.apache.curator.retry.RetryOneTime)20 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)9 KeeperException (org.apache.zookeeper.KeeperException)9 TestingCluster (org.apache.curator.test.TestingCluster)5 TestingServer (org.apache.curator.test.TestingServer)5 IOException (java.io.IOException)4 Semaphore (java.util.concurrent.Semaphore)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 InstanceSpec (org.apache.curator.test.InstanceSpec)4 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)3 ExecutorService (java.util.concurrent.ExecutorService)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2