use of org.apache.curator.framework.recipes.leader.LeaderSelectorListener in project xian by happyyangyuan.
the class TestResetConnectionWithBackgroundFailure method testConnectionStateListener.
@Test
public void testConnectionStateListener() throws Exception {
server.stop();
LeaderSelector selector = null;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
timing.sleepABit();
LeaderSelectorListener listenerLeader = new LeaderSelectorListenerAdapter() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
Thread.currentThread().join();
}
};
selector = new LeaderSelector(client, "/leader", listenerLeader);
selector.autoRequeue();
selector.start();
final BlockingQueue<ConnectionState> listenerSequence = Queues.newLinkedBlockingQueue();
ConnectionStateListener listener1 = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
listenerSequence.add(newState);
}
};
Timing forWaiting = timing.forWaiting();
client.getConnectionStateListenable().addListener(listener1);
log.debug("Starting ZK server");
server.restart();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
log.debug("Stopping ZK server");
server.stop();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
log.debug("Starting ZK server");
server.restart();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
log.debug("Stopping ZK server");
server.close();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.recipes.leader.LeaderSelectorListener in project xian by happyyangyuan.
the class TestReaper method testUsingManualLeader.
@Test
public void testUsingManualLeader() throws Exception {
final Timing timing = new Timing();
final CuratorFramework client = makeClient(timing, null);
final CountDownLatch latch = new CountDownLatch(1);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
Reaper reaper = new Reaper(client, 1);
try {
reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_DELETE);
reaper.start();
timing.sleepABit();
latch.countDown();
} finally {
CloseableUtils.closeQuietly(reaper);
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
};
LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
try {
client.start();
client.create().creatingParentsIfNeeded().forPath("/one/two/three");
Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
selector.start();
timing.awaitLatch(latch);
Assert.assertNull(client.checkExists().forPath("/one/two/three"));
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
Aggregations