final String PATH = "/foo/bar/lock";
ExecutorService executorService = Executors.newFixedThreadPool(QTY);
ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
final Timing timing = new Timing();
TestingCluster cluster = new TestingCluster(3);
List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
try
{
cluster.start();
final AtomicInteger opCount = new AtomicInteger(0);
for ( int i = 0; i < QTY; ++i )
{
SemaphoreClient semaphoreClient = new SemaphoreClient
(
cluster.getConnectString(),
PATH,
new Callable<Void>()
{
@Override
public Void call() throws Exception
{
opCount.incrementAndGet();
Thread.sleep(OPERATION_TIME_MS);
return null;
}
}
);
completionService.submit(semaphoreClient);
semaphoreClients.add(semaphoreClient);
}
timing.forWaiting().sleepABit();
Assert.assertNotNull(SemaphoreClient.getActiveClient());
final CountDownLatch latch = new CountDownLatch(1);
CuratorFramework client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
ConnectionStateListener listener = new ConnectionStateListener()
{
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
if ( newState == ConnectionState.LOST )
{
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
client.start();
try
{
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
cluster.stop();
latch.await();
}
finally
{
CloseableUtils.closeQuietly(client);
}
long startTicks = System.currentTimeMillis();
for(;;)
{
int thisOpCount = opCount.get();
Thread.sleep(2 * OPERATION_TIME_MS);
if ( thisOpCount == opCount.get() )
{
break; // checking that the op count isn't increasing
}
Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
}
int thisOpCount = opCount.get();
Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
cluster = new TestingCluster(iterator.next(), iterator.next());
cluster.start();
timing.forWaiting().sleepABit();
startTicks = System.currentTimeMillis();
for(;;)
{