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));
}
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);
}
});
}
Aggregations