ZooKeeperWatcher zk = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
"testMasterAddressManagerFromZK", null);
ZKUtil.createAndFailSilent(zk, zk.baseZNode);
// Should not have a master yet
MasterAddressTracker addressManager = new MasterAddressTracker(zk, null);
addressManager.start();
assertFalse(addressManager.hasMaster());
zk.registerListener(addressManager);
// Use a listener to capture when the node is actually created
NodeCreationListener listener = new NodeCreationListener(zk, zk.getMasterAddressZNode());
zk.registerListener(listener);
// Create the master node with a dummy address
String host = "localhost";
int port = 1234;
ServerName sn = new ServerName(host, port, System.currentTimeMillis());
LOG.info("Creating master node");
MasterAddressTracker.setMasterAddress(zk, zk.getMasterAddressZNode(), sn);
// Wait for the node to be created
LOG.info("Waiting for master address manager to be notified");
listener.waitForCreation();
LOG.info("Master node created");
assertTrue(addressManager.hasMaster());
ServerName pulledAddress = addressManager.getMasterAddress();
assertTrue(pulledAddress.equals(sn));
}