Search in sources :

Example 86 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestWatcherIdentity method testCuratorWatcher.

@Test
public void testCuratorWatcher() throws Exception {
    Timing timing = new Timing();
    CountCuratorWatcher watcher = new CountCuratorWatcher();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        client.create().forPath(PATH);
        // Add twice the same watcher on the same path
        client.getData().usingWatcher(watcher).forPath(PATH);
        client.getData().usingWatcher(watcher).forPath(PATH);
        // Ok, let's test it
        client.setData().forPath(PATH, new byte[] {});
        timing.sleepABit();
        Assert.assertEquals(1, watcher.count.get());
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 87 with Timing

use of org.apache.curator.test.Timing 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 88 with Timing

use of org.apache.curator.test.Timing 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)

Example 89 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestResetConnectionWithBackgroundFailure method testConnectionStateListener.

@Test
public void testConnectionStateListener() throws Exception {
    server.stop();
    LeaderSelector selector = null;
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        timing.sleepABit();
        LeaderSelectorListener listenerLeader = new LeaderSelectorListenerAdapter() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                Thread.currentThread().join();
            }
        };
        selector = new LeaderSelector(client, "/leader", listenerLeader);
        selector.autoRequeue();
        selector.start();
        final BlockingQueue<ConnectionState> listenerSequence = Queues.newLinkedBlockingQueue();
        ConnectionStateListener listener1 = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                listenerSequence.add(newState);
            }
        };
        Timing forWaiting = timing.forWaiting();
        client.getConnectionStateListenable().addListener(listener1);
        log.debug("Starting ZK server");
        server.restart();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
        log.debug("Stopping ZK server");
        server.stop();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
        log.debug("Starting ZK server");
        server.restart();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
        log.debug("Stopping ZK server");
        server.close();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) Timing(org.apache.curator.test.Timing) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 90 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestBlockUntilConnected method testBlockUntilConnectedCurrentlyConnected.

/**
 * Test the case where we're already connected
 */
@Test
public void testBlockUntilConnectedCurrentlyConnected() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
    try {
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState.isConnected()) {
                    connectedLatch.countDown();
                }
            }
        });
        client.start();
        Assert.assertTrue(timing.awaitLatch(connectedLatch), "Timed out awaiting latch");
        Assert.assertTrue(client.blockUntilConnected(1, TimeUnit.SECONDS), "Not connected");
    } catch (InterruptedException e) {
        Assert.fail("Unexpected interruption");
    } 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) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

Timing (org.apache.curator.test.Timing)166 CuratorFramework (org.apache.curator.framework.CuratorFramework)147 Test (org.testng.annotations.Test)138 RetryOneTime (org.apache.curator.retry.RetryOneTime)129 CountDownLatch (java.util.concurrent.CountDownLatch)56 ConnectionState (org.apache.curator.framework.state.ConnectionState)39 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)29 KeeperException (org.apache.zookeeper.KeeperException)28 ExecutorService (java.util.concurrent.ExecutorService)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Semaphore (java.util.concurrent.Semaphore)18 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)16 TestingServer (org.apache.curator.test.TestingServer)15 Stat (org.apache.zookeeper.data.Stat)12 TestingCluster (org.apache.curator.test.TestingCluster)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 InstanceSpec (org.apache.curator.test.InstanceSpec)9 Test (org.junit.Test)9