Java Code Examples for software.amazon.awssdk.http.async.SdkAsyncHttpClient#close()

The following examples show how to use software.amazon.awssdk.http.async.SdkAsyncHttpClient#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void customThreadCountIsRespected() throws Exception {
    final int threadCount = 10;
    ThreadFactory threadFactory = spy(new CustomThreadFactory());
    SdkAsyncHttpClient customClient =
            NettyNioAsyncHttpClient.builder()
                                   .eventLoopGroupBuilder(SdkEventLoopGroup.builder()
                                                                           .threadFactory(threadFactory)
                                                                           .numberOfThreads(threadCount))
                                   .build();

    // Have to make enough requests to prime the threads
    for (int i = 0; i < threadCount + 1; i++) {
        makeSimpleRequest(customClient);
    }
    customClient.close();

    Mockito.verify(threadFactory, times(threadCount)).newThread(Mockito.any());
}
 
Example 2
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void customEventLoopGroup_NotClosedWhenClientIsClosed() throws Exception {

    ThreadFactory threadFactory = spy(new CustomThreadFactory());
    // Cannot use DefaultEventLoopGroupFactory because the concrete
    // implementation it creates is platform-dependent and could be a final
    // (i.e. non-spyable) class.
    EventLoopGroup eventLoopGroup = spy(new NioEventLoopGroup(0, threadFactory));
    SdkAsyncHttpClient customClient =
            NettyNioAsyncHttpClient.builder()
                                   .eventLoopGroup(SdkEventLoopGroup.create(eventLoopGroup, NioSocketChannel::new))
                                   .build();

    makeSimpleRequest(customClient);
    customClient.close();

    Mockito.verify(threadFactory, atLeastOnce()).newThread(Mockito.any());
    Mockito.verify(eventLoopGroup, never()).shutdownGracefully();
}
 
Example 3
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void customChannelFactoryIsUsed() throws Exception {

    ChannelFactory channelFactory = mock(ChannelFactory.class);

    when(channelFactory.newChannel()).thenAnswer((Answer<NioSocketChannel>) invocationOnMock -> new NioSocketChannel());
    EventLoopGroup customEventLoopGroup = new NioEventLoopGroup();

    SdkAsyncHttpClient customClient =
        NettyNioAsyncHttpClient.builder()
                               .eventLoopGroup(SdkEventLoopGroup.create(customEventLoopGroup, channelFactory))
                               .build();

    makeSimpleRequest(customClient);
    customClient.close();

    Mockito.verify(channelFactory, atLeastOnce()).newChannel();
    assertThat(customEventLoopGroup.isShuttingDown()).isFalse();
    customEventLoopGroup.shutdownGracefully().awaitUninterruptibly();
}
 
Example 4
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void closeClient_shouldCloseUnderlyingResources() {
    SdkEventLoopGroup eventLoopGroup = SdkEventLoopGroup.builder().build();
    ChannelPool channelPool = mock(ChannelPool.class);
    SdkChannelPoolMap<URI, ChannelPool> sdkChannelPoolMap = new SdkChannelPoolMap<URI, ChannelPool>() {
        @Override
        protected ChannelPool newPool(URI key) {
            return channelPool;
        }
    };

    sdkChannelPoolMap.get(URI.create("http://blah"));
    NettyConfiguration nettyConfiguration = new NettyConfiguration(AttributeMap.empty());

    SdkAsyncHttpClient customerClient =
        new NettyNioAsyncHttpClient(eventLoopGroup, sdkChannelPoolMap, nettyConfiguration);

    customerClient.close();
    assertThat(eventLoopGroup.eventLoopGroup().isShuttingDown()).isTrue();
    assertThat(eventLoopGroup.eventLoopGroup().isTerminated()).isTrue();
    assertThat(sdkChannelPoolMap).isEmpty();
    Mockito.verify(channelPool).close();
}
 
Example 5
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionMessageChanged_WhenPendingAcquireQueueIsFull() throws Exception {
    String expectedErrorMsg = "Maximum pending connection acquisitions exceeded.";

    SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder()
                                                             .maxConcurrency(1)
                                                             .maxPendingConnectionAcquires(1)
                                                             .build();

    List<CompletableFuture<Void>> futures = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        futures.add(makeSimpleRequestAndReturnResponseHandler(customClient).completeFuture);
    }

    assertThatThrownBy(() -> CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join())
        .hasMessageContaining(expectedErrorMsg);

    customClient.close();
}
 
Example 6
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionMessageChanged_WhenConnectionTimeoutErrorEncountered() throws Exception {
    String expectedErrorMsg = "Acquire operation took longer than the configured maximum time. This indicates that a request "
                              + "cannot get a connection from the pool within the specified maximum time.";

    SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder()
                                                             .maxConcurrency(1)
                                                             .connectionTimeout(Duration.ofMillis(1))
                                                             .connectionAcquisitionTimeout(Duration.ofMillis(1))
                                                             .build();

    List<CompletableFuture<Void>> futures = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        futures.add(makeSimpleRequestAndReturnResponseHandler(customClient).completeFuture);
    }

    assertThatThrownBy(() -> CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join())
        .hasMessageContaining(expectedErrorMsg);

    customClient.close();
}
 
Example 7
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void customFactoryIsUsed() throws Exception {
    ThreadFactory threadFactory = spy(new CustomThreadFactory());
    SdkAsyncHttpClient customClient =
        NettyNioAsyncHttpClient.builder()
                               .eventLoopGroupBuilder(SdkEventLoopGroup.builder()
                                                                       .threadFactory(threadFactory))
                               .build();

    makeSimpleRequest(customClient);
    customClient.close();

    Mockito.verify(threadFactory, atLeastOnce()).newThread(Mockito.any());
}
 
Example 8
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void closeMethodClosesOpenedChannels() throws InterruptedException, TimeoutException, ExecutionException {
    String body = randomAlphabetic(10);
    URI uri = URI.create("https://localhost:" + mockServer.httpsPort());
    stubFor(any(urlPathEqualTo("/")).willReturn(aResponse().withHeader("Some-Header", "With Value").withBody(body)));

    SdkHttpFullRequest request = createRequest(uri, "/", body, SdkHttpMethod.POST, Collections.emptyMap());
    RecordingResponseHandler recorder = new RecordingResponseHandler();

    CompletableFuture<Boolean> channelClosedFuture = new CompletableFuture<>();
    ChannelFactory<NioSocketChannel> channelFactory = new ChannelFactory<NioSocketChannel>() {
        @Override
        public NioSocketChannel newChannel() {
            return new NioSocketChannel() {
                @Override
                public ChannelFuture close() {
                    ChannelFuture cf = super.close();
                    channelClosedFuture.complete(true);
                    return cf;
                }
            };
        }
    };

    SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder()
            .eventLoopGroup(new SdkEventLoopGroup(new NioEventLoopGroup(1), channelFactory))
            .buildWithDefaults(mapWithTrustAllCerts());

    try {
        customClient.execute(AsyncExecuteRequest.builder()
                .request(request)
                .requestContentPublisher(createProvider(body))
                .responseHandler(recorder).build())
                .join();
    } finally {
        customClient.close();
    }

    assertThat(channelClosedFuture.get(5, TimeUnit.SECONDS)).isTrue();
}
 
Example 9
Source File: NettyNioAsyncHttpClientWireMockTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void createNettyClient_ReadWriteTimeoutCanBeZero() throws Exception {
    SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder()
            .readTimeout(Duration.ZERO)
            .writeTimeout(Duration.ZERO)
            .build();

    makeSimpleRequest(customClient);

    customClient.close();
}