Search in sources :

Example 71 with Timing

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

the class TestPersistentEphemeralNodeListener method testListenersReconnectedIsOK.

@Test
public void testListenersReconnectedIsOK() throws Exception {
    server.stop();
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        PersistentEphemeralNode 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.restart();
        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);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) 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 72 with Timing

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

the class TestPersistentNode method testQuickCloseNodeExists.

@Test
public void testQuickCloseNodeExists() throws Exception {
    Timing timing = new Timing();
    PersistentNode pen = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/test/one/two");
        pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test/one/two", new byte[0]);
        pen.start();
        pen.close();
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/test/one/two"));
    } finally {
        CloseableUtils.closeQuietly(pen);
        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 73 with Timing

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

the class TestPersistentNode method testBasic.

@Test
public void testBasic() throws Exception {
    final byte[] TEST_DATA = "hey".getBytes();
    Timing timing = new Timing();
    PersistentNode pen = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test", TEST_DATA);
        pen.start();
        Assert.assertTrue(pen.waitForInitialCreate(timing.milliseconds(), TimeUnit.MILLISECONDS));
        // cause session to end - force checks that node is persistent
        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        client.start();
        byte[] bytes = client.getData().forPath("/test");
        Assert.assertEquals(bytes, TEST_DATA);
    } finally {
        CloseableUtils.closeQuietly(pen);
        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 74 with Timing

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

the class BasicTests method testExpiredSession.

@Test
public void testExpiredSession() throws Exception {
    // see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4
    final Timing timing = new Timing();
    final CountDownLatch latch = new CountDownLatch(1);
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.Expired) {
                latch.countDown();
            }
        }
    };
    final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2));
    client.start();
    try {
        final AtomicBoolean firstTime = new AtomicBoolean(true);
        RetryLoop.callWithRetry(client, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                if (firstTime.compareAndSet(true, false)) {
                    try {
                        client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    } catch (KeeperException.NodeExistsException ignore) {
                    // ignore
                    }
                    KillSession.kill(client.getZooKeeper(), server.getConnectString());
                    Assert.assertTrue(timing.awaitLatch(latch));
                }
                ZooKeeper zooKeeper = client.getZooKeeper();
                client.blockUntilConnectedOrTimedOut();
                Assert.assertNotNull(zooKeeper.exists("/foo", false));
                return null;
            }
        });
    } finally {
        client.close();
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZooKeeper(org.apache.zookeeper.ZooKeeper) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 75 with Timing

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

the class TestSessionFailRetryLoop method testRetryStatic.

@Test
public void testRetryStatic() throws Exception {
    Timing timing = new Timing();
    final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
    SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
    retryLoop.start();
    try {
        client.start();
        final AtomicBoolean secondWasDone = new AtomicBoolean(false);
        final AtomicBoolean firstTime = new AtomicBoolean(true);
        SessionFailRetryLoop.callWithRetry(client, SessionFailRetryLoop.Mode.RETRY, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                RetryLoop.callWithRetry(client, new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        if (firstTime.compareAndSet(true, false)) {
                            Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                            KillSession.kill(client.getZooKeeper(), server.getConnectString());
                            client.getZooKeeper();
                            client.blockUntilConnectedOrTimedOut();
                        }
                        Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                        return null;
                    }
                });
                RetryLoop.callWithRetry(client, new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        Assert.assertFalse(firstTime.get());
                        Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                        secondWasDone.set(true);
                        return null;
                    }
                });
                return null;
            }
        });
        Assert.assertTrue(secondWasDone.get());
    } finally {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Callable(java.util.concurrent.Callable) 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