|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.http.nio.netty.internal.http2;
|
17 | 17 |
|
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Matchers.eq; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | +import static software.amazon.awssdk.http.SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT; |
| 23 | +import static software.amazon.awssdk.http.SdkHttpConfigurationOption.MAX_PENDING_CONNECTION_ACQUIRES; |
| 24 | +import static software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE; |
| 25 | + |
18 | 26 | import io.netty.channel.Channel;
|
19 | 27 | import io.netty.channel.EventLoopGroup;
|
20 | 28 | import io.netty.channel.nio.NioEventLoopGroup;
|
21 | 29 | import io.netty.channel.pool.ChannelPool;
|
22 | 30 | import io.netty.channel.socket.nio.NioSocketChannel;
|
23 | 31 | import io.netty.util.concurrent.Future;
|
24 | 32 | import io.netty.util.concurrent.Promise;
|
| 33 | +import java.time.Duration; |
| 34 | +import java.util.concurrent.CompletableFuture; |
25 | 35 | import org.junit.AfterClass;
|
26 | 36 | import org.junit.Before;
|
27 | 37 | import org.junit.BeforeClass;
|
|
30 | 40 | import org.mockito.Mock;
|
31 | 41 | import org.mockito.runners.MockitoJUnitRunner;
|
32 | 42 | import software.amazon.awssdk.http.Protocol;
|
| 43 | +import software.amazon.awssdk.http.nio.netty.internal.MockChannel; |
33 | 44 | import software.amazon.awssdk.http.nio.netty.internal.NettyConfiguration;
|
34 | 45 | import software.amazon.awssdk.utils.AttributeMap;
|
35 | 46 |
|
36 |
| -import java.time.Duration; |
37 |
| -import java.util.concurrent.CompletableFuture; |
38 |
| - |
39 |
| -import static org.assertj.core.api.Assertions.assertThat; |
40 |
| -import static org.mockito.Matchers.eq; |
41 |
| -import static org.mockito.Mockito.verify; |
42 |
| -import static org.mockito.Mockito.when; |
43 |
| -import static software.amazon.awssdk.http.SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT; |
44 |
| -import static software.amazon.awssdk.http.SdkHttpConfigurationOption.MAX_PENDING_CONNECTION_ACQUIRES; |
45 |
| -import static software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE; |
46 |
| - |
47 | 47 | /**
|
48 | 48 | * Tests for {@link HttpOrHttp2ChannelPool}.
|
49 | 49 | */
|
@@ -82,6 +82,34 @@ public void protocolConfigNotStarted_closeSucceeds() {
|
82 | 82 | httpOrHttp2ChannelPool.close();
|
83 | 83 | }
|
84 | 84 |
|
| 85 | + @Test(timeout = 5_000) |
| 86 | + public void invalidProtocolConfig_shouldFailPromise() throws Exception { |
| 87 | + HttpOrHttp2ChannelPool invalidChannelPool = new HttpOrHttp2ChannelPool(mockDelegatePool, |
| 88 | + eventLoopGroup, |
| 89 | + 4, |
| 90 | + new NettyConfiguration(AttributeMap.builder() |
| 91 | + .put(CONNECTION_ACQUIRE_TIMEOUT, Duration.ofSeconds(1)) |
| 92 | + .put(MAX_PENDING_CONNECTION_ACQUIRES, 0) |
| 93 | + .build())); |
| 94 | + |
| 95 | + Promise<Channel> acquirePromise = eventLoopGroup.next().newPromise(); |
| 96 | + when(mockDelegatePool.acquire()).thenReturn(acquirePromise); |
| 97 | + |
| 98 | + Thread.sleep(500); |
| 99 | + |
| 100 | + Channel channel = new MockChannel(); |
| 101 | + eventLoopGroup.register(channel); |
| 102 | + |
| 103 | + channel.attr(PROTOCOL_FUTURE).set(CompletableFuture.completedFuture(Protocol.HTTP1_1)); |
| 104 | + |
| 105 | + acquirePromise.setSuccess(channel); |
| 106 | + |
| 107 | + Future<Channel> p = invalidChannelPool.acquire(); |
| 108 | + assertThat(p.await().cause().getMessage()).contains("maxPendingAcquires: 0 (expected: >= 1)"); |
| 109 | + verify(mockDelegatePool).release(channel); |
| 110 | + assertThat(channel.isOpen()).isFalse(); |
| 111 | + } |
| 112 | + |
85 | 113 | @Test
|
86 | 114 | public void protocolConfigNotStarted_closeClosesDelegatePool() throws InterruptedException {
|
87 | 115 | httpOrHttp2ChannelPool.close();
|
|
0 commit comments