client.start();
client.sync().forPath("/");
final AtomicReference<Exception> error = new AtomicReference<Exception>(null);
final AtomicReference<String> lockNode = new AtomicReference<String>(null);
final Semaphore semaphore = new Semaphore(0);
final CountDownLatch lostLatch = new CountDownLatch(1);
final CountDownLatch internalLostLatch = new CountDownLatch(1);
LeaderSelectorListener listener = new LeaderSelectorListener()
{
@Override
public void takeLeadership(CuratorFramework client) throws Exception
{
try
{
List<String> names = client.getChildren().forPath("/leader");
if ( names.size() != 1 )
{
semaphore.release();
Exception exception = new Exception("Names size isn't 1: " + names.size());
error.set(exception);
return;
}
lockNode.set(names.get(0));
semaphore.release();
if ( !timing.multiple(4).awaitLatch(internalLostLatch) )
{
error.set(new Exception("internalLostLatch await failed"));
}
}
finally
{
lostLatch.countDown();
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
if ( newState == ConnectionState.LOST )
{
internalLostLatch.countDown();
}
}
};
LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
selector.start();
Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
if ( error.get() != null )
{
throw new AssertionError(error.get());
}
Collection<InstanceSpec> instances = cluster.getInstances();
cluster.stop();
Assert.assertTrue(timing.multiple(4).awaitLatch(lostLatch));
timing.sleepABit();
Assert.assertFalse(selector.hasLeadership());
Assert.assertNotNull(lockNode.get());
cluster = new TestingCluster(instances.toArray(new InstanceSpec[instances.size()]));
cluster.start();
try
{
client.delete().forPath(ZKPaths.makePath("/leader", lockNode.get())); // simulate the lock deleting due to session expiration
}
catch ( Exception ignore )
{
// ignore
}
Assert.assertTrue(semaphore.availablePermits() == 0);
Assert.assertFalse(selector.hasLeadership());
selector.requeue();
Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
}