code: public static int findAvailablePort(int maxRetries) {
  
          int retries = 0;
          int randomPort;
          boolean portAvailable;
          do {
              randomPort = randomPort();
              portAvailable = isPortAvailable(randomPort);
              retries++;
          } while (retries <= maxRetries && !portAvailable);
          assumeTrue("no open port found", portAvailable);
          return randomPort;
      }
docs: Finds an available port.
  
  @param maxRetries
  the maximum number of retries before an {@link org.junit.internal .AssumptionViolatedException} is
  thrown.
  
  @return the number of the port that is available