Search in sources :

Example 1 with RetrySleeper

use of org.apache.curator.RetrySleeper in project xian by happyyangyuan.

the class TestFrameworkEdges method testRetry.

@Test
public void testRetry() throws Exception {
    final int MAX_RETRIES = 3;
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(10));
    client.start();
    try {
        final AtomicInteger retries = new AtomicInteger(0);
        final Semaphore semaphore = new Semaphore(0);
        RetryPolicy policy = new RetryPolicy() {

            @Override
            public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) {
                semaphore.release();
                if (retries.incrementAndGet() == MAX_RETRIES) {
                    try {
                        server.restart();
                    } catch (Exception e) {
                        throw new Error(e);
                    }
                }
                try {
                    sleeper.sleepFor(100, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                return true;
            }
        };
        client.getZookeeperClient().setRetryPolicy(policy);
        server.stop();
        // test foreground retry
        client.checkExists().forPath("/hey");
        Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, timing.forWaiting().seconds(), TimeUnit.SECONDS), "Remaining leases: " + semaphore.availablePermits());
        // make sure we're reconnected
        client.getZookeeperClient().setRetryPolicy(new RetryOneTime(100));
        client.checkExists().forPath("/hey");
        client.getZookeeperClient().setRetryPolicy(policy);
        semaphore.drainPermits();
        retries.set(0);
        server.stop();
        // test background retry
        client.checkExists().inBackground().forPath("/hey");
        Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, timing.forWaiting().seconds(), TimeUnit.SECONDS), "Remaining leases: " + semaphore.availablePermits());
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) RetrySleeper(org.apache.curator.RetrySleeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Semaphore(java.util.concurrent.Semaphore) RetryPolicy(org.apache.curator.RetryPolicy) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Aggregations

Semaphore (java.util.concurrent.Semaphore)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RetryPolicy (org.apache.curator.RetryPolicy)1 RetrySleeper (org.apache.curator.RetrySleeper)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 RetryOneTime (org.apache.curator.retry.RetryOneTime)1 KeeperException (org.apache.zookeeper.KeeperException)1 Test (org.testng.annotations.Test)1