@Test
public void testBlockUntilActive() throws IOException, KeeperException, InterruptedException {
// the first server, expect to be active master
HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
failoverWatcher1.blockUntilActive();
assertTrue(failoverWatcher1.hasActiveServer());
String activeServer = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher1,
failoverWatcher1.masterZnode));
assertTrue(activeServer.equals(hostPort1.getHostPort()));
// the second server, expect to be backup master
HostPort hostPort2 = new HostPort("127.0.0.1", 22222);
final FailoverWatcher failoverWatcher2 = createFailoverWatcher(hostPort2);
Thread thread2 = new Thread() {
@Override
public void run() {
failoverWatcher2.blockUntilActive();
}
};
thread2.start();
Thread.sleep(500); // wait for second watcher to create znode
List<String> backupMastersList = ZooKeeperUtil.listChildrenAndWatchForNewChildren(
failoverWatcher1, failoverWatcher1.backupServersZnode);
assertTrue(failoverWatcher2.hasActiveServer());
assertTrue(backupMastersList.contains(failoverWatcher2.getHostPort().getHostPort()));
thread2.interrupt();
failoverWatcher1.close();
failoverWatcher2.close();
}