|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.nio.netty.internal.http2; |
| 17 | + |
| 18 | +import io.netty.channel.Channel; |
| 19 | +import io.netty.channel.EventLoopGroup; |
| 20 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 21 | +import io.netty.channel.pool.ChannelPool; |
| 22 | +import io.netty.channel.socket.SocketChannel; |
| 23 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 24 | +import io.netty.util.concurrent.DefaultPromise; |
| 25 | +import io.netty.util.concurrent.Promise; |
| 26 | +import org.junit.AfterClass; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.mockito.InOrder; |
| 31 | +import org.mockito.Mockito; |
| 32 | +import org.mockito.runners.MockitoJUnitRunner; |
| 33 | + |
| 34 | +import java.util.Collections; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for {@link Http2MultiplexedChannelPool}. |
| 41 | + */ |
| 42 | +@RunWith(MockitoJUnitRunner.class) |
| 43 | +public class Http2MultiplexedChannelPoolTest { |
| 44 | + private static EventLoopGroup loopGroup; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setup() { |
| 48 | + loopGroup = new NioEventLoopGroup(4); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterClass |
| 52 | + public static void teardown() { |
| 53 | + loopGroup.shutdownGracefully().awaitUninterruptibly(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool() throws InterruptedException { |
| 58 | + SocketChannel channel = new NioSocketChannel(); |
| 59 | + try { |
| 60 | + loopGroup.register(channel).awaitUninterruptibly(); |
| 61 | + Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next()); |
| 62 | + channelPromise.setSuccess(channel); |
| 63 | + |
| 64 | + ChannelPool connectionPool = Mockito.mock(ChannelPool.class); |
| 65 | + Promise<Void> releasePromise = Mockito.spy(new DefaultPromise<>(loopGroup.next())); |
| 66 | + Mockito.doCallRealMethod().when(releasePromise).await(); |
| 67 | + releasePromise.setSuccess(null); |
| 68 | + Mockito.when(connectionPool.release(Mockito.eq(channel))).thenReturn(releasePromise); |
| 69 | + |
| 70 | + MultiplexedChannelRecord record = new MultiplexedChannelRecord(channelPromise, |
| 71 | + channel, |
| 72 | + 8, |
| 73 | + (ch, rec) -> {}); |
| 74 | + Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), 2, Collections.singletonList(record)); |
| 75 | + |
| 76 | + h2Pool.close(); |
| 77 | + |
| 78 | + InOrder inOrder = Mockito.inOrder(connectionPool, releasePromise); |
| 79 | + inOrder.verify(releasePromise).await(); |
| 80 | + inOrder.verify(connectionPool).close(); |
| 81 | + } finally { |
| 82 | + channel.close().awaitUninterruptibly(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void acquireAfterCloseFails() throws InterruptedException { |
| 88 | + ChannelPool connectionPool = Mockito.mock(ChannelPool.class); |
| 89 | + |
| 90 | + Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), 2, Collections.emptyList()); |
| 91 | + |
| 92 | + h2Pool.close(); |
| 93 | + |
| 94 | + assertThat(h2Pool.acquire().await().isSuccess()).isFalse(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void releaseAfterCloseFails() throws InterruptedException { |
| 99 | + ChannelPool connectionPool = Mockito.mock(ChannelPool.class); |
| 100 | + |
| 101 | + Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), 2, Collections.emptyList()); |
| 102 | + |
| 103 | + h2Pool.close(); |
| 104 | + |
| 105 | + assertThat(h2Pool.release(Mockito.mock(Channel.class)).await().isSuccess()).isFalse(); |
| 106 | + } |
| 107 | +} |
0 commit comments