use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method minConnectionTest.
/**
* Minimum Connection is required.
*/
@Test
public void minConnectionTest() throws Exception {
int minConnections = 2;
int totalStreamToAcquire = 4;
List<Channel> channels = new ArrayList<>();
try {
ChannelPool connectionPool = Mockito.mock(ChannelPool.class);
OngoingStubbing<Future<Channel>> mockito = Mockito.when(connectionPool.acquire());
for (int i = 0; i < minConnections; i++) {
Channel channel = newHttp2Channel();
channels.add(channel);
loopGroup.register(channel).awaitUninterruptibly();
Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
channelPromise.setSuccess(channel);
mockito = mockito.thenReturn(channelPromise);
}
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, new HashSet<>(), null, http2ClientConfigForTwoConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
List<Channel> toRelease = new ArrayList<>();
for (int i = 0; i < minConnections; i++) {
Channel streamChannel = h2Pool.acquire().awaitUninterruptibly().getNow();
toRelease.add(streamChannel);
assertTrue(streamChannel instanceof Http2StreamChannel);
Mockito.verify(connectionPool, Mockito.times(i + 1)).acquire();
}
for (int i = minConnections; i < totalStreamToAcquire; i++) {
Channel streamChannel = h2Pool.acquire().awaitUninterruptibly().getNow();
toRelease.add(streamChannel);
assertTrue(streamChannel instanceof Http2StreamChannel);
// No more parent channel acquisition
Mockito.verify(connectionPool, Mockito.times(minConnections)).acquire();
}
for (Channel streamChannel : toRelease) {
h2Pool.release(streamChannel).getNow();
}
} finally {
for (Channel channel : channels) {
channel.close();
}
}
}
use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool.
/**
* Connection pool is released first and then close upon h2 pool close.
*/
@Test
public void closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool() {
SocketChannel channel = new NioSocketChannel();
try {
loopGroup.register(channel).awaitUninterruptibly();
ChannelPool connectionPool = mock(ChannelPool.class);
ArgumentCaptor<Promise> releasePromise = ArgumentCaptor.forClass(Promise.class);
when(connectionPool.release(eq(channel), releasePromise.capture())).thenAnswer(invocation -> {
Promise<?> promise = releasePromise.getValue();
promise.setSuccess(null);
return promise;
});
MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
h2Pool.close();
InOrder inOrder = Mockito.inOrder(connectionPool);
inOrder.verify(connectionPool).release(eq(channel), isA(Promise.class));
inOrder.verify(connectionPool).close();
} finally {
channel.close().awaitUninterruptibly();
}
}
use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method acquireAfterCloseFails.
/**
* Acquire should fail if pool is closed.
*/
@Test
public void acquireAfterCloseFails() throws InterruptedException {
ChannelPool connectionPool = mock(ChannelPool.class);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), new HashSet<>(), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
h2Pool.close();
Future<Channel> acquireResult = h2Pool.acquire().await();
assertFalse(acquireResult.isSuccess());
assertTrue(acquireResult.cause() instanceof IOException);
}
use of io.netty.channel.pool.ChannelPool in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method releaseParentChannelsIfPoolIsClosed.
/**
* Channel acquire should fail if pool is closed.
*/
@Test
public void releaseParentChannelsIfPoolIsClosed() {
SocketChannel channel = new NioSocketChannel();
try {
loopGroup.register(channel).awaitUninterruptibly();
ChannelPool connectionPool = mock(ChannelPool.class);
ArgumentCaptor<Promise> releasePromise = ArgumentCaptor.forClass(Promise.class);
when(connectionPool.release(eq(channel), releasePromise.capture())).thenAnswer(invocation -> {
Promise<?> promise = releasePromise.getValue();
promise.setSuccess(null);
return promise;
});
MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
h2Pool.close();
InOrder inOrder = Mockito.inOrder(connectionPool);
inOrder.verify(connectionPool).release(eq(channel), isA(Promise.class));
inOrder.verify(connectionPool).close();
} finally {
channel.close().awaitUninterruptibly();
}
}
Aggregations