use of com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader in project commons by twitter.
the class SingletonServiceTest method testLeaderDisconnect.
@Test
public void testLeaderDisconnect() throws Exception {
Capture<LeaderControl> controlCapture = createCapture();
CountDownLatch leading = new CountDownLatch(1);
listener.onLeading(capture(controlCapture));
expectLastCall().andAnswer(countDownAnswer(leading));
CountDownLatch defeated = new CountDownLatch(1);
listener.onDefeated(null);
expectLastCall().andAnswer(countDownAnswer(defeated));
control.replay();
ZooKeeperClient zkClient = createZkClient();
serverSet = new ServerSetImpl(zkClient, "/fake/path");
candidate = new CandidateImpl(new Group(zkClient, ZooKeeperUtils.OPEN_ACL_UNSAFE, "/fake/path"));
DefeatOnDisconnectLeader leader = new DefeatOnDisconnectLeader(zkClient, listener);
service = new SingletonService(serverSet, candidate);
service.lead(InetSocketAddress.createUnresolved("foo", PORT_A), ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo", PORT_B)), leader);
leading.await();
shutdownNetwork();
defeated.await();
}
use of com.twitter.common.zookeeper.SingletonService.DefeatOnDisconnectLeader in project commons by twitter.
the class SingletonServiceTest method testNonLeaderDisconnect.
@Test
public void testNonLeaderDisconnect() throws Exception {
CountDownLatch elected = new CountDownLatch(1);
listener.onLeading(EasyMock.<LeaderControl>anyObject());
expectLastCall().andAnswer(countDownAnswer(elected));
listener.onDefeated(null);
expectLastCall().anyTimes();
control.replay();
ZooKeeperClient zkClient = createZkClient();
String path = "/fake/path";
// Create a fake leading candidate node to ensure that the leader in this test is never
// elected.
ZooKeeperUtils.ensurePath(zkClient, ZooKeeperUtils.OPEN_ACL_UNSAFE, path);
String leaderNode = zkClient.get().create(path + "/" + SingletonService.LEADER_ELECT_NODE_PREFIX, "fake_leader".getBytes(), ZooKeeperUtils.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
serverSet = new ServerSetImpl(zkClient, path);
candidate = SingletonService.createSingletonCandidate(zkClient, path, ZooKeeperUtils.OPEN_ACL_UNSAFE);
DefeatOnDisconnectLeader leader = new DefeatOnDisconnectLeader(zkClient, listener);
service = new SingletonService(serverSet, candidate);
service.lead(InetSocketAddress.createUnresolved("foo", PORT_A), ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo", PORT_B)), leader);
final CountDownLatch disconnected = new CountDownLatch(1);
zkClient.register(new Watcher() {
@Override
public void process(WatchedEvent event) {
if ((event.getType() == EventType.None) && (event.getState() == KeeperState.Disconnected)) {
disconnected.countDown();
}
}
});
shutdownNetwork();
disconnected.await();
restartNetwork();
zkClient.get().delete(leaderNode, ZooKeeperUtils.ANY_VERSION);
// Upon deletion of the fake leader node, the candidate should become leader.
elected.await();
}
Aggregations