Search in sources :

Example 1 with PROTOCOL_FUTURE

use of software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE in project aws-sdk-java-v2 by aws.

the class NettyRequestExecutorTest method cancelExecuteFuture_channelAcquired_submitsRunnable.

@Test
public void cancelExecuteFuture_channelAcquired_submitsRunnable() throws InterruptedException {
    EventLoop mockEventLoop = mock(EventLoop.class);
    Channel mockChannel = mock(Channel.class);
    ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
    when(mockChannel.pipeline()).thenReturn(mockPipeline);
    when(mockChannel.eventLoop()).thenReturn(mockEventLoop);
    when(mockChannel.isActive()).thenReturn(true);
    Attribute<Boolean> mockInUseAttr = mock(Attribute.class);
    when(mockInUseAttr.get()).thenReturn(Boolean.TRUE);
    CompletableFuture<Protocol> protocolFuture = CompletableFuture.completedFuture(Protocol.HTTP1_1);
    Attribute<CompletableFuture<Protocol>> mockProtocolFutureAttr = mock(Attribute.class);
    when(mockProtocolFutureAttr.get()).thenReturn(protocolFuture);
    when(mockChannel.attr(any(AttributeKey.class))).thenAnswer(i -> {
        AttributeKey argumentAt = i.getArgument(0, AttributeKey.class);
        if (argumentAt == IN_USE) {
            return mockInUseAttr;
        }
        if (argumentAt == PROTOCOL_FUTURE) {
            return mockProtocolFutureAttr;
        }
        return mock(Attribute.class);
    });
    when(mockChannel.writeAndFlush(any(Object.class))).thenReturn(new DefaultChannelPromise(mockChannel));
    ChannelConfig mockChannelConfig = mock(ChannelConfig.class);
    when(mockChannel.config()).thenReturn(mockChannelConfig);
    CountDownLatch submitLatch = new CountDownLatch(1);
    when(mockEventLoop.submit(any(Runnable.class))).thenAnswer(i -> {
        i.getArgument(0, Runnable.class).run();
        // Need to wait until the first submit() happens which sets up the channel before cancelling the future.
        submitLatch.countDown();
        return null;
    });
    when(mockChannelPool.acquire(any(Promise.class))).thenAnswer((Answer<Promise>) invocationOnMock -> {
        Promise p = invocationOnMock.getArgument(0, Promise.class);
        p.setSuccess(mockChannel);
        return p;
    });
    CountDownLatch exceptionFiredLatch = new CountDownLatch(1);
    when(mockPipeline.fireExceptionCaught(any(FutureCancelledException.class))).thenAnswer(i -> {
        exceptionFiredLatch.countDown();
        return mockPipeline;
    });
    CompletableFuture<Void> executeFuture = nettyRequestExecutor.execute();
    submitLatch.await(1, TimeUnit.SECONDS);
    executeFuture.cancel(true);
    exceptionFiredLatch.await(1, TimeUnit.SECONDS);
    verify(mockEventLoop, times(2)).submit(any(Runnable.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AttributeKey(io.netty.util.AttributeKey) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Protocol(software.amazon.awssdk.http.Protocol) CompletableFuture(java.util.concurrent.CompletableFuture) AttributeMap(software.amazon.awssdk.utils.AttributeMap) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) IN_USE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.IN_USE) Duration(java.time.Duration) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Attribute(io.netty.util.Attribute) EventLoopGroup(io.netty.channel.EventLoopGroup) Promise(io.netty.util.concurrent.Promise) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoop(io.netty.channel.EventLoop) WRITE_TIMEOUT(software.amazon.awssdk.http.SdkHttpConfigurationOption.WRITE_TIMEOUT) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Mockito.verify(org.mockito.Mockito.verify) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) PROTOCOL_FUTURE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) ChannelConfig(io.netty.channel.ChannelConfig) Mockito.mock(org.mockito.Mockito.mock) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelPipeline(io.netty.channel.ChannelPipeline) AttributeKey(io.netty.util.AttributeKey) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Promise(io.netty.util.concurrent.Promise) CompletableFuture(java.util.concurrent.CompletableFuture) EventLoop(io.netty.channel.EventLoop) ChannelConfig(io.netty.channel.ChannelConfig) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Protocol(software.amazon.awssdk.http.Protocol) Test(org.junit.jupiter.api.Test)

Example 2 with PROTOCOL_FUTURE

use of software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE in project aws-sdk-java-v2 by aws.

the class Http2MultiplexedChannelPool method acquireStreamOnNewConnection.

private void acquireStreamOnNewConnection(Promise<Channel> promise) {
    Future<Channel> newConnectionAcquire = connectionPool.acquire();
    newConnectionAcquire.addListener(f -> {
        if (!newConnectionAcquire.isSuccess()) {
            promise.setFailure(newConnectionAcquire.cause());
            return;
        }
        Channel parentChannel = newConnectionAcquire.getNow();
        try {
            parentChannel.attr(HTTP2_MULTIPLEXED_CHANNEL_POOL).set(this);
            // When the protocol future is completed on the new connection, we're ready for new streams to be added to it.
            parentChannel.attr(PROTOCOL_FUTURE).get().thenAccept(protocol -> acquireStreamOnFreshConnection(promise, parentChannel, protocol)).exceptionally(throwable -> failAndCloseParent(promise, parentChannel, throwable));
        } catch (Throwable e) {
            failAndCloseParent(promise, parentChannel, e);
        }
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) HTTP2_MULTIPLEXED_CHANNEL_POOL(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.HTTP2_MULTIPLEXED_CHANNEL_POOL) Protocol(software.amazon.awssdk.http.Protocol) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) BetterFixedChannelPool(software.amazon.awssdk.http.nio.netty.internal.utils.BetterFixedChannelPool) NettyUtils.doInEventLoop(software.amazon.awssdk.http.nio.netty.internal.utils.NettyUtils.doInEventLoop) MAX_CONCURRENT_STREAMS(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.MAX_CONCURRENT_STREAMS) Sharable(io.netty.channel.ChannelHandler.Sharable) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HTTP2_INITIAL_WINDOW_SIZE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.HTTP2_INITIAL_WINDOW_SIZE) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Duration(java.time.Duration) Http2Stream(io.netty.handler.codec.http2.Http2Stream) SdkTestInternalApi(software.amazon.awssdk.annotations.SdkTestInternalApi) SdkChannelPool(software.amazon.awssdk.http.nio.netty.internal.SdkChannelPool) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Validate(software.amazon.awssdk.utils.Validate) EventLoopGroup(io.netty.channel.EventLoopGroup) Promise(io.netty.util.concurrent.Promise) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) ClosedChannelException(java.nio.channels.ClosedChannelException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpMetric(software.amazon.awssdk.http.HttpMetric) NettyUtils(software.amazon.awssdk.http.nio.netty.internal.utils.NettyUtils) Set(java.util.Set) IOException(java.io.IOException) PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) EventLoop(io.netty.channel.EventLoop) HTTP2_CONNECTION(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.HTTP2_CONNECTION) NettyClientLogger(software.amazon.awssdk.http.nio.netty.internal.utils.NettyClientLogger) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) PROTOCOL_FUTURE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Http2Connection(io.netty.handler.codec.http2.Http2Connection) ChannelPool(io.netty.channel.pool.ChannelPool) Http2LocalFlowController(io.netty.handler.codec.http2.Http2LocalFlowController) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) Future(io.netty.util.concurrent.Future) Channel(io.netty.channel.Channel)

Aggregations

Channel (io.netty.channel.Channel)2 EventLoop (io.netty.channel.EventLoop)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 AttributeKey (io.netty.util.AttributeKey)2 Promise (io.netty.util.concurrent.Promise)2 Duration (java.time.Duration)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 Protocol (software.amazon.awssdk.http.Protocol)2 PROTOCOL_FUTURE (software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE)2 ChannelConfig (io.netty.channel.ChannelConfig)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 Sharable (io.netty.channel.ChannelHandler.Sharable)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 ChannelPool (io.netty.channel.pool.ChannelPool)1 Http2Connection (io.netty.handler.codec.http2.Http2Connection)1 Http2Exception (io.netty.handler.codec.http2.Http2Exception)1