@Test
  public void testRebalanceOnRegionServerNumberChange() throws IOException, InterruptedException {
    try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
      Admin admin = connection.getAdmin()) {
      admin.createTable(this.tableDescriptor,
        Arrays.copyOfRange(HBaseTestingUtil.KEYS, 1, HBaseTestingUtil.KEYS.length));
      this.regionLocator = connection.getRegionLocator(this.tableDescriptor.getTableName());

      MetaTableAccessor.fullScanMetaAndPrint(admin.getConnection());

      assertEquals("Test table should have right number of regions", HBaseTestingUtil.KEYS.length,
        this.regionLocator.getStartKeys().length);

      // verify that the region assignments are balanced to start out
      assertRegionsAreBalanced();

      // add a region server - total of 2
      LOG.info("Started second server="
        + UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
      UTIL.getHBaseCluster().getMaster().balance();
      assertRegionsAreBalanced();

      // On a balanced cluster, calling balance() should return true
      BalanceResponse response = UTIL.getHBaseCluster().getMaster().balance();
      assertTrue(response.isBalancerRan());
      assertEquals(0, response.getMovesCalculated());
      assertEquals(0, response.getMovesExecuted());

      // if we add a server, then the balance() call should return true
      // add a region server - total of 3
      LOG.info("Started third server="
        + UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
      waitForAllRegionsAssigned();

      response = UTIL.getHBaseCluster().getMaster().balance();
      assertTrue(response.isBalancerRan());
      assertTrue(response.getMovesCalculated() > 0);
      assertEquals(response.getMovesCalculated(), response.getMovesExecuted());
      assertRegionsAreBalanced();

      // kill a region server - total of 2
      LOG.info("Stopped third server=" + UTIL.getHBaseCluster().stopRegionServer(2, false));
      UTIL.getHBaseCluster().waitOnRegionServer(2);
      waitOnCrashProcessing();
      UTIL.getHBaseCluster().getMaster().balance();
      assertRegionsAreBalanced();

      // start two more region servers - total of 4
      LOG.info("Readding third server="
        + UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
      LOG.info("Added fourth server="
        + UTIL.getHBaseCluster().startRegionServer().getRegionServer().getServerName());
      waitOnCrashProcessing();
      waitForAllRegionsAssigned();

      response = UTIL.getHBaseCluster().getMaster().balance();
      assertTrue(response.isBalancerRan());
      assertTrue(response.getMovesCalculated() > 0);
      assertEquals(response.getMovesCalculated(), response.getMovesExecuted());

      assertRegionsAreBalanced();
      for (int i = 0; i < 6; i++) {
        LOG.info("Adding " + (i + 5) + "th region server");
        UTIL.getHBaseCluster().startRegionServer();
      }
      waitForAllRegionsAssigned();

      response = UTIL.getHBaseCluster().getMaster().balance();
      assertTrue(response.isBalancerRan());
      assertTrue(response.getMovesCalculated() > 0);
      assertEquals(response.getMovesCalculated(), response.getMovesExecuted());

      assertRegionsAreBalanced();
      regionLocator.close();
    }
  }
For HBASE-71. Try a few different configurations of starting and stopping region servers to see
if the assignment or regions is pretty balanced.
hbase-server/src/test/java/org/apache/hadoop/hbase/TestRegionRebalancing.java