@Test
  public void testRSTerminationAfterRegisteringToMasterBeforeCreatingEphemeralNode()
    throws Exception {
    // Create config to use for this cluster
    Configuration conf = HBaseConfiguration.create();
    conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
    // Start the cluster
    final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(conf);
    TEST_UTIL.startMiniDFSCluster(3);
    TEST_UTIL.startMiniZKCluster();
    TEST_UTIL.createRootDir();
    final LocalHBaseCluster cluster = new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS,
      HMaster.class, RegisterAndDieRegionServer.class);
    final MasterThread master = startMaster(cluster.getMasters().get(0));
    try {
      // Master is up waiting on RegionServers to check in. Now start RegionServers.
      for (int i = 0; i < NUM_RS; i++) {
        cluster.getRegionServers().get(i).start();
      }
      // Expected total regionservers depends on whether Master can host regions or not.
      int expectedTotalRegionServers = NUM_RS;
      List<ServerName> onlineServersList = null;
      do {
        onlineServersList = master.getMaster().getServerManager().getOnlineServersList();
      } while (onlineServersList.size() < expectedTotalRegionServers);
      // Wait until killedRS is set. Means RegionServer is starting to go down.
      while (killedRS.get() == null) {
        Threads.sleep(1);
      }
      // Wait on the RegionServer to fully die.
      while (cluster.getLiveRegionServers().size() >= expectedTotalRegionServers) {
        Threads.sleep(1);
      }
      // Make sure Master is fully up before progressing. Could take a while if regions
      // being reassigned.
      while (!master.getMaster().isInitialized()) {
        Threads.sleep(1);
      }

      // Now in steady state. How many regions open? Master should have too many regionservers
      // showing still. The downed RegionServer should still be showing as registered.
      assertTrue(master.getMaster().getServerManager().isServerOnline(killedRS.get()));
      // Find non-meta region (namespace?) and assign to the killed server. That'll trigger cleanup.
      Map<RegionInfo, ServerName> assignments = null;
      do {
        assignments =
          master.getMaster().getAssignmentManager().getRegionStates().getRegionAssignments();
      } while (assignments == null || assignments.size() < 2);
      RegionInfo hri = null;
      for (Map.Entry<RegionInfo, ServerName> e : assignments.entrySet()) {
        if (e.getKey().isMetaRegion()) continue;
        hri = e.getKey();
        break;
      }
      // Try moving region to the killed server. It will fail. As by-product, we will
      // remove the RS from Master online list because no corresponding znode.
      assertEquals(expectedTotalRegionServers,
        master.getMaster().getServerManager().getOnlineServersList().size());
      LOG.info("Move " + hri.getEncodedName() + " to " + killedRS.get());
      master.getMaster().move(hri.getEncodedNameAsBytes(),
        Bytes.toBytes(killedRS.get().toString()));

      // TODO: This test could do more to verify fix. It could create a table
      // and do round-robin assign. It should fail if zombie RS. HBASE-19515.

      // Wait until the RS no longer shows as registered in Master.
      while (onlineServersList.size() > (NUM_RS + 1)) {
        Thread.sleep(100);
        onlineServersList = master.getMaster().getServerManager().getOnlineServersList();
      }
    } finally {
      // Shutdown is messy with complaints about fs being closed. Why? TODO.
      cluster.shutdown();
      cluster.join();
      TEST_UTIL.shutdownMiniDFSCluster();
      TEST_UTIL.shutdownMiniZKCluster();
      TEST_UTIL.cleanupTestDir();
    }
  }
Test verifies whether a region server is removed from online servers list in master if it went
down after registering with master. Test will TIMEOUT if an error!!!!
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRSKilledWhenInitializing.java