@Test
  public void testBackupMasterUpdates() throws Exception {
    Configuration conf = TEST_UTIL.getConfiguration();
    try (ZKWatcher zk = new ZKWatcher(conf, "testBackupMasterUpdates", null, true)) {
      ServerName sn1 = ServerName.valueOf("localhost", 1, -1);
      DummyMaster master1 = new DummyMaster(zk, sn1);
      ActiveMasterManager activeMasterManager = master1.getActiveMasterManager();
      activeMasterManager.blockUntilBecomingActiveMaster(100, mockTaskGroup());
      assertEquals(sn1, activeMasterManager.getActiveMasterServerName().get());
      assertEquals(0, activeMasterManager.getBackupMasters().size());
      // Add backup masters
      List<String> backupZNodes = new ArrayList<>();
      for (int i = 1; i <= 10; i++) {
        ServerName backupSn = ServerName.valueOf("localhost", 1000 + i, -1);
        String backupZn =
          ZNodePaths.joinZNode(zk.getZNodePaths().backupMasterAddressesZNode, backupSn.toString());
        backupZNodes.add(backupZn);
        MasterAddressTracker.setMasterAddress(zk, backupZn, backupSn, 1234);
        TEST_UTIL.waitFor(10000,
          () -> activeMasterManager.getBackupMasters().size() == backupZNodes.size());
      }
      // Remove backup masters
      int numBackups = backupZNodes.size();
      for (String backupZNode : backupZNodes) {
        ZKUtil.deleteNode(zk, backupZNode);
        final int currentBackups = --numBackups;
        TEST_UTIL.waitFor(10000,
          () -> activeMasterManager.getBackupMasters().size() == currentBackups);
      }
    }
  }
Unit tests that uses ZooKeeper but does not use the master-side methods but rather acts
directly on ZK.
hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestActiveMasterManager.java