Search in sources :

Example 1 with AsyncHttpClientConfig

use of com.ning.http.client.AsyncHttpClientConfig in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method constructor_clears_out_tracing_and_mdc_info_before_building_underlying_client_and_resets_afterward.

@DataProvider(value = { "true   |   true", "true   |   false", "false  |   true", "false  |   false" }, splitBy = "\\|")
@Test
public void constructor_clears_out_tracing_and_mdc_info_before_building_underlying_client_and_resets_afterward(boolean emptyBeforeCall, boolean explode) {
    AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
    AsyncHttpClientConfig.Builder builderMock = mock(AsyncHttpClientConfig.Builder.class);
    List<Span> traceAtTimeOfBuildCall = new ArrayList<>();
    List<Map<String, String>> mdcAtTimeOfBuildCall = new ArrayList<>();
    RuntimeException explodeEx = new RuntimeException("kaboom");
    doAnswer(invocation -> {
        traceAtTimeOfBuildCall.add(Tracer.getInstance().getCurrentSpan());
        mdcAtTimeOfBuildCall.add(MDC.getCopyOfContextMap());
        if (explode)
            throw explodeEx;
        return config;
    }).when(builderMock).build();
    Span spanBeforeCall = (emptyBeforeCall) ? null : Tracer.getInstance().startRequestWithRootSpan("foo");
    Map<String, String> mdcBeforeCall = MDC.getCopyOfContextMap();
    assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanBeforeCall);
    if (emptyBeforeCall)
        assertThat(mdcBeforeCall).isNull();
    else
        assertThat(mdcBeforeCall).isNotEmpty();
    // when
    Throwable ex = catchThrowable(() -> new AsyncHttpClientHelper(builderMock, true));
    // then
    verify(builderMock).build();
    assertThat(traceAtTimeOfBuildCall).hasSize(1);
    assertThat(traceAtTimeOfBuildCall.get(0)).isNull();
    assertThat(mdcAtTimeOfBuildCall).hasSize(1);
    assertThat(mdcAtTimeOfBuildCall.get(0)).isNull();
    assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanBeforeCall);
    assertThat(MDC.getCopyOfContextMap()).isEqualTo(mdcBeforeCall);
    if (explode)
        assertThat(ex).isSameAs(explodeEx);
}
Also used : ArrayList(java.util.ArrayList) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Matchers.anyString(org.mockito.Matchers.anyString) Span(com.nike.wingtips.Span) Map(java.util.Map) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 2 with AsyncHttpClientConfig

use of com.ning.http.client.AsyncHttpClientConfig in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method kitchen_sink_constructor_sets_up_underlying_client_with_expected_config.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void kitchen_sink_constructor_sets_up_underlying_client_with_expected_config(boolean performSubspan) {
    // given
    int customRequestTimeoutVal = 4242;
    AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setRequestTimeout(customRequestTimeoutVal).build();
    AsyncHttpClientConfig.Builder builderMock = mock(AsyncHttpClientConfig.Builder.class);
    doReturn(config).when(builderMock).build();
    // when
    AsyncHttpClientHelper instance = new AsyncHttpClientHelper(builderMock, performSubspan);
    // then
    assertThat(instance.performSubSpanAroundDownstreamCalls).isEqualTo(performSubspan);
    assertThat(instance.asyncHttpClient.getConfig()).isSameAs(config);
    assertThat(instance.asyncHttpClient.getConfig().getRequestTimeout()).isEqualTo(customRequestTimeoutVal);
}
Also used : AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 3 with AsyncHttpClientConfig

use of com.ning.http.client.AsyncHttpClientConfig in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method verifyDefaultUnderlyingClientConfig.

private void verifyDefaultUnderlyingClientConfig(AsyncHttpClientHelper instance) {
    AsyncHttpClientConfig config = instance.asyncHttpClient.getConfig();
    assertThat(config.isAllowPoolingConnections()).isTrue();
    assertThat(config.getMaxRequestRetry()).isEqualTo(0);
    assertThat(config.getRequestTimeout()).isEqualTo(DEFAULT_REQUEST_TIMEOUT_MILLIS);
    assertThat(config.getConnectionTTL()).isEqualTo(DEFAULT_POOLED_DOWNSTREAM_CONNECTION_TTL_MILLIS);
}
Also used : AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig)

Example 4 with AsyncHttpClientConfig

use of com.ning.http.client.AsyncHttpClientConfig in project sonatype-aether by sonatype.

the class AsyncRepositoryConnector method getProvider.

private AsyncHttpProvider getProvider(RepositorySystemSession session, AsyncHttpClientConfig config) {
    String className = ConfigUtils.getString(session, "", "aether.connector.ahc.provider");
    if (className != null && className.length() > 0) {
        if ("netty".equals(className)) {
            className = "com.ning.http.client.providers.netty.NettyAsyncHttpProvider";
        } else if ("jdk".equals(className)) {
            className = "com.ning.http.client.providers.jdk.JDKAsyncHttpProvider";
        } else if ("apache".equals(className)) {
            className = "com.ning.http.client.providers.apache.ApacheAsyncHttpProvider";
        }
        RepositoryCache cache = session.getCache();
        try {
            if (cache == null || cache.get(session, className) == null) {
                logger.debug("Using AHC provider " + className);
                Class<?> providerClass = getClass().getClassLoader().loadClass(className);
                Object inst = providerClass.getConstructor(AsyncHttpClientConfig.class).newInstance(config);
                return (AsyncHttpProvider) inst;
            }
        } catch (LinkageError e) {
            warn("Could not load AHC provider " + className + ", falling back to default", e);
        } catch (ClassNotFoundException e) {
            logger.warn("Could not load AHC provider " + className + ", falling back to default");
        } catch (ClassCastException e) {
            logger.warn("Could not load type-compatible AHC provider " + className + ", falling back to default");
        } catch (Exception e) {
            Throwable cause = e;
            if (e instanceof InvocationTargetException && e.getCause() != null) {
                cause = e.getCause();
            }
            warn("Could not instantiate AHC provider " + className + ", falling back to default", cause);
        }
        if (cache != null) {
            cache.put(session, className, Boolean.TRUE);
        }
    }
    return getDefaultProvider(config);
}
Also used : AsyncHttpProvider(com.ning.http.client.AsyncHttpProvider) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) RepositoryCache(org.sonatype.aether.RepositoryCache) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ChecksumFailureException(org.sonatype.aether.transfer.ChecksumFailureException) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) ConnectException(java.net.ConnectException) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)4 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 Test (org.junit.Test)2 Span (com.nike.wingtips.Span)1 AsyncHttpProvider (com.ning.http.client.AsyncHttpProvider)1 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ConnectException (java.net.ConnectException)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 Matchers.anyString (org.mockito.Matchers.anyString)1 RepositoryCache (org.sonatype.aether.RepositoryCache)1 ArtifactNotFoundException (org.sonatype.aether.transfer.ArtifactNotFoundException)1 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)1 ChecksumFailureException (org.sonatype.aether.transfer.ChecksumFailureException)1 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)1