Repository: apache/hadoop
File: hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/services/ITestApacheClientConnectionPool.java (Java)

Code before:
  public void testApacheClientFallbackDuringConnectionWarmup()
      throws Exception {
    try (KeepAliveCache keepAliveCache = new KeepAliveCache(
        new AbfsConfiguration(new Configuration(), EMPTY_STRING))) {
      // Create a connection manager with invalid URL to force fallback to JDK client
      // during connection warmup.
      // This is to simulate failure during connection warmup in the connection manager.
      // The invalid URL will cause the connection manager to fail to create connections
      // during warmup, forcing it to fall back to JDK client.
      final AbfsConnectionManager connMgr = new AbfsConnectionManager(
          RegistryBuilder.<ConnectionSocketFactory>create()
              .register(HTTPS_SCHEME, new SSLConnectionSocketFactory(
                  DelegatingSSLSocketFactory.getDefaultFactory(),
                  getDefaultHostnameVerifier()))
              .build(),
          new AbfsHttpClientConnectionFactory(), keepAliveCache,
          new AbfsConfiguration(new Configuration(), EMPTY_STRING),
          new URL("https://test.com"), true);

      Assertions.assertThat(AbfsApacheHttpClient.usable())
          .describedAs("Apache HttpClient should be not usable")
          .isFalse();
      AbfsApacheHttpClient.setUsable();
    }
  }

  private Map.Entry<HttpRoute, AbfsManagedApacheHttpConnection> getTestConnection()
      throws IOException {
    HttpHost host = new HttpHost(getFileSystem().getUri().getHost(),
        getFileSystem().getUri().getPort(),
        HTTPS_SCHEME);
    HttpRoute httpRoute = new HttpRoute(host);

    AbfsManagedApacheHttpConnection conn
        = (AbfsManagedApacheHttpConnection) new AbfsHttpClientConnectionFactory().create(
        httpRoute, null);

    return Tuples.pair(httpRoute, conn);
  }
}


Review comment (suggestion):
Can you make one http call and validate now jdk is being used, user agent can be used for validation

Code after:
   * Test to verify that the ApacheHttpClient falls back to JDK client
   * when connection warmup fails.
   * This test is applicable only for ApacheHttpClient.
   */
  @Test
  public void testApacheClientFallbackDuringConnectionWarmup()
      throws Exception {
    try (KeepAliveCache keepAliveCache = new KeepAliveCache(
        new AbfsConfiguration(new Configuration(), EMPTY_STRING))) {
      // Create a connection manager with invalid URL to force fallback to JDK client
      // during connection warmup.
      // This is to simulate failure during connection warmup in the connection manager.
      // The invalid URL will cause the connection manager to fail to create connections
      // during warmup, forcing it to fall back to JDK client.
      final AbfsConnectionManager connMgr = new AbfsConnectionManager(
          RegistryBuilder.<ConnectionSocketFactory>create()
              .register(HTTPS_SCHEME, new SSLConnectionSocketFactory(
                  DelegatingSSLSocketFactory.getDefaultFactory(),
                  getDefaultHostnameVerifier()))
              .build(),
          new AbfsHttpClientConnectionFactory(), keepAliveCache,
          new AbfsConfiguration(new Configuration(), EMPTY_STRING),
          new URL("https://test.com"), true);

      Assertions.assertThat(AbfsApacheHttpClient.usable())
          .describedAs("Apache HttpClient should be not usable")
          .isFalse();
      // Make a rest API call to verify that the client falls back to JDK client.
      AzureBlobFileSystem fs = getFileSystem();
      verifyClientRequestId(fs, JDK_FALLBACK);
      AbfsApacheHttpClient.setUsable();
      verifyClientRequestId(fs, APACHE_IMPL);
    }
  }

  /**
   * Verify that the client request id contains the expected client.
   * @param fs AzureBlobFileSystem instance
   * @param expectedClient Expected client in the client request id.
   * @throws AzureBlobFileSystemException if any failure occurs during the operation.
   */
  private void verifyClientRequestId(AzureBlobFileSystem fs,
      String expectedClient)
      throws AzureBlobFileSystemException {
    AbfsRestOperation op = fs.getAbfsStore()
        .getClient()
        .getFilesystemProperties(getTestTracingContext(fs, true));
    String[] clientRequestIdList = op.getResult()
        .getClientRequestId().split(COLON);
    Assertions.assertThat(clientRequestIdList[clientRequestIdList.length - 1])
        .describedAs("Http Client in use should be %s", expectedClient)