@Test
  public void testRegionAssignmentAfterMasterRecoveryDueToZKExpiry() throws Exception {
    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
    cluster.startRegionServer();
    cluster.waitForActiveAndReadyMaster(10000);
    HMaster m = cluster.getMaster();
    final ZKWatcher zkw = m.getZooKeeper();
    // now the cluster is up. So assign some regions.
    try (Admin admin = TEST_UTIL.getAdmin()) {
      byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"),
        Bytes.toBytes("c"), Bytes.toBytes("d"), Bytes.toBytes("e"), Bytes.toBytes("f"),
        Bytes.toBytes("g"), Bytes.toBytes("h"), Bytes.toBytes("i"), Bytes.toBytes("j") };
      TableDescriptor htd =
        TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
          .setColumnFamily(ColumnFamilyDescriptorBuilder.of(HConstants.CATALOG_FAMILY)).build();
      admin.createTable(htd, SPLIT_KEYS);
      TEST_UTIL.waitUntilNoRegionsInTransition(60000);
      m.getZooKeeper().close();
      MockLoadBalancer.retainAssignCalled = false;
      final int expectedNumOfListeners = countPermanentListeners(zkw);
      // the master could already been aborted by some background tasks but here we call abort
      // directly to make sure this will happen
      m.abort("Test recovery from zk session expired",
        new KeeperException.SessionExpiredException());
      // it is possible that our abort call above returned earlier because of someone else has
      // already called abort, but it is possible that it has not finished the abort call yet so the
      // isStopped flag is still false, let's wait for sometime.
      TEST_UTIL.waitFor(5000, () -> m.isStopped()); // Master doesn't recover any more

      // The recovered master should not call retainAssignment, as it is not a
      // clean startup.
      assertFalse("Retain assignment should not be called", MockLoadBalancer.retainAssignCalled);
      // number of listeners should be same as the value before master aborted
      // wait for new master is initialized
      cluster.waitForActiveAndReadyMaster(120000);
      final HMaster newMaster = cluster.getMasterThread().getMaster();
      assertEquals(expectedNumOfListeners, countPermanentListeners(newMaster.getZooKeeper()));
    }
  }
Tests that the master does not call retainAssignment after recovery from expired zookeeper
session. Without the HBASE-6046 fix master always tries to assign all the user regions by
calling retainAssignment.
hbase-server/src/test/java/org/apache/hadoop/hbase/TestZooKeeper.java