@Test
public void testRaceAtStateChanged() throws Exception
{
LeaderSelector selector = null;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try
{
client.start();
final CountDownLatch takeLeadershipLatch = new CountDownLatch(1);
final CountDownLatch lostLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
LeaderSelectorListener listener = new LeaderSelectorListener()
{
@Override
public void takeLeadership(CuratorFramework client) throws Exception
{
takeLeadershipLatch.countDown(); // should never get here
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState)
{
if ( newState == ConnectionState.RECONNECTED )
{
reconnectedLatch.countDown();
}
else if ( newState == ConnectionState.LOST )
{
lostLatch.countDown();
throw new CancelLeadershipException();
}
}
};
selector = new LeaderSelector(client, "/leader", listener);
CountDownLatch debugLeadershipLatch = new CountDownLatch(1);
CountDownLatch debugLeadershipWaitLatch = new CountDownLatch(1);
selector.debugLeadershipLatch = debugLeadershipLatch;
selector.debugLeadershipWaitLatch = debugLeadershipWaitLatch;
selector.start();
Assert.assertTrue(timing.awaitLatch(debugLeadershipLatch));
server.stop();
Assert.assertTrue(timing.awaitLatch(lostLatch));
timing.sleepABit();
debugLeadershipWaitLatch.countDown();
server = new TestingServer(server.getPort(), server.getTempDirectory());
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
Assert.assertFalse(takeLeadershipLatch.await(3, TimeUnit.SECONDS));
}
finally
{