static SimpleKdcServer getRunningSimpleKdcServer(File testDir,
    Supplier<Integer> randomPortGenerator, final boolean portClash)
    throws KrbException, IOException {
    File kdcDir = new File(testDir, SimpleKdcServer.class.getSimpleName());
    Preconditions.checkArgument(kdcDir.mkdirs(), "Failed create of " + kdcDir);
    String hostName = InetAddress.getLoopbackAddress().getHostName();
    BoundSocketMaker bsm = portClash ? new BoundSocketMaker(randomPortGenerator) : null;
    final int retries = 10;
    for (int i = 0; i < retries; i++) {
      SimpleKdcServer kdc = new SimpleKdcServer();
      kdc.setWorkDir(kdcDir);
      kdc.setKdcHost(hostName);
      kdc.setAllowTcp(true);
      kdc.setAllowUdp(false);
      int kdcPort = bsm != null ? bsm.getPort() : randomPortGenerator.get();
      try {
        kdc.setKdcTcpPort(kdcPort);
        LOG.info("Starting KDC server at {}:{}", hostName, kdcPort);
        kdc.init();
        kdc.start();
        return kdc;
      } catch (KrbException ke) {
        if (kdc != null) {
          kdc.stop();
        }
        // new kerby just eats the cause exception so we can not test for BindException any more.
        // the only way is to always retry...
        LOG.info("Failed to init/start kdc server, retry = {}", i, ke);
      } finally {
        if (bsm != null) {
          bsm.close();
          bsm = null;
        }
      }
    }
    // If we get here, we exhausted our retries. Fail.
    throw new KrbException("Failed create of SimpleKdcServer after " + retries + " attempts");
  }
Internal method for testing.
@param portClash True if we want to generate BindException (for testing).
@return A running SimpleKdcServer on loopback/'localhost' on a random port
@see #getRunningSimpleKdcServer(File, Supplier)
hbase-common/src/test/java/org/apache/hadoop/hbase/util/SimpleKdcServerUtil.java