public void testRetry() throws Exception
{
final int MAX_RETRIES = 3;
final int serverPort = server.getPort();
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000, new RetryOneTime(10));
client.start();
try
{
final AtomicInteger retries = new AtomicInteger(0);
final Semaphore semaphore = new Semaphore(0);
client.getZookeeperClient().setRetryPolicy
(
new RetryPolicy()
{
@Override
public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper)
{
semaphore.release();
if ( retries.incrementAndGet() == MAX_RETRIES )
{
try
{
server = new TestingServer(serverPort);
}
catch ( Exception e )
{
throw new Error(e);
}
}
return true;
}
}
);
server.stop();
// test foreground retry
client.checkExists().forPath("/hey");
Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, 10, TimeUnit.SECONDS));
semaphore.drainPermits();
retries.set(0);
server.stop();
// test background retry
client.checkExists().inBackground().forPath("/hey");
Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, 10, TimeUnit.SECONDS));
}
catch ( Throwable e )
{
Assert.fail("Error", e);