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);
}
}
Aggregations