@Test
  public void testOfflineRegion() throws Exception {
    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
    MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
    TableName table = TableName.valueOf("testRegionNumber");
    try {
      RegionInfo hri;
      byte[] FAMILY = Bytes.toBytes("FAMILY");
      TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(table)
        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
      TEST_UTIL.getAdmin().createTable(tableDescriptor, Bytes.toBytes("A"), Bytes.toBytes("Z"), 5);

      // wait till the table is assigned
      long timeoutTime = EnvironmentEdgeManager.currentTime() + 1000;
      while (true) {
        List<RegionInfo> regions =
          master.getAssignmentManager().getRegionStates().getRegionsOfTable(table);
        if (regions.size() > 3) {
          hri = regions.get(2);
          break;
        }
        long now = EnvironmentEdgeManager.currentTime();
        if (now > timeoutTime) {
          fail("Could not find an online region");
        }
        Thread.sleep(10);
      }

      PairOfSameType<Integer> regionNumberPair = info.getRegionCounts();
      assertEquals(5, regionNumberPair.getFirst().intValue());
      assertEquals(0, regionNumberPair.getSecond().intValue());

      TEST_UTIL.getAdmin().offline(hri.getRegionName());

      timeoutTime = EnvironmentEdgeManager.currentTime() + 800;
      RegionStates regionStates = master.getAssignmentManager().getRegionStates();
      while (true) {
        if (
          regionStates.getRegionByStateOfTable(table).get(RegionState.State.OFFLINE).contains(hri)
        ) {
          break;
        }
        long now = EnvironmentEdgeManager.currentTime();
        if (now > timeoutTime) {
          fail("Failed to offline the region in time");
          break;
        }
        Thread.sleep(10);
      }
      regionNumberPair = info.getRegionCounts();
      assertEquals(4, regionNumberPair.getFirst().intValue());
      assertEquals(1, regionNumberPair.getSecond().intValue());
    } finally {
      TEST_UTIL.deleteTable(table);
    }
  }
tests online and offline region number
hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetricsWrapper.java