@Test
  public void testPortReleaseAfterSSO() throws Exception {
    setupIDP(true, USER_PASS_MODE);
    for (int i=0; i<2; i++) {
      int samlResponsePort = MetaStoreTestUtils.findFreePort();
      try (TestHiveConnection connection = new TestHiveConnection(
          getSamlJdbcConnectionUrl(30, samlResponsePort), new Properties(), USER1,
          USER1_PASSWORD)) {
        assertLoggedInUser(connection, USER1);
        assertEquals(samlResponsePort,
            connection.getBrowserClient().getPort().intValue());
        assertTrue("Port must be released after SSO flow",
            isPortAvailable(samlResponsePort));
        break;
      }
    }
  }
Makes sure that the port is released once the SSO flow completes. The test retries
twice to make sure that there is no flakiness caused by the race between finding
a free port and immediately some other process acquiring that port later before
Browser client could be started on it.
itests/hive-unit/src/test/java/org/apache/hive/service/auth/saml/TestHttpSamlAuthentication.java