private void assertRegionsAreBalanced() throws IOException {
    // TODO: Fix this test. Old balancer used to run with 'slop'. New
    // balancer does not.
    boolean success = false;
    float slop = (float) UTIL.getConfiguration().getFloat("hbase.regions.slop", 0.1f);
    if (slop <= 0) slop = 1;

    for (int i = 0; i < 5; i++) {
      success = true;
      // make sure all the regions are reassigned before we test balance
      waitForAllRegionsAssigned();

      long regionCount = UTIL.getMiniHBaseCluster().countServedRegions();
      List<HRegionServer> servers = getOnlineRegionServers();
      double avg = (double) regionCount / (double) servers.size();
      int avgLoadPlusSlop = (int) Math.ceil(avg * (1 + slop));
      int avgLoadMinusSlop = (int) Math.floor(avg * (1 - slop)) - 1;
      // Increase the margin a little to accommodate StochasticLoadBalancer
      if (this.balancerName.contains("StochasticLoadBalancer")) {
        avgLoadPlusSlop++;
        avgLoadMinusSlop--;
      }
      LOG.debug("There are " + servers.size() + " servers and " + regionCount
        + " regions. Load Average: " + avg + " low border: " + avgLoadMinusSlop + ", up border: "
        + avgLoadPlusSlop + "; attempt: " + i);

      for (HRegionServer server : servers) {
        int serverLoad = ProtobufUtil.getOnlineRegions(server.getRSRpcServices()).size();
        LOG.debug(server.getServerName() + " Avg: " + avg + " actual: " + serverLoad);
        if (!(avg > 2.0 && serverLoad <= avgLoadPlusSlop && serverLoad >= avgLoadMinusSlop)) {
          for (RegionInfo hri : ProtobufUtil.getOnlineRegions(server.getRSRpcServices())) {
            if (hri.isMetaRegion()) serverLoad--;
            // LOG.debug(hri.getRegionNameAsString());
          }
          if (!(serverLoad <= avgLoadPlusSlop && serverLoad >= avgLoadMinusSlop)) {
            LOG.debug(server.getServerName() + " Isn't balanced!!! Avg: " + avg + " actual: "
              + serverLoad + " slop: " + slop);
            success = false;
            break;
          }
        }
      }

      if (!success) {
        // one or more servers are not balanced. sleep a little to give it a
        // chance to catch up. then, go back to the retry loop.
        try {
          Thread.sleep(10000);
        } catch (InterruptedException e) {
        }

        UTIL.getHBaseCluster().getMaster().balance();
        continue;
      }

      // if we get here, all servers were balanced, so we should just return.
      return;
    }
    // if we get here, we tried 5 times and never got to short circuit out of
    // the retry loop, so this is a failure.
    fail("After 5 attempts, region assignments were not balanced.");
  }
Determine if regions are balanced. Figure out the total, divide by the number of online
servers, then test if each server is +/- 1 of average rounded up.
hbase-server/src/test/java/org/apache/hadoop/hbase/TestRegionRebalancing.java