}
@Test
public void testHandleMasterNodeChange() throws IOException, KeeperException,
InterruptedException {
HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
failoverWatcher1.blockUntilActive();
// hostPort2 is 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();
// hostPort3 is backup master
HostPort hostPort3 = new HostPort("127.0.0.1", 33333);
final FailoverWatcher failoverWatcher3 = createFailoverWatcher(hostPort3);
Thread thread3 = new Thread() {
@Override
public void run() {
failoverWatcher3.blockUntilActive();
}
};
thread3.start();
// delete the master node, then one of them will become master, the other will be backup master
ZooKeeperUtil.deleteNode(failoverWatcher1, failoverWatcher1.masterZnode);
Thread.sleep(500); // wait for leader election
String masterZnodeData = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher1,
failoverWatcher1.masterZnode));
String backupMasterNode = ZooKeeperUtil.listChildrenAndWatchForNewChildren(failoverWatcher1,
failoverWatcher1.backupServersZnode).get(0);
if (masterZnodeData.equals(hostPort2.getHostPort())) {
if (!backupMasterNode.equals(hostPort3.getHostPort())) {
assert false;
}
} else if (masterZnodeData.equals(hostPort3.getHostPort())) {
if (!backupMasterNode.equals(hostPort2.getHostPort())) {
assert false;
}
} else {
assert false;