private void removePeerAndWait(String peerId) throws Exception {
    ReplicationPeers rp = manager.getReplicationPeers();
    rp.getPeerStorage().removePeer(peerId);
    manager.removePeer(peerId);
    UTIL.waitFor(20000, () -> {
      if (rp.getPeer(peerId) != null) {
        return false;
      }
      if (manager.getSource(peerId) != null) {
        return false;
      }
      return manager.getOldSources().stream().noneMatch(rs -> rs.getPeerId().equals(peerId));
    });
  }
Remove a peer and wait for it to get cleaned up
hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java